Recovery from the Great Depression and Great Recession

In [1]:
# JAVASCRIPT DISABLED IN JUPYTERLAB
#
# %%javascript
#
# IPython.OutputArea.prototype._should_scroll = function(lines) {
#    return false;}
In [2]:
# the above "magic" (%%) command is there to keep output cells 
# from shifting to autoscroll, which leaves you peering at a 
# small portion of a window looking for whatever the computer
# just did in the output cell...
In [3]:
# set up the environment by reading in libraries: 
# os... graphics... data manipulation... time... math... statistics...
# and reporting version numbers to help at debugging
#
# needs to be cleaned, as contains excess and unneeded libraries...

import sys
import os
from urllib.request import urlretrieve

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import PIL as pil

import plotly
plotly.tools.set_credentials_file(
    username='delong', 
    api_key='d6vMMwVn4sEBmR2MLN9H')

import plotly.plotly as py
import plotly.graph_objs as go
from plotly.graph_objs import Scatter
from IPython.display import Image

import pandas as pd
from pandas import DataFrame, Series
import pandas_datareader
from datetime import datetime

import scipy as sp
import numpy as np
import math
import random

import seaborn as sns
import statsmodels
import statsmodels.api as sm
import statsmodels.formula.api as smf

# report library versions...

print("LIBRARY VERSIONS")
print(" ")
print("Python version:\n{}\n".format(sys.version))
print("matplotlib version: {}".format(mpl.__version__))
print("pandas version: {}".format(pd.__version__))
print("numpy version: {}".format(np.__version__))
print("statsmodels version: {}".format(statsmodels.__version__))
print("PIL version: {}".format(pil.__version__))
print("scipy version: {}".format(sp.__version__))
/Users/delong/anaconda3/lib/python3.6/site-packages/statsmodels/compat/pandas.py:56: FutureWarning:

The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.

LIBRARY VERSIONS
 
Python version:
3.6.1 |Anaconda custom (x86_64)| (default, May 11 2017, 13:04:09) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]

matplotlib version: 2.0.2
pandas version: 0.20.1
numpy version: 1.12.1
statsmodels version: 0.8.0
PIL version: 4.1.1
scipy version: 0.19.0
In [4]:
%matplotlib inline 
In [5]:
# the above cell "magic" (%) tells the matplot lib graphics library
# to plot graphs as static figures output cells, rather than in new
# windows (or in the alternative "%matplotlib notebook" view, as a
# dynamic interactive figure)
In [6]:
# graphics setup: seaborn-whitegrid to make the output from matplotlib
# prettier and figure_size to make the output from matplotlib
# somewhat larger...

plt.style.use('seaborn-whitegrid')

figure_size = plt.rcParams["figure.figsize"]
figure_size[0] = 10
figure_size[1] = 8
plt.rcParams["figure.figsize"] = figure_size
In [7]:
# reading in the previously-downloaded long-run real GDP and 
# GDP per capita file from Johnston and Williamson's "Measuring 
# Worth" website
#
# constructing a dataframe to hold the data, and then wrapping
# that dataframe inside a dict object to append source notes and
# links

Source_URL = 'http://delong.typepad.com/2017-09-12_us_real_gdp_and_per_capita.csv' 

uslrnp_df = pd.read_csv(
    Source_URL, 
    converters = {'Source': str, 'Source_URL': str}, 
#    parse_dates = True,
    index_col = 0)

uslrnp_dict = {}
uslrnp_dict["Source"] = uslrnp_df.Source.values[0]
uslrnp_dict["SourceURL"] = Source_URL

sourceD1 = "See Johnston and Williamson: "
sourceD2 = "'Sources and Techniques Used in the Construction of Annual GDP, 1790 - Present' "
sourceD3 = "https://www.measuringworth.com/usgdp/#"
uslrnp_dict["SourceNotes"] = sourceD1 + sourceD2 + sourceD3

sourceD4 = "See https://www.measuringworth.com/aboutus.php; "
sourceD5 = "https://www.measuringworth.com/datasets/usgdp/export.php?"
sourceD6 = "year_source=1790&year_result=2016&use%5B%5D=REALGDP&use%5B%5D=GDPCP"
uslrnp_dict["SourceDescription"] = sourceD4 + sourceD5 + sourceD6

del uslrnp_df["Source"]
del uslrnp_df["Source_URL"]

uslrnp_dict["df"] = uslrnp_df

