Compare quarterly GDP growth rate and stock returns
With your new skill to downsample and aggregate time series, you can compare higher-frequency stock price series to lower-frequency economic time series.
As a first example, let's compare the quarterly GDP growth rate to the quarterly rate of return on the (resampled) Dow Jones Industrial index of 30 large US stocks.
GDP growth is reported at the beginning of each quarter for the previous quarter. To calculate matching stock returns, you'll resample the stock index to quarter start frequency using the alias 'QS', and aggregating using the .first() observations.
This exercise is part of the course
Manipulating Time Series Data in Python
Exercise instructions
As usual, we have imported pandas as pd and matplotlib.pyplot as plt for you.
- Use
pd.read_csv()to import'gdp_growth.csv'and'djia.csv', for both set aDateTimeIndexbased on the'date'column usingparse_datesandindex_col, and assign the results togdp_growthanddjiarespectively, then inspect using.info(). - Resample
djiausing frequency alias'QS', aggregate using.first(), and assign todjia_quarterly. - Apply
.pct_change()todjia_quarterlyand.mul()by 100 to obtaindjia_quarterly_return. - Use
pd.concat()to concatenategdp_growthanddjia_quarterly_returnalongaxis=1, and assign todata. Rename the columns using.columnsand the new labels'gdp'and'djia', then.plot()the results.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import and inspect gdp_growth here
gdp_growth = ____
# Import and inspect djia here
djia = ____
# Calculate djia quarterly returns here
djia_quarterly = ____
djia_quarterly_return = ____
# Concatenate, rename and plot djia_quarterly_return and gdp_growth here
data = ____