说明:本系列实验全部在优矿平台上进行。
import seaborn as sns
from CAL.PyCAL import *
import numpy as np
import scipy
spot = 2.45
strike = 2.50
maturity = 0.25
r = 0.05
vol = 0.25
def call_option_pricer_monte_carlo(spot,strike,maturity,r,vol,numOfPath=5000):
randomSeries=scipy.random.randn(numOfPath)
s_t=spot*np.exp((r-0.5*vol**2)*maturity+randomSeries*vol*np.sqrt(maturity))
sumValue=np.maximum(s_t-strike,0.0).sum()
price=np.exp(-r*maturity)*sumValue/numOfPath
return price
print '期权价格(蒙特卡洛) : %.4f' % call_option_pricer_monte_carlo(spot, strike, maturity, r, vol)
pathScenario = range(1000, 50000, 1000)
numOfTrials = 100
confidenceIntervalUpper = []
confidenceIntervalLower = []
means = []
for scenario in pathScenario:
res=np.zeros(numOfTrials)
for i in range(numOfTrials):
res[i]=call_option_pricer_monte_carlo(spot,strike,maturity,r,vol,numOfPath=scenario)
means.append(res.mean())
confidenceIntervalUpper.append(res.mean()+1.96*res.std())
confidenceIntervalLower.append(res.mean()-1.96*res.std())
pylab.figure(figsize=(12,8))
table=np.array([means,confidenceIntervalUpper,confidenceIntervalLower]).T
pylab.plot(pathScenario,table)
pylab.title(u'期权蒙特卡洛模拟',fontproperties=font,fontsize=18)
pylab.legend([u'均值',u'95%置信区间上界',u'95%置信区间下界'],prop=font)
pylab.xlabel(u'模拟次数',fontproperties=font,fontsize=15)
pylab.ylabel(u'价格',fontproperties=font,fontsize=15)
pylab.grid(True)
Paste_Image.png
网友评论