diff --git a/Documentation/CHANGELOG.md b/Documentation/CHANGELOG.md index 0f5d14870..f3e7bc0f1 100644 --- a/Documentation/CHANGELOG.md +++ b/Documentation/CHANGELOG.md @@ -19,6 +19,7 @@ Release Data: TBD #### Minor Changes +* Use shock_history namespace for pre-evaluated shock history [#812](https://github.com/econ-ark/HARK/pull/812) * Fixes seed of PrefShkDstn on initialization and add tests for simulation output ### 0.10.7 diff --git a/HARK/ConsumptionSaving/tests/test_ConsPrefShockModel.py b/HARK/ConsumptionSaving/tests/test_ConsPrefShockModel.py index 6ddb3c24e..689f0d514 100644 --- a/HARK/ConsumptionSaving/tests/test_ConsPrefShockModel.py +++ b/HARK/ConsumptionSaving/tests/test_ConsPrefShockModel.py @@ -34,7 +34,7 @@ def test_solution(self): def test_simulation(self): self.agent.T_sim = 10 - self.agent.track_vars = ["cNrmNow"] + self.agent.track_vars = ["cNrmNow", "PrefShkNow"] self.agent.makeShockHistory() # This is optional self.agent.initializeSim() self.agent.simulate() @@ -44,6 +44,16 @@ def test_simulation(self): 0.7366020536567589 ) + self.assertEqual( + self.agent.shock_history['PrefShkNow'][0][5], + self.agent.history['PrefShkNow'][0][5] + ) + + self.assertEqual( + self.agent.history['PrefShkNow'][0][5], + 0.4909415933881665 + ) + class testKinkyPrefConsumerType(unittest.TestCase): def setUp(self): diff --git a/HARK/core.py b/HARK/core.py index eea1001f9..77ff1dea2 100644 --- a/HARK/core.py +++ b/HARK/core.py @@ -211,6 +211,7 @@ def __init__(self, solution_terminal=None, cycles=1, pseudo_terminal=True, self.track_vars = [] # NOQA self.poststate_vars = [] # NOQA self.read_shocks = False # NOQA + self.shock_history = {} self.history = {} self.assignParameters(**kwds) # NOQA self.resetRNG() # NOQA @@ -478,16 +479,16 @@ def makeShockHistory(self): # Make blank history arrays for each shock variable (and mortality) for var_name in self.shock_vars: - self.history[var_name] = np.zeros((self.T_sim, self.AgentCount)) + np.nan - self.history['who_dies'] = np.zeros((self.T_sim, self.AgentCount), dtype=bool) + self.shock_history[var_name] = np.zeros((self.T_sim, self.AgentCount)) + np.nan + self.shock_history['who_dies'] = np.zeros((self.T_sim, self.AgentCount), dtype=bool) # Make and store the history of shocks for each period for t in range(self.T_sim): self.getMortality() - self.history['who_dies'][t,:] = self.who_dies + self.shock_history['who_dies'][t,:] = self.who_dies self.getShocks() for var_name in self.shock_vars: - self.history[var_name][self.t_sim,:] = getattr(self, var_name) + self.shock_history[var_name][self.t_sim,:] = getattr(self, var_name) self.t_sim += 1 self.t_age = self.t_age + 1 # Age all consumers by one period self.t_cycle = self.t_cycle + 1 # Age all consumers within their cycle @@ -513,7 +514,7 @@ def getMortality(self): None ''' if self.read_shocks: - who_dies = self.history['who_dies'][self.t_sim,:] + who_dies = self.shock_history['who_dies'][self.t_sim,:] else: who_dies = self.simDeath() self.simBirth(who_dies) @@ -590,7 +591,7 @@ def readShocks(self): None ''' for var_name in self.shock_vars: - setattr(self, var_name, self.history[var_name][self.t_sim, :]) + setattr(self, var_name, self.shock_history[var_name][self.t_sim, :]) def getStates(self): '''