开始使用免费开始使用

Monte Carlo 模拟

Monte-Carlo 模拟用于刻画广泛的可能性范围。

Monte-Carlo 可以用多种方式构建,但它们都涉及为给定模型生成大量随机变体,从而分析可能路径的宽分布。这使您即使没有大量历史数据,也能构建可供采样的全面情景预测。

为 USO 原油 ETF 生成 100 次 Monte-Carlo 模拟。

参数 muvolTS0 已在上一个练习中提供。

本练习是课程的一部分

Python 投资组合风险管理入门

查看课程

练习说明

  • 使用 range() 函数从 0 循环到 100(不包含 100)。
  • 在每次迭代中调用绘图函数 plt.plot(),将 T 的取值范围 range(T) 作为第一个参数,将 forecasted_values 作为第二个参数。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Loop through 100 simulations
for i in ____:

    # Generate the random returns
    rand_rets = np.random.normal(mu, vol, T) + 1
    
    # Create the Monte carlo path
    forecasted_values = S0*(rand_rets).cumprod()
    
    # Plot the Monte Carlo path
    plt.plot(____, ____)

# Show the simulations
plt.show()
编辑并运行代码