python模拟布朗运动_Python代码:几何布朗运动-怎么了?

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

解决方案

According to Wikipedia,

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值