# commented out 
uslrnp_dict
Out[7]:
{'Source': 'Louis Johnston and Samuel H. Williamson (2017), "What Was the U.S. GDP Then?" MeasuringWorth',
 'SourceDescription': 'See https://www.measuringworth.com/aboutus.php; https://www.measuringworth.com/datasets/usgdp/export.php?year_source=1790&year_result=2016&use%5B%5D=REALGDP&use%5B%5D=GDPCP',
 'SourceNotes': "See Johnston and Williamson: 'Sources and Techniques Used in the Construction of Annual GDP, 1790 - Present' https://www.measuringworth.com/usgdp/#",
 'SourceURL': 'http://delong.typepad.com/2017-09-12_us_real_gdp_and_per_capita.csv',
 'df':       Real_GDP  Real_GDP_per_Capita
 Year                               
 1790      4351              1107.32
 1791      4612              1139.28
 1792      4952              1186.84
 1793      5345              1243.34
 1794      6052              1366.45
 1795      6436              1410.40
 1796      6640              1412.53
 1797      6774              1398.33
 1798      7064              1415.67
 1799      7563              1471.05
 1800      7994              1509.06
 1801      8383              1535.08
 1802      8647              1535.28
 1803      8793              1513.74
 1804      9135              1524.74
 1805      9619              1556.43
 1806     10074              1579.17
 1807     10083              1530.49
 1808     10105              1486.74
 1809     10878              1552.07
 1810     11481              1589.31
 1811     12003              1614.19
 1812     12482              1631.42
 1813     13192              1676.92
 1814     13744              1699.98
 1815     13855              1667.65
 1816     13854              1622.25
 1817     14176              1612.73
 1818     14691              1622.02
 1819     14975              1604.18
 ...        ...                  ...
 1987   8132600             33489.00
 1988   8474500             34581.00
 1989   8786400             35517.00
 1990   8955000             35794.00
 1991   8948400             35295.00
 1992   9266600             36068.00
 1993   9521000             36580.00
 1994   9905400             37598.00
 1995  10174800             38167.00
 1996  10561000             39156.00
 1997  11034900             40427.00
 1998  11525900             41737.00
 1999  12065900             43196.00
 2000  12559700             44475.00
 2001  12682200             44464.00
 2002  12908800             44829.00
 2003  13271100             45664.00
 2004  13773500             46967.00
 2005  14234200             48090.00
 2006  14613800             48905.00
 2007  14873700             49300.00
 2008  14830400             48697.00
 2009  14418700             46930.00
 2010  14783800             47720.00
 2011  15020600             48125.00
 2012  15354600             48841.00
 2013  15612200             49317.00
 2014  16013300             50216.00
 2015  16471500             51286.00
 2016  16716200             51690.00
 
 [227 rows x 2 columns]}
In [8]:
# generating four simple graphs of Real GDP — total and per capita,
# linear and log scale
# 
# this first graph is the version that belongs in the textook, IMHO...

uslrnp_df.Real_GDP_per_Capita.plot()

plt.xlabel("Year", size = 15)
plt.ylabel("Real Output per Capita", size = 15)
plt.ylim(0, )
plt.title("Real Output per Capita in the United States", size = 24)
Out[8]:
<matplotlib.text.Text at 0x117c6d978>
In [9]:
uslrnp_df.Real_GDP_per_Capita.plot(logy = True)


plt.xlabel("Year", size = 15)
plt.ylabel("Real National Product per Capita (Log Scale)", size = 15)
plt.title("Real National Product per Capita in the United States", 
    size = 30)
Out[9]:
<matplotlib.text.Text at 0x11af9b780>
In [10]:
uslrnp_df.Real_GDP.plot()

plt.xlabel("Year", size = 15)
plt.ylim(0, )
plt.ylabel("Real National Product (in Tens of Trillions of Today's Dollars)", size = 15)
plt.title("Real National Product in the United States since 1790", size = 30)
Out[10]:
<matplotlib.text.Text at 0x11bce9710>
In [11]:
uslrnp_df.Real_GDP.plot(logy = True)

plt.xlabel("Year", size = 15)
plt.ylabel("Real National Product", size = 15)
plt.title("Real National Product in the United States since 1790:\nLog Output", size = 30)
Out[11]:
<matplotlib.text.Text at 0x11c071f60>
In [35]:
depression_real_GDP = []

for t in range(1929, 1942):
    depression_real_GDP = depression_real_GDP + [uslrnp_df.Real_GDP[t]/uslrnp_df.Real_GDP[1929]]

depression_real_GDP_per_capita = []

for t in range(1929, 1942):
    depression_real_GDP_per_capita = depression_real_GDP_per_capita + [uslrnp_df.Real_GDP_per_Capita[t]/uslrnp_df.Real_GDP_per_Capita[1929]]
    
depression_real_GDP_per_capita
Out[35]:
[1.0,
 0.90517937478371213,
 0.84069673549429003,
 0.7276502480101511,
 0.71426923520590613,
 0.78636520936670895,
 0.85050178798015919,
 0.95431999077171525,
 0.9970008074749106,
 0.9565117083862037,
 1.0244549544353443,
 1.1054331526127581,
 1.2886146037605259]
In [40]:
recession_real_GDP = []

for t in range(2017, 2017):
    recession_real_GDP = recession_real_GDP + [uslrnp_df.Real_GDP[t]/uslrnp_df.Real_GDP[2007]]
    
recession_real_GDP = recession_real_GDP + [1.149] + [1.1779] + [1.2074] 


recession_real_GDP_per_capita = []

for t in range(2007, 2017):
    recession_real_GDP_per_capita = recession_real_GDP_per_capita + [uslrnp_df.Real_GDP_per_Capita[t]/uslrnp_df.Real_GDP_per_Capita[2007]]
    
recession_real_GDP_per_capita = recession_real_GDP_per_capita + [1.0635] + [1.0795] + [1.0957]

recession_real_GDP_per_capita
Out[40]:
[1.0,
 0.98776876267748481,
 0.9519269776876268,
 0.96795131845841786,
 0.97616632860040564,
 0.99068965517241381,
 1.0003448275862068,
 1.0185801217038539,
 1.0402839756592293,
 1.0484787018255579,
 1.0635,
 1.0795,
 1.0957]
In [29]:
year = range(0,11)

print(print(year))
range(0, 11)
None
In [44]:
data = [('Great Recession', recession_real_GDP_per_capita),
        ('GreatDepression', depression_real_GDP_per_capita)]

comparison_per_capita_df = pd.DataFrame.from_items(data)

comparison_per_capita_df.plot()
plt.xlabel("Years After Previous Business Cycle Peak", size = 12)
plt.ylabel("Relative to Value at Previous Business Cycle Peak", size = 12)
plt.title("Downturn and Recovery Trends in the \nGreat Depression and Great Recession", size = 24)
Out[44]:
<matplotlib.text.Text at 0x11ca249b0>