Ha Khanh Nguyen (hknguyen)
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)
matplotlib
package by running the following command in the command line prompt (Terminal on Mac, Anaconda Prompt on Windows).conda install matplotlib
import matplotlib.pyplot as plt
plt.plot(walk[:100])
[<matplotlib.lines.Line2D at 0x7fe2c2f8d5b0>]
walk
is simply the cumulative sum of the random steps and could be evaluated as an array expression.import numpy as np
np.random.seed(430)
nsteps = 1000
draws = np.random.randint(0, 2, size=nsteps)
steps = np.where(draws > 0, 1, -1)
walk = steps.cumsum()
np.random.randint()
to generate an array of random integers. random.randint()
only returns 1 number at the time.walk
:plt.plot(walk)
[<matplotlib.lines.Line2D at 0x7fe2c30934c0>]
walk.min()
-23
walk.max()
15
(np.abs(walk) >= 10).argmax()
11
np.abs(walk) >= 10
gives us a boolean array indicating where the walk has reached or exceeded 10.argmax()
returns the first index of the maximum value of the array. In this case, it returns the index of the first True
value.nwalks = 5000
nsteps = 1000
draws = np.random.randint(0, 2, size=(nwalks, nsteps))
steps = np.where(draws > 0, 1, -1)
walks = steps.cumsum(1) # sum across the columns
walks
array([[ -1, 0, 1, ..., -28, -27, -26], [ -1, 0, -1, ..., 54, 55, 56], [ -1, -2, -3, ..., 2, 3, 2], ..., [ -1, 0, -1, ..., -22, -21, -22], [ 1, 2, 1, ..., 48, 49, 48], [ 1, 0, 1, ..., -38, -39, -38]])
hits30 = (np.abs(walks) >= 30).any(1)
hits30
array([ True, True, False, ..., True, True, True])
any(1)
returns True
if at least 1 of the values of the row is True
.# number of walks that hit 30 or -30
hits30.sum()
3336
# estimate for probability a walk hitting 30 in either direction
hits30.sum()/nwalks
0.6672
hist30
to select only the walks that actually hit 30 or -30! Then use argmax()
across axis 1 (the column) to get the crossing times:crossing_times = (np.abs(walks[hits30]) >= 30).argmax(1)
crossing_times.mean()
511.1636690647482
This lecture note is modified from Chapter 4 of Wes McKinney's Python for Data Analysis 2nd Ed.