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.
Toto cvičení je součástí kurzu
Manipulace s časovými řadami v Pythonu
Pokyny k cvičení
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'.
Interaktivní cvičení na vyzkoušení si v praxi
Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.
# 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