Skip to content

Commit

Permalink
Merge pull request #144 from sbenthall/sb-0.10.8b
Browse files Browse the repository at this point in the history
fixes for 0.10.b
  • Loading branch information
sbenthall authored Nov 4, 2020
2 parents 6a421b7 + 85db458 commit 2927f63
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 155 deletions.
55 changes: 40 additions & 15 deletions notebooks/IncExpectationExample.ipynb

Large diffs are not rendered by default.

44 changes: 36 additions & 8 deletions notebooks/IncExpectationExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.2'
# jupytext_version: 1.2.4
# format_version: '1.3'
# jupytext_version: 1.6.0
# kernelspec:
# display_name: Python 3
# language: python
Expand All @@ -23,7 +23,35 @@
# name: python
# nbconvert_exporter: python
# pygments_lexer: ipython3
# version: 3.6.9
# version: 3.7.5
# latex_envs:
# LaTeX_envs_menu_present: true
# autoclose: false
# autocomplete: true
# bibliofile: biblio.bib
# cite_by: apalike
# current_citInitial: 1
# eqLabelWithNumbers: true
# eqNumInitial: 1
# hotkeys:
# equation: Ctrl-E
# itemize: Ctrl-I
# labels_anchors: false
# latex_user_defs: false
# report_style_numbering: false
# user_envs_cfg: false
# toc:
# base_numbering: 1
# nav_menu: {}
# number_sections: true
# sideBar: true
# skip_h1_title: false
# title_cell: Table of Contents
# title_sidebar: Contents
# toc_cell: false
# toc_position: {}
# toc_section_display: true
# toc_window_display: false
# ---

# %% [markdown]
Expand Down Expand Up @@ -60,7 +88,7 @@
from HARK.utilities import getLorenzShares, calcSubpopAvg
mystr = lambda number : "{:.4f}".format(number)

# %% {"code_folding": [1]}
# %% {"code_folding": []}
# This cell makes a subclass of PersistentShockConsumerType including the MPC
class PersistentShockConsumerTypeX(PersistentShockConsumerType):
def getControls(self):
Expand All @@ -70,10 +98,10 @@ def getControls(self):
these = t == self.t_cycle
cLvlNow[these] = self.solution[t].cFunc(self.state_now["mLvlNow"][these],self.state_now["pLvlNow"][these])
MPCnow[these] =self.solution[t].cFunc.derivativeX(self.state_now["mLvlNow"][these],self.state_now["pLvlNow"][these])
self.cLvlNow = cLvlNow
self.controls["cLvlNow"] = cLvlNow
self.MPCnow = MPCnow

# %% {"code_folding": [1]}
# %% {"code_folding": []}
# This cell defines a dictionary to make an instance of "persistent shocks" consumer for the infinite horizon model
BaselineDict = {
"CRRA": 2.0, # Coefficient of relative risk aversion
Expand Down Expand Up @@ -114,7 +142,7 @@ def getControls(self):
"PrstIncCorr": 0.99, # Serial correlation coefficient for persistence of income
}

