# Why do I get this difference when simulating geometric Brownian motion?

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?

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

time_step = 1e-6
N = 30000

np.random.seed(987654321)
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):
print(B)
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.grid()
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.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.7751515507698059, "perplexity": 4842.225018835337}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-10/segments/1581875146341.16/warc/CC-MAIN-20200226084902-20200226114902-00158.warc.gz"}