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

前端 未结 2 1788
执念已碎
执念已碎 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: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

提交回复
热议问题