In [2]:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import requests
%matplotlib inline

Class 11: Business Cycle Data

The business cycle is the fluctuation of many macroeconomic quantities that last for about 1.5 to 8 years. Colloquially, the term refers to the alternating periods of expansion and contraction in the macroeconomy. Business cycle fluctuations are costly because they are associated with misallocations of capital and labor. The burden of recessions is particularly painful for workers that become unemployed and their families. The costs of the business cycle have driven research into understanding the cause of the cycle. The collective set of theories to explain the cycle is called business cycle theory.

Business cycle theory is tested against data. A time series $X_t$ can be decomposed into a trend component $X_t^{trend}$ and a cyclical component $X_t^{cycle}$ such that:

\begin{align} X_t & = X_t^{trend} + X_t^{cycle}. \tag{1} \end{align}

In equation (1), $X_t^{trend}$ is the long-run value about which $X_t$ fluctuates. $X^{cycle}_t$ is the amount by which $X_t$ excedes its trend. The process for decomposing a series into trend and cyclical components is called filtering and is more technical than we want to get into. We'll take for granted that such procedures exist.

Often times, it's useful to express the cyclical component of a time series as the difference between the (natural) log of the series and the log of the trend:

\begin{align} \hat{x}_t & = \log\left(X_t\right) - \log\left(X_t^{trend}\right) \approx\frac{X_t-X_t^{trend}}{X_t^{trend}} \end{align}

The log-deviation from trend is approximately equal to the percent deviation of the series from trend (divided by 100).

The file Econ129_Rbc_Data.csv, available at http://www.briancjenkins.com/teaching/winter2017/econ129/data/Econ129_Rbc_Data.csv, contains actual and trend data for real GDP per worker, real consumption per worker, real investment per worker, and hours per worker. The GDP, consumption, and investment data are in terms of 2009 dollars. Hours is measured as an index with the value in October 2009 set to 100.


In [3]:
# Use the requests module to download (real) business cycle data
url = 'http://www.briancjenkins.com/teaching/winter2017/econ129/data/Econ129_Rbc_Data.csv'

r = requests.get(url,verify=True)

with open('Econ129_Rbc_Data.csv','wb') as newFile:
    
    newFile.write(r.content)

In [4]:
# Read Econ129_Rbc_Data.csv into a Pandas DataFrame with the first column set as the index
df = pd.read_csv('Econ129_Rbc_Data.csv',index_col=0)

# Print the last five rows of the data
df.tail()


Out[4]:
gdp gdp_trend consumption consumption_trend investment investment_trend hours hours_trend
2015-10-01 65.673797 65.482197 44.828634 44.771621 11.027682 10.903478 105.925398 105.328612
2016-01-01 65.642749 65.664189 44.875891 44.907826 10.904073 10.987555 106.011666 105.591110
2016-04-01 65.654934 65.846589 45.167147 45.045227 10.631060 11.070764 106.108308 105.853549
2016-07-01 66.067404 66.029501 45.396333 45.183475 10.673915 11.153877 106.018356 106.116077
2016-10-01 66.198774 66.212913 45.559391 45.322296 10.903381 11.237401 105.977184 106.379004

In [5]:
# Construct a 2x2 grid of plots of GDP, consumption, investment, and hours

fig = plt.figure(figsize=(12,8))

ax1 = fig.add_subplot(2,2,1)
ax1.plot_date(df.index,df.gdp,'-',lw=3,alpha = 0.7)
ax1.grid()
ax1.set_title('GDP per worker')
ax1.set_ylabel('Thousands of 2009 dollars')

ax2 = fig.add_subplot(2,2,2)
ax2.plot_date(df.index,df.consumption,'-',lw=3,alpha = 0.7)
ax2.grid()
ax2.set_title('Consumption per worker')
ax2.set_ylabel('Thousands of 2009 dollars')

ax3 = fig.add_subplot(2,2,3)
ax3.plot_date(df.index,df.investment,'-',lw=3,alpha = 0.7)
ax3.grid()
ax3.set_title('Investment per worker')
ax3.set_ylabel('Thousands of 2009 dollars')

ax4 = fig.add_subplot(2,2,4)
ax4.plot_date(df.index,df.hours,'-',lw=3,alpha = 0.7)
ax4.grid()
ax4.set_title('Hours per worker')
ax4.set_ylabel('Index 2009=100')

fig.tight_layout()



In [21]:
# Construct a plot of real GDP with its trend
plt.plot_date(df.index,df.gdp,'-',lw=1,alpha = 0.7,label='actual')
plt.plot_date(df.index,df.gdp_trend,'r-',lw=3,alpha = 0.7,label='trend')
plt.grid()
plt.ylabel('Thousands of dollars per person')
plt.title('GDP per worker')
plt.legend(loc='lower right',ncol=2)


Out[21]:
<matplotlib.legend.Legend at 0x11a52aba8>

In [42]:
# Create a new column called gdp_cycle equal to the difference between acual and trend GDP
df['gdp_cycle'] = df['gdp'] - df['gdp_trend']

# Create a new column called gdp_cycle_dev equal to the log difference between actual GDP and trend GDP:
df['gdp_cycle_dev'] = np.log(df['gdp']) - np.log(df['gdp_trend'])

# Plot the log deviation of GDP from its trend (times 100)
plt.plot_date(df.index,df.gdp_cycle_dev*100,'b-',lw=3,alpha = 0.7)
plt.grid()
plt.ylabel('Percent')
plt.title('GDP: Percent deviation from trend')


