Skip to content

Instantly share code, notes, and snippets.

@IlievskiV
Last active April 30, 2020 22:40
Show Gist options
  • Save IlievskiV/ef340bd5ba0fdb63bb1f09658b45653c to your computer and use it in GitHub Desktop.
Save IlievskiV/ef340bd5ba0fdb63bb1f09658b45653c to your computer and use it in GitHub Desktop.
import numpy as np
def drifted_brownian_motion(mu, sigma, N, T, seed=42):
"""Simulates a Brownian Motion with drift.
:param float mu: drift coefficient
:param float sigma: volatility coefficient
:param int N : number of discrete steps
:param int T: number of continuous time steps
:param int seed: initial seed of the random generator
:returns list: drifted Brownian motion
"""
# set the seed
np.random.seed(seed)
# standard brownian motion
W, _ = brownian_motion(N, T ,1.0)
# the normalizing constant
dt = 1. * T/N
# generate the time steps
time_steps = np.linspace(0.0, N*dt, N+1)
# calculate the Brownian Motion with drift
X = mu * time_steps + sigma * W
return X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment