Datenanalyse und Stochastische Modellierung - Dr. Philipp Meyer
Uebung 1

Random Walks

In this exercise we use the random number generator from the numpy module. We first import:

import numpy as np
import matplotlib.pyplot as plt

The random walk is a time series X given recursively as

\[X_t=X_{t-1}+\xi_{t}\]

In Python, an array of M such trajectories of length T can be generated by the cumulative values

alle_werte = np.cumsum( np.random.normal( mean , standard_deviation , (M,T) ) , axis=1 )

1A Create an array of 100 trajectories of length 1000. Store the array in a variable all_values and output it with the print command.

1B Plot 10 of the trajectories together into a figure with the command plt.plot().

To get all values of the 100 trajectories at time t, you can specify the index as

all_values[:,t]

2A Find a function in Python that you can use to plot a histogram. Plot the histogram at time points t=0 , t=9 , t=99 , t=999

The mean square deviation is defined as

\[\mathrm{MSD}_t = \langle (X_t-X_0)^2 \rangle\]

2B Plot the mean square deviation of the generated data set for all times t in a log-log plot (plt.loglog()).

2C How does the mean squared deviation depend on time t? Use a function that fits the exponent by which the MSD scales.

def scaling( ts ):
    print("SCALING > Fit scaling exponent ")
    y=np.array(ts)
    ly=np.log10(y[y>0])
    x=np.arange(1,len(ly)+1)
    lx=np.log10(x)

    m,b = np.polyfit(lx, ly, 1)
    print("m="+str(m))
    #plt.loglog( 10**(lx),10**(m*lx+b),lw=3,label=r'$\propto$'+r' $x^{{{}}}$'.format(str(abs(m)+0.005)[:4]) ) #plot the fit

    return m,b

2D Repeat task 2A and plot the histograms. Is there a factor depending on t that you can multiply to the values so that the distribution remains the same for all t?