開始使用免費開始

繪製有效邊界並標示最佳 Sharpe 比率

現在再繪一次有效邊界,但這次加上 Sharpe 指標最佳投資組合的標記。將資料視覺化能幫助你更清楚地理解結果。

回顧一下,有效邊界是以散佈圖呈現:x 軸是投資組合波動度,y 軸是投資組合報酬。我们將從 covariances.keys() 取得資料中的最新日期(其實也可以用 portfolio_returns 等任一字典來取日期)。接著,從 portfolio_volatilityportfolio_returns 取出該最新日期的波動度與報酬。最後,從 max_sharpe_idxs[date] 取得 Sharpe 指標最佳的投資組合索引,並用 plt.scatter() 把所有點畫出來。

本練習屬於課程

Python 金融 Machine Learning

檢視課程

練習說明

  • cur_volatility 設為最新 date 的投資組合波動度。
  • 以 x 軸為波動度、y 軸為報酬,繪製「efficient frontier」圖。
  • max_sharpe_idxs 取得最新 date 的最佳投資組合索引。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Get most recent (current) returns and volatility
date = sorted(covariances.keys())[-1]
cur_returns = portfolio_returns[date]
cur_volatility = ____

# Plot efficient frontier with sharpe as point
plt.scatter(x=____, y=____, alpha=0.1, color='blue')
best_idx = max_sharpe_idxs[____]

# Place an orange "X" on the point with the best Sharpe ratio
plt.scatter(x=cur_volatility[best_idx], y=cur_returns[best_idx], marker='x', color='orange')
plt.xlabel('Volatility')
plt.ylabel('Returns')
plt.show()
編輯並執行程式碼