Random walk

Random walk is a random process describing a succession of random steps in the mathematical space.

[1]:
# Import auxiliary libraries for demonstration

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

plt.rcParams[ "figure.figsize" ] = [ 5, 4 ]

plt.rcParams[ "figure.dpi" ] = 80
plt.rcParams[ "font.family" ] = "Times New Roman"
plt.rcParams[ "font.size" ] = '14'

Uniform random walk

Function randomWalkUniform implements the uniform random walk function.

The uniform random walk starts from the mathematical origin. The function then updates the coordinates by randomly picking a dimension and a direction.

Function help

[2]:
from ffpack.lsg import randomWalkUniform
help( randomWalkUniform )
Help on function randomWalkUniform in module ffpack.lsg.randomWalk:

randomWalkUniform(numSteps, dim=1, randomSeed=None)
    Generate load sequence by a random walk.

    Parameters
    ----------
    numSteps: integer
        Number of steps for generating.
    dim: scalar, optional
        Data dimension.
    randomSeed: integer, optional
        Random seed. If randomSeed is none or is not an integer, the random seed in
        global config will be used.

    Returns
    -------
    rst: 2d array
        A 2d (numSteps by dim) matrix holding the coordinates
        of the position at each step.

    Raises
    ------
    ValueError
        If the numSteps is less than 1 or the dim is less than 1.

    Examples
    --------
    >>> from ffpack.lsg import randomWalkUniform
    >>> rst = randomWalkUniform( 5 )

Example in 1D space

[3]:
urw1dResults = randomWalkUniform( 500, 1, randomSeed=2023 )
[4]:
fig, ax = plt.subplots()

ax.plot( np.array( urw1dResults ) )

ax.tick_params(axis='x', direction="in", length=5)
ax.tick_params(axis='y', direction="in", length=5)
ax.set_ylabel( "X" )
ax.set_xlabel( "T" )
ax.set_title( "1D random walk" )

plt.tight_layout()
plt.show()
../_images/moduleCookbook_lsgRandomWalk_9_0.png

Example in 2D space

[5]:
urw2dResults = randomWalkUniform( 500, 2, randomSeed=2023 )
[6]:
fig, ax = plt.subplots( figsize=( 5, 5 ) )

ax.plot( np.array( urw2dResults )[ :, 0 ],
         np.array( urw2dResults )[ :, 1 ] )

ax.tick_params(axis='x', direction="in", length=5)
ax.tick_params(axis='y', direction="in", length=5)
ax.set_ylabel( "Y" )
ax.set_xlabel( "X" )
ax.set_title( "2D random walk" )

plt.tight_layout()
plt.show()
../_images/moduleCookbook_lsgRandomWalk_12_0.png