以下内容转自 数析学院,只节选了部分,有需要的同学可以直接查看原文
主要内容包括:
用 Numpy 生成不同分布的伪随机数,主要包括均匀分布、正太分布、直方分布和泊松分布。
用生成的随机数模拟随机变量、随机过程和进行方差缩减,主要包括几何布朗运动、平方根扩散、随机波动模型和跳跃扩散模型。
用生成的随机数对欧式期权和美式期权进行估值,主要使用蒙特卡洛估计方法。
用随机数建立模型进行风险测度,主要是测度风险价值和进行信用价值调整。
一、随机数
- import numpy as np
- import numpy.random as npr
- import matplotlib.pyplot as plt
- %matplotlib inline
- npr.rand(10)
- npr.rand(5, 5)
- a = 5.
- b = 10.
- npr.rand(10) * (b - a) + a
- npr.rand(5, 5) * (b - a) + a
- sample_size = 500
- rn1 = npr.rand(sample_size, 3)
- rn2 = npr.randint(0, 10, sample_size)
- rn3 = npr.sample(size=sample_size)
- a = [0, 25, 50, 75, 100]
- rn4 = npr.choice(a, size=sample_size)
- # 简单伪随机数
- # 标签: rand_samples
- # 大小: 70
- fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2,
- figsize=(7, 7))
- ax1.hist(rn1, bins=25, stacked=True)
- ax1.set_title('rand')
- ax1.set_ylabel('frequency')
- ax1.grid(True)
- ax2.hist(rn2, bins=25)
- ax2.set_title('randint')
- ax2.grid(True)
- ax3.hist(rn3, bins=25)
- ax3.set_title('sample')
- ax3.set_ylabel('frequency')
- ax3.grid(True)
- ax4.hist(rn4, bins=25)
- ax4.set_title('choice')
- ax4.grid(True)
- sample_size = 500
- rn1 = npr.standard_normal(sample_size)
- rn2 = npr.normal(100, 20, sample_size)
- rn3 = npr.chisquare(df=0.5, size=sample_size)
- rn4 = npr.poisson(lam=1.0, size=sample_size)
- # 不同分布的伪随机数
- # 标签: rand_distris
- # 大小: 70
- fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(7, 7))
- ax1.hist(rn1, bins=25)
- ax1.set_title('standard normal')
- ax1.set_ylabel('frequency')
- ax1.grid(True)
- ax2.hist(rn2, bins=25)
- ax2.set_title('normal(100, 20)')
- ax2.grid(True)
- ax3.hist(rn3, bins=25)
- ax3.set_title('chi square')
- ax3.set_ylabel('frequency')
- ax3.grid(True)
- ax4.hist(rn4, bins=25)
- ax4.set_title('Poisson')
- ax4.grid(True)
以上内容转自 数析学院,如需完整内容可以直接查看原文