# Python 3.7 # For the course Stochastic Calculus, Fall semester 2019, Courant Institute, NYU # Author: course instructor, Jonathan Goodman # https://www.math.nyu.edu/faculty/goodman/teaching/StochCalc2019/StochCalc.html # filename: RandomWalkDemo.py # For assignment 2. # Simulate random walk paths up to N steps # Plot a histogram of hitting times # Compare to the ``theoretical'' density of hitting for Brownian motion import numpy as np # numerical computing library import matplotlib.pyplot as plt # library of plot routines import numpy.random as ran # random number generators print("Assignment 2") print("Random walk hitting times") n_max = 3000 # maximum number of random walk steps m = 20000 # number of paths pu = .5 # probability that X -> X+dx in one step pd = 1. - pu # probability that X -> X+dx in one step dx = .104239 # step size for stepping right x0 = 2. # starting point Nk = np.zeros( n_max, dtype = int ) # Nk[k] will count times K=k cpe = np.zeros( n_max, dtype = float ) # empirical cumulative probability # cpe[k] = empirical prob(K