Law of large numbers

In [2]:
import numpy as np 
# makes numpy routines and data types available as np.[name ouf routine or data type]

import matplotlib.pyplot as plt 
# makes plotting command available as plt.[name of command]

stepsmin = 10
# minimal number of steps that will be simulated
steps = stepsmin
# initial value for steps

for i in range(5):
    h = 1
    # stepsize for time steps 
    std = np.sqrt(h)
    # standard deviation for the distribution of each step

    k = 5
    # number of random walks that will be simulated

    noise = (np.random.choice(2,size=(steps,k),p=(0.45,0.55)))*std
    # create a steps times k dimensional matrix of uniform random numbers with values 0 and std 
    bm = np.cumsum(noise,axis=0)
    # For each entry in the second component (axis=1), the matrix bm contains the cumulative 
    # sums of the random numbers in the first component (axis=0). Thus the array bm contains 
    # simulations of k trajectories. We now plot these. 


    t = np.arange(1,steps+1,1)*h
    # creates vector of time points
  

    plt.figure(figsize=(9,5), dpi=80)
    # sets size of plot
    plt.plot(t,bm,linewidth=1.0)
    # produces a plot of the trajectories in the components of bm versus t with thin lines
    plt.show()
    # output of plot 
    
    steps = steps*10
    # do the same again with 10 times as many steps

Simulation of random walks

In [2]:
import numpy as np 
# makes numpy routines and data types available as np.[name ouf routine or data type]

import matplotlib.pyplot as plt 
# makes plotting command available as plt.[name of command]

stepsmin = 10
# minimal number of steps of a random walk that will be simulated
steps = stepsmin
# initial value for steps

for i in range(5):
    h = 1
    # time stepsize for each step 
    std = np.sqrt(h)
    # standard deviation for the distribution of each step

    k = 200
    # number of random walks that will be simulated


    noise = (2.*np.random.choice(2,size=(steps,k),p=(0.45,0.55))-1)*std
    # create a steps times k dimensional matrix of random numbers 
    bm = np.cumsum(noise,axis=0)
    # For each entry in the second component (axis=1), the matrix bm contains the cumulative 
    # sums of the random numbers in the first component (axis=0). Thus the array bm contains 
    # simulations of k trajectories. We now plot these. 


    t = np.arange(1,steps+1,1)*h
    # creates vector of time points

    plt.figure(figsize=(9,5), dpi=80)
    # sets size of plot
    plt.plot(t,bm,linewidth=1.0)
    # produces a plot of the trajectories in the components of bm versus t with thin lines
    plt.show()
    # output of plot 
    
    steps = steps*10
    # do the same again with 10 times as many steps