Part 1: Simulating Random Walk in Python

In this article, I will discuss briefly about Random Forest and write code in Python to simulate this concept.

Kaveti Sai
Analytics Vidhya
3 min readMay 4, 2020

--

Later, we will expand the learning by simulating bacteria trying to find food using Random Walk.

First about Random Walk, it’s basically a process of objects randomly walking from the their starting point.

The concept might seem trivial but we can relate lot of phenomenons and behaviors in the nature to “Random Walk.”

There are applications in various fields for this concept.

I wanted to simulate this concept on Python and plot and view the code run visually.

You can find the full code in my GitHub page.

Explaining the Code:

Part 1: Importing modules required

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

We are using two modules here, Numpy and Matplotlib.

Part 2: Setting up and simulating the Random Walk

#setting up steps for simulating 2Ddims = 2
step_n = 200
step_set = [-1, 0, 1]
origin = np.zeros((1,dims))
#Simulate steps in 2D
step_shape = (step_n,dims)
steps = np.random.choice(a=step_set, size=step_shape)
path = np.concatenate([origin, steps]).cumsum(0)

Explaining the code here, the variable “dims” refers to dimension, Random Walk can be simulated in 1-dimension, 2-dimension and 3-dimension. Here, I’m simulating it in two-dimension as hence “dims = 2”.

step_n :: This refers to the number of steps we want to assign to Random Walk. Here want it to take 200 random steps.

origin :: Using Numpy.zeros to get an array of zeroes of size 1*2 (1 row, 2 columns).

step_shape:: A tuple of origin and step_set. This essentially stores the size of the result array that we need to fit the data we are about to simulate.

For example if we want to perform 50 steps, we will assign, step_n=50 and the step_shape = (50,2), which is the size of array we would need later.

steps:: We are using Numpy function random.choice to generate a random sample of array from the given 1D array and of the size of step_change.

So, since the input given is of step-set(containing -1,0 and 1), the output would be an array containing -1, 0 and 1 of the size of step_set.

path:: I’ve done two steps at once of concatenating and performing cumulative sum along 0 axis.

Check documentation to know about these methods in detail.

Now, we have the simulated result of Random Walk.

If you need information or explanation about this step, you can write down in the comments so that I can try to post it.

Step 3: Plotting and visualizing.

Now, to visualize the random walk data that we just simulated, we will use ‘matplotlib’ library.

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2, c=”green”)
ax.set_ylim(-10, 10)
ax.set_xlim(-5, 10)
plt.title(‘2D Random Walk’)
xdata, ydata = [], []
del xdata[:]
del ydata[:]
line.set_data(xdata, ydata)

This above code should allow us to plot the simulated Random Walk on the graph.

However, my goal is not to put a static plot but to output live simulation of my Random Walk code.

Step 4: Animating the Random Walk

I referred to Matplotlib’s documentation and examples on how to do this. Matplotlib provides the means to do this by providing Animation class.

matplotlib.Animation is used to get live simulation.

I can write detailed description about matplotlib.Animation, I’ll write a separate article about this in the future for reference.

The result is this:

Random Walks when number of steps is 300

P.S: I added code to redraw the graph if the Random Walk passes out of frame.

Part 2: Bacteria And Their Relation to Biased Random Walks

In part 2, I will create a bacteria simulation using Random Walks, the bacteria will try to find the food source using the technique.

There are multiple applications of Random Walks, but this one fascinates me as a quick fun project.

--

--