Compare index performance against benchmark II
The next step in analyzing the performance of your index is to compare it against a benchmark.
In the video, we have use the S&P 500 as benchmark. You can also use the Dow Jones Industrial Average, which contains the 30 largest stocks, and would also be a reasonable benchmark for the largest stocks from all sectors across the three exchanges.
This exercise is part of the course
Manipulating Time Series Data in Python
Exercise instructions
We have already imported numpy
as np
, pandas
as pd
, matplotlib.pyplot
as plt
for you. We have also loaded your Index and the Dow Jones Industrial Average (normalized) in a variable called data
.
- Inspect
data
and print the first five rows. - Define a function
multi_period_return
that takes anumpy
array
of period returns as input, and returns the total return for the period. Use the formula from the video - add 1 to the input, pass the result tonp.prod()
, subtract 1 and multiply by 100. - Create a
.rolling()
window of length'360D'
fromdata
, and applymulti_period_return
. Assign torolling_return_360
. - Plot
rolling_return_360
using thetitle
'Rolling 360D Return'
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Inspect data
print(____)
print(____)
# Create multi_period_return function here
def multi_period_return(r):
return (____) * 100
# Calculate rolling_return_360
rolling_return_360 = data.pct_change().____
# Plot rolling_return_360 here