@BruceWang
2018-01-03T19:09:56.000000Z
字数 2891
阅读 1701
NumpyPandasMatplot
import random
position = 0
walk = [position]
steps = 1000
for i in range(steps):
step = 1 if random.randint(0,1) else -1
position += step
walk.append([position])
import matplotlib.pyplot as plt
%matplotlib inline
walk[:10]
[0, [-1], [-2], [-1], [-2], [-1], [-2], [-1], [-2], [-1]]
import numpy as np
nsteps = 1000
draws = np.random.randint(0,2,size=nsteps)
steps = np.where(draws>0, 1, -1)
print(steps)
[-1 -1 -1 1 -1 1 -1 1 1 1 -1 1 -1 1 1 -1 -1 1 -1 -1 -1 1 -1 1 -1
-1 -1 1 1 1 1 1 -1 1 -1 -1 1 -1 -1 -1 1 -1 1 1 -1 1 -1 -1 -1 1
1 -1 1 1 1 1 -1 1 -1 1 1 1 -1 -1 1 1 -1 1 1 1 1 -1 1 1 -1
1 -1 -1 -1 -1 1 1 -1 1 -1 -1 1 1 -1 -1 1 -1 -1 1 -1 1 1 -1 -1 1
1 -1 1 -1 1 1 -1 1 1 1 -1 -1 1 1 -1 -1 1 1 1 1 -1 1 -1 -1 -1
1 1 1 -1 1 -1 -1 -1 1 -1 1 1 1 -1 -1 1 1 -1 1 -1 -1 1 1 1 1
1 1 -1 -1 1 1 1 1 1 -1 -1 1 1 -1 -1 1 -1 -1 1 1 -1 1 1 -1 1
1 1 -1 -1 -1 1 -1 -1 -1 1 1 -1 1 -1 1 -1 -1 -1 -1 1 1 1 1 1 -1
-1 -1 1 1 1 1 -1 1 1 1 -1 1 1 1 -1 -1 1 1 1 -1 1 -1 1 1 1
-1 1 -1 -1 1 1 1 1 1 -1 1 1 1 -1 1 1 1 -1 1 -1 -1 1 1 1 -1
-1 -1 1 1 1 -1 -1 1 1 1 -1 -1 1 -1 -1 1 -1 1 1 -1 -1 -1 -1 -1 1
1 1 -1 1 -1 1 1 1 1 1 1 -1 -1 1 1 1 -1 -1 1 -1 -1 1 1 1 -1
-1 1 1 1 1 1 1 -1 -1 -1 1 -1 -1 -1 -1 1 -1 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1 -1 -1 -1 1 -1 -1 -1 1 -1 -1 1 -1 -1 -1 1 1 -1 -1 1 -1 -1 -1 -1 1
1 -1 1 1 -1 1 -1 1 -1 1 1 1 -1 1 1 1 -1 1 1 -1 -1 1 -1 -1 1
1 1 -1 1 1 1 1 -1 1 -1 1 -1 1 -1 -1 -1 1 -1 1 1 1 -1 -1 1 1
1 1 -1 1 1 -1 1 1 -1 -1 -1 1 1 1 -1 1 1 -1 -1 1 1 -1 1 1 1
-1 1 -1 1 1 1 -1 1 -1 -1 1 -1 -1 -1 1 -1 -1 1 1 -1 -1 -1 -1 1 -1
1 1 -1 -1 -1 -1 1 -1 1 -1 -1 -1 1 1 -1 1 -1 -1 1 1 1 -1 1 1 -1
1 1 -1 1 -1 -1 -1 -1 1 -1 1 1 -1 -1 1 -1 -1 -1 1 1 1 1 -1 1 1
1 -1 1 -1 -1 -1 -1 -1 1 1 1 -1 -1 1 1 -1 1 -1 1 -1 -1 1 -1 1 1
1 1 1 1 1 -1 -1 -1 -1 -1 1 1 1 -1 -1 1 1 1 -1 1 1 -1 -1 -1 1
-1 -1 1 -1 -1 -1 1 1 1 -1 1 -1 1 1 -1 1 -1 -1 -1 -1 -1 1 1 -1 1
1 1 -1 1 -1 1 -1 -1 1 1 -1 1 -1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1]
walk = steps.cumsum()
walk.min()
-5
walk.max()
41
nwalks = 5000
nsteps = 1000
draws = np.random.randint(0,2, size=(nwalks, nsteps))
steps = np.where(draws>0, 1,-1)
walks = steps.cumsum(1)
walks
array([[ 1, 0, -1, ..., -6, -7, -8],
[ 1, 0, 1, ..., 6, 7, 6],
[ -1, -2, -3, ..., 2, 3, 2],
...,
[ 1, 0, 1, ..., 54, 53, 54],
[ 1, 0, 1, ..., -28, -29, -30],
[ 1, 2, 3, ..., -32, -31, -30]], dtype=int32)
walks.max()
116
walks.min()
-124
steps = np.random.normal(loc=0, scale=0.25, size=(nwalks, nsteps))
steps
array([[-0.16399234, -0.0267885 , 0.05142689, ..., -0.37457757,
0.03966075, 0.39067126],
[ 0.12018613, -0.08792517, -0.05285084, ..., 0.26438388,
-0.21433596, 0.33423605],
[-0.06996542, -0.17732628, -0.41869773, ..., -0.00390321,
-0.12775795, 0.16524036],
...,
[-0.18181599, -0.37014665, -0.01165527, ..., -0.11092812,
0.05949846, 0.09946047],
[-0.36011367, -0.12865467, -0.31651241, ..., -0.02512507,
-0.00548912, 0.30151192],
[ 0.32561003, 0.28333045, -0.28733556, ..., 0.14309592,
-0.31387699, 0.16887155]])