def get_shocks(self): """ Draw idiosyncratic income shocks, just as for IndShockConsumerType, then draw a single common value for the risky asset return. Also draws whether each agent is able to adjust their portfolio this period. Parameters ---------- None Returns ------- None """ IndShockConsumerType.get_shocks(self) self.get_Risky() self.get_Adjust()
def get_shocks(self): """ Gets permanent and transitory income shocks for this period as well as preference shocks. Parameters ---------- None Returns ------- None """ IndShockConsumerType.get_shocks( self) # Get permanent and transitory income shocks PrefShkNow = np.zeros(self.AgentCount) # Initialize shock array for t in range(self.T_cycle): these = t == self.t_cycle N = np.sum(these) if N > 0: PrefShkNow[these] = self.PrefShkDstn[t].draw(N) self.shocks["PrefShk"] = PrefShkNow
class testIndShockConsumerType(unittest.TestCase): def setUp(self): self.agent = IndShockConsumerType(AgentCount=2, T_sim=10) self.agent.solve() def test_get_shocks(self): self.agent.initialize_sim() self.agent.sim_birth(np.array([True, False])) self.agent.sim_one_period() self.agent.sim_birth(np.array([False, True])) self.agent.get_shocks() self.assertEqual(self.agent.shocks['PermShk'][0], 1.0427376294215103) self.assertAlmostEqual(self.agent.shocks['PermShk'][1], 0.9278094171517413) self.assertAlmostEqual(self.agent.shocks['TranShk'][0], 0.881761797501595) def test_ConsIndShockSolverBasic(self): LifecycleExample = IndShockConsumerType(**init_lifecycle) LifecycleExample.cycles = 1 LifecycleExample.solve() # test the solution_terminal self.assertAlmostEqual(LifecycleExample.solution[-1].cFunc(2).tolist(), 2) self.assertAlmostEqual(LifecycleExample.solution[9].cFunc(1), 0.79429538) self.assertAlmostEqual(LifecycleExample.solution[8].cFunc(1), 0.79391692) self.assertAlmostEqual(LifecycleExample.solution[7].cFunc(1), 0.79253095) self.assertAlmostEqual(LifecycleExample.solution[0].cFunc(1).tolist(), 0.7506184692092213) self.assertAlmostEqual(LifecycleExample.solution[1].cFunc(1).tolist(), 0.7586358637239385) self.assertAlmostEqual(LifecycleExample.solution[2].cFunc(1).tolist(), 0.7681247572911291) solver = ConsIndShockSolverBasic( LifecycleExample.solution[1], LifecycleExample.IncShkDstn[0], LifecycleExample.LivPrb[0], LifecycleExample.DiscFac, LifecycleExample.CRRA, LifecycleExample.Rfree, LifecycleExample.PermGroFac[0], LifecycleExample.BoroCnstArt, LifecycleExample.aXtraGrid, LifecycleExample.vFuncBool, LifecycleExample.CubicBool, ) solver.prepare_to_solve() self.assertAlmostEqual(solver.DiscFacEff, 0.9586233599999999) self.assertAlmostEqual(solver.PermShkMinNext, 0.6554858756904397) self.assertAlmostEqual(solver.cFuncNowCnst(4).tolist(), 4.0) self.assertAlmostEqual(solver.prepare_to_calc_EndOfPrdvP()[0], -0.19792871012285213) self.assertAlmostEqual(solver.prepare_to_calc_EndOfPrdvP()[-1], 19.801071289877118) EndOfPrdvP = solver.calc_EndOfPrdvP() self.assertAlmostEqual(EndOfPrdvP[0], 6657.839372100613) self.assertAlmostEqual(EndOfPrdvP[-1], 0.2606075215645896) solution = solver.make_basic_solution(EndOfPrdvP, solver.aNrmNow, solver.make_linear_cFunc) solver.add_MPC_and_human_wealth(solution) self.assertAlmostEqual(solution.cFunc(4).tolist(), 1.0028005137373956) def test_simulated_values(self): self.agent.initialize_sim() self.agent.simulate() self.assertAlmostEqual(self.agent.MPCnow[1], 0.5711503906043797) self.assertAlmostEqual(self.agent.state_now['aLvl'][1], 0.18438326264597635)