Python代码:几何布朗运动-怎么了? [英] Python Code: Geometric Brownian Motion - what's wrong?

查看:708
本文介绍了Python代码:几何布朗运动-怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python还是很陌生,但是对于大学的一篇论文,我需要应用一些模型,最好使用Python.我花了几天的时间来编写代码,但是我实在无济于事,这是怎么回事,它没有创建一个看起来像带有漂移的标准布朗运动的随机过程.我的参数,例如mu和sigma(预期的收益或漂移和波动率),除了噪声过程的斜率外,什么都不会改变.那是我的问题,看起来都像噪音.希望我的问题足够具体,这是我的诀窍:

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:

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()

推荐答案

根据 Wikipedia

看来

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

而不是

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


由于T代表时间范围,所以我认为t应该是


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

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


现在,根据这些Matlab示例(此处


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 ###

不是

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()

收益

这篇关于Python代码:几何布朗运动-怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