楼主: casey_c
1685 3

[程序分享] Python 金融数据分析基础:随机数(二) [推广有奖]

  • 0关注
  • 10粉丝

博士生

92%

还不是VIP/贵宾

-

威望
0
论坛币
96 个
通用积分
2.1003
学术水平
2 点
热心指数
15 点
信用等级
2 点
经验
11502 点
帖子
278
精华
0
在线时间
94 小时
注册时间
2016-11-22
最后登录
2022-5-2

楼主
casey_c 发表于 2017-12-15 11:24:16 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币





以下内容转自 数析学院,只节选了部分,有需要的同学可以直接查看原文


本节为 Python 金融数据分析基础课程,将重点介绍使用 Numpy 生成随机数的方法,此外,也对随机数在金融方面的应用也进行了说明。建议初学者认真学习本节内容,已经掌握 numpy.random 的读者可以直接跳转至第二小节。


三、估值


1、欧式期权


  1. S0 = 100.
  2. r = 0.05
  3. sigma = 0.25
  4. T = 1.0
  5. I = 50000
  6. def gbm_mcs_stat(K):
  7.     ''' Valuation of European call option in Black-Scholes-Merton
  8.     by Monte Carlo simulation (of index level at maturity)
  9.    
  10.     Parameters
  11.     ==========
  12.     K : float
  13.         (positive) strike price of the option
  14.    
  15.     Returns
  16.     =======
  17.     C0 : float
  18.         estimated present value of European call option
  19.     '''
  20.     sn = gen_sn(1, I)
  21.     # 模拟成熟期的指数水平
  22.     ST = S0 * np.exp((r - 0.5 * sigma ** 2) * T
  23.                  + sigma * np.sqrt(T) * sn[1])
  24.     # 计算到期收益
  25.     hT = np.maximum(ST - K, 0)
  26.     # 计算MCS估计
  27.     C0 = np.exp(-r * T) * 1 / I * np.sum(hT)
  28.     return C0
复制代码
  1. gbm_mcs_stat(K=105.)
复制代码
10.044221852841922
  1. M = 50
  2. def gbm_mcs_dyna(K, option='call'):
  3.     ''' Valuation of European options in Black-Scholes-Merton
  4.     by Monte Carlo simulation (of index level paths)
  5.    
  6.     Parameters
  7.     ==========
  8.     K : float
  9.         (positive) strike price of the option
  10.     option : string
  11.         type of the option to be valued ('call', 'put')
  12.    
  13.     Returns
  14.     =======
  15.     C0 : float
  16.         estimated present value of European call option
  17.     '''
  18.     dt = T / M
  19.     # 模拟指数水平变化路径
  20.     S = np.zeros((M + 1, I))
  21.     S[0] = S0
  22.     sn = gen_sn(M, I)
  23.     for t in range(1, M + 1):
  24.         S[t] = S[t - 1] * np.exp((r - 0.5 * sigma ** 2) * dt
  25.                 + sigma * np.sqrt(dt) * sn[t])
  26.     # 基于案例的收益计算
  27.     if option == 'call':
  28.         hT = np.maximum(S[-1] - K, 0)
  29.     else:
  30.         hT = np.maximum(K - S[-1], 0)
  31.     # MCS估计器的计算
  32.     C0 = np.exp(-r * T) * 1 / I * np.sum(hT)
  33.     return C0
复制代码
  1. gbm_mcs_dyna(K=110., option='call')
复制代码
7.9500085250284336
  1. gbm_mcs_dyna(K=110., option='put')
复制代码
12.629934942682004
  1. from bsm_functions import bsm_call_value
  2. stat_res = []
  3. dyna_res = []
  4. anal_res = []
  5. k_list = np.arange(80., 120.1, 5.)
  6. np.random.seed(200000)
  7. for K in k_list:
  8.     stat_res.append(gbm_mcs_stat(K))
  9.     dyna_res.append(gbm_mcs_dyna(K))
  10.     anal_res.append(bsm_call_value(S0, K, T, r, sigma))
  11. stat_res = np.array(stat_res)
  12. dyna_res = np.array(dyna_res)
  13. anal_res = np.array(anal_res)
复制代码
  1. # 静态和动态蒙特卡洛估计值的比较
  2. # 标签: opt_val_comp_1
  3. # 尺寸: 60
  4. fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 6))
  5. ax1.plot(k_list, anal_res, 'b', label='analytical')
  6. ax1.plot(k_list, stat_res, 'ro', label='static')
  7. ax1.set_ylabel('European call option value')
  8. ax1.grid(True)
  9. ax1.legend(loc=0)
  10. ax1.set_ylim(ymin=0)
  11. wi = 1.0
  12. ax2.bar(k_list - wi / 2, (anal_res - stat_res) / anal_res * 100, wi)
  13. ax2.set_xlabel('strike')
  14. ax2.set_ylabel('difference in %')
  15. ax2.set_xlim(left=75, right=125)
  16. ax2.grid(True)
复制代码
1.jpg
以上内容转自 数析学院,如需完整内容可以直接查看原文
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python 金融数据分析 数据分析 金融数据 随机数

沙发
飞天玄舞6 在职认证  发表于 2017-12-15 18:21:21

藤椅
largered2 发表于 2017-12-15 22:54:07
厉害厉害

板凳
casey_c 发表于 2017-12-19 10:16:53

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-24 19:06