Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug-fix] Mortality and t_cycle when shocks are read #1181

Merged
merged 6 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion HARK/Calibration/Income/IncomeTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Mv77 marked this conversation as resolved.
Show resolved Hide resolved
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