1. What are Random Walks?

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

plt.plot(walk[:100])

2. Using NumPy for Simulating Random Walks

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()
plt.plot(walk)

walk.min()
## -23
walk.max()
## 15
(np.abs(walk) >= 10).argmax()
## 11

3. Simulating Many Random Walks at Once

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])
# 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
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.