投資組合標準差
為了計算投資組合的波動度,你需要用到共變異數矩陣、投資組合權重,以及轉置運算的知識。numpy 陣列的轉置可以使用 .T 屬性。np.dot() 函式會計算兩個陣列的內積。
投資組合波動度的公式為:
$$ \sigma_{Portfolio} = \sqrt{ w_T \cdot \Sigma \cdot w } $$
- $ \sigma_{Portfolio} $:投資組合波動度
- $ \Sigma $:報酬率的共變異數矩陣
- w:投資組合權重(\( w_T \) 為權重的轉置)
- $ \cdot $:內積運算子
portfolio_weights 與 cov_mat_annual 已經在你的工作區中可用。
本練習屬於課程
Python 投資組合風險管理入門
練習說明
依照上方公式,使用 portfolio_weights 計算投資組合的波動度。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import numpy as np
import numpy as np
# Calculate the portfolio standard deviation
portfolio_volatility = ____(np.dot(portfolio_weights.T, np.dot(cov_mat_annual, portfolio_weights)))
print(portfolio_volatility)