Part 2: Biased Random Walks & and it’s Applications

Kaveti Sai
Analytics Vidhya
Published in
4 min readMay 27, 2020

In part 2 we will continue the learning of Random Walk concept. I also wrote code to explain this in a fun way.

Photo by Daniel Born on Unsplash

In this part, I wanted to create multiple Random Walk plots, each with different color and observe the randomness of each of them.

You can check the full code on my GitHub, click here.

In my previous post, I explained(at least tried to) explain my whole code.

Also, I’ve made some changes to my code but using functions to put code in blocks and tidy up.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
no_of_steps = 50
def randomwalksimulate():#setting up steps for simulating 2D
dims = 2
step_n = no_of_steps
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)
return pathdef prepareplot():#plot the figure
ax = fig.add_subplot(111)
ax.set_ylim(-2, 10)
ax.set_xlim(-5, 10)
ax.set_xticks(np.arange(-15, 100, step = 10))
plt.title('Baised Random Walk')
return ax
def data_extract():path = randomwalksimulate()for pos in range(len(path)):
sample = path[pos]
yield np.array([ sample[0], sample[1] ])
#below code of if statements are to re-size the simulation if the random walk goes out of the framedef resize_plot(xmin, xmax,ymin, ymax, t, y):if t >= xmax:
ax.set_xlim(1.1*xmin, 1.1*xmax)
ax.figure.canvas.draw()
if t < xmin:
ax.set_xlim(1.1*xmin, 1.1*xmax)
ax.figure.canvas.draw()
if y <= ymin:
ax.set_ylim(1.1*ymin, 1.1*ymax)
ax.figure.canvas.draw()
if t >= ymax:
ax.set_ylim(1.1*ymin, 1.1*ymax)
ax.figure.canvas.draw()
def run_animation(num, data, line):
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
resize_plot(xmin,xmax, ymin, ymax, t=data[0, num-1], y=data[1, num-1])
line.set_data(data[:2, :num])def update_all(num, data1, line1, point1, pntline1,data2, line2, point2, pntline2,data3, line3, point3, pntline3,data4, line4, point4, pntline4):run_animation(num, data1, line1)
run_animation(num, point1, pnt1)
run_animation(num, data2, line2)
run_animation(num, point2, pnt2)
run_animation(num, data3, line3)
run_animation(num, point3, pnt3)
run_animation(num, data4, line4)
run_animation(num, point4, pnt4)
#Step 1: Create a plot and lines for each random paths
# Setting the axes properties
ax = prepareplot()
#step 2: create 4 unique random walk paths(converted to ndarray)temp1 = np.array(list(data_extract())).T
temp2 = np.array(list(data_extract())).T
temp3 = np.array(list(data_extract())).T
temp4 = np.array(list(data_extract())).T
point1 = temp1
point2 = temp2
point3 = temp3
point4 = temp4
line1, = ax.plot([],[], c='#7B56BF', linewidth=3)
line2, = ax.plot([],[], c='#360259', linewidth=3)
line3, = ax.plot([],[], c='#3e1785', linewidth=3)
line4, = ax.plot([],[], c='#e33341', linewidth=3)
pnt1, = ax.plot([],[], 'o', c='#7B56BF')
pnt2, = ax.plot([],[], 'o', c='#360259')
pnt3, = ax.plot([],[], 'o', c='#3e1785')
pnt4, = ax.plot([],[], 'o', c='#e33341')
#To add gradient
plotlim = tuple(4*x for x in plt.xlim()) + tuple(7*x for x in plt.ylim())
test = np.array( [[0,0],[4,1]]).T
ax.imshow(test, cmap=plt.cm.Greens, interpolation='bicubic', extent=plotlim)
plt.draw()
N = no_of_steps + 2anim = animation.FuncAnimation(fig, update_all, N, fargs =(temp1, line1, point1, pnt1,temp2, line2, point2, pnt2,temp3, line3, point3, pnt3,temp4, line4, point4, pnt4),interval=150, repeat=False)#If you want to view the graph, comment out the anim save line and vice versa.This is because both the functions use and flush the data and allowing both lines will cause issues with the later code.anim.save('rand.gif', writer='imagemagick')
#plt.show()

The result is the below plot:

4 Random Paths

Biased Random Walk

So far all of the random walks we have considered allowed an object to move with equal probability in any direction. But not all random walks follow this rule. A biased random walk is a random walk that is biased in one direction, leading to a drift on average of objects in one specific direction.

There are several ways that a random walk can be biased.

  • Suppose that instead of an equal probability of moving left to right, there was a higher probability of moving to the right.
  • Or, suppose the probabilities of moving to the left or right remained equal, but whenever the dot moved to the right, it moved 2 spaces, and when it moved to the left, it only moved 1 space.

In both of these cases, we would expect a net drift to the right. In other words, the random walk is biased toward the right. I’ve used the second method to plot a biased random walk.

dims = 2
step_n = no_of_steps
step_set = [-1, 0, 4]
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)

My code will form a list of steps with 1 left move(-1) and 4 right move(4). Hence, now the path drift will be significantly towards right because of the bias.

Now, one of the phenomenon that can be applied to random biased system is how bacteria find their food.

Food releasing chemicals in the air

As you can see, the nearer you go to the source the greater the chemical concentration would be. Now, foods emit chemicals in the air that bacteria uses to track the food source.

The bacteria take Random Walk in any direction. If they are a step closer to the bacteria, it will sense a stronger chemical trail. If it is moving down the concentration gradient, it will start detecting the chemical’s molecules less and less frequently. This ultimately determines the direction and strength of the bias in its random walk.

Then it takes random step again till it finds stronger concentration and the process repeats. This ultimately determines the direction and strength of the bias in its random walk.

The plot should be able to explain this phenomenon:

Multiple Bacteria using Random Walk to find the food source

Conclusion

Random Walk is an interesting concept that I stumbled upon and there are very interesting applications to it.

I would love to get feedback about the stories I’m posting.

Sources: https://www.mit.edu/~kardar/teaching/projects/chemotaxis(AndreaSchmidt)/random.htm

https://en.wikipedia.org/wiki/Random_walk

--

--