開始使用免費開始

調整 span 參數

在上一個練習中,你發現指數加權的風險與報酬所使用的 span 會影響最佳投資組合的結果。事實上,span 的影響非常大!透過設定 span,你可以只使用最近的的資料,或改用最近數年的資料。極限情況下,當 span 與完整樣本一樣長時,就等同於使用一般的歷史平均值。

現在,讓我們感受看看短 span 與長 span 會如何改變你的最佳投資組合。已提供 stock_prices 資料。

本練習屬於課程

Python 投資組合分析入門

檢視課程

動手互動練習

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

# Calculate expected returns and sample covariance
mu_ema = expected_returns.ema_historical_return(stock_prices, span=____ ,frequency=252)
Sigma_ew = risk_models.exp_cov(stock_prices, span=____, frequency=252)
ef_2 = EfficientFrontier(mu_ema, Sigma_ew)

# Calculate weights for the maximum Sharpe ratio portfolio
weights = ef_2.max_sharpe()
cleaned_weights_maxsharpe_EW = ef_2.clean_weights()
perf_max_sharpe_EW = ef_2.portfolio_performance(verbose=True)
編輯並執行程式碼