The random processes are fundamental in a plethora of engineering and science domains. They are used to model complex systems like weather forecasting, which are highly unpredictable.

In this blog post, we will introduce the simplest random process, i.e. the simple random walk, which is the basis and foundation for many random processes. We will simulate a random walk using the Python numerical libraries like NumPy. Moreover, we will make an animated visualization leveraging the Matplotlib Animation API in order to emphasize the random nature of this phenomenon. This is an important part because we convey our message more easily. So let’s begin and stay tuned!

Intuition

We are not in control of everything and we can’t predict all outcomes although we are used thinking that way. For example, how could we know the exact number of people in our favorite book store exactly at 12:36? We never know since this number is random and can’t be calculated based on all observations we have. It is not possible to fit a magical deterministic formula to give us the answer. Instead, we can think of rough estimates and let the exact number of people be pure mere of luck. Thus, the number of visitors in the book store from minute to minute is a random process.

Fig. 1: Queueing System used to model arrivals and departures. Source: Wikipedia

The random processes are used to model plenty of phenomena in the exact sciences and engineering. The best possible way to start thinking of them is to play a game with coin tossing. Every time we flip the coin if the outcome is head we win 1 dollar and of the outcome is tail we lose 1 dollar. Can we know how much dollars we would win or lose after 10 consecutive coin flips? Certainly no, but what we can know is how probable some outcome is after 10 steps. This leads us to the definition of a random walk.

Random Walk Definition

Let’s have a sequence of independent and identically distributed (i.i.d) discrete random variables such that:

Random Increments [1][2]

We can think of this in terms of the coin-tossing we had before. With a probability of 0.5, it can happen to be a head, thus winning 1 dollar, and with a probability of 0.5 it can be a tail thus losing 1 dollar.

Then, a simple random walk is defined as:

Random Walk Definition [1][2]

We can think of this random variable like the cumulative sum of dollars we would have at time step n. After flipping the coin 10 times, this sum might range from -10 to +10, or in general from -N to +N. Because N might be a large number we can normalize this range to be most of the time between -1 and +1. We can construct such a thing using the Central Limit Theorem:

Central Limit Theorem for a Simple Random Walk

thus following a Normal Distribution with given mean and variance, in this case, 0 and 1. There is a proof showing that the given identity always holds.

Once we know the definition of a simple random walk, we can implement a simulation in Python and make a visualization of the possible outcomes.

Random Walk in Python

By using the NumPy utilities we can easily simulate a simple random walk. Given the number of steps N as an input argument, we can randomly generate N samples from the set { +1, -1} with an equal probability of 0.5. Then, we will only use the cumsum function, to give us the cumulative sum in every time step. The Python code is given below:

Simulate a Random Walk in Python

Matplotlib library provides an animation API that offers a different perspective of the plotting in Python. Instead of serving static charts, we can use an eye-catching animation to emphasize and more efficiently transfer the message we would like to send. This comes quite handy and the effect is furthermore amplified for time-series data such as the simple random walk.

Using this animation API we can animate one realization of a simple random walk as shown in the code snippet below:

Animate the Random Walk using Matplotlib’s Animation API

The animation function needs three essential inputs: i) an instance of the figure to show the animation, ii) data initialization function and iii) a function that defines the animation logic. Then we provide the number of frames to draw (how many times to iterate over the data) and the delay in milliseconds between the consecutive frames. One final option we could use is the bit blit, i.e. to redraw only those parts that have changed between two consecutive frames, thus leading to faster animation. The result we get is shown in the animation below: