Skip to content

Commit

Permalink
generalize params
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlujan91 committed Feb 26, 2024
1 parent 99271e2 commit 31b13c1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 40 deletions.
21 changes: 0 additions & 21 deletions code/estimark/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,15 @@
income as defined in ConsIndShockModel.
"""

# Parameters for the consumer type and the estimation

from HARK.ConsumptionSaving.ConsBequestModel import (
BequestWarmGlowConsumerType,
BequestWarmGlowPortfolioType,
)

# Import modules from core HARK libraries:
# The consumption-saving micro model
from HARK.ConsumptionSaving.ConsIndShockModel import IndShockConsumerType
from HARK.ConsumptionSaving.ConsPortfolioModel import PortfolioConsumerType
from HARK.ConsumptionSaving.ConsWealthPortfolioModel import WealthPortfolioConsumerType
from HARK.core import AgentType

# Method for sampling from a discrete distribution

# Estimation methods

# Set booleans to determine which tasks should be done
# Which agent type to estimate ("IndShock" or "Portfolio")
local_estimation_agent = "IndShock"
local_estimate_model = True # Whether to estimate the model
# Whether to get standard errors via bootstrap
local_compute_standard_errors = False
# Whether to compute a measure of estimates' sensitivity to moments
local_compute_sensitivity = True
# Whether to make a contour map of the objective function
local_make_contour_plot = True


# =====================================================
# Define objects and functions used for the estimation
# =====================================================
Expand Down
26 changes: 13 additions & 13 deletions code/estimark/estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ def weighted_median(values, weights):
return median


def get_targeted_moments(data=scf_data, weights=scf_weights, groups=scf_groups):
def get_targeted_moments(
data=scf_data, weights=scf_weights, groups=scf_groups, mapping=scf_mapping
):
# Initialize
group_count = len(scf_mapping)
group_count = len(mapping)
target_moments = np.zeros(group_count)

for g in range(group_count):
Expand All @@ -164,13 +166,15 @@ def get_initial_guess(agent_name):

# Define the objective function for the simulated method of moments estimation
# todo: params, bounds, agent
def simulate_moments(DiscFacAdj, CRRA, agent):
def simulate_moments(params, agent):
"""
A quick check to make sure that the parameter values are within bounds.
Far flung falues of DiscFacAdj or CRRA might cause an error during solution or
simulation, so the objective function doesn't even bother with them.
"""

DiscFacAdj, CRRA = params

bounds_DiscFacAdj = options["bounds_DiscFacAdj"]
bounds_CRRA = options["bounds_CRRA"]

Expand Down Expand Up @@ -210,7 +214,7 @@ def simulate_moments(DiscFacAdj, CRRA, agent):
return sim_moments


def smm_obj_func(DiscFacAdj, CRRA, agent, moments):
def smm_obj_func(params, agent, moments):
"""
The objective function for the SMM estimation. Given values of discount factor
adjuster DiscFacAdj, coeffecient of relative risk aversion CRRA, a base consumer
Expand Down Expand Up @@ -258,7 +262,7 @@ def smm_obj_func(DiscFacAdj, CRRA, agent, moments):
median wealth-to-permanent-income ratio in the simulation.
"""

sim_moments = simulate_moments(DiscFacAdj, CRRA, agent)
sim_moments = simulate_moments(params, agent)
errors = moments - sim_moments
loss = np.dot(errors, errors)

Expand Down Expand Up @@ -313,8 +317,7 @@ def calculate_se_bootstrap(initial_estimate, N, agent, seed=0, verbose=False):
# Make a temporary function for use in this estimation run
def smm_obj_func_bootstrap(params):
return smm_obj_func(
DiscFacAdj=params[0],
CRRA=params[1],
params,
agent=agent,
moments=bootstrap_moments,
)
Expand Down Expand Up @@ -358,8 +361,7 @@ def smm_obj_func_redux(params):
list representing [DiscFacAdj,CRRA].
"""
return smm_obj_func(
DiscFacAdj=params[0],
CRRA=params[1],
params,
agent=estimation_agent,
moments=target_moments,
)
Expand Down Expand Up @@ -445,9 +447,8 @@ def do_compute_sensitivity(agent_name, estimation_agent, model_estimate, initial

# Find the Jacobian of the function that simulates moments
def simulate_moments_redux(params):
moments = simulate_moments(params[0], params[1], agent=estimation_agent)

return moments
return simulate_moments(params, agent=estimation_agent)

n_moments = len(scf_mapping)
jac = np.array(
Expand Down Expand Up @@ -507,8 +508,7 @@ def do_make_contour_plot(agent_name, estimation_agent, model_estimate, target_mo
for k in range(grid_density):
CRRA = CRRA_list[k]
smm_obj_levels[j, k] = smm_obj_func(
DiscFacAdj,
CRRA,
np.array([DiscFacAdj, CRRA]),
agent=estimation_agent,
moments=target_moments,
)
Expand Down
4 changes: 1 addition & 3 deletions code/estimark/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
model. The empirical data is stored in a separate csv file and is loaded in setup_scf_data.
"""

from pathlib import Path

import numpy as np
from HARK.Calibration.Income.IncomeTools import CGM_income, parse_income_spec
from HARK.ConsumptionSaving.ConsIndShockModel import init_lifecycle
Expand Down Expand Up @@ -64,7 +62,7 @@

# Age-varying discount factors over the lifecycle, lifted from Cagetti (2003)
# Get the directory containing the current file and construct the full path to the CSV file
csv_file_path = Path(__file__).resolve().parent / ".." / "data" / "Cagetti2003.csv"
csv_file_path = "code/data/Cagetti2003.csv"
timevary_DiscFac = np.genfromtxt(csv_file_path) * 0.0 + 1.0 # todo
constant_DiscFac = np.ones_like(timevary_DiscFac)

Expand Down
4 changes: 1 addition & 3 deletions code/estimark/scf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
Sets up the SCF data for use in the EstimatingMicroDSOPs estimation.
"""

from pathlib import Path

import numpy as np # Numerical Python
import pandas as pd
from estimark.parameters import initial_age

# Get the directory containing the current file and construct the full path to the CSV file
csv_file_path = Path(__file__).resolve().parent / ".." / "data" / "SCFdata.csv"
csv_file_path = "code/data/SCFdata.csv"

# Read the CSV file
scf_full_data = pd.read_csv(csv_file_path)
Expand Down

0 comments on commit 31b13c1

Please sign in to comment.