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

shock_history instead of history namespace for read_shocks #812

Merged
merged 3 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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
Expand Down
12 changes: 11 additions & 1 deletion HARK/ConsumptionSaving/tests/test_ConsPrefShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand Down
13 changes: 7 additions & 6 deletions HARK/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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):
'''
Expand Down