楼主: casey_c
1060 2

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

  • 0关注
  • 10粉丝

博士生

92%

还不是VIP/贵宾

-

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

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币




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




主要内容包括:


用 Numpy 生成不同分布的伪随机数,主要包括均匀分布、正太分布、直方分布和泊松分布。
用生成的随机数模拟随机变量、随机过程和进行方差缩减,主要包括几何布朗运动、平方根扩散、随机波动模型和跳跃扩散模型。
用生成的随机数对欧式期权和美式期权进行估值,主要使用蒙特卡洛估计方法。
用随机数建立模型进行风险测度,主要是测度风险价值和进行信用价值调整。

一、随机数


  1. import numpy as np
  2. import numpy.random as npr
  3. import matplotlib.pyplot as plt
  4. %matplotlib inline
复制代码
  1. npr.rand(10)
复制代码
array([ 0.38149781,  0.67472245,  0.52930643,  0.82789491,  0.32402136,        0.91759317,  0.70999488,  0.91402186,  0.38792711,  0.14082676])
  1. npr.rand(5, 5)
复制代码
array([[ 0.54056252,  0.34555326,  0.89572902,  0.26026525,  0.43574886],       [ 0.60084973,  0.20991542,  0.86092347,  0.26358636,  0.89639337],       [ 0.1549069 ,  0.45802605,  0.91081355,  0.08183648,  0.94818369],       [ 0.64659773,  0.44279118,  0.24208235,  0.91540828,  0.92345946],       [ 0.70147286,  0.14103389,  0.87856977,  0.57875223,  0.17051678]])
  1. a = 5.
  2. b = 10.
  3. npr.rand(10) * (b - a) + a
复制代码
array([ 6.91294755,  6.17425098,  7.04935596,  7.58279497,  8.91989095,        6.53815176,  8.52897859,  7.66654959,  8.37488787,  7.26440555])
  1. npr.rand(5, 5) * (b - a) + a
复制代码
array([[ 6.16321316,  9.32303829,  9.97670443,  8.60697599,  7.71428921],       [ 7.50955004,  5.74440407,  5.83774446,  7.50908669,  8.96393653],       [ 8.80295133,  6.53463824,  8.77144642,  5.92119901,  6.40813   ],       [ 6.10818935,  8.39621717,  7.15254198,  9.1797606 ,  7.80898567],       [ 8.9060506 ,  9.08388979,  9.78395034,  7.7338426 ,  8.73691598]])
  1. sample_size = 500
  2. rn1 = npr.rand(sample_size, 3)
  3. rn2 = npr.randint(0, 10, sample_size)
  4. rn3 = npr.sample(size=sample_size)
  5. a = [0, 25, 50, 75, 100]
  6. rn4 = npr.choice(a, size=sample_size)
复制代码
  1. # 简单伪随机数
  2. # 标签: rand_samples
  3. # 大小: 70
  4. fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2,
  5.                                              figsize=(7, 7))
  6. ax1.hist(rn1, bins=25, stacked=True)
  7. ax1.set_title('rand')
  8. ax1.set_ylabel('frequency')
  9. ax1.grid(True)
  10. ax2.hist(rn2, bins=25)
  11. ax2.set_title('randint')
  12. ax2.grid(True)
  13. ax3.hist(rn3, bins=25)
  14. ax3.set_title('sample')
  15. ax3.set_ylabel('frequency')
  16. ax3.grid(True)
  17. ax4.hist(rn4, bins=25)
  18. ax4.set_title('choice')
  19. ax4.grid(True)
复制代码
1.jpg
  1. sample_size = 500
  2. rn1 = npr.standard_normal(sample_size)
  3. rn2 = npr.normal(100, 20, sample_size)
  4. rn3 = npr.chisquare(df=0.5, size=sample_size)
  5. rn4 = npr.poisson(lam=1.0, size=sample_size)
复制代码
  1. # 不同分布的伪随机数
  2. # 标签: rand_distris
  3. # 大小: 70
  4. fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(7, 7))
  5. ax1.hist(rn1, bins=25)
  6. ax1.set_title('standard normal')
  7. ax1.set_ylabel('frequency')
  8. ax1.grid(True)
  9. ax2.hist(rn2, bins=25)
  10. ax2.set_title('normal(100, 20)')
  11. ax2.grid(True)
  12. ax3.hist(rn3, bins=25)
  13. ax3.set_title('chi square')
  14. ax3.set_ylabel('frequency')
  15. ax3.grid(True)
  16. ax4.hist(rn4, bins=25)
  17. ax4.set_title('Poisson')
  18. ax4.grid(True)
复制代码
2.jpg
以上内容转自 数析学院,如需完整内容可以直接查看原文

二维码

扫码加我 拉你入群

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

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


沙发
eeabcde 发表于 2017-10-21 22:05:09 |只看作者 |坛友微信交流群
谢谢楼主分享

使用道具

藤椅
casey_c 发表于 2017-10-24 10:51:03 |只看作者 |坛友微信交流群

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-25 01:20