Skip to content

Commit

Permalink
Merge pull request #12 from jdebacker/macro_params
Browse files Browse the repository at this point in the history
Merging
  • Loading branch information
rickecon authored Aug 5, 2024
2 parents f25305c + 1382e1f commit 9a1f3ed
Show file tree
Hide file tree
Showing 18 changed files with 101,790 additions and 2,051 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include ogidn/ogidn_default_parameters.json
include ogidn/data/002_IFPRI_SAM_IDN_2018_SAM.csv
Binary file modified docs/book/content/calibration/images/OrigVsAdjImm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/book/content/calibration/images/OrigVsFixSSpop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/book/content/calibration/images/PopDistPath.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/book/content/calibration/images/fert_rates.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/book/content/calibration/images/mort_rates.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/create_doc_figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"""
# IDN profiles
pp.plot_ability_profiles(
p, p2=None, t=None, log_scale=True, include_title=False, path=plot_path
p, p2=None, t=None, log_scale=False, include_title=False, path=plot_path
)
# Plotting with USA also is too busy, so do separately
pp.plot_ability_profiles(
Expand Down
55 changes: 20 additions & 35 deletions examples/run_og_idn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
import json
import time
import copy
import importlib.resources
import matplotlib.pyplot as plt
from ogidn.calibrate import Calibration
import ogcore
from ogcore.parameters import Specifications
from ogcore import output_tables as ot
from ogcore import output_plots as op
from ogcore.execute import runner
from ogcore.utils import safe_read_pickle, param_dump_json
from ogidn.calibrate import Calibration
from ogidn.utils import is_connected

# Use a custom matplotlib style file for plots
style_file_url = (
"https://raw.githubusercontent.com/PSLmodels/OG-Core/"
+ "master/ogcore/OGcorePlots.mplstyle"
)
plt.style.use(style_file_url)
plt.style.use("ogcore.OGcorePlots")


def main():
Expand All @@ -30,8 +29,9 @@ def main():

# Directories to save data
CUR_DIR = os.path.dirname(os.path.realpath(__file__))
base_dir = os.path.join(CUR_DIR, "OG-IDN-Example", "OUTPUT_BASELINE")
reform_dir = os.path.join(CUR_DIR, "OG-IDN-Example", "OUTPUT_REFORM")
save_dir = os.path.join(CUR_DIR, "OG-IDN-Example")
base_dir = os.path.join(save_dir, "OUTPUT_BASELINE")
reform_dir = os.path.join(save_dir, "OUTPUT_REFORM")

"""
---------------------------------------------------------------------------
Expand All @@ -46,31 +46,16 @@ def main():
output_base=base_dir,
)
# Update parameters for baseline from default json file
p.update_specifications(
json.load(
open(
os.path.join(
CUR_DIR, "..", "ogidn", "ogidn_default_parameters.json"
)
)
)
)
with importlib.resources.open_text(
"ogidn", "ogidn_default_parameters.json"
) as file:
defaults = json.load(file)
p.update_specifications(defaults)
# Update parameters from calibrate.py Calibration class
c = Calibration(p)
updated_params = c.get_dict()
p.update_specifications(updated_params)
# set tax rates
p.update_specifications(
{
"cit_rate": [[0.27]],
"tax_func_type": "linear",
"age_specific": False,
# "etr_params": [[[0.22]]],
# "mtrx_params": [[[0.31]]],
# "mtry_params": [[[0.25]]],
"tau_c": [[0.15]],
}
)
if is_connected(): # only update if connected to internet
c = Calibration(p)
updated_params = c.get_dict()
p.update_specifications(updated_params)

# Run model
start_time = time.time()
Expand All @@ -88,7 +73,7 @@ def main():
p2.baseline = False
p2.output_base = reform_dir

# additional parameters to change
# Parameter change for the reform run
updated_params_ref = {
"cit_rate": [[0.30]],
}
Expand Down Expand Up @@ -126,12 +111,12 @@ def main():

# create plots of output
op.plot_all(
base_dir, reform_dir, os.path.join(CUR_DIR, "OG-IDN_example_plots")
base_dir, reform_dir, os.path.join(save_dir, "OG-IDN_example_plots")
)

print("Percentage changes in aggregates:", ans)
# save percentage change output to csv file
ans.to_csv("ogidn_example_output.csv")
ans.to_csv(os.path.join(save_dir, "OG-IDN_example_output.csv"))


if __name__ == "__main__":
Expand Down
27 changes: 13 additions & 14 deletions ogidn/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
from ogidn import input_output as io
import os
import numpy as np
import datetime
from ogcore import demographics

UN_COUNTRY_CODE = "360"


class Calibration:
"""OG-IDN calibration class"""

def __init__(
self,
p,
estimate_tax_functions=False,
estimate_chi_n=False,
macro_data_start_year=datetime.datetime(1947, 1, 1),
macro_data_end_year=datetime.date.today(),
demographic_data_path=None,
output_path=None,
):
Expand All @@ -21,9 +24,6 @@ def __init__(
Args:
p (OG-Core Specifications object): model parameters
estimate_tax_functions (bool): whether to estimate tax
function parameters
estimate_chi_n (bool): whether to estimate chi_n
demographic_data_path (str): path to save demographic data
output_path (str): path to save output to
Expand All @@ -35,11 +35,11 @@ def __init__(
if output_path is not None:
if not os.path.exists(output_path):
os.makedirs(output_path)
self.estimate_tax_functions = estimate_tax_functions
self.estimate_chi_n = estimate_chi_n

# Macro estimation
self.macro_params = macro_params.get_macro_params()
self.macro_params = macro_params.get_macro_params(
macro_data_start_year, macro_data_end_year
)

# io matrix and alpha_c
if p.I > 1: # no need if just one consumption good
Expand All @@ -64,7 +64,7 @@ def __init__(
p.T,
0,
99,
country_id="360",
country_id=UN_COUNTRY_CODE,
initial_data_year=p.start_year - 1,
final_data_year=p.start_year + 1,
GraphDiag=False,
Expand All @@ -78,26 +78,25 @@ def __init__(
p.T,
0,
99,
country_id="360",
country_id=UN_COUNTRY_CODE,
initial_data_year=p.start_year - 1,
final_data_year=p.start_year + 1,
GraphDiag=False,
)

# earnings profiles
self.e = income.get_e_interp(
p.E,
p.S,
self.demographic_params["omega_SS"],
demog80["omega_SS"],
p.J,
p.lambdas,
demog80["omega_SS"],
plot_path=output_path,
)

# method to return all newly calibrated parameters in a dictionary
def get_dict(self):
dict = {}
# if self.estimate_chi_n:
# dict["chi_n"] = self.chi_n
dict.update(self.macro_params)
dict["e"] = self.e
dict["alpha_c"] = self.alpha_c
Expand Down
Loading

0 comments on commit 9a1f3ed

Please sign in to comment.