Python Code: Geometric Brownian Motion - what's wrong?

前端 未结 2 1802
执念已碎
执念已碎 2021-02-01 10:34

I\'m pretty new to Python, but for a paper in University I need to apply some models, using preferably Python. I spent a couple of days with the code I attached, but I can\'t re

相关标签:
2条回答
  • 2021-02-01 10:37

    An additional implementation using the parametrization of the gaussian law though the normal fonction (instead of standard_normal), a bit shorter.

    import numpy as np
    
    T = 2
    mu = 0.1
    sigma = 0.01
    S0 = 20
    dt = 0.01
    N = round(T/dt)
    # reversely you can specify N and then compute dt, which is more common in financial litterature
    
    X = np.random.normal(mu * dt, sigma* np.sqrt(dt), N)
    X = np.cumsum(X)
    S = S0 * np.exp(X)
    
    0 讨论(0)
  • 2021-02-01 10:51

    According to Wikipedia,

    enter image description here

    So it appears that

    X=(mu-0.5*sigma**2)*t+(sigma*W) ###geometric brownian motion#### 
    

    rather than

    X=(mu-0.5*sigma**2)*dt+(sigma*sqrt(dt)*W)
    

    Since T represents the time horizon, I think t should be

    t = np.linspace(0, T, N)
    

    Now, according to these Matlab examples (here and here), it appears

    W = np.random.standard_normal(size = N) 
    W = np.cumsum(W)*np.sqrt(dt) ### standard brownian motion ###
    

    not,

    W=(standard_normal(size=Steps)+mu*t)
    

    Please check the math, however, I could be wrong.


    So, putting it all together:

    import matplotlib.pyplot as plt
    import numpy as np
    
    T = 2
    mu = 0.1
    sigma = 0.01
    S0 = 20
    dt = 0.01
    N = round(T/dt)
    t = np.linspace(0, T, N)
    W = np.random.standard_normal(size = N) 
    W = np.cumsum(W)*np.sqrt(dt) ### standard brownian motion ###
    X = (mu-0.5*sigma**2)*t + sigma*W 
    S = S0*np.exp(X) ### geometric brownian motion ###
    plt.plot(t, S)
    plt.show()
    

    yields

    enter image description here

    0 讨论(0)
提交回复
热议问题