S&P500 的 Sharpe 比率
在這個練習中,你要先從「只有價格資料」開始,計算 S&P500 的 Sharpe 比率。在下一個練習,你會用投資組合資料做同樣的事,這樣就能比較兩者的 Sharpe 比率。
已為你準備好的 S&P500 價格資料是 sp500_value。無風險利率在 rfr,而且方便地設為 0。來試試看吧!
本練習屬於課程
Python 投資組合分析入門
練習說明
- 使用索引對 S&P500 的價格資料
sp500_value計算總報酬,並將總報酬年化;資料涵蓋 4 年。 - 從 S&P500 的價格資料計算日報酬,你會用到它來計算波動度。
- 從報酬資料計算標準差,並以 250 個交易日將其年化。
- 最後,使用年化報酬與年化波動度計算 Sharpe 比率,並印出結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Calculate total return and annualized return from price data
total_return = (sp500_value[____] - ____[____]) / ____[____]
# Annualize the total return over 4 year
annualized_return = ((____ + ____)**(____/____))-1
# Create the returns data
returns_sp500 = ____.____()
# Calculate annualized volatility from the standard deviation
vol_sp500 = ____.____() * np.sqrt(____)
# Calculate the Sharpe ratio
sharpe_ratio = ((____ - rfr) / ____)
print (sharpe_ratio)