# %% {"code_folding": [1]}
# %% {"code_folding": []}
# This cell defines a function to solve and simulate a consumer misperceiving the correlation of persistent income shocks
def runRoszypalSchlaffmanExperiment(CorrAct, CorrPcvd, DiscFac_center, DiscFac_spread):
'''
Expand Down Expand Up @@ -178,7 +206,7 @@ def runRoszypalSchlaffmanExperiment(CorrAct, CorrPcvd, DiscFac_center, DiscFac_s
type_list.append(ThisType)

# Get the most recent simulated values of X = cLvlNow, MPCnow, aLvlNow, pLvlNow for all types
cLvl_all = np.concatenate([ThisType.cLvlNow for ThisType in type_list])
cLvl_all = np.concatenate([ThisType.controls["cLvlNow"] for ThisType in type_list])
aLvl_all = np.concatenate([ThisType.state_now["aLvlNow"] for ThisType in type_list])
MPC_all = np.concatenate([ThisType.MPCnow for ThisType in type_list])
pLvl_all = np.concatenate([ThisType.state_now["pLvlNow"] for ThisType in type_list])
Expand Down
381 changes: 312 additions & 69 deletions notebooks/KeynesFriedmanModigliani.ipynb

Large diffs are not rendered by default.

37 changes: 22 additions & 15 deletions notebooks/KeynesFriedmanModigliani.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.2'
# jupytext_version: 1.2.4
# format_version: '1.3'
# jupytext_version: 1.6.0
# kernelspec:
# display_name: Python 3
# language: python
Expand All @@ -23,7 +23,7 @@
# name: python
# nbconvert_exporter: python
# pygments_lexer: ipython3
# version: 3.6.9
# version: 3.7.5
# latex_envs:
# LaTeX_envs_menu_present: true
# autoclose: false
Expand All @@ -40,14 +40,26 @@
# latex_user_defs: false
# report_style_numbering: false
# user_envs_cfg: false
# toc:
# base_numbering: 1
# nav_menu: {}
# number_sections: true
# sideBar: true
# skip_h1_title: false
# title_cell: Table of Contents
# title_sidebar: Contents
# toc_cell: false
# toc_position: {}
# toc_section_display: true
# toc_window_display: false
# ---

# %% [markdown]
# ## Introduction: Keynes, Friedman, Modigliani
#
# [![badge](https://img.shields.io/badge/Launch%20using%20-Econ--ARK-blue)](https://econ-ark.org/materials/keynesfriedmanmodigliani#launch)

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# Some initial setup
import sys
import os
Expand All @@ -64,11 +76,6 @@
from copy import deepcopy

import pandas_datareader.data as web
# As of 09/03/2019 the latest available version of pandas-datareader
# has conflicts with the latest version of pandas. We temporarily fix
# this by loading data from files.
# This should not be necessary when pandas-datareader>0.7 becomes available.
from io import StringIO

from HARK.ConsumptionSaving.ConsIndShockModel import PerfForesightConsumerType
from HARK.utilities import plotFuncsDer, plotFuncs
Expand Down Expand Up @@ -97,7 +104,7 @@
# %% [markdown]
# #### The Keynesian Consumption Function

# %% {"code_folding": [0]}
# %% {"code_folding": []}
class KeynesianConsumer:
"""
This class represents consumers that behave according to a
Expand All @@ -124,7 +131,7 @@ def __init__(self):
self.a1 = self.cFunc(1) - self.cFunc(0)


# %% {"code_folding": [0]}
# %% {"code_folding": []}
# Plot cFunc(Y)=Y against the Keynesian consumption function
# Deaton-Friedman consumption function is a special case of perfect foresight model

Expand All @@ -143,7 +150,7 @@ def __init__(self):
plt.legend()
plt.show()

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# This looks like the first of the three equations, consumption as a linear function of income!
# This means that even in a microfounded model (that HARK provides), the consumption function can match Keynes reduced form
# prediction (given the right parameterization).
Expand Down Expand Up @@ -271,7 +278,7 @@ def __init__(self):
#
# We begin by creating a class that class implements the Friedman PIH consumption function as a special case of the [Perfect Foresight CRRA](http://econ.jhu.edu/people/ccarroll/courses/choice/lecturenotes/consumption/PerfForesightCRRA) model.

# %% {"code_folding": [0]}
# %% {"code_folding": []}
class FriedmanPIHConsumer:
"""
This class represents consumers that behave according to
Expand Down Expand Up @@ -386,7 +393,7 @@ def __init__(self, Rfree=1.001, CRRA = 2):
#
# Consider quarterly differences first:

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# Lets use the data from FRED that we used before.

# Using quarterly data (copying from above), we had:
Expand All @@ -406,7 +413,7 @@ def __init__(self, Rfree=1.001, CRRA = 2):
# %% [markdown]
# And now consider longer time differences, 20 quarters for instance, where the changes in permanent income should dominate transitory effects

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# Using longer differences
df_diff_long = df.diff(periods = 20) #create dataframe of differenced values
df_diff_long.columns = ['cons', 'inc']
Expand Down
77 changes: 45 additions & 32 deletions notebooks/Nondurables-During-Great-Recession.ipynb

Large diffs are not rendered by default.

54 changes: 41 additions & 13 deletions notebooks/Nondurables-During-Great-Recession.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.2'
# jupytext_version: 1.2.4
# format_version: '1.3'
# jupytext_version: 1.6.0
# kernelspec:
# display_name: Python 3
# language: python
Expand All @@ -23,7 +23,35 @@
# name: python
# nbconvert_exporter: python
# pygments_lexer: ipython3
# version: 3.6.9
# version: 3.7.5
# latex_envs:
# LaTeX_envs_menu_present: true
# autoclose: false
# autocomplete: true
# bibliofile: biblio.bib
# cite_by: apalike
# current_citInitial: 1
# eqLabelWithNumbers: true
# eqNumInitial: 1
# hotkeys:
# equation: Ctrl-E
# itemize: Ctrl-I
# labels_anchors: false
# latex_user_defs: false
# report_style_numbering: false
# user_envs_cfg: false
# toc:
# base_numbering: 1
# nav_menu: {}
# number_sections: true
# sideBar: true
# skip_h1_title: false
# title_cell: Table of Contents
# title_sidebar: Contents
# toc_cell: false
# toc_position: {}
# toc_section_display: true
# toc_window_display: false
# ---

# %% [markdown]
Expand All @@ -33,7 +61,7 @@
#
# <p style="text-align: center;"><small><small><small>Generator: QuARK-make/notebooks_byname</small></small></small></p>

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# Initial imports and notebook setup, click arrow to show

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -74,7 +102,7 @@
# With this basic setup, HARK's IndShockConsumerType is the appropriate subclass of $\texttt{AgentType}$. So we need to prepare the parameters to create instances of that class.
#

# %% {"code_folding": [1]}
# %% {"code_folding": []}
# Choose some calibrated parameters that roughly match steady state
init_infinite = {
"CRRA":1.0, # Coefficient of relative risk aversion
Expand Down Expand Up @@ -124,7 +152,7 @@
#
# First, let's create a list with seven copies of our baseline type.

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# A list in python can contain anything -- including consumers
num_consumer_types = 7 # declare the number of types we want
ConsumerTypes = [] # initialize an empty list
Expand All @@ -138,7 +166,7 @@
# %% [markdown]
# Now we can give each of the consumer types their own discount factor. (This approximates the distribution of parameters estimated in ["The Distribution of Wealth and the Marginal Propensity to Consume"](http://econ.jhu.edu/people/ccarroll/papers/cstwMPC)).

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# Seven types is enough to approximate the uniform distribution (5 is not quite enough)
from HARK.distribution import Uniform

Expand All @@ -159,7 +187,7 @@
#
# The cell below does both of those tasks, looping through the consumer types. For each one, it solves that type's infinite horizon model, then simulates 1000 periods to generate an approximation to the long run distribution of wealth.

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# tqdm presents a pretty bar that interactively shows how far the calculations have gotten
for ConsumerType in tqdm(ConsumerTypes):
## We configured their discount factor above. Now solve
Expand All @@ -175,7 +203,7 @@
#
# First, let's define a simple function that merely calculates the average consumption level across the entire population in the most recent simulated period.

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# We just merge the cNrm and pNrm lists already constructed for each ConsumerType
def calcAvgC(ConsumerTypes):
"""
Expand All @@ -184,7 +212,7 @@ def calcAvgC(ConsumerTypes):
"""
# Make arrays with all types' (normalized) consumption and permanent income level
# The brackets indicate that the contents will be a list (in this case, of lists)
cNrm = np.concatenate([ThisType.cNrmNow for ThisType in ConsumerTypes])
cNrm = np.concatenate([ThisType.controls["cNrmNow"] for ThisType in ConsumerTypes])
pLvl = np.concatenate([ThisType.state_now["pLvlNow"] for ThisType in ConsumerTypes])

# Calculate and return average consumption level in the economy
Expand All @@ -205,7 +233,7 @@ def calcAvgC(ConsumerTypes):
# 6. Calculate the new average consumption level as percentage change vs the prior level.
# 3. Return the list of percentage changes

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# Whenever you define a function, you should describe it (with a "docstring")
def calcConsChangeAfterUncertaintyChange(OriginalTypes,NewVals,ParamToChange):
'''
Expand Down Expand Up @@ -258,7 +286,7 @@ def calcConsChangeAfterUncertaintyChange(OriginalTypes,NewVals,ParamToChange):
# %% [markdown]
# Our counterfactual experiment function takes three inputs-- consumer types, counterfactual values, and the name of the parameter we want to change. For the sake of convenience, let's define small functions to run the experiment for each parameter with just a single input.

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# Trivial functions can be useful in making the logic of your program clear
def calcConsChangeAfterPermShkChange(newVals):
return calcConsChangeAfterUncertaintyChange(ConsumerTypes,newVals,"PermShkStd")
Expand All @@ -272,7 +300,7 @@ def calcConsChangeAfterUnempPrbChange(newVals):
# %% [markdown]
# Now we can finally run our experiment. In the cell below, we generate a plot of the change in aggregate consumption vs the (underlying) standard deviation of permanent income shocks.

