生活随笔
收集整理的這篇文章主要介紹了
蒙特卡洛法与BS模型法分别实现欧式期权定价
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
蒙特卡洛法與BS模型法分別實現歐式期權定價
并且可以通過逐漸增大蒙特卡洛法的路徑數量以及每一條模擬路徑的步數(縮小步長)來驗證蒙特卡洛方法最終結果會收斂于BS模型計算結果。
"""
Created on Tue Sep 13 16:57:43 2022@author: liangshanliao
"""import math
import numpy
as np
from scipy
.stats
import norm
from numpy
.random
import standard_normal
import matplotlib
.pyplot
as plt
plt
.rcParams
['font.sans-serif'] = ['STLiti']
plt
.rcParams
['axes.unicode_minus'] = False def MonteCarlo(S0
,r
,T
,K
,sigma
,M
,I
):'''S0 : 標的資產初始價格K : 期權執行價格r : 無風險利率sigma : 預期波動率T:到期期限/剩余期限M :模擬步數I:模擬路徑條數'''dt
=T
/MS
=np
.zeros
((M
+1,I
))S
[0]=S0
for t
in range(1,M
+1):S
[t
]=S
[t
-1]*np
.exp
((r
-0.5*sigma
**2)*dt
+sigma
*np
.sqrt
(dt
)*standard_normal
(I
))outcome
=sum(np
.maximum
(0,S
[-1]-K
))*(np
.exp
(-r
*T
))/I
return outcome
,SS_m
,S
=MonteCarlo
(50,0.1,0.5,52,0.4,100,1000)
plt
.figure
(figsize
=(10,8))
plt
.hist
(S
[-1],bins
=50)
plt
.xlabel
(u'到期日時幾何布朗運動模擬的股票價格')
plt
.ylabel
(u'頻率')plt
.figure
(figsize
=(10,8))
plt
.plot
(S
[:100],lw
=1.5)
plt
.xlabel
(u'時間')
plt
.ylabel
(u'股票價格')
plt
.title
(u'幾何布朗運動下的股票價格路徑')def BS(St
,K
,sigma
,T
,r
,t
):'''BS模型計算期權的價格St:標的資產的價格;K:期權的執行價格;sigma:基礎資產價格百分比變化的年化波動率;r:無風險收益率;T:期權合約最長期限t:現時期限;'''d1
=(np
.log
((St
)/K
)+(r
+0.5*sigma
**2)*(T
-t
))/(sigma
*np
.sqrt
(T
-t
))d2
=d1
-sigma
*np
.sqrt
(T
-t
)value
=St
*norm
.cdf
(d1
)-K
*np
.exp
(-r
*(T
-t
))*norm
.cdf
(d2
)return valueS_bs
=BS
(50,52,0.4,0.5,0.1,0)
print("蒙特卡洛法計算結果是:",S_m
)
print("BS模型計算結果是:",S_bs
)
總結
以上是生活随笔為你收集整理的蒙特卡洛法与BS模型法分别实现欧式期权定价的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。