蒙特卡罗模拟法
「基本步骤」:
假设股票价格符合几何布朗运动,即
简化处理,得到特定时期(0,T)资产价格变化过程:
于是得到:
也可表示为:
其中为收益率均值,为收益率方差,服从t分布或正态分布。
则收益率为:
蒙特卡罗模拟法的的Python实现
蒙特卡洛模拟法模拟股票收益率序列
收益率为:
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import pandas as pd
import math
'''
s:股票现价
t:期限(年)
r:股票年化收益率
sigma:股票年化波动率
nper_per_year:每年的期数
'''
def generate_simulated_stock_returns(t,r,sigma,nper_per_year):
simulated_returns=[]
dt=1/nper_per_year
term = int(t*nper_per_year)
for i in range (1, term+1):
z=np.random.normal()
simulated_return = (r-(sigma**2/2))*dt + z*sigma*(dt**(1/2))
simulated_returns.append(simulated_return)
array_return=np.array(simulated_returns)
return array_return
# 初始股价s:100; 预期收益率r:10%;标准差:30%
s=100;r=0.1;sigma=0.3
#1年期、每年2期
t=1;nper_per_year=2
array_return = generate_simulated_stock_returns(t,r,sigma,nper_per_year)
print(array_return)
#2年期、每年24期
t=2;nper_per_year=24
array_return = generate_simulated_stock_returns(t,r,sigma,nper_per_year)
print(array_return)
[ 0.21738696 -0.06383675]
[ 0.02899686 0.00131385 -0.09489962 -0.00440415 -0.0357566 -0.05227566
0.07905745 -0.03065636 -0.01726008 -0.0059791 0.05072394 0.01448947
0.03098366 -0.05170335 0.0161574 -0.18380967 -0.0629412 0.00289641
0.14890079 -0.05693315 0.0931597 0.0037413 -0.05493882 0.12309281
0.06119329 0.04241972 -0.02030099 -0.05180438 -0.05970102 0.0229074
0.12618542 0.0770313 0.05075201 -0.04261307 0.00168359 0.03529421
0.0850315 -0.09281302 -0.08985412 0.02220526 0.01642511 0.04967819
0.07372143 -0.01799848 0.05595597 -0.00384655 -0.09679426 -0.08459783]
蒙特卡洛模拟法模拟股价序列
股价为:
def generate_simulated_stock_values(s,t,r,sigma,nper_per_year):
rate=generate_simulated_stock_returns(t,r,sigma,nper_per_year)
stock_price = [s]
term = int(t*nper_per_year)
for i in range(1, term+1):
values = stock_price[i-1]*math.e**(rate[i-1])
stock_price.append(values)
array_price = np.array(stock_price)
return array_price
#1年期、每年2期
t=1;nper_per_year=2
array_price = generate_simulated_stock_values(s,t,r,sigma,nper_per_year)
print(array_price)
#2年期、每年24期
t=2;nper_per_year=24
array_price = generate_simulated_stock_values(s,t,r,sigma,nper_per_year)
print(array_price)
[100. 105.03146796 100.4594981 ]
[100. 95.90978914 95.65450188 104.70632493 102.12337933
104.26726892 99.06536039 100.63054422 93.6685905 88.57596138
92.41510048 89.91265499 86.27490259 87.29911775 84.26798089
86.5798334 87.06325173 87.61229376 81.72201584 85.49976969
82.96816113 80.11385795 83.01588423 77.73720797 72.770712
63.60523084 65.39745198 69.02682262 67.64864604 62.52653157
61.57041633 58.01208479 62.16882528 66.41108904 66.4236716
59.67428405 68.38557448 70.2657609 75.26920257 77.15860959
80.52151818 74.45625968 71.23642008 70.7874225 69.68587971
75.54529952 67.20571691 67.86359575 67.393064 ]
蒙特卡洛模拟法绘制模拟股价序列图
def plot_simulated_stock_values(s,t,r,sigma,nper_per_year,num_trials=1):
term = int(t*nper_per_year) + 1
x_axis = np.linspace(0,t,term)
for i in range(num_trials):
price=generate_simulated_stock_values(s,t,r,sigma,nper_per_year)
plt.plot(x_axis, price)
plt.title(str(num_trials)+" simulated trials")
plt.xlabel("years")
plt.ylabel("value")
plt.show()
# 2年期、每年250期,模拟5次
t=2;nper_per_year=250;num_trials=5
plot_simulated_stock_values(s,t,r,sigma,nper_per_year,num_trials)
# 2年期、每年250期,模拟1000次
t=2;nper_per_year=250;num_trials=1000
plot_simulated_stock_values(s,t,r,sigma,nper_per_year,num_trials)
原文始发于微信公众号(Python for Finance):【Python量化】蒙特卡洛模拟法预测股价走势
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/45675.html