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.
Diese Übung ist Teil des Kurses
Manipulating Time Series Data in Python
Anleitung zur Übung
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
dataand print the first five rows. - Define a function
multi_period_returnthat takes anumpyarrayof 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_360using thetitle'Rolling 360D Return'.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# 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