# %% {"code_folding": [0]}
# %% {"code_folding": []}
# Calculate the consequences of an "MIT shock" to the standard deviation of permanent shocks
ratio_min = 0.8 # minimum number to multiply uncertainty parameter by
TargetChangeInC = -6.3 # Source: FRED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
" # Calculate the MPC for each of the four lottery sizes for all agents\n",
" for ThisType in EstTypeList:\n",
" ThisType.simulate(1)\n",
" c_base = ThisType.cNrmNow\n",
" c_base = ThisType.controls[\"cNrmNow\"]\n",
" MPC_this_type = np.zeros((ThisType.AgentCount,4))\n",
" for k in range(4): # Get MPC for all agents of this type\n",
" Llvl = lottery_size[k]\n",
Expand Down Expand Up @@ -369,7 +369,7 @@
" Current function value: 0.500319\n",
" Iterations: 40\n",
" Function evaluations: 78\n",
"Time to estimate is 130.09294414520264 seconds.\n",
"Time to estimate is 182.82315731048584 seconds.\n",
"Finished estimating for scaling factor of 1.0 and \"splurge amount\" of $0.0\n",
"Optimal (beta,nabla) is [0.79054883 0.15918858], simulated MPCs are:\n",
"[[0.77397136 0.68429879 0.56361473 0.41012588]\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def FagerengObjFunc(center,spread,verbose=False):
# Calculate the MPC for each of the four lottery sizes for all agents
for ThisType in EstTypeList:
ThisType.simulate(1)
c_base = ThisType.cNrmNow
c_base = ThisType.controls["cNrmNow"]
MPC_this_type = np.zeros((ThisType.AgentCount,4))
for k in range(4): # Get MPC for all agents of this type
Llvl = lottery_size[k]
Expand Down

0 comments on commit 2927f63

Please sign in to comment.