发布日期:2024-12-04 10:59 点击次数:141
「基本情势」:
假定股票价钱适合几何布朗指示,即
简化处置,取得特定时辰(0,T)财富价钱变化历程:
于是取得:
也可暗意为:
其中为收益率均值,为收益率方差,服从t漫衍或正态漫衍。
则收益率为:
蒙特卡罗模拟法的的Python竣事蒙特卡洛模拟法模拟股票收益率序列收益率为:
import numpy as npfrom scipy import statsimport matplotlib.pyplot as pltimport pandas as pdimport 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=2array_return = generate_simulated_stock_returns(t,r,sigma,nper_per_year)print(array_return)#2年期、每年24期t=2;nper_per_year=24array_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=2array_price = generate_simulated_stock_values(s,t,r,sigma,nper_per_year)print(array_price)#2年期、每年24期t=2;nper_per_year=24array_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=5plot_simulated_stock_values(s,t,r,sigma,nper_per_year,num_trials)
图片
# 2年期、每年250期,模拟1000次t=2;nper_per_year=250;num_trials=1000plot_simulated_stock_values(s,t,r,sigma,nper_per_year,num_trials)图片
本站仅提供存储处事,系数骨子均由用户发布,如发现存害或侵权骨子,请点击举报。