Hi there. In this page, I play around with plotting random walk paths in the Python programming language. The code is heavily based from the book Data Analysis In Python by Wes McKinney. In the book, there is no code for the plots. I add in the code for plots.

__Plotting A Single Random Walk Path__

__Plotting A Single Random Walk Path__

To start import random, numpy as np, seed from random and matplotlib’s pyplot as plt.

1 2 3 4 |
import random from random import seed from matplotlib import pyplot as plt import numpy as np |

Having a random seed allows for the results to be reproduced. Not having one will change the results and plots every time random functions are used.

1 2 |
# Set seed for reproducibility seed(123) |

For simplicity, the random walk starts at the position of . The walk list will store the position of the random walk after each step/jump. The number of steps is 1000.

1 2 3 |
position = 0 walk = [position] steps = 1000 |

In a random walk jump the jump either goes up by one or down by one. This jump is done with the use of an if-else statement in one line within the for loop. Each step is added to the random walk’s position.

1 2 3 4 |
for i in range(steps): step = 1 if random.randint(0, 1) == 1 else -1 position += step walk.append(position) |

__Plotting The Result__

Now that the random walk positions have been obtained after each step, a plot can be generated with matplotlib’s pyplot. You can use plt.plot(walk) on its own but I wanted to add in some labels and a title.

1 2 3 4 5 6 7 8 9 |
# Plot results plt.plot(walk) plt.xlabel("\n Number Of Steps", size = 14) plt.ylabel("Position \n", size = 14) plt.title("Random Walk Plot \n", size = 20) plt.show() |

**Basic Statistics On The Random Walk**

From the generated random walk path, some basic statistics on it can be obtained with the use of Python’s Numpy functions.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Basic statistics on random walk: print(np.min(walk)) -15 print(np.max(walk)) 25 print(np.mean(walk)) 1.3966033966 print(np.std(walk)) 9.68327334474 |

__Multiple Random Walk Paths__

In the previous section, only one random walk path was generated. For multiple random walk paths, the code would need some adjustments.

In the code below, the number of random walk paths and the number of steps for each random walk are specified. Each random number is generated through Numpy’s randint (which is different from random’s randint function). In the steps variable, any draws that are a 1 stay a 1 and anything other than a 1 turns into a -1 with the use of Numpy’s where function. The random walk positions are in the walks variable.

1 2 3 4 5 6 7 8 |
### Multiple Random Walks nwalks = 200 nsteps = 100 draws = np.random.randint(0, 2, size=(nwalks, nsteps)) # 0 or 1 steps = np.where(draws == 1, 1, -1) walks = steps.cumsum(1) |

Plotting the results for multiple random walks is not much different than before in terms of the code.

1 2 3 4 5 6 7 8 9 |
# Plot results plt.plot(walks) plt.xlabel("\n Time", size = 14) plt.ylabel("Position \n", size = 14) plt.title("Multiple Random Walks Plot \n", size = 20) plt.show() |

Like in the previous section, you can gather some basic statistics with the use of Numpy functions.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Basic statistics on multiple random walks: print(np.min(walks)) -30 print(np.max(walks)) 27 print(np.mean(walks)) 0.6554 print(np.std(walks)) 7.04625083573 |