1. Learn
  2. /
  3. Courses
  4. /
  5. Manipulating Time Series Data in Python

Exercise

Calculate the contribution of each stock to the index

You have successfully built the value-weighted index. Let's now explore how it performed over the 2010-2016 period.

Let's also determine how much each stock has contributed to the index return.

Instructions

100 XP

We have already imported pandas as pd and matplotlib.pyplot as plt for you. We have also loaded components and the index you worked with in the last exercise.

  • Divide the last index value by the first, subtract 1 and multiply by 100. Assign the result to index_return and print it.
  • Select the 'Market Capitalization' column from components.
  • Calculate the total market cap for all components and assign this to total_market_cap.
  • Divide the components' market cap by total_market_cap to calculate the component weights, assign it to weights, and print weights with the values sorted in default (ascending) order.
  • Multiply weights by the index_return to calculate the contribution by component, sort the values in ascending order, and plot the result as a horizontal bar chart.