In [ ]:
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 [ ]:
# Use the requests module to download (real) business cycle data
url = ''

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

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

In [ ]:
# Read Econ129_Rbc_Data.csv into a Pandas DataFrame with the first column set as the index


# Print the last five rows of the data

In [ ]:
# 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')


fig.tight_layout()

In [ ]:
# Construct a plot of real GDP with its trend

In [ ]:
# Create a new column called gdp_cycle equal to the difference between acual and trend GDP


# Create a new column called gdp_cycle_dev equal to the log difference between actual GDP and trend GDP:


# Plot the log deviation of GDP from its trend (times 100)

In [ ]:
# Create three new columns called cons_cycle, invest_cycle, and hours_cycle equal to the cyclical components of the 
# respective series


# 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:

In [ ]:
# Construct a plot of consumption with its trend

In [ ]:
# Plot the log deviation of consumption from its trend (times 100)

In [ ]:
# Construct a plot of investment with its trend

In [ ]:
# Plot the log deviation of investment from its trend (times 100)

In [ ]:
# Construct a plot of hours with its trend

In [ ]:
# Plot the log deviation of hours from its trend (times 100)

In [ ]:
# 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.

In [ ]:
# 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

In [ ]:
# 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

In [ ]:
# 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