- Keras Reinforcement Learning Projects
- Giuseppe Ciaburro
- 1106字
- 2021-08-13 15:26:05
Simulating 1D random walk
We have previously said that a random walk contains a random term. To simulate a random walk, it is not enough to generate a list of random numbers, because the next value in the sequence is a modification of the previous value. This dependency provides a certain coherence from one passage to the next that does not happen in the generation of independent random numbers, which instead shows great jumps from one number to the next.
A simple model of a casual walk can be simulated through the following pseudocode:
- Start from position 0
- Randomly select a number with a value of -1 or 1
- Add it to the observation of the previous time step
- Repeat these steps from step 2
To implement this algorithm in Python, just refer to the previous equation:
In this formula, Xn is the next value in the walk, Xn-1 is the observation at the previous time step, and Zn is the random fluctuation at that time. In Python, it is obtained by looping over this process and building up a list of 1,000 time steps for the random walk. The following shows the Python code to generate a random walk:
from random import seed
from random import random
from matplotlib import pyplot
seed(4)
RandomWalk = list()
RandomWalk.append(-1 if random() < 0.5 else 1)
for i in range(1, 1000):
Zn = -1 if random() < 0.5 else 1
Xn = RandomWalk[i-1] + Zn
RandomWalk.append(Xn)
pyplot.plot(RandomWalk)
pyplot.show()
We will analyze this code line by line. The first lines load the libraries:
from random import seed
from random import random
The random module in Python contains a number of random number generators for various distributions. For integers, we use a uniform selection from a range. For sequences, we use a uniform selection of a random element, a function to generate a random permutation of a list in place, and a function for random sampling without replacement.
Two functions are loaded from this module: seed and random. The seed function sets the seed of a random number generator, which is useful for creating simulations or random objects that can be reproduced. You have to use this function every time you want to get a reproducible random result. In this case, the random numbers are the same, and they will continue to be the same no matter how far out in the sequence we go. Each seed value will correspond to a sequence of values that is generated for a given random number generator. That is, if you supply the same seed twice, you get the same sequence of numbers twice.
The random function returns the next random floating point number in the range [0.0, 1.0]. Another important library is loaded. This is the matplotlib library, as shown in the following code:
from matplotlib import pyplot
Matplotlib is a Python 2D-plotting library that produces publication-quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib tries to make easy things easy and hard things possible. You can generate plots, histograms, power spectra, bar charts, error charts, scatter plots, and so on with just a few lines of code. It consists of a collection of command style functions that make Matplotlib work like MATLAB. Each pyplot function makes a change to a figure, such as creating a figure, creating a plotting area in a figure, plotting some lines in a plotting area, decorating the plot with labels, and so on.
After correctly importing the libraries, we pass a command to analyze the individual operations. Let's start with setting the seed using the following code:
seed(4)
Recall that if you supply the same seed twice then you get the same sequence of numbers twice. Let's move on to creating the main variable, as shown in the following code:
RandomWalk = list()
The RandomWalk variable will be the variable that will contain the sequence of values that are representative of the random walk. This variable will be of the list type. A list is an ordered collection of values, and can contain various types of values. A list is a mutable container. This means that we can add values, delete values, or modify existing values. A Python list represents a mathematical concept of a finite sequence. For our needs (a sequence of mutable values), the list represents the most suitable container. The list() method takes sequence types and converts them to lists. To start, it is necessary to initialize the first value of the list, as shown in the following code:
RandomWalk.append(-1 if random() < 0.5 else 1)
This value will serve us in the next calculation. We can move on to the iterative cycle, as shown in the following code:
for i in range(1, 1000):
At each step, we get the following random term:
Zn = -1 if random() < 0.5 else 1
Two values will be returned: -1 and 1. We get -1 if the random function (which, as you may recall, returns values in the [0.0, 1.0] interval) returns a value lower than 0.5; otherwise, we get 1. At this point, we can evaluate the value of the walk at the current step using the following code:
Xn = RandomWalk[i-1] + Zn
Then the value of the walk at the current pace will be given by the sum of the value of the walk at the previous step and the random term. Once calculated, this value must be added to the list, as follows:
RandomWalk.append(Xn)
This procedure will be repeated for the n programmed steps. At the end, we will have the entire sequence stored in the list. We just have to visualize it, as shown in the following code:
pyplot.plot(RandomWalk)
pyplot.show()
The pyplot.plot phrase plots RandomWalk on the y axis using an array 0 .... N-1 as the x axis. The plot() phrase is a versatile command, and will take an arbitrary number of arguments. Finally, pyplot.show displays the created diagram.
The following graph shows a random walk drawn with the previous code:

In the graph, we see a random process describing the trends of a specific function. In it, the next step is independent of the position of the previous step, depending only on the current step. The random walk is a natural model that can be used to simulate the efficient market hypothesis (EMH)—that is, the theory according to which the price varies given the arrival of new information, which, by definition, is independent of what we already know.