Skip to content

Instantly share code, notes, and snippets.

View IlievskiV's full-sized avatar
🐻‍❄️

Vladimir Ilievski IlievskiV

🐻‍❄️
View GitHub Profile
def geometric_brownian_motion(G0, mu, sigma, N, T):
"""Simulates a Geometric Brownian Motion.
: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
:return list: the geometric Brownian Motion
"""
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
# import libraries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# ignore warnings
import warnings
warnings.filterwarnings("ignore")
def brownian_motion(N, T, h, seed=42):
from scipy import stats
num_simulations = 10000 # how many times to repeat
Ns, Ts, hs = 20000, 10.0, 1.0 # discrete steps, continuous steps, veriance
dts = 1.0 * T/N # total number of time steps
u = 2. # the difference in time points
t = int(np.floor((np.random.uniform(low=u+0.01, high=1. * T - u)/T) * N)) # random starting point
# initialize the means
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig = plt.figure(figsize=(21, 10))
ax = plt.axes(xlim=(0, 1))
line, = ax.step([], [], where='mid', color='#0492C2')
# formatting options
ax.set_xticks(np.linspace(0,1,11))
ax.set_xlabel('Time', fontsize=30)
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
"""
num_simulations = 10000 # num of simulation
N = 100000 # number od steps in each simulation
dt = 1./N # the time step
X_norm = [0] * num_simulations # the normalized random variable
# run the simulations
for i in range(num_simulations):
X, _ = random_walk(N)
X_norm[i] = X[N - 1] * np.sqrt(dt)
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure(figsize=(21, 10))
ax = plt.axes(xlim=(0, N), ylim=(np.min(X) - 0.5, np.max(X) + 0.5))
line, = ax.plot([], [], lw=2, color='#0492C2')
ax.set_xticks(np.arange(0, N+1, 50))
ax.set_yticks(np.arange(np.min(X) - 0.5, np.max(X) + 0.5, 0.2))
ax.set_title('2D Random Walk', fontsize=22)
ax.set_xlabel('Steps', fontsize=18)
import numpy as np
np.random.seed(1234)
def random_walk(N):
"""
Simulates a discrete random walk
:param int N : the number of steps to take
"""
# event space: set of possible increments
increments = np.array([1, -1])
@IlievskiV
IlievskiV / random_walk_animated.ipynb
Created March 31, 2020 09:03
Radnom Walk Animated using the MatplotLib's FuncAnimation
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.