說明:本系列實驗全部在優(yōu)礦平臺上進行。
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 '期權(quán)價格(蒙特卡洛) : %.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'期權(quán)蒙特卡洛模擬',fontproperties=font,fontsize=18)
pylab.legend([u'均值',u'95%置信區(qū)間上界',u'95%置信區(qū)間下界'],prop=font)
pylab.xlabel(u'模擬次數(shù)',fontproperties=font,fontsize=15)
pylab.ylabel(u'價格',fontproperties=font,fontsize=15)
pylab.grid(True)

Paste_Image.png