Python代码:几何布朗运动——怎么了?

时间:2021-10-13 12:37:40

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 really help, what's wrong, it's not creating a random process which looks like standard brownian motions with drift. My parameters like mu and sigma (expected return or drift and volatility) tend to change nothing but the slope of the noise process. That's my problem, it all looks like noise. Hope my problem is specific enough, here is my coode:

对于Python来说,我是一个新手,但是在大学里,我需要应用一些模型,最好是使用Python。我花了几天时间写了我附加的代码,但是我不能真正的帮助,什么是错误的,它不是创建一个随机的过程看起来像标准的布朗运动和漂移。我的参数如mu和sigma(预期返回或漂移和波动)只会改变噪声过程的斜率。这是我的问题,看起来像噪音。希望我的问题够具体,这是我的coode:

import math
from matplotlib.pyplot import *
from numpy import *
from numpy.random import standard_normal

'''
geometric brownian motion with drift!

Spezifikationen:

    mu=drift factor [Annahme von Risikoneutralitaet]
    sigma: volatility in %
    T: time span
    dt: lenght of steps
    S0: Stock Price in t=0
    W: Brownian Motion with Drift N[0,1] 
'''

T=1
mu=0.025
sigma=0.1
S0=20
dt=0.01

Steps=round(T/dt)

t=(arange(0, Steps))
x=arange(0, Steps)
W=(standard_normal(size=Steps)+mu*t)### standard brownian motion###
X=(mu-0.5*sigma**2)*dt+(sigma*sqrt(dt)*W) ###geometric brownian motion####
y=S0*math.e**(X)

plot(t,y)

show()

2 个解决方案

#1


15  

According to Wikipedia,

根据*,

Python代码:几何布朗运动——怎么了?

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代表时间范围,我认为应该是。

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

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

现在,根据这些Matlab示例(这里和这里),它出现了。

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

收益率

Python代码:几何布朗运动——怎么了?

#2


0  

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)

#1


15  

According to Wikipedia,

根据*,

Python代码:几何布朗运动——怎么了?

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代表时间范围,我认为应该是。

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

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

现在,根据这些Matlab示例(这里和这里),它出现了。

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

收益率

Python代码:几何布朗运动——怎么了?

#2


0  

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)