Example #1
0
    def setUp(self):
        """
        Prepare to compare the models by initializing and solving them
        """
        # Set up and solve infinite type
        import HARK.ConsumptionSaving.ConsumerParameters as Params

        InfiniteType = IndShockConsumerType(**Params.init_idiosyncratic_shocks)
        InfiniteType.assignParameters(
            LivPrb=[1.],
            DiscFac=0.955,
            PermGroFac=[1.],
            PermShkStd=[0.],
            TempShkStd=[0.],
            T_total=1,
            T_retire=0,
            BoroCnstArt=None,
            UnempPrb=0.,
            cycles=0)  # This is what makes the type infinite horizon

        InfiniteType.updateIncomeProcess()
        InfiniteType.solve()
        InfiniteType.timeFwd()
        InfiniteType.unpackcFunc()

        # Make and solve a perfect foresight consumer type with the same parameters
        PerfectForesightType = deepcopy(InfiniteType)
        PerfectForesightType.solveOnePeriod = solvePerfForesight

        PerfectForesightType.solve()
        PerfectForesightType.unpackcFunc()
        PerfectForesightType.timeFwd()

        self.InfiniteType = InfiniteType
        self.PerfectForesightType = PerfectForesightType
Example #2
0
    def setUp(self):
        """
        Prepare to compare the models by initializing and solving them
        """
        # Set up and solve infinite type
        import HARK.ConsumptionSaving.ConsumerParameters as Params

        # Define a test dictionary that should have the same solution in the
        # perfect foresight and idiosyncratic shocks models.
        test_dictionary = deepcopy(Params.init_idiosyncratic_shocks)
        test_dictionary['LivPrb'] = [1.]
        test_dictionary['DiscFac'] = 0.955
        test_dictionary['PermGroFac'] = [1.]
        test_dictionary['PermShkStd'] = [0.]
        test_dictionary['TranShkStd'] = [0.]
        test_dictionary['UnempPrb'] = 0.
        test_dictionary['T_cycle'] = 1
        test_dictionary['T_retire'] = 0
        test_dictionary['BoroCnstArt'] = None

        InfiniteType = IndShockConsumerType(**test_dictionary)
        InfiniteType.cycles = 0

        InfiniteType.updateIncomeProcess()
        InfiniteType.solve()
        InfiniteType.timeFwd()
        InfiniteType.unpackcFunc()

        # Make and solve a perfect foresight consumer type with the same parameters
        PerfectForesightType = PerfForesightConsumerType(**test_dictionary)
        PerfectForesightType.cycles = 0

        PerfectForesightType.solve()
        PerfectForesightType.unpackcFunc()
        PerfectForesightType.timeFwd()

        self.InfiniteType = InfiniteType
        self.PerfectForesightType = PerfectForesightType
    'T_cycle': 1  # Number of periods in the cycle for this agent type
}

# In[ ]:

# Create the baseline instance by passing the dictionary to the class.
BaselineExample = IndShockConsumerType(**baseline_bufferstock_dictionary)
BaselineExample.cycles = 100  # Make this type have an finite horizon (Set T = 100)

start_time = clock()
BaselineExample.solve()
end_time = clock()
print('Solving a baseline buffer stock saving model (100 periods) took ' +
      mystr(end_time - start_time) + ' seconds.')
BaselineExample.unpackcFunc()
BaselineExample.timeFwd()

# In[ ]:

# Now we start plotting the different periods' consumption rules.

m1 = np.linspace(0, 9.5, 1000)  # Set the plot range of m
m2 = np.linspace(0, 6.5, 500)
c_m = BaselineExample.cFunc[0](
    m1
)  # c_m can be used to define the limiting infinite-horizon consumption rule here
c_t1 = BaselineExample.cFunc[-2](
    m1)  # c_t1 defines the second-to-last period consumption rule
c_t5 = BaselineExample.cFunc[-6](
    m1)  # c_t5 defines the T-5 period consumption rule
c_t10 = BaselineExample.cFunc[-11](
    PFexample.initializeSim()
    PFexample.simulate()

# %%
# Make and solve an example consumer with idiosyncratic income shocks
IndShockExample = IndShockConsumerType()
IndShockExample.cycles = 0  # Make this type have an infinite horizon

# %%
start_time = time()
IndShockExample.solve()
end_time = time()
print("Solving a consumer with idiosyncratic shocks took " +
      mystr(end_time - start_time) + " seconds.")
IndShockExample.unpackcFunc()
IndShockExample.timeFwd()

# %%
# Plot the consumption function and MPC for the infinite horizon consumer
print("Concave consumption function:")
plotFuncs(IndShockExample.cFunc[0], IndShockExample.solution[0].mNrmMin, 5)
print("Marginal consumption function:")
plotFuncsDer(IndShockExample.cFunc[0], IndShockExample.solution[0].mNrmMin, 5)

# %%
# Compare the consumption functions for the perfect foresight and idiosyncratic
# shock types.  Risky income cFunc asymptotically approaches perfect foresight cFunc.
print("Consumption functions for perfect foresight vs idiosyncratic shocks:")
plotFuncs(
    [PFexample.cFunc[0], IndShockExample.cFunc[0]],
    IndShockExample.solution[0].mNrmMin,
Example #5
0
# ## Counterclockwise Concavification
#

# + {"code_folding": [0]}
# This figure illustrates how both risks and constraints are examples of counterclockwise concavifications.
# It plots three lines: the linear consumption function of a perfect foresight consumer, the kinked consumption
# function of a consumer who faces a constraint, and the curved consumption function of a consumer that faces risk.

# load the three agents: unconstrained perfect foresight, constrained perfect foresight, unconstrained with risk

CCC_unconstr = IndShockConsumerType(**init_lifecycle)
CCC_unconstr.delFromTimeInv('BoroCnstArt')
CCC_unconstr.addToTimeVary('BoroCnstArt')
CCC_unconstr.solve()
CCC_unconstr.unpackcFunc()
CCC_unconstr.timeFwd()

CCC_constraint = IndShockConsumerType(**init_lifecycle)
CCC_constraint.delFromTimeInv('BoroCnstArt')
CCC_constraint.addToTimeVary('BoroCnstArt')
CCC_constraint(
    BoroCnstArt=[None, -1, None, None, None, None, None, None, None, None])
CCC_constraint.solve()
CCC_constraint.unpackcFunc()
CCC_constraint.timeFwd()

CCC_risk = IndShockConsumerType(**init_lifecycle_risk1)
CCC_risk.delFromTimeInv('BoroCnstArt')
CCC_risk.addToTimeVary('BoroCnstArt')
CCC_risk.solve()
CCC_risk.unpackcFunc()