Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import error in LifecycleModelExample ipynb and py #54

Merged
merged 1 commit into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions notebooks/LifecycleModelExample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,32 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 2,
"metadata": {
"scrolled": false
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"***NOTE: using a 'quick fix' for an attribute error. See 'Error Notes' in EstimationParameter.py for further discussion.***\n"
]
}
],
"source": [
"# Initial imports and notebook setup, click arrow to show\n",
"\n",
"import HARK.ConsumptionSaving.ConsIndShockModel as Model # The consumption-saving micro model\n",
"import HARK.SolvingMicroDSOPs.EstimationParameters as Params # Parameters for the consumer type and the estimation\n",
"import HARK.SolvingMicroDSOPs.Calibration.EstimationParameters as Params # Parameters for the consumer type and the estimation\n",
"from HARK.utilities import plotFuncsDer, plotFuncs # Some tools\n",
"\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -341,7 +349,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
"version": "3.5.2"
},
"varInspector": {
"cols": {
Expand Down
203 changes: 49 additions & 154 deletions notebooks/LifecycleModelExample.py
Original file line number Diff line number Diff line change
@@ -1,168 +1,41 @@
# ---
# jupyter:
# cite2c:
# citations:
# 6202365/7MR8GUVS:
# DOI: 10.3982/QE694
# URL: https://onlinelibrary.wiley.com/doi/abs/10.3982/QE694
# abstract: In a model calibrated to match micro- and macroeconomic evidence
# on household income dynamics, we show that a modest degree of heterogeneity
# in household preferences or beliefs is sufficient to match empirical measures
# of wealth inequality in the United States. The heterogeneity-augmented model's
# predictions are consistent with microeconomic evidence that suggests that
# the annual marginal propensity to consume (MPC) is much larger than the
# roughly 0.04 implied by commonly used macroeconomic models (even ones including
# some heterogeneity). The high MPC arises because many consumers hold little
# wealth despite having a strong precautionary motive. Our model also plausibly
# predicts that the aggregate MPC can differ greatly depending on how the
# shock is distributed across households (depending, e.g., on their wealth,
# or employment status).
# accessed:
# day: 5
# month: 2
# year: 2019
# author:
# - family: Carroll
# given: Christopher
# - family: Slacalek
# given: Jiri
# - family: Tokuoka
# given: Kiichi
# - family: White
# given: Matthew N.
# container-title: Quantitative Economics
# id: 6202365/7MR8GUVS
# issue: '3'
# issued:
# year: 2017
# language: en
# note: 'Citation Key: carrollDistributionWealthMarginal2017'
# page: 977-1020
# page-first: '977'
# title: The distribution of wealth and the marginal propensity to consume
# type: article-journal
# volume: '8'
# 6202365/B9BGV9W3:
# URL: http://www.nber.org/papers/w22822
# abstract: "We provide a systematic analysis of the properties of individual\
# \ returns to wealth using twenty years of population data from Norway\u2019\
# s administrative tax records. We document a number of novel results. First,\
# \ in a given cross-section, individuals earn markedly different returns\
# \ on their assets, with a difference of 500 basis points between the 10th\
# \ and the 90th percentile. Second, heterogeneity in returns does not arise\
# \ merely from differences in the allocation of wealth between safe and risky\
# \ assets: returns are heterogeneous even within asset classes. Third, returns\
# \ are positively correlated with wealth. Fourth, returns have an individual\
# \ permanent component that accounts for 60% of the explained variation.\
# \ Fifth, for wealth below the 95th percentile, the individual permanent\
# \ component accounts for the bulk of the correlation between returns and\
# \ wealth; the correlation at the top reflects both compensation for risk\
# \ and the correlation of wealth with the individual permanent component.\
# \ Finally, the permanent component of the return to wealth is also (mildly)\
# \ correlated across generations. We discuss the implications of these findings\
# \ for several strands of the wealth inequality debate."
# accessed:
# day: 17
# month: 3
# year: 2019
# author:
# - family: Fagereng
# given: Andreas
# - family: Guiso
# given: Luigi
# - family: Malacrino
# given: Davide
# - family: Pistaferri
# given: Luigi
# genre: Working Paper
# id: 6202365/B9BGV9W3
# issued:
# month: 11
# year: 2016
# note: 'DOI: 10.3386/w22822'
# number: '22822'
# publisher: National Bureau of Economic Research
# title: Heterogeneity and Persistence in Returns to Wealth
# type: report
# jupytext:
# formats: ipynb,py:percent
# metadata_filter:
# cells: collapsed
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.1'
# jupytext_version: 0.8.3
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# language_info:
# codemirror_mode:
# name: ipython
# version: 3
# file_extension: .py
# mimetype: text/x-python
# name: python
# nbconvert_exporter: python
# pygments_lexer: ipython3
# version: 3.6.7
# varInspector:
# cols:
# lenName: 16
# lenType: 16
# lenVar: 40
# kernels_config:
# python:
# delete_cmd_postfix: ''
# delete_cmd_prefix: 'del '
# library: var_list.py
# varRefreshCmd: print(var_dic_list())
# r:
# delete_cmd_postfix: ') '
# delete_cmd_prefix: rm(
# library: var_list.r
# varRefreshCmd: 'cat(var_dic_list()) '
# types_to_exclude:
# - module
# - function
# - builtin_function_or_method
# - instance
# - _Feature
# window_display: false
# ---

# %% [markdown]
#!/usr/bin/env python
# coding: utf-8

# # The Distribution of Assets By Age
#
#
# National registry data on income and wealth from Scandinavian countries has recently become available (with a lot of security) to some (lucky!) researchers. These data offer a uniquely powerful tool for testing (and improving) our models of consumption and saving behavior over the life cycle.
#
#
#
#
# But as of this writing (in March of 2019), the data are so new that there do not seem to be any published attempts to compare the data to the implications a standard life cycle model with income uncertainty, constraints, and other modern features.
#
#
# This notebook is an example of how one could counstruct a life cycle model with the HARK toolkit that would make predictions about the model analogues of the raw data statistics that are available.
#
#
# For example, the papers have shown information about the growth rate of assets at different ages over the life cycle. Here, we show how (under a given parameterization) we could produce the life cycle model's prediction about the distribution of assets at age 65 and age 66, and the growth rate between 65 and 66.
#
#
# The parameters of the model have not been optmized to match features of the Norwegian data; a first step in "structural" estimation would be to calibrate the inputs to the model (like the profile of income over the life cycle, and the magnitude of income shocks), and then to find the values of parameters like the time preference rate that allow the model to fit the data best.
#
#
# An interesting question is whether this exercise will suggest that it is necessary to allow for _ex ante_ heterogeneity in such preference parameters.
#
#
# This seems likely; a paper by [<cite data-cite="6202365/7MR8GUVS"></cite>](http://econ.jhu.edu/people/ccarroll/papers/cstwMPC) (all of whose results were constructed using the HARK toolkit) finds that, if all other parameters (e.g., rates of return on savings) are the same, models of this kind require substantial heterogeneity in preferences to generate the degree of inequality in U.S. data.
#
#
# But in one of the many new and interesting findings from the Norwegian data, <cite data-cite="6202365/B9BGV9W3"></cite> have shown that there is substantial heterogeneity in rates of return, even on wealth held in public markets.
# [Derin Aksit](https://github.com/econ-ark/REMARK) has shown that the degree of time preference heterogeneity needed to match observed inequality is considerably less when rate-of-return heterogeneity is calibrated to match these data.

# %%
# In[2]:


# Initial imports and notebook setup, click arrow to show

import HARK.ConsumptionSaving.ConsIndShockModel as Model # The consumption-saving micro model
import HARK.SolvingMicroDSOPs.EstimationParameters as Params # Parameters for the consumer type and the estimation
import HARK.SolvingMicroDSOPs.Calibration.EstimationParameters as Params # Parameters for the consumer type and the estimation
from HARK.utilities import plotFuncsDer, plotFuncs # Some tools

import numpy as np

# %%

# In[3]:


# Set up default values for CRRA, DiscFac, and simulation variables in the dictionary
Params.init_consumer_objects["CRRA"]= 2.00 # Default coefficient of relative risk aversion (rho)
Params.init_consumer_objects["DiscFac"]= 0.97 # Default intertemporal discount factor (beta)
Expand All @@ -172,11 +45,17 @@
Params.init_consumer_objects["pLvlInitMean"]= 0.0 # Mean of log initial permanent income
Params.init_consumer_objects["pLvlInitStd"]= 0.0 # Standard deviation of log initial permanent income

# %%

# In[3]:


# Make a lifecycle consumer to be used for estimation
LifeCyclePop = Model.IndShockConsumerType(**Params.init_consumer_objects)

# %%

# In[18]:


# Solve and simulate the model (ignore the "warning" message)
LifeCyclePop.solve() # Obtain consumption rules by age
LifeCyclePop.unpackcFunc() # Expose the consumption rules
Expand All @@ -188,14 +67,20 @@
LifeCyclePop.initializeSim() # Construct the age-25 distribution of income and assets
LifeCyclePop.simulate() # Simulate a population behaving according to this model

# %%

# In[19]:


# Plot the consumption functions during working life

print('Consumption as a function of market resources while working:')
mMin = min([LifeCyclePop.solution[t].mNrmMin for t in range(LifeCyclePop.T_cycle)])
plotFuncs(LifeCyclePop.cFunc[:LifeCyclePop.T_retire],mMin,5)

# %% {"code_folding": [0]}

# In[20]:


# Define the saving rate function
def savingRateFunc(SomeType, m):
"""
Expand All @@ -216,7 +101,10 @@ def savingRateFunc(SomeType, m):
SavingRate = Saving / inc
return SavingRate

# %% {"code_folding": []}

# In[25]:


# Create a Giant matrix gathering useful data:
# 't_now', 'aNrmNow_hist', 'cNrmNow_hist', employment-status in date t, in date t-1, aLvlGro_hist, Saving rate

Expand Down Expand Up @@ -251,13 +139,20 @@ def savingRateFunc(SomeType, m):
# Print command disabled to prevent giant print!
#print giant_list

# %%

# In[26]:


# Construct the level of assets A from a*p where a is the ratio to permanent income p
LifeCyclePop.aLvlNow_hist = LifeCyclePop.aNrmNow_hist*LifeCyclePop.pLvlNow_hist
aGro41=LifeCyclePop.aLvlNow_hist[41]/LifeCyclePop.aLvlNow_hist[40]
aGro41NoU=aGro41[aGro41[:]>0.2] # Throw out extreme outliers

# %%

# In[27]:


# Plot the distribution of growth rates of wealth between age 65 and 66 (=25 + 41)
from matplotlib import pyplot as plt
n, bins, patches = plt.hist(aGro41NoU,50,density=True)