From 19a79b65f2a55d7944fe13460b1a91351046b969 Mon Sep 17 00:00:00 2001 From: Alan Lujan Date: Tue, 31 Jan 2023 10:02:16 -0500 Subject: [PATCH 1/3] update cycles = 0 --- simulate/parameters.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/simulate/parameters.py b/simulate/parameters.py index 942d05a..db1419c 100644 --- a/simulate/parameters.py +++ b/simulate/parameters.py @@ -6,10 +6,13 @@ from sharkfin.population import AgentPopulation -def build_population(agent_type, parameters, rng = None, dphm = 1500): - pop = AgentPopulation(agent_type(), parameters, rng = rng, dollars_per_hark_money_unit = dphm) - if 'approx_params' in parameters: - pop.approx_distributions(parameters['approx_params']) + +def build_population(agent_type, parameters, rng=None, dphm=1500): + pop = AgentPopulation( + agent_type(), parameters, rng=rng, dollars_per_hark_money_unit=dphm + ) + if "approx_params" in parameters: + pop.approx_distributions(parameters["approx_params"]) pop.parse_params() pop.create_distributed_agents() @@ -60,9 +63,11 @@ def build_population(agent_type, parameters, rng = None, dphm = 1500): ### Configuring the agent population -whiteshark_parameter_dict = whiteshark_agent_population_params | whiteshark_continuous_dist_params -whiteshark_parameter_dict['approx_params'] = whiteshark_approx_params -whiteshark_parameter_dict['ex_post'] = ["RiskyAvg", "RiskyStd"] +whiteshark_parameter_dict = ( + whiteshark_agent_population_params | whiteshark_continuous_dist_params +) +whiteshark_parameter_dict["approx_params"] = whiteshark_approx_params +whiteshark_parameter_dict["ex_post"] = ["RiskyAvg", "RiskyStd"] whiteshark_parameter_dict["AgentCount"] = 1 WHITESHARK = whiteshark_parameter_dict @@ -76,23 +81,24 @@ def build_population(agent_type, parameters, rng = None, dphm = 1500): ### TODO: Population generators that take parameters like CRRA, DisCFac lucas0_agent_population_params = { + "cycles": 0, # issue 186 "aNrmInitStd": 0.0, "LivPrb": 0.98**0.25, "PermGroFac": 1.0, - "pLvlInitMean": 0.0, ## Intention: Shut down income. But this might not do it. + "pLvlInitMean": 0.0, ## Intention: Shut down income. But this might not do it. "pLvlInitStd": 0.0, "Rfree": 1.0, # Scaling from annual to quarterly "TranShkStd": [0], "PermShkStd": [0], ### These are placeholders that will be set when the system is set up. - "CRRA" : 5, - "DiscFac" : 0.96, - "ex_post" : None # ex post heterogeneous parameters over which to merge solutions + "CRRA": 5, + "DiscFac": 0.96, + "ex_post": None, # ex post heterogeneous parameters over which to merge solutions } lucas0_parameter_dict = lucas0_agent_population_params -lucas0_parameter_dict["AgentCount"] = 10 # TODO: What should this be? +lucas0_parameter_dict["AgentCount"] = 10 # TODO: What should this be? -LUCAS0 = lucas0_parameter_dict \ No newline at end of file +LUCAS0 = lucas0_parameter_dict From 0785401865aa877a8fdfb5a179b3164d5af88e6c Mon Sep 17 00:00:00 2001 From: Alan Lujan Date: Wed, 1 Feb 2023 15:27:20 -0500 Subject: [PATCH 2/3] fix Rfree bug --- sharkfin/population.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/sharkfin/population.py b/sharkfin/population.py index 9956d3a..a213354 100644 --- a/sharkfin/population.py +++ b/sharkfin/population.py @@ -27,7 +27,6 @@ class AgentPopulation: dollars_per_hark_money_unit: float = 1500 def __post_init__(self): - self.time_var = self.agent_class.time_vary self.time_inv = self.agent_class.time_inv @@ -45,12 +44,10 @@ def __post_init__(self): self.stored_class_stats = None def infer_counts(self): - param_dict = self.parameter_dict # if agent_clas_count is not specified, infer from parameters if self.agent_class_count is None: - agent_class_count = 1 for key_param in param_dict: parameter = param_dict[key_param] @@ -63,7 +60,6 @@ def infer_counts(self): self.agent_class_count = agent_class_count if self.t_age is None: - t_age = 1 for key_param in param_dict: parameter = param_dict[key_param] @@ -79,7 +75,6 @@ def infer_counts(self): # return t_age and agent_class_count def approx_distributions(self, approx_params: dict): - param_dict = self.parameter_dict self.continuous_distributions = {} @@ -111,7 +106,6 @@ def approx_distributions(self, approx_params: dict): self.infer_counts() def parse_params(self): - param_dict = self.parameter_dict agent_dicts = [] # container for dictionaries of each agent subgroup @@ -227,7 +221,6 @@ def class_stats(self, store=False): return cs def create_distributed_agents(self): - rng = self.rng if self.rng is not None else np.random.default_rng() self.agents = [ @@ -236,7 +229,6 @@ def create_distributed_agents(self): ] def create_database(self): - database = pd.DataFrame(self.agent_dicts) database["agents"] = self.agents @@ -249,7 +241,6 @@ def solve_distributed_agents(self): agent.solve() def unpack_solutions(self): - self.solution = [agent.solution for agent in self.agents] def init_simulation(self, T_sim=1000): @@ -262,9 +253,10 @@ def init_simulation(self, T_sim=1000): agent.initialize_sim() if self.stored_class_stats is None: - ## build an IndShockConsumerType "double" of this agent, with the same parameters - ind_shock_double = cism.IndShockConsumerType(**agent.parameters) + parameters = agent.parameters.copy() + parameters["Rfree"] = parameters["Rfree"][0] + ind_shock_double = cism.IndShockConsumerType(**parameters) ## solve to get the mNrmStE value ## that is, the Steady-state Equilibrium value of mNrm, for the IndShockModel @@ -290,7 +282,6 @@ def init_simulation(self, T_sim=1000): agent.state_now["aLvl"] = agent.state_now["aNrm"] * agent.state_now["pLvl"] def solve(self, merge_by=None): - self.solve_distributed_agents() if merge_by is not None: @@ -534,7 +525,6 @@ def __init__(self, agent_population): self.agent_database = self.agent_population.agent_database def merge_solutions(self, continuous_states): - # check that continous states are in heterogeneous parameters for state in continuous_states: if state not in self.dist_params: @@ -548,7 +538,6 @@ def merge_solutions(self, continuous_states): self._merge_solutions_3d(continuous_states) def _merge_solutions_2d(self, continuous_states): - discrete_params = list(set(self.dist_params) - set(continuous_states)) discrete_params.sort() @@ -613,7 +602,6 @@ def _merge_solutions_2d(self, continuous_states): return self.solution_database def _merge_solutions_3d(self, continuous_states): - discrete_params = list(set(self.dist_params) - set(continuous_states)) discrete_params.sort() From 349808b4fc8ed31eaaf1e48a307101514d85e21d Mon Sep 17 00:00:00 2001 From: Alan Lujan Date: Fri, 3 Feb 2023 11:47:21 -0500 Subject: [PATCH 3/3] conditional --- sharkfin/population.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sharkfin/population.py b/sharkfin/population.py index 3acbada..c9105d7 100644 --- a/sharkfin/population.py +++ b/sharkfin/population.py @@ -255,7 +255,9 @@ def init_simulation(self, T_sim=1000): if self.stored_class_stats is None: ## build an IndShockConsumerType "double" of this agent, with the same parameters parameters = agent.parameters.copy() - parameters["Rfree"] = parameters["Rfree"][0] + if "Rfree" in parameters and isinstance(parameters["Rfree"], list): + # patch potential bug until HARK is updated + parameters["Rfree"] = parameters["Rfree"][0] ind_shock_double = cism.IndShockConsumerType(**parameters) ## solve to get the mNrmStE value