class testPersistentShockConsumerType(unittest.TestCase): def setUp(self): # "persistent idiosyncratic shocks" model PrstIncCorr = 0.98 # Serial correlation coefficient for persistent income persistent_shocks = copy(GenIncDictionary) persistent_shocks["PrstIncCorr"] = PrstIncCorr # "persistent idisyncratic shocks" consumer self.agent = PersistentShockConsumerType(cycles=1, **persistent_shocks) self.agent.solve() def test_solution(self): pLvlGrid = self.agent.pLvlGrid[0] self.assertAlmostEqual( self.agent.solution[0].cFunc(10, pLvlGrid[1]).tolist(), 5.6030075768585075) def test_simulation(self): self.agent.T_sim = 25 # why does ,"bLvlNow" not work here? self.agent.track_vars = ["aLvl", "mLvl", "cLvl", "pLvl"] self.agent.initialize_sim() self.agent.simulate() self.assertAlmostEqual(np.mean(self.agent.history["mLvl"]), 1.2043946738813716)
class testPersistentShockConsumerType(unittest.TestCase): def setUp(self): # "persistent idiosyncratic shocks" model PrstIncCorr = 0.98 # Serial correlation coefficient for persistent income persistent_shocks = copy(GenIncDictionary) persistent_shocks['PrstIncCorr'] = PrstIncCorr # "persistent idisyncratic shocks" consumer self.agent = PersistentShockConsumerType(cycles=1, **persistent_shocks) self.agent.solve() def test_solution(self): pLvlGrid = self.agent.pLvlGrid[0] self.assertAlmostEqual( self.agent.solution[0].cFunc(10, pLvlGrid[1]).tolist(), 5.6030075768585075) def test_simulation(self): self.agent.T_sim = 25 self.agent.track_vars = ['mLvlNow', 'cLvlNow', 'pLvlNow'] self.agent.initializeSim() self.agent.simulate() self.assertAlmostEqual(np.mean(self.agent.history['mLvlNow']), 1.2043701902476343)
class testPersistentShockConsumerType(unittest.TestCase): def setUp(self): # "persistent idiosyncratic shocks" model PrstIncCorr = 0.98 # Serial correlation coefficient for persistent income persistent_shocks = copy(GenIncDictionary) persistent_shocks['PrstIncCorr'] = PrstIncCorr # "persistent idisyncratic shocks" consumer self.agent = PersistentShockConsumerType(**persistent_shocks) self.agent.solve() def test_solution(self): pLvlGrid = self.agent.pLvlGrid[0] self.assertAlmostEqual( self.agent.solution[0].cFunc(10, pLvlGrid[1]).tolist(), 0.9141970598224893) def test_simulation(self): self.agent.T_sim = 25 self.agent.track_vars = ['mLvlNow', 'cLvlNow', 'pLvlNow'] self.agent.initializeSim() self.agent.simulate() self.assertAlmostEqual(np.mean(self.agent.mLvlNow_hist), 2.372861738538392)
# # %% {"code_folding": []} # Make a dictionary for the "persistent idiosyncratic shocks" model PrstIncCorr = 0.98 # Serial correlation coefficient for persistent income persistent_shocks = copy(GenIncDictionary) persistent_shocks['PrstIncCorr'] = PrstIncCorr # %% [markdown] # The `PersistentShockConsumerType` class solves the problem of a consumer facing idiosyncratic shocks to his persistent and transitory income, and for which the (log) persistent income follows an AR1 process rather than random walk. # %% # Make and solve an example of "persistent idisyncratic shocks" consumer PersistentExample = PersistentShockConsumerType(**persistent_shocks) PersistentExample.solve() # %% # Plot the consumption function at various levels of persistent income pLvl print( 'Consumption function by persistent income level pLvl for a consumer with AR1 coefficient of ' + str(PersistentExample.PrstIncCorr) + ':') pLvlGrid = PersistentExample.pLvlGrid[0] mLvlGrid = np.linspace(0, 20, 300) for p in pLvlGrid: M_temp = mLvlGrid + PersistentExample.solution[0].mLvlMin(p) C = PersistentExample.solution[0].cFunc(M_temp, p * np.ones_like(M_temp)) plt.plot(M_temp, C) plt.xlim(0., 20.) plt.xlabel('Market resource level mLvl') plt.ylabel('Consumption level cLvl')