大数定律
您在上一个练习中已经了解到,由于蒙特卡洛模拟具有随机性,每次模拟的结果可能差异很大。这个练习中,您将利用大数定律,通过大量模拟的平均值来推断 2050 年的通胀水平。
您在上一练习中编写的 monte_carlo_inflation() 函数可直接使用。以下是该函数代码供参考:
def monte_carlo_inflation(year, seed):
random.seed(seed)
inflation_rate = 8.6
yearly_increase = random.randint(1, 3)
for i in range(year - 2022):
inflation_rate = inflation_rate * ((100 + yearly_increase)/100)
return(inflation_rate)
已为您导入 numpy 和 random 包。
本练习是课程的一部分
Python 中的蒙特卡洛模拟
练习说明
- 计算 1,000 次模拟的平均值。每次都在 0 到 20,000 之间随机选择一个种子。
- 计算 10,000 次模拟的平均值。每次都在 0 到 20,000 之间随机选择一个种子。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Calculate the average of 1,000 simulation results with a seed between 0 and 20000
rates_1 = []
for i in range(____):
seed = random.randint(____, ____)
rates_1.append(monte_carlo_inflation(2050, ____))
print(np.mean(rates_1))
# Calculate the average of 10,000 simulation results with a seed between 0 and 20000
rates_2 = []
for i in range(____):
seed = random.randint(____, ____)
rates_2.append(monte_carlo_inflation(2050, ____))
print(np.mean(rates_2))