From 31b13c1ce953816e313794c30fd5945c13d547b2 Mon Sep 17 00:00:00 2001 From: alanlujan91 Date: Mon, 26 Feb 2024 15:19:40 -0500 Subject: [PATCH] generalize params --- code/estimark/agents.py | 21 --------------------- code/estimark/estimation.py | 26 +++++++++++++------------- code/estimark/parameters.py | 4 +--- code/estimark/scf.py | 4 +--- 4 files changed, 15 insertions(+), 40 deletions(-) diff --git a/code/estimark/agents.py b/code/estimark/agents.py index 884d1c1..2369e08 100644 --- a/code/estimark/agents.py +++ b/code/estimark/agents.py @@ -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 # ===================================================== diff --git a/code/estimark/estimation.py b/code/estimark/estimation.py index fe72e69..088b5ee 100644 --- a/code/estimark/estimation.py +++ b/code/estimark/estimation.py @@ -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): @@ -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"] @@ -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 @@ -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) @@ -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, ) @@ -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, ) @@ -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( @@ -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, ) diff --git a/code/estimark/parameters.py b/code/estimark/parameters.py index d1a5d42..c8bc117 100644 --- a/code/estimark/parameters.py +++ b/code/estimark/parameters.py @@ -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 @@ -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) diff --git a/code/estimark/scf.py b/code/estimark/scf.py index 4f13845..d72a8de 100644 --- a/code/estimark/scf.py +++ b/code/estimark/scf.py @@ -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)