开始使用免费开始使用

计算每只股票对指数的贡献

您已经成功构建了按市值加权的指数。现在让我们看看它在 2010–2016 年期间的表现。

同时,也来确定每只股票对指数收益的贡献。

本练习是课程的一部分

Python 中的时间序列数据处理

查看课程

练习说明

我们已为您导入 pandaspd,并将 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


编辑并运行代码