以下内容转自 数析学院,只节选了部分,有需要的同学可以直接查看原文
本节为 Python 金融数据分析基础课程,将重点介绍使用 Numpy 生成随机数的方法,此外,也对随机数在金融方面的应用也进行了说明。建议初学者认真学习本节内容,已经掌握 numpy.random 的读者可以直接跳转至第二小节。
三、估值
1、欧式期权
- S0 = 100.
- r = 0.05
- sigma = 0.25
- T = 1.0
- I = 50000
- def gbm_mcs_stat(K):
- ''' Valuation of European call option in Black-Scholes-Merton
- by Monte Carlo simulation (of index level at maturity)
-
- Parameters
- ==========
- K : float
- (positive) strike price of the option
-
- Returns
- =======
- C0 : float
- estimated present value of European call option
- '''
- sn = gen_sn(1, I)
- # 模拟成熟期的指数水平
- ST = S0 * np.exp((r - 0.5 * sigma ** 2) * T
- + sigma * np.sqrt(T) * sn[1])
- # 计算到期收益
- hT = np.maximum(ST - K, 0)
- # 计算MCS估计
- C0 = np.exp(-r * T) * 1 / I * np.sum(hT)
- return C0
- gbm_mcs_stat(K=105.)
- M = 50
- def gbm_mcs_dyna(K, option='call'):
- ''' Valuation of European options in Black-Scholes-Merton
- by Monte Carlo simulation (of index level paths)
-
- Parameters
- ==========
- K : float
- (positive) strike price of the option
- option : string
- type of the option to be valued ('call', 'put')
-
- Returns
- =======
- C0 : float
- estimated present value of European call option
- '''
- dt = T / M
- # 模拟指数水平变化路径
- S = np.zeros((M + 1, I))
- S[0] = S0
- sn = gen_sn(M, I)
- for t in range(1, M + 1):
- S[t] = S[t - 1] * np.exp((r - 0.5 * sigma ** 2) * dt
- + sigma * np.sqrt(dt) * sn[t])
- # 基于案例的收益计算
- if option == 'call':
- hT = np.maximum(S[-1] - K, 0)
- else:
- hT = np.maximum(K - S[-1], 0)
- # MCS估计器的计算
- C0 = np.exp(-r * T) * 1 / I * np.sum(hT)
- return C0
- gbm_mcs_dyna(K=110., option='call')
- gbm_mcs_dyna(K=110., option='put')
- from bsm_functions import bsm_call_value
- stat_res = []
- dyna_res = []
- anal_res = []
- k_list = np.arange(80., 120.1, 5.)
- np.random.seed(200000)
- for K in k_list:
- stat_res.append(gbm_mcs_stat(K))
- dyna_res.append(gbm_mcs_dyna(K))
- anal_res.append(bsm_call_value(S0, K, T, r, sigma))
- stat_res = np.array(stat_res)
- dyna_res = np.array(dyna_res)
- anal_res = np.array(anal_res)
- # 静态和动态蒙特卡洛估计值的比较
- # 标签: opt_val_comp_1
- # 尺寸: 60
- fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 6))
- ax1.plot(k_list, anal_res, 'b', label='analytical')
- ax1.plot(k_list, stat_res, 'ro', label='static')
- ax1.set_ylabel('European call option value')
- ax1.grid(True)
- ax1.legend(loc=0)
- ax1.set_ylim(ymin=0)
- wi = 1.0
- ax2.bar(k_list - wi / 2, (anal_res - stat_res) / anal_res * 100, wi)
- ax2.set_xlabel('strike')
- ax2.set_ylabel('difference in %')
- ax2.set_xlim(left=75, right=125)
- ax2.grid(True)
以上内容转自 数析学院,如需完整内容可以直接查看原文


雷达卡






京公网安备 11010802022788号







