Skip to content

Commit

Permalink
update using tranquilo_ls
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlujan91 committed Mar 20, 2024
1 parent 7122eab commit 3a79b05
Show file tree
Hide file tree
Showing 24 changed files with 113,847 additions and 275 deletions.
34 changes: 21 additions & 13 deletions code/do_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
still run.
"""

from estimark.min import estimate_min
from estimark.estimation import estimate
from estimark.options import (
all_replications,
high_resource,
Expand Down Expand Up @@ -99,15 +99,15 @@ def run_replication():
replication_specs = {}

if which_model == "1" or which_model == "":
replication_specs["agent_name"] = "IndShock"
agent_name = "IndShock"
elif which_model == "2":
replication_specs["agent_name"] = "Portfolio"
agent_name = "Portfolio"
elif which_model == "3":
replication_specs["agent_name"] = "WarmGlow"
agent_name = "WarmGlow"
elif which_model == "4":
replication_specs["agent_name"] = "WarmGlowPortfolio"
agent_name = "WarmGlowPortfolio"
elif which_model == "5":
replication_specs["agent_name"] = "WealthPortfolio"
agent_name = "WealthPortfolio"
else:
print("Invalid model choice.")
return
Expand Down Expand Up @@ -135,15 +135,23 @@ def run_replication():
print("Invalid replication choice.")
return

if subjective_markets == "2" or subjective_markets == "4":
replication_specs["subjective_stock"] = True
print("Adding subjective stock market beliefs...")
if int(subjective_markets) > 1:
agent_name += "Sub"

if subjective_markets == "3" or subjective_markets == "4":
replication_specs["subjective_labor"] = True
print("Adding subjective labor market beliefs...")
if subjective_markets == "2" or subjective_markets == "4":
agent_name += "(Stock)"
print("Adding subjective stock market beliefs...")

estimate_min(**replication_specs)
if subjective_markets == "3" or subjective_markets == "4":
agent_name += "(Labor)"
print("Adding subjective labor market beliefs...")

agent_name += "Market"

replication_specs["agent_name"] = agent_name
replication_specs["save_dir"] = "content/tables/min"

estimate(**replication_specs)


if __name__ == "__main__":
Expand Down
10 changes: 3 additions & 7 deletions code/estimark/estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ def msm_criterion(params, agent=None, emp_moments=None):
median wealth-to-permanent-income ratio in the simulation.
"""
emp_moments = emp_moments.copy()
sim_moments = simulate_moments(params, agent, emp_moments)

# normalize wealth moments by the maximum value in the empirical moments
Expand Down Expand Up @@ -521,12 +522,7 @@ def do_estimate_model(
# Create the simple estimate table
estimate_results_file = save_dir / (agent.name + "_estimate_results.csv")

keys_to_save = [
key
for key in vars(res)
if key
not in ["history", "convergence_report", "multistart_info", "algorithm_output"]
]
keys_to_save = vars(res)

with open(estimate_results_file, "w") as f:
writer = csv.writer(f)
Expand Down Expand Up @@ -858,7 +854,7 @@ def estimate(
if __name__ == "__main__":
# Set booleans to determine which tasks should be done
# Which agent type to estimate ("IndShock" or "Portfolio")
local_agent_name = "IndShock"
local_agent_name = "IndShockSub(Labor)Market"
local_params_to_estimate = ["CRRA", "DiscFac"]
local_estimate_model = True # Whether to estimate the model
# Whether to get standard errors via bootstrap
Expand Down
15 changes: 8 additions & 7 deletions code/estimark/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# Number of points in discrete approximation to transitory income shocks
TranShkCount = 7
UnempPrb = 0.05 # Probability of unemployment while working
UnempPrbRet = 0.005 # Probability of "unemployment" while retired # maybe one more zero
UnempPrbRet = 0.005 # Probability of "unemployment" while retired
IncUnemp = 0.3 # Unemployment benefits replacement rate
IncUnempRet = 0.0 # "Unemployment" benefits when retired
ss_variances = True # Use the Sabelhaus-Song variance profiles
Expand Down Expand Up @@ -63,7 +63,7 @@
bounds_CRRA = [1.1, 20.0]

# Initial guess of the adjustment to the discount factor during estimation (beth)
init_DiscFac = 1.0
init_DiscFac = 0.95
# Bounds for beth; if violated, objective function returns "penalty value"
bounds_DiscFac = [0.5, 1.1]

Expand Down Expand Up @@ -152,17 +152,18 @@
}

minimize_options = {
"algorithm": "scipy_neldermead",
"algorithm": "tranquilo_ls",
"multistart": True,
"error_handling": "continue",
"algo_options": {
"convergence.absolute_params_tolerance": 1e-3,
"convergence.absolute_criterion_tolerance": 1e-3,
"stopping.max_iterations": 50,
"stopping.max_criterion_evaluations": 100,
# "n_cores": 12,
"trustregion_initial_radius": 0.01,
"n_cores": 12,
},
# "numdiff_options": {"n_cores": 12},
"numdiff_options": {"n_cores": 12},
}

# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -219,6 +220,6 @@

# from Tao's JMP
init_subjective_labor = {
"TranShkStd": [0.03] * len(inc_calib["TranShkStd"]),
"PermShkStd": [0.03] * len(inc_calib["PermShkStd"]),
"TranShkStd": [0.03] * (retirement_t + 1) + [0.0] * (terminal_t - retirement_t - 1),
"PermShkStd": [0.03] * (retirement_t + 1) + [0.0] * (terminal_t - retirement_t - 1),
}
13 changes: 1 addition & 12 deletions code/run_all.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import itertools

import dask
from dask.distributed import Client
from estimark.estimation import estimate
from estimark.options import low_resource

Expand All @@ -16,10 +14,6 @@

# Ask the user which replication to run, and run it:
def run_replication():
client = Client(threads_per_worker=10, n_workers=20)

lazy_results = []

for agent_name in agent_names:
for sub_stock, sub_labor in itertools.product(range(2), repeat=2):
temp_agent_name = agent_name
Expand All @@ -37,12 +31,7 @@ def run_replication():

print("Model: ", replication_specs["agent_name"])

lazy_result = dask.delayed(estimate)(**replication_specs)
lazy_results.append(lazy_result)

dask.compute(*lazy_results)

client.close()
estimate(**replication_specs)

print("All replications complete.")

Expand Down
Loading

0 comments on commit 3a79b05

Please sign in to comment.