Skip to content

Commit

Permalink
Merge pull request #1181 from Mv77/plumbing/mkshks_bugfix
Browse files Browse the repository at this point in the history
[Bug-fix] Mortality and `t_cycle` when shocks are read
  • Loading branch information
Mv77 committed Oct 19, 2022
2 parents 73f6521 + c635830 commit 76d0e48
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions Documentation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions HARK/Calibration/Income/IncomeTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}

Expand Down
75 changes: 75 additions & 0 deletions HARK/ConsumptionSaving/tests/test_IndShockConsumerType.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
1 change: 1 addition & 0 deletions HARK/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 76d0e48

Please sign in to comment.