Ejemplo 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)
# %% [markdown]
# Let us then create a solver for the first period.

# %%
from HARK.ConsumptionSaving.ConsIndShockModel import ConsIndShockSolverBasic
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()

# %% [markdown]
# Many important values are now calculated and stored in solver, such as the effective discount factor, the smallest permanent income shock, and more.

# %%
solver.DiscFacEff

# %%
solver.PermShkMinNext

# %% [markdown]
# These values were calculated in `setAndUpdateValues`. In `defBoroCnst` that was also called, several things were calculated, for example the consumption function defined by the borrowing constraint.

# %%
plot_funcs([solver.cFuncNowCnst], solver.mNrmMinNow, 10)