I am trying to generate a numerical solution to the SDE for Geometric Brownian Motion.
The stochastic process is given by $S_t = \exp(\sigma W_t + \mu t)$, and by Ito's lemma, we have that the SDE is given by:
$dS_t = \sigma S_t dW_t + (\mu + \frac{1}{2} \sigma^2) S_t dt$.
For some reason, I can't get my code to work. I tried following the procedure to a T, but my code is only plotting a straight (horizontal) line. Can anyone tell me what is wrong?
# This program is to practice solving SDEs numerically:
# For the stochastic process of Geometric Brownian Motion,
# S_t = S_0 exp( sigma * W_t + mu * t)
# The SDE is given by:
# dS_t = sigma * S_t * dW_t + (mu + 1\2 sigma^2) * S_t * dt
# We will apply the Euler-Maruyama scheme to solve the SDE
import numpy as np
import math
import matplotlib.pyplot as plt
#parameters
mu = 0.75
sigma = 0.30
#time-scheme
t_init = 0
t_end = 2
s_init = 0
N = 1000
dt = float(t_end - t_init) / N
sqrtdt = np.sqrt(dt)
t = np.arange(t_init, t_end, dt)
s = np.zeros(N)
s[0] = s_init
for i in xrange(1, t.size):
dW = np.random.normal(loc = 0.0, scale = 1.0) * sqrtdt
s[i] = s[i-1] + (sigma * s[i-1] * dW) + (mu + 0.5 * math.pow(sigma, 2.0)) * s[i-1] * dt
plt.plot(t, s)
plt.show()