#!/usr/bin/env python # coding: utf-8 # # 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 get_ipython().run_line_magic('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__)) # In[4]: get_ipython().run_line_magic('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 # 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) # 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) # 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) # 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) # 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 # 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 # In[29]: year = range(0,11) print(print(year)) # 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)