繪製有效邊界(efficient frontier)
我們終於可以把 MPT 投資組合的結果畫出來,並呈現「有效邊界(efficient frontier)」。這是將波動度與報酬率對照的散佈圖,有助於你視覺化投資組合的風險-報酬可能性。點雲左上方的邊界代表在特定風險下報酬最高的解,也就是有效邊界。
為了建立這張圖,我們會使用幾個練習前建立的 covariances 字典中的最新日期。該字典以日期作為鍵,所以我們會用 sorted() 搭配 .keys() 取得排序後的鍵,然後用 Python 索引([-1])抓到最後一筆。最後,再用 matplotlib 將變異數與報酬率畫成散佈圖,觀察資料中最新日期的有效邊界。
本練習屬於課程
Python 金融 Machine Learning
練習說明
- 從
covariances字典取得最新日期——記得日期是鍵。 - 以散佈圖繪製最新日期的波動度對報酬率(portfolio_returns),並將透明度
alpha設為0.1。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Get latest date of available data
date = sorted(covariances.____)[____]
# Plot efficient frontier
# warning: this can take at least 10s for the plot to execute...
plt.scatter(x=portfolio_volatility[date], y=____, alpha=____)
plt.xlabel('Volatility')
plt.ylabel('Returns')
plt.show()