計算平均報酬
在這個練習中,你要計算一個由 4 檔股票組成的投資組合,在 2015 年 1 月到 2019 年 3 月期間的表現。這個投資組合包含 Proctor & Gamble、Microsoft、JP Morgan 與 General Electric。你會發現,將每檔股票的平均報酬乘上其在投資組合中的權重,是一種快速又直接的方式,用來估算特定期間內的組合表現。
DataFrame data 的 4 欄分別是上述 4 檔股票的價格。請在主控台查看並檢視 data。
本練習屬於課程
Python 投資組合分析入門
練習說明
- 以今日價格相對於昨日價格,計算 DataFrame
data中各股票的百分比報酬。 - 在新的
returnsDataFrame 中計算每檔股票的平均報酬。 - 將各股票的權重指派給陣列
weights。權重為 0.5、0.2、0.2、0.1。 - 將百分比報酬與權重相乘後加總,計算整體投資組合的總表現,並印出結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Calculate percentage returns
returns = data.____()
# Calculate individual mean returns
meanDailyReturns = ____.____()
# Define weights for the portfolio
weights = np.array([____, ____, ____, ____])
# Calculate expected portfolio performance
portReturn = np.____(____*____)
# Print the portfolio return
print(____)