Random Walk Plots In Python
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.
This post can also be found on my website here.
Plotting A Single Random Walk Path
To start off import random, numpy as np, seed from random and matplotlib's pyplot as plt.
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.
# Set seed for reproducibility
seed(123)
For simplicity, the random walk starts at the position of y = 0. The walk list will store the position of the random walk after each step/jump. The number of steps is 1000.
position = 0
walk = [position]
steps = 1000
In a random walk, each 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. The position can be viewed as the cumulative sum of all the random walk up and down jumps at a time point.
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.
# 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()
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 function (which is different from random's randint function). This random number is either a zero or one.
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. Recall that a 1 represents an up jump in the random walk and a -1 represents a down jump. The random walk positions are in the walks variable.
### 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.
# 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()
upvote for me please? https://steemit.com/news/@bible.com/6h36cq
What is this a 1 stay a 1 etc? You may want to clarify the description. Also the image you have used in the beginning lacks license information. Are you sure if it is free to use?
A #steemstem mentor @dexterdev
I have removed the first image. The other images were generated in Python.
From here:
I randomly generate 0s and 1s with numpy's random.randint. Any ones stay a one which represent up jumps in the random walk. Anything other than a 1 in numpy's where function (i.e 0) turns into a -1 for a down jump. The
where()function is like an if-else statement.