使用python與蒙地卡羅模擬法(Monte Carlo simulation)定價選擇權
蒙地卡羅評價選擇權主要步驟是先模擬多條股價走勢,再根據選擇權的payoff評價並取期望值,最後則是用折現回來。
首先假設股價走勢服從幾何布朗運動,且是對數常態分配,在搭配Ito lemma則可以得到以下:
ϵ是服從標準常態的隨機變數
r是無風險利率
St+1,St是下一期股價以及本期股價
σ是標的資產的報酬標準差
dt是變動期數或是切割期數,例如1年切割365天,則 dt就是1/365
import numpy as np
#parameter setting
np.random.seed(111)
S = 14460 #現貨價
K = 14650 #履約價
r = 0.01 #無風險利率
sigma = 0.05 #現貨的報酬標準差
T = 1 #假設到期1年
days = 1 #每幾天抽樣一次
steps = int(365/days)#切割期數
N = 10000 #模擬路徑
dt = T/steps #delta t
epsilon = np.random.normal(size=(steps, N))
ST = np.log(S) + np.cumsum((r - 0.5*sigma**2)*dt + sigma * epsilon * np.sqrt(dt), axis=0)
ST = np.exp(ST)
將模擬的股價路徑繪製出來。
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['xtick.labelsize'] = 16
mpl.rcParams['ytick.labelsize'] = 16
plt.plot(ST)
plt.xlabel('Period', fontsize=14)
plt.ylabel('Underlying price', fontsize=14)
plt.title('Monte Carlo simulation', fontsize=16)
# plt.savefig('/CallOption.png', bbox_inches='tight')#save fig
plt.show()
接著使用選擇權的payoff公式評價,再取期望值,最後則是折現
#示範call
CT = np.maximum(ST[-1]-K, 0).mean()#max(ST-K, 0)
Ct = CT*np.exp(-r*T) #折現
為了方便使用,將本文的程式碼變成函式庫def
。
def MCS(S, steps, N, dt, r):
epsilon = np.random.normal(size=(steps, N))
ST = np.log(S) + np.cumsum((r - 0.5*sigma**2)*dt + sigma * epsilon * np.sqrt(dt), axis=0)
ST = np.exp(ST)
return ST
#parameter setting
np.random.seed(111)
S = 14460 #現貨價
K = 14650 #履約價
r = 0.01 #無風險利率
sigma = 0.05 #現貨的報酬標準差
T = 1 #假設到期1年
days = 1 #每幾天抽樣一次
steps = int(365/days)#切割期數
N = 10000 #模擬路徑
dt = T/steps #delta t
ST = MCS(S, steps, N, dt, r)
CT = np.maximum(ST[-1]-K, 0).mean()
Ct = CT*np.exp(-r*T)
print('CT: ', CT, 'Ct: ', Ct)
PT = np.maximum(K-ST[-1], 0).mean()
Pt = PT*np.exp(-r*T)
print('PT: ', PT, 'Pt: ', Pt)
本文程式碼在https://github.com/MinKuanIsHere/OptionPricing/blob/main/MCS.ipynb