I tried simulating GBM using both the SDE definition and the closed form solution. The paths I get through these methods are very different. Can someone help me figure my mistake?enter image description here

import numpy as np
import matplotlib.pyplot as mp
import statsmodels.api as sm

time_step = 1e-6
N = 30000

A = np.zeros((N,1))
A[0] = 2
B = np.zeros((N,1))
B[0] = 2
volvol = 40.0

s = np.sqrt(time_step)

y = np.exp(-volvol**2.0/2 * time_step + volvol * s * np.random.normal(0, 1, N))
y1 = np.random.normal(0, s, N)

for i in np.arange(1, N, 1):
   A[i] = A[i-1] * y[i]

for i in np.arange(1, N, 1):
    dB = volvol * B[i - 1] * y1[i]
    B[i] = B[i-1]+ dB

mp.plot(A, label = 'exp')
mp.plot(B, label = 'SDE')

mp.legend(loc='lower right', ncol=1, fancybox=True, shadow=True, prop={'size': 6})
mp.show()[![enter image description here][1]][1]

Here are the things you need to correct in your code:

Although you are setting a seed, you are generating the random numbers twice, and therefore they are not identical. Try this:

rand = np.random.randn(N)
y = np.exp(-volvol**2.0/2 * time_step + volvol * s * rand)
y1 = rand

You also need to multiply the sigma by the square root of the timestep in the SDE version :

dB = volvol * B[i-1] * y1[i] * s

That should be enough to get the same results.

Apart from that, your volatility is 4000%. The sigma should be 0.4 and not 40.0.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.