Monte Carlo 模擬
Monte-Carlo 模擬 用來建構各種可能性的模型。
Monte-Carlo 可以用許多方式建立,但它們都會針對某個既定模型產生大量隨機變體,讓你能分析廣泛分佈的可能路徑。這有助於在歷史資料不多的情況下,也能建立一個可供取樣的完整可能性預測。
為 USO 原油 ETF 產生 100 條 Monte-Carlo 模擬路徑。
參數 mu、vol、T 與 S0 已從上一個練習提供。
本練習屬於課程
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()