From e72d638be5c40d17561dd2ed25c6c047b9e97e17 Mon Sep 17 00:00:00 2001 From: Mateo VG Date: Wed, 12 Oct 2022 19:13:17 -0400 Subject: [PATCH 1/5] Reset t_cycle when reading shocks --- HARK/core.py | 1 + 1 file changed, 1 insertion(+) diff --git a/HARK/core.py b/HARK/core.py index d4295871a..66a7ba76c 100644 --- a/HARK/core.py +++ b/HARK/core.py @@ -747,6 +747,7 @@ def get_mortality(self): # Reset ages of newborns self.t_age[who_dies] = 0 + self.t_cycle[who_dies] = 0 else: who_dies = self.sim_death() self.sim_birth(who_dies) From 283a433fb1cd287dc58a07f5dfc7533fdf22027a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateo=20Vel=C3=A1squez-Giraldo?= Date: Wed, 12 Oct 2022 19:30:15 -0400 Subject: [PATCH 2/5] Update CHANGELOG.md --- Documentation/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Documentation/CHANGELOG.md b/Documentation/CHANGELOG.md index 1c4e8b47c..282fb9d0c 100644 --- a/Documentation/CHANGELOG.md +++ b/Documentation/CHANGELOG.md @@ -30,6 +30,7 @@ Release Date: TBD * Adds support for the calculation of dreivatives in the `interpolation.py` wrappers. [#1157](https://github.com/econ-ark/HARK/pull/1157) * Adds class `DecayInterp` to `econforgeinterp.py`. It implements interpolators that "decay" to some limiting function when extrapolating. [#1165](https://github.com/econ-ark/HARK/pull/1165) * Add methods to non stochastically simulate an economy by computing transition matrices. Functions to compute transition matrices and ergodic distribution have been added [#1155](https://github.com/econ-ark/HARK/pull/1155). +* Fixes a bug that causes `t_age` and `t_cycle` to get out of sync when reading pre-computed mortality. [#1181](https://github.com/econ-ark/HARK/pull/1181) ### Minor Changes From 5065b7e8f3e77e15f988be478be8c4f5cf4e1e0d Mon Sep 17 00:00:00 2001 From: Mateo VG Date: Wed, 12 Oct 2022 21:59:43 -0400 Subject: [PATCH 3/5] Add test --- .../tests/test_IndShockConsumerType.py | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/HARK/ConsumptionSaving/tests/test_IndShockConsumerType.py b/HARK/ConsumptionSaving/tests/test_IndShockConsumerType.py index 963e943c2..878930088 100644 --- a/HARK/ConsumptionSaving/tests/test_IndShockConsumerType.py +++ b/HARK/ConsumptionSaving/tests/test_IndShockConsumerType.py @@ -773,6 +773,81 @@ def test_NewbornStatesAndShocks(self): ) +class testLCMortalityReadShocks(unittest.TestCase): + """ + Tests that mortality is working adequately when shocks are read + """ + + def setUp(self): + # Make a dictionary containing all parameters needed to solve the model + self.base_params = copy(init_lifecycle) + + agent_count = 10 + t_sim = 2000 + + self.base_params.update( + { + "AgentCount": agent_count, + "T_sim": t_sim, + "track_vars": ["t_age", "t_cycle"], + } + ) + + def test_compare_t_age_t_cycle(self): + + # Make agent, shock and initial condition histories + agent = IndShockConsumerType(**self.base_params) + agent.make_shock_history() + + # Solve and simulate the agent + agent.solve() + agent.initialize_sim() + agent.simulate() + + hist = copy(agent.history) + for key, array in hist.items(): + hist[key] = array.flatten(order="F") + + # Check that t_age is always t_cycle + # Except possibly in cases where the agent reach t_age = T_age. In this case, + # t_cycle is set to 0 at the end of the period, and the agent dies, + # But t_age is reset only at the start of next period and thus we can have + # t_age = T_age and t_cycle = 0 + self.assertTrue( + np.all( + np.logical_or( + hist["t_age"] == hist["t_cycle"], + np.logical_and( + hist["t_cycle"] == 0, hist["t_age"] == agent.T_cycle + ), + ) + ) + ) + + def test_compare_t_age_t_cycle_premature_death(self): + + # Re-do the previous test in an instance where we prematurely + # kill agents + par = copy(self.base_params) + par["T_age"] = par["T_age"] - 8 + # Make agent, shock and initial condition histories + agent = IndShockConsumerType(**par) + agent.make_shock_history() + + # Solve and simulate the agent + agent.solve() + agent.initialize_sim() + agent.simulate() + + hist = copy(agent.history) + for key, array in hist.items(): + hist[key] = array.flatten(order="F") + + # Check that t_age is always t_cycle + # (the exception from before should not happen + # because we are killing agents before T_cycle) + self.assertTrue(np.all(hist["t_age"] == hist["t_cycle"])) + #%% Test Transition Matrix Methods From f377f51187e9d57060c44c2c5e7b2719060b5680 Mon Sep 17 00:00:00 2001 From: Mateo VG Date: Wed, 12 Oct 2022 21:59:56 -0400 Subject: [PATCH 4/5] Update incometools --- HARK/Calibration/Income/IncomeTools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HARK/Calibration/Income/IncomeTools.py b/HARK/Calibration/Income/IncomeTools.py index baab613c6..68c8e550f 100644 --- a/HARK/Calibration/Income/IncomeTools.py +++ b/HARK/Calibration/Income/IncomeTools.py @@ -52,7 +52,7 @@ def parse_time_params(age_birth, age_death): T_cycle = age_death - age_birth # T_age is the age at which the agents are killed with certainty in # simulations (at the end of the T_age-th period) - T_age = age_death - age_birth + 1 + T_age = age_death - age_birth return {"T_cycle": T_cycle, "T_age": T_age} From dbd87112183920870704bd89484cebd86658e4c9 Mon Sep 17 00:00:00 2001 From: Mateo VG Date: Wed, 19 Oct 2022 18:10:23 -0400 Subject: [PATCH 5/5] Correct typo --- HARK/Calibration/Income/IncomeTools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HARK/Calibration/Income/IncomeTools.py b/HARK/Calibration/Income/IncomeTools.py index 68c8e550f..39da332c3 100644 --- a/HARK/Calibration/Income/IncomeTools.py +++ b/HARK/Calibration/Income/IncomeTools.py @@ -31,7 +31,7 @@ def parse_time_params(age_birth, age_death): """ Converts simple statements of the age at which an agent is born and the - age at which he dies with certaintiy into the parameters that HARK needs + age at which he dies with certainty into the parameters that HARK needs for figuring out the timing of the model. Parameters