Exemplo n.º 1
0
    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)
# Then, we set up all the grids, grabs the discrete shock distributions, and state grids in `prepare_to_calc_EndOfPrdvP`.

# %%
solver.prepare_to_calc_EndOfPrdvP()

# %% [markdown]
# Then we calculate the marginal utility of next period's resources given the stochastic environment and current grids.

# %%
EndOfPrdvP = solver.calc_EndOfPrdvP()

# %% [markdown]
# Then, we essentially just have to construct the (resource, consumption) pairs by completing the EGM step, and constructing the interpolants by using the knowledge that the limiting solutions are those of the perfect foresight model. This is done with `make_basic_solution` as discussed above.

# %%
solution = solver.make_basic_solution(EndOfPrdvP, solver.aNrmNow,
                                      solver.make_linear_cFunc)

# %% [markdown]
# Lastly, we add the MPC and human wealth quantities we calculated in the method that prepared the solution of this period.

# %%
solver.add_MPC_and_human_wealth(solution)

# %% [markdown]
# All that is left is to verify that the solution in `solution` is identical to `LifecycleExample.solution[0]`. We can plot the against each other:

# %%
plot_funcs([LifecycleExample.solution[0].cFunc, solution.cFunc],
           LifecycleExample.solution[0].mNrmMin, 10)

# %% [markdown]