Skip to content

Instantly share code, notes, and snippets.

@IlievskiV
Created April 18, 2020 21:27
Show Gist options
  • Save IlievskiV/282efd67f34d8995c2d0953b964e42cd to your computer and use it in GitHub Desktop.
Save IlievskiV/282efd67f34d8995c2d0953b964e42cd to your computer and use it in GitHub Desktop.
import numpy as np
np.random.seed(1234)
def brownian_motion(N, T, h):
"""
Simulates a Brownian motion
:param int N : the number of discrete steps
:param int T: the number of continuous time steps
:param float h: the variance of the increments
"""
dt = 1. * T/N # the normalizing constant
random_increments = np.random.normal(0.0, 1.0 * h, N)*np.sqrt(dt) # the epsilon values
brownian_motion = np.cumsum(random_increments) # calculate the brownian motion
brownian_motion = np.insert(brownian_motion, 0, 0.0) # insert the initial condition
return brownian_motion, random_increments
N = 50 # the number of discrete steps
T = 1 # the number of continuous time steps
h = 1 # the variance of the increments
dt = 1.0 * T/N # total number of time steps
# generate a brownian motion
X, epsilon = brownian_motion(N, T ,h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment