Skip to content

Commit

Permalink
Merge pull request #200 from alanlujan91/cstwmpc
Browse files Browse the repository at this point in the history
use AgentPopulation
  • Loading branch information
alanlujan91 committed Apr 26, 2023
2 parents a4ecf03 + 1ea5e66 commit 01ee13b
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 691 deletions.
2 changes: 1 addition & 1 deletion notebooks/Alternative-Combos-Of-Parameter-Values.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand Down
2 changes: 1 addition & 1 deletion notebooks/ChangeLiqConstr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand Down
2 changes: 1 addition & 1 deletion notebooks/Chinese-Growth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3
# language: python
Expand Down
2 changes: 1 addition & 1 deletion notebooks/DCEGM-Upper-Envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand Down
12 changes: 6 additions & 6 deletions notebooks/DiamondOLG.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand Down Expand Up @@ -1444,7 +1444,7 @@
# %% [markdown]
# ### Convergence of OLG Economy to Steady State

# %% tags=[]
# %%
# Some initial setup
from HARK.ConsumptionSaving.ConsIndShockModel import (
PerfForesightConsumerType,
Expand Down Expand Up @@ -1472,7 +1472,7 @@
years_per_gen = 30


# %% jupyter={"source_hidden": true} tags=[]
# %% jupyter={"source_hidden": true}
# Define a function that plots something given some inputs
def plot1(Epsilon, DiscFac, PopGrowth, YearsPerGeneration, Initialk):
"""Inputs:
Expand Down Expand Up @@ -1568,7 +1568,7 @@ def plot1(Epsilon, DiscFac, PopGrowth, YearsPerGeneration, Initialk):
return None


# %% jupyter={"source_hidden": true} tags=[]
# %% jupyter={"source_hidden": true}
# Define some widgets to control the plot

# Define a slider for Epsilon
Expand Down Expand Up @@ -1619,7 +1619,7 @@ def plot1(Epsilon, DiscFac, PopGrowth, YearsPerGeneration, Initialk):
style={"description_width": "initial"},
)

# %% tags=[]
# %%
# Make the widget
interact(
plot1,
Expand Down Expand Up @@ -1885,7 +1885,7 @@ def plot3(Epsilon, DiscFac, z, Rfree, YearsPerGeneration):
)


# %% tags=[]
# %%
# Make the widget
interact(
plot3,
Expand Down
2 changes: 1 addition & 1 deletion notebooks/FisherTwoPeriod.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3
# language: python
Expand Down
2 changes: 1 addition & 1 deletion notebooks/Gentle-Intro-To-HARK-Buffer-Stock-Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand Down
2 changes: 1 addition & 1 deletion notebooks/Gentle-Intro-To-HARK-PerfForesightCRRA.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand Down
5 changes: 3 additions & 2 deletions notebooks/Harmenberg-Aggregation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"cell_type": "markdown",
"id": "d940f86a",
"metadata": {
"incorrectly_encoded_metadata": "jp-MarkdownHeadingCollapsed=true tags=[] jp-MarkdownHeadingCollapsed=true"
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"# Description of the problem\n",
Expand Down Expand Up @@ -865,7 +866,7 @@
],
"metadata": {
"jupytext": {
"cell_metadata_filter": "ExecuteTime,collapsed,title,code_folding,tags,incorrectly_encoded_metadata,-autoscroll",
"cell_metadata_filter": "ExecuteTime,collapsed,title,code_folding,tags,incorrectly_encoded_metadata,jp-MarkdownHeadingCollapsed,-autoscroll",
"encoding": "# -*- coding: utf-8 -*-",
"formats": "ipynb,py:percent",
"notebook_metadata_filter": "all,-widgets,-varInspector"
Expand Down
32 changes: 16 additions & 16 deletions notebooks/Harmenberg-Aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# ---
# jupyter:
# jupytext:
# cell_metadata_filter: ExecuteTime,collapsed,title,code_folding,tags,incorrectly_encoded_metadata,-autoscroll
# cell_metadata_filter: ExecuteTime,collapsed,title,code_folding,tags,incorrectly_encoded_metadata,jp-MarkdownHeadingCollapsed,-autoscroll
# formats: ipynb,py:percent
# notebook_metadata_filter: all,-widgets,-varInspector
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand All @@ -36,7 +36,7 @@
# %% [markdown]
# `# Set Up the Computational Environment: (in JupyterLab, click the dots)`

# %% code_folding=[0] tags=[]
# %% code_folding=[0]
# Preliminaries
import numpy as np
import pandas as pd
Expand All @@ -49,7 +49,7 @@
init_idiosyncratic_shocks,
)

# %% [markdown] jp-MarkdownHeadingCollapsed=true tags=[] jp-MarkdownHeadingCollapsed=true
# %% [markdown] jp-MarkdownHeadingCollapsed=true
# # Description of the problem
#
# $\newcommand{\pLvl}{\mathbf{p}}$
Expand Down Expand Up @@ -116,7 +116,7 @@
#
# This notebook briefly describes Harmenberg's method and demonstrates its implementation in the HARK toolkit.

# %% [markdown] tags=[]
# %% [markdown]
# # Description of the method
#
# To illustrate Harmenberg's idea, consider a [buffer stock saving](https://econ-ark.github.io/BufferStockTheory) model in which:
Expand Down Expand Up @@ -188,7 +188,7 @@
# %% [markdown]
# `# Implementation in HARK:`

# %% [markdown] code_folding=[] tags=[]
# %% [markdown] code_folding=[]
# #### Farther down in the notebook, code like this solves the standard model:
#
# ```python
Expand All @@ -212,7 +212,7 @@
# popn.simulate()
# ```

# %% [markdown] code_folding=[] tags=[]
# %% [markdown] code_folding=[]
# #### Later, code like this simulates using the permanent-income-neutral measure
# ```python
# # Harmenberg permanent-income-neutral simulation
Expand All @@ -230,7 +230,7 @@
# ntrl.simulate()
# ```

# %% [markdown] tags=[]
# %% [markdown]
# All we had to do differently to simulate using the permanent-income-neutral measure was to set the agent's property `neutral_measure=True`.
#
# This is implemented when the function `update_income_process` re-constructs the agent's income process. The specific lines that achieve the change of measure in HARK are in [this link](https://github.com/econ-ark/HARK/blob/760df611a6ec2ff147d00b7d866dbab6fc4e18a1/HARK/ConsumptionSaving/ConsIndShockModel.py#L2734-L2735), or reproduced here:
Expand Down Expand Up @@ -289,7 +289,7 @@
# %% [markdown]
# `# Setup computational environment:`

# %% Experiment setup tags=[]
# %% Experiment setup
# How long to run the economies without sampling? T_0
# Because we start the population at mBalLvl which turns out to be close
# to MBalLvl so we don't need a long burn in period
Expand Down Expand Up @@ -319,7 +319,7 @@
# %% [markdown]
# `# Define tool to calculate summary statistics:`

# %% Define function to get our stats of interest code_folding=[0] tags=[]
# %% Define function to get our stats of interest code_folding=[0]
# Now create a function that takes HARK's simulation output
# and computes all the summary statistics we need

Expand Down Expand Up @@ -351,7 +351,7 @@ def sumstats(sims, sample_periods):
# %% [markdown]
# We now configure and solve a buffer-stock agent with a default parametrization.

# %% Create and simulate agent tags=[]
# %% Create and simulate agent
# Create and solve agent

popn = IndShockConsumerType(**init_idiosyncratic_shocks)
Expand All @@ -377,7 +377,7 @@ def sumstats(sims, sample_periods):
#
# We now check both conditions with our parametrization.

# %% tags=[]
# %%
# Two versions for different HARK versions
try: # This works with HARK 2.0 pre-alpha
Bilt = popn.solution[0].Bilt
Expand Down Expand Up @@ -413,7 +413,7 @@ def sumstats(sims, sample_periods):
#
# First, we simulate using the traditional approach.

# %% tags=[]
# %%
# Base simulation

# Start assets at m balanced growth (levels) point
Expand All @@ -436,7 +436,7 @@ def sumstats(sims, sample_periods):
# %% [markdown]
# Update and simulate using Harmenberg's strategy. This time, not multiplying by permanent income.

# %% tags=[]
# %%
# Harmenberg permanent income neutral simulation

# Start by duplicating the previous setup
Expand All @@ -462,7 +462,7 @@ def sumstats(sims, sample_periods):
#
# The plots below show the (logs of) the estimated variances for the two methods as a function of the (logs of) the number of agents.

# %% code_folding=[0] tags=[]
# %% code_folding=[0]
# Plots

# Construct aggregate levels and growth rates
Expand Down Expand Up @@ -531,7 +531,7 @@ def sumstats(sims, sample_periods):
plt.show()


# %% [markdown] tags=[]
# %% [markdown]
# # Harmenberg's Method Produces Large Gains in Efficiency
#
# The number of agents required to achieve a given variance is revealed by choosing that variance and then finding the points on the horizontal axis that correspond to the two methods.
Expand Down
2 changes: 1 addition & 1 deletion notebooks/IncExpectationExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand Down
2 changes: 1 addition & 1 deletion notebooks/KeynesFriedmanModigliani.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3
# language: python
Expand Down
22 changes: 11 additions & 11 deletions notebooks/LC-Model-Expected-Vs-Realized-Income-Growth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand All @@ -25,15 +25,15 @@
# version: 3.10.8
# ---

# %% [markdown] tags=[]
# %% [markdown]
# # Expectated vs Realized Income Growth in A Standard Life Cycle Model

# %% [markdown]
# This notebook uses the income process in [Cocco, Gomes & Maenhout (2005)](https://academic.oup.com/rfs/article/18/2/491/1599892?login=true) to demonstrate that estimates of a regression of expected income changes on realized income changes are sensitive to the size of transitory shocks.
#
# We first load some tools from the [HARK toolkit](https://github.com/econ-ark/HARK).

# %% jupyter={"source_hidden": true} tags=[]
# %% jupyter={"source_hidden": true}
import statsmodels.api as sm
from linearmodels.panel.model import PanelOLS
from HARK.distribution import calc_expectation
Expand All @@ -52,10 +52,10 @@
import pandas as pd
from copy import copy

# %% [markdown] tags=[]
# %% [markdown]
# We now create a population of agents with the income process of [Cocco, Gomes & Maenhout (2005)](https://academic.oup.com/rfs/article/18/2/491/1599892?login=true), which is implemented as a default calibration in the toolkit.

# %% Alter calibration jupyter={"source_hidden": true} tags=[]
# %% Alter calibration jupyter={"source_hidden": true}
birth_age = 21
death_age = 66
adjust_infl_to = 1992
Expand Down Expand Up @@ -104,7 +104,7 @@
# %% Create and solve agent [markdown]
# We simulate a population of agents

# %% Simulation tags=[]
# %% Simulation
# %%capture
# Run the simulations
Agent.initialize_sim()
Expand Down Expand Up @@ -156,7 +156,7 @@
# \end{equation}
#

# %% Compute expectations jupyter={"source_hidden": true} tags=[]
# %% Compute expectations jupyter={"source_hidden": true}
exp = [
calc_expectation(Agent.IncShkDstn[i], func=lambda x: x[0] * x[1])
for i in range(Agent.T_cycle)
Expand Down Expand Up @@ -197,7 +197,7 @@
# \end{equation*}
# We now estimate an analogous regression in our simulated population.

# %% jupyter={"source_hidden": true} tags=[]
# %% jupyter={"source_hidden": true}
Data = Data.set_index(["id", "Age"])

# Create the variables they actually use
Expand All @@ -218,7 +218,7 @@
#
# However, with less volatile transitory shocks, the regression coefficient would be positive. We demonstrate this by shutting off transitory shocks, simulating another population of agents, and re-running the regression.

# %% tags=[]
# %%
# %%capture
params_no_transitory = copy(params)
params_no_transitory.update({"TranShkStd": [0.0] * len(params["TranShkStd"])})
Expand All @@ -231,7 +231,7 @@
Agent_nt.simulate()


# %% jupyter={"source_hidden": true} tags=[]
# %% jupyter={"source_hidden": true}
exp = [
calc_expectation(Agent_nt.IncShkDstn[i], func=lambda x: x[0] * x[1])
for i in range(Agent_nt.T_cycle)
Expand Down Expand Up @@ -266,7 +266,7 @@

Data["Y_change"] = Data.groupby("id")["Y"].diff(1)

# %% jupyter={"source_hidden": true} tags=[]
# %% jupyter={"source_hidden": true}
# Create variables
Data["ExpBin"] = 0
Data.loc[Data["ExpIncChange"] > 0, "ExpBin"] = 1
Expand Down
2 changes: 1 addition & 1 deletion notebooks/LifeCycleModelTheoryVsData.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.4
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand Down

0 comments on commit 01ee13b

Please sign in to comment.