Python Random Walk Simuation



Random walks are one of the fundamental time series models. Despite this simplicity, they are able to model many real world scenarios. In this article, we will learn how to simulate a random walk in Python.

What is a Random walk

A random walk is defined that for each observation, we can compute this observation from the last plus some random noise. The equation is as follows:

xt=xt1+wtx_t = x_{t-1} + w_t

The way to read this is, the current value at time t is computed based on the previous value (t - 1) plus some random noise. Where wtw_t is distributed from normal distribution of mean 0 and 1.

Simulating by Hand

To get a very good understanding, let's try to simulate this by hand. To start, we need a initial value which we will produce randomly.

Let's say we ran rnorm(1, mean = 0, sd = 1) and we got the value 1.5. We will use this as our first value, X_1

To get the next value, X_2, we need a W_2 which is the random error for the current step. Again, we can run rnorm(1, mean = 0, sd = 1). Let's say we get .4. Now, we have the following.

X2=X21+w2=X1+w2=1.5+.4=1.9X_2 = X_{2 - 1} + w_2 \\ = X_1 + w_2 \\ = 1.5 + .4 \\ = 1.9

And now we have a small time series [1.5,1.9][1.5, 1.9]. We can continue this for as long as we want to simulate. Let's continue that way in code.

Simulating a Random Walk in R

First we will set a seed so that you can reproduce the same results, and we will create a size variable to designate how large of a time series we want to simulate.

import numpy as np


size = 200

Next, we will create an empty vector to hold all the data and initialize the first time series entry from a normal distribution.

x = []

# Create the first value
x.append(np.random.normal(0, 1, 1))

Now, we loop until we have enough variables using the same computation we did above to create a variable at each step.

for t in range(1, size):
    # Create random noise for this observation
    w_error = np.random.normal(0, 1, 1)
    # Compute the current value based on the previous value and the current noise
    x_t = x[t - 1] + w_error

Now, we can plot this data, and we will see a familiar random walk shape.

import matplotlib.pyplot as plt

plt.scatter(x = range(0, size), y = x)
<matplotlib.collections.PathCollection at 0x19d7ed56310>


Simulating with a Library

In practice, we often want simulate a random walk by hand or incrementally in code. Instead, we can extend our use of numpy to create a random walk. Below is an example of that.

first_step = np.random.normal(loc = 0, scale = 1, size = 1)
steps = np.random.normal(loc = 0, scale = 1, size = size)

random_walk = first_step + np.cumsum(steps)

We can end by plotting to see the random walk shape as expected.

import matplotlib.pyplot as plt

plt.scatter(x = range(0, size), y = random_walk)
<matplotlib.collections.PathCollection at 0x19d7ee93b80>