Пример #1
0
    def test_ConsIndShockSolverBasic(self):
        LifecycleExample = IndShockConsumerType(**init_lifecycle)
        LifecycleExample.cycles = 1
        LifecycleExample.solve()

        solver = ConsIndShockSolverBasic(
            LifecycleExample.solution[1], LifecycleExample.IncomeDstn[0],
            LifecycleExample.LivPrb[0], LifecycleExample.DiscFac,
            LifecycleExample.CRRA, LifecycleExample.Rfree,
            LifecycleExample.PermGroFac[0], LifecycleExample.BoroCnstArt,
            LifecycleExample.aXtraGrid, LifecycleExample.vFuncBool,
            LifecycleExample.CubicBool)

        solver.prepareToSolve()

        self.assertAlmostEqual(solver.DiscFacEff, 0.9503999999999999)
        self.assertAlmostEqual(solver.PermShkMinNext, 0.850430160026919)
        self.assertAlmostEqual(solver.cFuncNowCnst(4).tolist(), 4.0)
        self.assertAlmostEqual(solver.prepareToCalcEndOfPrdvP()[0],
                               -0.2491750859108316)
        self.assertAlmostEqual(solver.prepareToCalcEndOfPrdvP()[-1],
                               19.74982491408914)

        EndOfPrdvP = solver.calcEndOfPrdvP()

        self.assertAlmostEqual(EndOfPrdvP[0], 6622.251864311334)
        self.assertAlmostEqual(EndOfPrdvP[-1], 0.026301061207747087)

        solution = solver.makeBasicSolution(EndOfPrdvP, solver.aNrmNow,
                                            solver.makeLinearcFunc)
        solver.addMPCandHumanWealth(solution)

        self.assertAlmostEqual(solution.cFunc(4).tolist(), 1.7391265696400773)
Пример #2
0
# Then we calculate the marginal utility of next period's resources given the stochastic environment and current grids.

# %%
EndOfPrdvP = solver.calcEndOfPrdvP()

# %% [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 `makeBasicSolution` as discussed above.

# %%
solution = solver.makeBasicSolution(EndOfPrdvP,solver.aNrmNow,solver.makeLinearcFunc)

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

# %%
solver.addMPCandHumanWealth(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:

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

# %% [markdown]
# Although, it's probably even clearer if we just subtract the function values from each other at some grid.

# %%
eval_grid = np.linspace(0, 20, 200)
LifecycleExample.solution[0].cFunc(eval_grid) - solution.cFunc(eval_grid)

# %%