Skip to content

Instantly share code, notes, and snippets.

@IlievskiV
Last active May 15, 2020 15:38
Show Gist options
  • Save IlievskiV/e21690cd8018ded352450f7b6f06967b to your computer and use it in GitHub Desktop.
Save IlievskiV/e21690cd8018ded352450f7b6f06967b to your computer and use it in GitHub Desktop.
import numpy as np
def gbm_mean(G0, mu, sigma, N, T):
"""Simulates the mean of the Geometric Brownian Motion, which is:
E(t) = G0*e^{(mu + sigma^{2}/2)*t}
:param float G0: initial value
:param float mu: drift coefficient
:param float sigma: diffusion coefficient
:param int N: number of discrete steps
:param int T: number of continuous time steps
"""
# generate the time steps
t = np.linspace(0.0, T, N+1)
# calculate the mean
E = G0 * np.exp((mu + 0.5*sigma**2)*t)
return E
def gbm_var(G0, mu, sigma, N, T):
"""Simulates the variance of the Geometric Brownian Motion, which is:
Var(t) = G0^2 * e^{(2*mu + sigma^{2})*t} * (e^{sigma^{2}*t} - 1)
:param float G0: initial value
:param float mu: drift coefficient
:param float sigma: diffusion coefficient
:param int N: number of discrete steps
:param int T: number of continuous time steps
"""
# generate the time steps
t = np.linspace(0.0, T, N+1)
# calculate the variance
V = G0**2 * np.exp(t * (2*mu + sigma**2)) * (np.exp(t * sigma**2) - 1)
return V
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment