Repository: econ-ark/HARK

Issue:
track_vars cannot track PrefShkNow
**Describe the bug**
Adding `PrefShkNow` to `track_vars` results in broken history records.

**To Reproduce**

Run this code:
https://gist.github.com/sbenthall/e398fd3c389a9cc094974c397e49b160

See this output:

```
python pref_test.py 
0.6904681186891202 0.69046812
/home/sb/projects/econ-ark/HARK/HARK/interpolation.py:1721: RuntimeWarning: All-NaN slice encountered
  y = self.compare(fx,axis=1)
[[nan nan nan ... nan nan nan]
 [nan nan nan ... nan nan nan]
 [nan nan nan ... nan nan nan]
 ...
 [nan nan nan ... nan nan nan]
 [nan nan nan ... nan nan nan]
 [nan nan nan ... nan nan nan]]
[[nan nan nan ... nan nan nan]
 [nan nan nan ... nan nan nan]
 [nan nan nan ... nan nan nan]
 ...
 [nan nan nan ... nan nan nan]
 [nan nan nan ... nan nan nan]
 [nan nan nan ... nan nan nan]]
```

**Expected behavior**

When `PrefShkNow` is removed from `track_vars`, and the corresponding print statement is removed, the numerical values of `cNrmNow` are displayed.

```
$ python pref_test.py 
0.6904681186891202 0.69046812
[[1.31820519 1.13599726 1.19117047 ... 1.15580804 0.92238984 0.79139412]
 [1.75227515 0.87249487 1.03166147 ... 1.09467154 1.04217198 0.81947656]
 [1.6132044  1.21100077 1.10807752 ... 1.04517571 0.97166597 0.93927158]
 ...
 [1.41013941 0.98367178 0.99266247 ... 1.19269696 1.00552796 1.02108079]
 [1.19004436 1.32309231 1.52353897 ... 1.10650642 0.87607768 0.92796219]
 [1.07060879 0.94171512 1.06713576 ... 0.94867343 0.8665761  0.93075895]]
```

I expect a similar thing when adding `PrefShkNow` to be tracked.


Discussion:

Patch:
diff --git a/Documentation/CHANGELOG.md b/Documentation/CHANGELOG.md
index 0f5d1487..f3e7bc0f 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/core.py b/HARK/core.py
index eea1001f..77ff1dea 100644
--- a/HARK/core.py
+++ b/HARK/core.py
@@ -211,6 +211,7 @@ class AgentType(HARKobject):
         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 @@ class AgentType(HARKobject):
 
         # 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 @@ class AgentType(HARKobject):
         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 @@ class AgentType(HARKobject):
         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):
         '''


Test patch:
diff --git a/HARK/ConsumptionSaving/tests/test_ConsPrefShockModel.py b/HARK/ConsumptionSaving/tests/test_ConsPrefShockModel.py
index 6ddb3c24..689f0d51 100644
--- a/HARK/ConsumptionSaving/tests/test_ConsPrefShockModel.py
+++ b/HARK/ConsumptionSaving/tests/test_ConsPrefShockModel.py
@@ -34,7 +34,7 @@ class testPrefShockConsumerType(unittest.TestCase):
 
     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 @@ class testPrefShockConsumerType(unittest.TestCase):
             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):