Skip to content

Instantly share code, notes, and snippets.

@IlievskiV
Created April 13, 2020 22:50
Show Gist options
  • Save IlievskiV/39686652cacf72eebbd486e86e40c811 to your computer and use it in GitHub Desktop.
Save IlievskiV/39686652cacf72eebbd486e86e40c811 to your computer and use it in GitHub Desktop.
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])
# the probability to generate 1
p=0.5
# the epsilon values
random_increments = np.random.choice(increments, N, p)
# calculate the random walk
random_walk = np.cumsum(random_increments)
# return the entire walk and the increments
return random_walk, random_increments
# generate a random walk
N = 500
X, epsilon = random_walk(N)
# normalize the random walk using the Central Limit Theorem
X = X * np.sqrt(1./N)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment