Skip to content

Commit

Permalink
update code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlujan91 committed Jan 4, 2024
1 parent 947ed2f commit 707b443
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 182 deletions.
Empty file removed code/calibration/__init__.py
Empty file.
77 changes: 3 additions & 74 deletions code/calibration/estimation_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,73 +74,7 @@
)

# Age-varying discount factors over the lifecycle, lifted from Cagetti (2003)
DiscFac_timevary = [
1.064914,
1.057997,
1.051422,
1.045179,
1.039259,
1.033653,
1.028352,
1.023348,
1.018632,
1.014198,
1.010037,
1.006143,
1.002509,
0.9991282,
0.9959943,
0.9931012,
0.9904431,
0.9880143,
0.9858095,
0.9838233,
0.9820506,
0.9804866,
0.9791264,
0.9779656,
0.9769995,
0.9762239,
0.9756346,
0.9752274,
0.9749984,
0.9749437,
0.9750595,
0.9753422,
0.9757881,
0.9763936,
0.9771553,
0.9780698,
0.9791338,
0.9803439,
0.981697,
0.8287214,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
0.9902111,
]
DiscFac_timevary = np.genfromtxt("code/data/Cagetti2003.csv")

# Survival probabilities over the lifecycle
liv_prb = parse_ssa_life_table(
Expand All @@ -150,15 +84,10 @@
# Age groups for the estimation: calculate average wealth-to-permanent income ratio
# for consumers within each of these age groups, compare actual to simulated data
empirical_cohort_age_groups = [
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35],
[36, 37, 38, 39, 40],
[41, 42, 43, 44, 45],
[46, 47, 48, 49, 50],
[51, 52, 53, 54, 55],
[56, 57, 58, 59, 60],
[age for age in range(start, start + 5)] for start in range(26, 61, 5)
]


# Three point discrete distribution of initial w
initial_wealth_income_ratio_vals = np.array([0.17, 0.5, 0.83])
# Equiprobable discrete distribution of initial w
Expand Down
38 changes: 7 additions & 31 deletions code/calibration/setup_scf_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,20 @@
Sets up the SCF data for use in the SolvingMicroDSOPs estimation.
"""

import os
from code.calibration.estimation_parameters import (
empirical_cohort_age_groups,
initial_age,
)

import numpy as np # Numerical Python
import pandas as pd

from calibration.estimation_parameters import empirical_cohort_age_groups, initial_age

# Find pathname to this file:
my_file_path = os.path.dirname(os.path.abspath(__file__))

# Pathnames to the other files:
# Relative directory for primitive parameter files
calibration_dir = os.path.join(my_file_path, "../calibration/")
# Relative directory for primitive parameter files
tables_dir = os.path.join(my_file_path, "../tables/")
# Relative directory for primitive parameter files
figures_dir = os.path.join(my_file_path, "../figures/")
# Relative directory for primitive parameter files
code_dir = os.path.join(my_file_path, "../code/")

# Need to rely on the manual insertion of pathnames to all files in do_all.py
# NOTE sys.path.insert(0, os.path.abspath(tables_dir)), etc. may need to be
# copied from do_all.py to here

# Import files first:


# The following libraries are part of the standard python distribution

# Set the path to the empirical data:
# os.path.abspath('./') #'./'
scf_data_path = data_location = os.path.dirname(os.path.abspath(__file__))

data = pd.read_csv(scf_data_path + "/data/SCFdata.csv")
scf_data = pd.read_csv("code/data/SCFdata.csv")

# Keep only observations with normal incomes > 0,
# otherwise wealth/income is not well defined
data = data[data.norminc > 0.0]
scf_data = scf_data[scf_data.norminc > 0.0]

# Initialize empty lists for the data
w_to_y_data = [] # Ratio of wealth to permanent income
Expand All @@ -49,7 +25,7 @@
# Only extract the data required from the SCF dataset
search_ages = [ages[-1] for ages in empirical_cohort_age_groups]
for idx, age in enumerate(search_ages):
age_data = data[data.age_group.str.contains(f"{age}]")]
age_data = scf_data[scf_data.age_group.str.contains(f"{age}]")]
w_to_y_data.append(age_data.wealth_income_ratio.values)
empirical_weights.append(age_data.weight.values)
# create a group id 1-7 for every observation
Expand Down
65 changes: 65 additions & 0 deletions code/data/Cagetti2003.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
1.064914
1.057997
1.051422
1.045179
1.039259
1.033653
1.028352
1.023348
1.018632
1.014198
1.010037
1.006143
1.002509
0.9991282
0.9959943
0.9931012
0.9904431
0.9880143
0.9858095
0.9838233
0.9820506
0.9804866
0.9791264
0.9779656
0.9769995
0.9762239
0.9756346
0.9752274
0.9749984
0.9749437
0.9750595
0.9753422
0.9757881
0.9763936
0.9771553
0.9780698
0.9791338
0.9803439
0.981697
0.8287214
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
0.9902111
File renamed without changes.
35 changes: 3 additions & 32 deletions do_all.py → code/do_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,43 +46,14 @@
still run.
"""

import os
import sys

from calibration.options import (
from code.calibration.options import (
all_replications,
high_resource,
low_resource,
medium_resource,
high_resource,
all_replications,
)
from code.estimation import estimate

# Find pathname to this file:
my_file_path = os.path.dirname(os.path.abspath(__file__))

# Pathnames to the other files:
# Relative directory for primitive parameter files
calibration_dir = os.path.join(my_file_path, "calibration")
# Relative directory for primitive parameter files
tables_dir = os.path.join(my_file_path, "tables")
# Relative directory for primitive parameter files
figures_dir = os.path.join(my_file_path, "figures")
# Relative directory for primitive parameter files
code_dir = os.path.join(my_file_path, "code")

# manually add the pathnames to the various files directly to the beginning
# of the Python path. This will be needed for all files that will run in
# lower directories.
sys.path.insert(0, calibration_dir)
sys.path.insert(0, tables_dir)
sys.path.insert(0, figures_dir)
sys.path.insert(0, code_dir)
sys.path.insert(0, my_file_path)


# Manual import needed, should draw from first instance at start of Python
# PATH added above:


# Ask the user which replication to run, and run it:
def run_replication():
Expand Down
File renamed without changes.
60 changes: 25 additions & 35 deletions code/estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
income as defined in ConsIndShockModel.
"""

# Parameters for the consumer type and the estimation
import code.calibration.estimation_parameters as parameters
import code.calibration.setup_scf_data as data # SCF 2004 data on household wealth
import csv
import os
import sys
from time import time # Timing utility

import matplotlib.pyplot as plt
Expand All @@ -35,29 +36,16 @@
from HARK.estimation import bootstrap_sample_from_data, minimize_nelder_mead
from scipy.optimize import approx_fprime

# Parameters for the consumer type and the estimation
import calibration.estimation_parameters as parameters
import calibration.setup_scf_data as data # SCF 2004 data on household wealth

# Find pathname to this file:
my_file_path = os.path.dirname(os.path.abspath(__file__))

# Pathnames to the other files:
# Relative directory for primitive parameter files
calibration_dir = os.path.join(my_file_path, "../calibration/")
calibration_dir = "code/calibration/"
# Relative directory for primitive parameter files
tables_dir = os.path.join(my_file_path, "../tables/")
tables_dir = "content/tables/"
# Relative directory for primitive parameter files
figures_dir = os.path.join(my_file_path, "../figures/")
figures_dir = "content/figures/"
# Relative directory for primitive parameter files
code_dir = os.path.join(my_file_path, "../code/")

# Add the calibration folder to the path
sys.path.insert(0, os.path.abspath(calibration_dir))

# Need to rely on the manual insertion of pathnames to all files in do_all.py
# NOTE sys.path.insert(0, os.path.abspath(tables_dir)), etc. may need to be
# copied from do_all.py to here
code_dir = "code/"


# Set booleans to determine which tasks should be done
Expand Down Expand Up @@ -513,9 +501,10 @@ def smmObjectiveFxnReduced(parameters_to_estimate):
)

# Create the simple estimate table
estimate_results_file = os.path.join(
tables_dir, estimation_agent + "_estimate_results.csv"
estimate_results_file = (
tables_dir + "/" + estimation_agent + "_estimate_results.csv"
)

with open(estimate_results_file, "wt") as f:
writer = csv.writer(f)
writer.writerow(["DiscFacAdj", "CRRA"])
Expand Down Expand Up @@ -559,9 +548,10 @@ def smmObjectiveFxnReduced(parameters_to_estimate):
)

# Create the simple bootstrap table
bootstrap_results_file = os.path.join(
tables_dir, estimation_agent + "_bootstrap_results.csv"
bootstrap_results_file = (
tables_dir + "/" + estimation_agent + "_bootstrap_results.csv"
)

with open(bootstrap_results_file, "wt") as f:
writer = csv.writer(f)
writer.writerow(
Expand Down Expand Up @@ -630,9 +620,9 @@ def simulate_moments_reduced(x):
axs[1].set_ylabel("Sensitivity")
axs[1].set_xlabel("Median W/Y Ratio")

plt.savefig(os.path.join(figures_dir, estimation_agent + "Sensitivity.pdf"))
plt.savefig(os.path.join(figures_dir, estimation_agent + "Sensitivity.png"))
plt.savefig(os.path.join(figures_dir, estimation_agent + "Sensitivity.svg"))
plt.savefig(figures_dir + "/" + estimation_agent + "Sensitivity.pdf")
plt.savefig(figures_dir + "/" + estimation_agent + "Sensitivity.png")
plt.savefig(figures_dir + "/" + estimation_agent + "Sensitivity.svg")

plt.show()

Expand Down Expand Up @@ -675,9 +665,9 @@ def simulate_moments_reduced(x):
pylab.plot(model_estimate[1], model_estimate[0], "*r", ms=15)
pylab.xlabel(r"coefficient of relative risk aversion $\rho$", fontsize=14)
pylab.ylabel(r"discount factor adjustment $\beth$", fontsize=14)
pylab.savefig(os.path.join(figures_dir, estimation_agent + "SMMcontour.pdf"))
pylab.savefig(os.path.join(figures_dir, estimation_agent + "SMMcontour.png"))
pylab.savefig(os.path.join(figures_dir, estimation_agent + "SMMcontour.svg"))
pylab.savefig(figures_dir + "/" + estimation_agent + "SMMcontour.pdf")
pylab.savefig(figures_dir + "/" + estimation_agent + "SMMcontour.png")
pylab.savefig(figures_dir + "/" + estimation_agent + "SMMcontour.svg")
pylab.show()


Expand Down Expand Up @@ -871,14 +861,14 @@ def simulate_moments_reduced(x):
f"Time to execute all: {time_to_contour / 60:.2f} min, {time_to_contour:.2f} sec"
)

fig_sensitivity.savefig(os.path.join(figures_dir, "AllSensitivity.pdf"))
fig_sensitivity.savefig(os.path.join(figures_dir, "AllSensitivity.png"))
fig_sensitivity.savefig(os.path.join(figures_dir, "AllSensitivity.svg"))
fig_sensitivity.savefig(figures_dir + "/AllSensitivity.pdf")
fig_sensitivity.savefig(figures_dir + "/AllSensitivity.png")
fig_sensitivity.savefig(figures_dir + "/AllSensitivity.svg")

# Save and show the plot
fig_contour.savefig(os.path.join(figures_dir, "AllSMMcontour.pdf"))
fig_contour.savefig(os.path.join(figures_dir, "AllSMMcontour.png"))
fig_contour.savefig(os.path.join(figures_dir, "AllSMMcontour.svg"))
fig_contour.savefig(figures_dir + "/AllSMMcontour.pdf")
fig_contour.savefig(figures_dir + "/AllSMMcontour.png")
fig_contour.savefig(figures_dir + "/AllSMMcontour.svg")


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion code/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from calibration.options import low_resource, medium_resource
from code.calibration.options import low_resource, medium_resource
from code.estimation import estimate


Expand Down
Loading

0 comments on commit 707b443

Please sign in to comment.