計算每檔股票對指數的貢獻度
你已成功建立值加權指數。現在來看看它在 2010–2016 年間的表現如何。
我們也要計算每檔股票對指數報酬的貢獻程度。
本練習屬於課程
Manipulating Time Series Data in Python
練習說明
我們已替你匯入 pandas 為 pd,以及 matplotlib.pyplot 為 plt。也已載入你在上一個練習中使用過的 components 與 index。
- 以最後一個
index數值除以第一個,減去 1 後再乘以 100。將結果指定給index_return,並印出它。 - 從
components中選取'Market Capitalization'欄位。 - 計算所有成分的總市值,並將其指定為
total_market_cap。 - 將各成分的市值除以
total_market_cap以計算成分權重,指定為weights,並在預設(遞增)排序下印出weights。 - 將
weights乘上index_return以計算各成分的貢獻度,將結果由小到大排序,並以水平長條圖繪製。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Calculate and print the index return here
index_return = ____
print(____)
# Select the market capitalization
market_cap = ____
# Calculate the total market cap
total_market_cap = ____
# Calculate the component weights, and print the result
weights = ____
print(____)
# Calculate and plot the contribution by component