Out[42]:
<matplotlib.text.Text at 0x11b992e80>

In [43]:
# Create three new columns called cons_cycle, invest_cycle, and hours_cycle equal to the cyclical components of the 
# respective series
df['cons_cycle'] = df['consumption'] - df['consumption_trend']
df['invest_cycle'] = df['investment'] - df['investment_trend']
df['hours_cycle'] = df['hours'] - df['hours_trend']


# Create a new column called cons_cycle_dev, invest_cycle_dev, and hours_cycle_dev equal to the log difference between 
# the actual and trend values of the respective series:
df['cons_cycle_dev'] = np.log(df['consumption']) - np.log(df['consumption_trend'])
df['invest_cycle_dev'] = np.log(df['investment']) - np.log(df['investment_trend'])
df['hours_cycle_dev'] = np.log(df['hours']) - np.log(df['hours_trend'])

In [44]:
# Construct a plot of consumption with its trend
plt.plot_date(df.index,df.consumption,'-',lw=1,alpha = 0.7,label='actual')
plt.plot_date(df.index,df.consumption_trend,'r-',lw=3,alpha = 0.7,label='trend')
plt.grid()
plt.ylabel('Thousands of dollars per person')
plt.title('Consumption per worker')
plt.legend(loc='lower right',ncol=2)


Out[44]:
<matplotlib.legend.Legend at 0x11baf2f98>

In [45]:
# Plot the log deviation of consumption from its trend (times 100)
plt.plot_date(df.index,df.cons_cycle_dev*100,'b-',lw=3,alpha = 0.7)
plt.grid()
plt.ylabel('Percent')
plt.title('Consumption: Percent deviation from trend')


Out[45]:
<matplotlib.text.Text at 0x11bb46dd8>

In [46]:
# Construct a plot of investment with its trend
plt.plot_date(df.index,df.investment,'-',lw=1,alpha = 0.7,label='actual')
plt.plot_date(df.index,df.investment_trend,'r-',lw=3,alpha = 0.7,label='trend')
plt.grid()
plt.ylabel('Thousands of dollars per person')
plt.title('Investment per worker')
plt.legend(loc='lower right',ncol=2)


Out[46]:
<matplotlib.legend.Legend at 0x11bc9cd30>

In [47]:
# Plot the log deviation of investment from its trend (times 100)
plt.plot_date(df.index,df.invest_cycle_dev*100,'b-',lw=3,alpha = 0.7)
plt.grid()
plt.ylabel('Percent')
plt.title('Investment: Percent deviation from trend')


Out[47]:
<matplotlib.text.Text at 0x11bee3e10>

In [48]:
# Construct a plot of hours with its trend
plt.plot_date(df.index,df.hours,'-',lw=1,alpha = 0.7,label='actual')
plt.plot_date(df.index,df.hours_trend,'r-',lw=3,alpha = 0.7,label='trend')
plt.grid()
plt.ylabel('Index')
plt.title('Hours per worker')
plt.legend(loc='lower left',ncol=2)


Out[48]:
<matplotlib.legend.Legend at 0x11c237b00>

In [49]:
# Plot the log deviation of hours from its trend (times 100)
plt.plot_date(df.index,df.hours_cycle_dev*100,'b-',lw=3,alpha = 0.7)
plt.grid()
plt.ylabel('Percent')
plt.title('Hours: Percent deviation from trend')


Out[49]:
<matplotlib.text.Text at 0x11c345470>

In [51]:
# Create a new DataFrame called df_cycle equal to the columns gdp_cycle_dev, cons_cycle_dev, invest_cycle_dev, 
# and hours_cycle_dev from df.
df_cycle = df[['gdp_cycle_dev', 'cons_cycle_dev', 'invest_cycle_dev', 'hours_cycle_dev']]

In [52]:
# Use the DataFrame method .mean() to find the average values of the gdp_cycle_dev, cons_cycle_dev, invest_cycle_dev, 
# and hours_cycle_dev columns
df_cycle.mean()


Out[52]:
gdp_cycle_dev      -5.761371e-14
cons_cycle_dev     -5.739247e-14
invest_cycle_dev   -4.989342e-14
hours_cycle_dev    -6.290887e-14
dtype: float64

In [53]:
# Use the DataFrame method .std() to find the standard deviations of the gdp_cycle_dev, cons_cycle_dev, invest_cycle_dev, 
# and hours_cycle_dev columns
df_cycle.std()


Out[53]:
gdp_cycle_dev       0.016342
cons_cycle_dev      0.011926
invest_cycle_dev    0.075850
hours_cycle_dev     0.019202
dtype: float64

In [54]:
# Use the DataFrame method .corr() to find the coeficients of correlation among the gdp_cycle_dev, cons_cycle_dev, 
# invest_cycle_dev, and hours_cycle_dev columns
df_cycle.corr()


Out[54]:
gdp_cycle_dev cons_cycle_dev invest_cycle_dev hours_cycle_dev
gdp_cycle_dev 1.000000 0.797516 0.845620 0.875874
cons_cycle_dev 0.797516 1.000000 0.677855 0.714883
invest_cycle_dev 0.845620 0.677855 1.000000 0.783989
hours_cycle_dev 0.875874 0.714883 0.783989 1.000000