計算投資組合報酬
為了建立並回測一個投資組合,你需要能熟悉在單一物件中處理多檔資產的報酬。
在本練習中,你會使用 pandas 的 DataFrame 物件(已經以變數 StockReturns 儲存),來承載多檔資產的報酬,並計算一個模型投資組合的報酬。
這個模型投資組合是以預先定義的權重配置於 2017 年 1 月前全球市值最大的部分公司:
| Company Name | Ticker | Portfolio Weight |
|---|---|---|
| Apple | AAPL | 12% |
| Microsoft | MSFT | 15% |
| Exxon Mobil | XOM | 8% |
| Johnson & Johnson | JNJ | 5% |
| JP Morgan | JPM | 9% |
| Amazon | AMZN | 10% |
| General Electric | GE | 11% |
| FB | 14% | |
| AT&T | T | 16% |
請注意,多數情況下投資組合權重應加總為 100%
本練習屬於課程
Python 投資組合風險管理入門
練習說明
- 依上表的數值完成定義模型的 numpy 陣列
portfolio_weights。 - 使用
.mul()方法,將portfolio_weights乘到StockReturns的每一列,得到加權後的股票報酬。 - 接著在
WeightedReturns物件上,沿列使用.sum()方法來計算投資組合報酬。 - 最後,檢視隨時間累積報酬的圖形。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Finish defining the portfolio weights as a numpy array
portfolio_weights = np.array([0.12, 0.15, 0.08, 0.05, 0.09, 0.10, 0.11, ____, ____])
# Calculate the weighted stock returns
WeightedReturns = StockReturns.____(portfolio_weights, axis=____)
# Calculate the portfolio returns
StockReturns['Portfolio'] = WeightedReturns.____(axis=____)
# Plot the cumulative portfolio returns over time
CumulativeReturns = ((1+StockReturns["Portfolio"]).cumprod()-1)
CumulativeReturns.plot()
plt.show()