開始使用免費開始

計算每檔股票對指數的貢獻度

你已成功建立值加權指數。現在來看看它在 2010–2016 年間的表現如何。

我們也要計算每檔股票對指數報酬的貢獻程度。

本練習屬於課程

Manipulating Time Series Data in Python

檢視課程

練習說明

我們已替你匯入 pandaspd,以及 matplotlib.pyplotplt。也已載入你在上一個練習中使用過的 componentsindex

  • 以最後一個 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


編輯並執行程式碼