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.
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:
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 is distributed from normal distribution of mean 0 and 1.
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.
And now we have a small time series . We can continue this for as long as we want to simulate. Let's continue that way in code.
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
np.random.seed(1)
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))
x
[array([-0.51634791])]
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
x.append(x_t)
x[1:5]
[array([-0.16523101]),
array([-0.23400148]),
array([-1.58176642]),
array([-0.11102656])]
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>
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>