Created
August 8, 2017 15:12
-
-
Save luk-f-a/51623e6d8d3a668da6645b9e949641e3 to your computer and use it in GitHub Desktop.
Brownian bridge simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gen_bw_bridge(steps, W, n): | |
""" | |
Given a column vector of end values, it will create n brownian bridge | |
simulations per value for the amount of steps as defined. | |
Can be easily generalized to include random initial values | |
and more general start and end times | |
steps: integer, how many time intervals there will be | |
between 0 and 1 | |
W: (sims, 1) nump vector with end values of brownian motion | |
n: integer, how many paths per end value to create | |
MIT License | |
""" | |
assert len(W.shape)==2 | |
assert W.shape[1]==1 | |
dt = 1.0 / steps | |
sims = W.shape[0] | |
W = W.repeat(n) | |
W = np.zeros((sims*n, steps+1)) | |
dW = np.zeros((sims*n, steps)) | |
s2=1 | |
s1=0 | |
x1=0 | |
x2=W | |
for s in range(1, steps+1): | |
t = s * dt | |
mean = ((s2-t)*x1+(t-s1)*x2)/(s2-s1) | |
var = (s2-t)*(t-s1)/(s2-s1) | |
W[:, s] = np.random.randn(sims*mult) * sqrt(var)+mean | |
s1 = t | |
x1 = W[:, s] | |
return W |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment