Example #1
0
    def main_test(self):

        Markov_vFuncBool_example = MarkovConsumerType(**Markov_Dict)

        TranShkDstn_e = MeanOneLogNormal(
            Markov_vFuncBool_example.TranShkStd[0],
            123).approx(Markov_vFuncBool_example.TranShkCount)
        TranShkDstn_u = DiscreteDistribution(np.ones(1), np.ones(1) * .2)
        PermShkDstn = MeanOneLogNormal(
            Markov_vFuncBool_example.PermShkStd[0],
            123).approx(Markov_vFuncBool_example.PermShkCount)

        #employed Income shock distribution
        employed_IncShkDstn = combine_indep_dstns(PermShkDstn, TranShkDstn_e)

        #unemployed Income shock distribution
        unemployed_IncShkDstn = combine_indep_dstns(PermShkDstn, TranShkDstn_u)

        # Specify list of IncShkDstns for each state
        Markov_vFuncBool_example.IncShkDstn = [[
            employed_IncShkDstn, unemployed_IncShkDstn
        ]]

        #solve the consumer's problem
        Markov_vFuncBool_example.solve()

        self.assertAlmostEqual(
            Markov_vFuncBool_example.solution[0].vFunc[1](0.4),
            -4.127935542867632)
Example #2
0
    def setUp(self):

        # Define the Markov transition matrix for serially correlated unemployment
        unemp_length = 5  # Averange length of unemployment spell
        urate_good = 0.05  # Unemployment rate when economy is in good state
        urate_bad = 0.12  # Unemployment rate when economy is in bad state
        bust_prob = 0.01  # Probability of economy switching from good to bad
        recession_length = 20  # Averange length of bad state
        p_reemploy = 1.0 / unemp_length
        p_unemploy_good = p_reemploy * urate_good / (1 - urate_good)
        p_unemploy_bad = p_reemploy * urate_bad / (1 - urate_bad)
        boom_prob = 1.0 / recession_length
        MrkvArray = np.array([
            [
                (1 - p_unemploy_good) * (1 - bust_prob),
                p_unemploy_good * (1 - bust_prob),
                (1 - p_unemploy_good) * bust_prob,
                p_unemploy_good * bust_prob,
            ],
            [
                p_reemploy * (1 - bust_prob),
                (1 - p_reemploy) * (1 - bust_prob),
                p_reemploy * bust_prob,
                (1 - p_reemploy) * bust_prob,
            ],
            [
                (1 - p_unemploy_bad) * boom_prob,
                p_unemploy_bad * boom_prob,
                (1 - p_unemploy_bad) * (1 - boom_prob),
                p_unemploy_bad * (1 - boom_prob),
            ],
            [
                p_reemploy * boom_prob,
                (1 - p_reemploy) * boom_prob,
                p_reemploy * (1 - boom_prob),
                (1 - p_reemploy) * (1 - boom_prob),
            ],
        ])

        init_serial_unemployment = copy(init_idiosyncratic_shocks)
        init_serial_unemployment["MrkvArray"] = [MrkvArray]
        init_serial_unemployment[
            "UnempPrb"] = 0.0  # to make income distribution when employed
        init_serial_unemployment["global_markov"] = False
        self.model = MarkovConsumerType(**init_serial_unemployment)
        self.model.cycles = 0
        self.model.vFuncBool = False  # for easy toggling here

        # Replace the default (lognormal) income distribution with a custom one
        employed_income_dist = DiscreteDistribution(
            np.ones(1), np.array([[1.0], [1.0]]))  # Definitely get income
        unemployed_income_dist = DiscreteDistribution(
            np.ones(1), np.array([[1.0], [0.0]]))  # Definitely don't
        self.model.IncShkDstn = [[
            employed_income_dist,
            unemployed_income_dist,
            employed_income_dist,
            unemployed_income_dist,
        ]]
Example #3
0
 def initializeSim(self):
     MarkovConsumerType.initializeSim(self)
     if hasattr(self,'T_advance'):
         self.restoreState()
         self.MrkvArray = self.MrkvArray_sim
     elif not hasattr(self,'mortality_off'):
         self.calcAgeDistribution()
         self.initializeAges()
     if (hasattr(self,'Mrkv_univ') and self.Mrkv_univ is not None):
         self.MrkvNow[:] = self.Mrkv_univ
Example #4
0
 def getMarkovStates(self):
     '''
     A modified method that forces all agents to be in a particular Markov
     state when the attribute Mrkv_univ is not None.  This allows us to draw
     income shocks for every Markov state for each agent in each simulated
     period when pre-specifying the shocks.  When the model is *actually*
     simulated, this ensures that agent i in period t and Markov state k will
     get the same income shocks *no matter which specification we use*.
     '''
     MarkovConsumerType.getMarkovStates(self)  # Basic Markov state draw
     if (hasattr(self, 'Mrkv_univ') and self.Mrkv_univ is not None):
         self.MrkvNow_temp = self.MrkvNow
         self.MrkvNow = self.Mrkv_univ * np.ones(self.AgentCount, dtype=int)
Example #5
0
    def test_MarkovConsumerType(self):
        try:
            unemp_length = 5 # Averange length of unemployment spell
            urate_good = 0.05        # Unemployment rate when economy is in good state
            urate_bad = 0.12         # Unemployment rate when economy is in bad state
            bust_prob = 0.01         # Probability of economy switching from good to bad
            recession_length = 20    # Averange length of bad state
            p_reemploy =1.0/unemp_length
            p_unemploy_good = p_reemploy*urate_good/(1-urate_good)
            p_unemploy_bad = p_reemploy*urate_bad/(1-urate_bad)
            boom_prob = 1.0/recession_length
            MrkvArray = np.array([[(1-p_unemploy_good)*(1-bust_prob),p_unemploy_good*(1-bust_prob),
                                   (1-p_unemploy_good)*bust_prob,p_unemploy_good*bust_prob],
                                  [p_reemploy*(1-bust_prob),(1-p_reemploy)*(1-bust_prob),
                                   p_reemploy*bust_prob,(1-p_reemploy)*bust_prob],
                                  [(1-p_unemploy_bad)*boom_prob,p_unemploy_bad*boom_prob,
                                   (1-p_unemploy_bad)*(1-boom_prob),p_unemploy_bad*(1-boom_prob)],
                                  [p_reemploy*boom_prob,(1-p_reemploy)*boom_prob,
                                   p_reemploy*(1-boom_prob),(1-p_reemploy)*(1-boom_prob)]])

            # Make a consumer with serially correlated unemployment, subject to boom and bust cycles
            init_serial_unemployment = copy(init_idiosyncratic_shocks)
            init_serial_unemployment['MrkvArray'] = [MrkvArray]
            init_serial_unemployment['UnempPrb'] = 0 # to make income distribution when employed
            init_serial_unemployment['global_markov'] = False
            SerialUnemploymentExample = MarkovConsumerType(**init_serial_unemployment)
        except:
            self.fail("MarkovConsumerType failed to initialize with boom/bust unemployment.")
Example #6
0
 def getStates(self):
     MarkovConsumerType.getStates(self)
     if hasattr(self,'T_advance'): # This means we're in the policy experiment
         self.noticeStimulus()
         self.makeWeights()
     if hasattr(self,'ContUnempBenefits'):
         if self.ContUnempBenefits==True:
             self.continueUnemploymentBenefits()
     
     # Store indicators of whether this agent is a worker and unemployed
     w = self.t_cycle <= self.T_retire
     u = np.logical_and(np.mod(self.MrkvNow,3) > 0, w)
     lLvl = self.pLvlNow*self.TranShkNow
     lLvl[u] = 0.
     lLvl[self.t_cycle > self.T_retire] = 0.
     self.lLvlNow = lLvl
     self.uNow = u
     self.wNow = w
Example #7
0
    def setUp(self):

        # Define the Markov transition matrix for serially correlated unemployment
        unemp_length = 5  # Averange length of unemployment spell
        urate_good = 0.05  # Unemployment rate when economy is in good state
        urate_bad = 0.12  # Unemployment rate when economy is in bad state
        bust_prob = 0.01  # Probability of economy switching from good to bad
        recession_length = 20  # Averange length of bad state
        p_reemploy = 1.0 / unemp_length
        p_unemploy_good = p_reemploy * urate_good / (1 - urate_good)
        p_unemploy_bad = p_reemploy * urate_bad / (1 - urate_bad)
        boom_prob = 1.0 / recession_length
        self.MrkvArray = np.array(
            [
                [
                    (1 - p_unemploy_good) * (1 - bust_prob),
                    p_unemploy_good * (1 - bust_prob),
                    (1 - p_unemploy_good) * bust_prob,
                    p_unemploy_good * bust_prob,
                ],
                [
                    p_reemploy * (1 - bust_prob),
                    (1 - p_reemploy) * (1 - bust_prob),
                    p_reemploy * bust_prob,
                    (1 - p_reemploy) * bust_prob,
                ],
                [
                    (1 - p_unemploy_bad) * boom_prob,
                    p_unemploy_bad * boom_prob,
                    (1 - p_unemploy_bad) * (1 - boom_prob),
                    p_unemploy_bad * (1 - boom_prob),
                ],
                [
                    p_reemploy * boom_prob,
                    (1 - p_reemploy) * boom_prob,
                    p_reemploy * (1 - boom_prob),
                    (1 - p_reemploy) * (1 - boom_prob),
                ],
            ]
        )

        init_serial_unemployment = copy(Params.init_idiosyncratic_shocks)
        init_serial_unemployment["MrkvArray"] = [self.MrkvArray]       
        self.model = MarkovConsumerType(**init_serial_unemployment) 
Example #8
0
class test_ConsMarkovSolver(unittest.TestCase):
    def setUp(self):

        # Define the Markov transition matrix for serially correlated unemployment
        unemp_length = 5  # Averange length of unemployment spell
        urate_good = 0.05  # Unemployment rate when economy is in good state
        urate_bad = 0.12  # Unemployment rate when economy is in bad state
        bust_prob = 0.01  # Probability of economy switching from good to bad
        recession_length = 20  # Averange length of bad state
        p_reemploy = 1.0 / unemp_length
        p_unemploy_good = p_reemploy * urate_good / (1 - urate_good)
        p_unemploy_bad = p_reemploy * urate_bad / (1 - urate_bad)
        boom_prob = 1.0 / recession_length
        MrkvArray = np.array([
            [
                (1 - p_unemploy_good) * (1 - bust_prob),
                p_unemploy_good * (1 - bust_prob),
                (1 - p_unemploy_good) * bust_prob,
                p_unemploy_good * bust_prob,
            ],
            [
                p_reemploy * (1 - bust_prob),
                (1 - p_reemploy) * (1 - bust_prob),
                p_reemploy * bust_prob,
                (1 - p_reemploy) * bust_prob,
            ],
            [
                (1 - p_unemploy_bad) * boom_prob,
                p_unemploy_bad * boom_prob,
                (1 - p_unemploy_bad) * (1 - boom_prob),
                p_unemploy_bad * (1 - boom_prob),
            ],
            [
                p_reemploy * boom_prob,
                (1 - p_reemploy) * boom_prob,
                p_reemploy * (1 - boom_prob),
                (1 - p_reemploy) * (1 - boom_prob),
            ],
        ])

        init_serial_unemployment = copy(init_idiosyncratic_shocks)
        init_serial_unemployment["MrkvArray"] = [MrkvArray]
        init_serial_unemployment[
            "UnempPrb"] = 0.0  # to make income distribution when employed
        init_serial_unemployment["global_markov"] = False
        self.model = MarkovConsumerType(**init_serial_unemployment)
        self.model.cycles = 0
        self.model.vFuncBool = False  # for easy toggling here

        # Replace the default (lognormal) income distribution with a custom one
        employed_income_dist = DiscreteDistribution(
            np.ones(1), np.array([[1.0], [1.0]]))  # Definitely get income
        unemployed_income_dist = DiscreteDistribution(
            np.ones(1), np.array([[1.0], [0.0]]))  # Definitely don't
        self.model.IncShkDstn = [[
            employed_income_dist,
            unemployed_income_dist,
            employed_income_dist,
            unemployed_income_dist,
        ]]

    def test_check_markov_inputs(self):
        # check Rfree
        self.assertRaises(ValueError, self.model.check_markov_inputs)
        # fix Rfree
        self.model.Rfree = np.array(4 * [self.model.Rfree])
        # check MrkvArray, first mess up the setup
        self.MrkvArray = self.model.MrkvArray
        self.model.MrkvArray = np.random.rand(3, 3)
        self.assertRaises(ValueError, self.model.check_markov_inputs)
        # then fix it back
        self.model.MrkvArray = self.MrkvArray
        # check LivPrb
        self.assertRaises(ValueError, self.model.check_markov_inputs)
        # fix LivPrb
        self.model.LivPrb = [np.array(4 * self.model.LivPrb)]
        # check PermGroFac
        self.assertRaises(ValueError, self.model.check_markov_inputs)
        # fix PermGroFac
        self.model.PermGroFac = [np.array(4 * self.model.PermGroFac)]

    def test_solve(self):
        self.model.Rfree = np.array(4 * [self.model.Rfree])
        self.model.LivPrb = [np.array(4 * self.model.LivPrb)]
        self.model.PermGroFac = [np.array(4 * self.model.PermGroFac)]
        self.model.solve()

    def test_simulation(self):
        self.model.Rfree = np.array(4 * [self.model.Rfree])
        self.model.LivPrb = [np.array(4 * self.model.LivPrb)]
        self.model.PermGroFac = [np.array(4 * self.model.PermGroFac)]
        self.model.solve()
        self.model.T_sim = 120
        self.model.MrkvPrbsInit = [0.25, 0.25, 0.25, 0.25]
        self.model.track_vars = ["mNrm", 'cNrm']
        self.model.make_shock_history()  # This is optional
        self.model.initialize_sim()
        self.model.simulate()
Example #9
0
 def simDeath(self):
     if hasattr(self,'mortality_off'):
         return np.zeros(self.AgentCount, dtype=bool)
     else:
         return MarkovConsumerType.simDeath(self)
Example #10
0
# %% [markdown]
# One other parameter needs to change: the number of agents in simulation.  We want to increase this, because later on when we vastly increase the variance of the permanent income shock, things get wonky.  (We need to change this value here, before we have used the parameters to initialize the $\texttt{MarkovConsumerType}$, because this parameter is used during initialization.)
#
# Other parameters that are not used during initialization can also be assigned here, by changing the appropriate value in the $\texttt{init_China_parameters_dictionary}$; however, they can also be changed later, by altering the appropriate attribute of the initialized $\texttt{MarkovConsumerType}$.

# %%
init_China_parameters['AgentCount']   = 10000

# %% [markdown]
# ### Import and initialize the Agents
#
# Here, we bring in an agent making a consumption/savings decision every period, subject to transitory and permanent income shocks, AND a Markov shock

# %%
from HARK.ConsumptionSaving.ConsMarkovModel import MarkovConsumerType
ChinaExample = MarkovConsumerType(**init_China_parameters)

# %% [markdown]
# Currently, Markov states can differ in their interest factor, permanent growth factor, survival probability, and income distribution.  Each of these needs to be specifically set.
#
# Do that here, except shock distribution, which will be done later (because we want to examine the consequences of different shock distributions).

# %%
GrowthFastAnn = 1.06 # Six percent annual growth 
GrowthSlowAnn = 1.00 # Stagnation
ChinaExample.assignParameters(PermGroFac = [np.array([GrowthSlow.,GrowthFast ** (.25)])], #needs to be a list, with 0th element of shape of shape (StateCount,)
                              Rfree      =  np.array(StateCount*[init_China_parameters['Rfree']]), #needs to be an array, of shape (StateCount,)
                              LivPrb     = [np.array(StateCount*[init_China_parameters['LivPrb']][0])], #needs to be a list, with 0th element of shape of shape (StateCount,)
                              cycles     = 0)

ChinaExample.track_vars = ['aNrmNow','cNrmNow','pLvlNow'] # Names of variables to be tracked
Example #11
0
 def preSolve(self):
     self.MrkvArray = self.MrkvArray_pcvd
     MarkovConsumerType.preSolve(self)
     self.updateSolutionTerminal()
Example #12
0
 def reset_rng(self):
     MarkovConsumerType.reset_rng(self)
Example #13
0
    def setUp(self):
        # Set up and solve TBS
        base_primitives = {'UnempPrb': .015,
                           'DiscFac': 0.9,
                           'Rfree': 1.1,
                           'PermGroFac': 1.05,
                           'CRRA': .95}
        TBSType = TractableConsumerType(**base_primitives)
        TBSType.solve()

        # Set up and solve Markov
        MrkvArray = [np.array([[1.0-base_primitives['UnempPrb'], base_primitives['UnempPrb']],[0.0, 1.0]])]
        Markov_primitives = {"CRRA": base_primitives['CRRA'],
                             "Rfree": np.array(2*[base_primitives['Rfree']]),
                             "PermGroFac": [np.array(2*[base_primitives['PermGroFac'] /
                                            (1.0-base_primitives['UnempPrb'])])],
                             "BoroCnstArt": None,
                             "PermShkStd": [0.0],
                             "PermShkCount": 1,
                             "TranShkStd": [0.0],
                             "TranShkCount": 1,
                             "T_total": 1,
                             "UnempPrb": 0.0,
                             "UnempPrbRet": 0.0,
                             "T_retire": 0,
                             "IncUnemp": 0.0,
                             "IncUnempRet": 0.0,
                             "aXtraMin": 0.001,
                             "aXtraMax": TBSType.mUpperBnd,
                             "aXtraCount": 48,
                             "aXtraExtra": [None],
                             "aXtraNestFac": 3,
                             "LivPrb":[np.array([1.0,1.0]),],
                             "DiscFac": base_primitives['DiscFac'],
                             'Nagents': 1,
                             'psi_seed': 0,
                             'xi_seed': 0,
                             'unemp_seed': 0,
                             'tax_rate': 0.0,
                             'vFuncBool': False,
                             'CubicBool': True,
                             'MrkvArray': MrkvArray,
                             'T_cycle':1
                             }

        MarkovType = MarkovConsumerType(**Markov_primitives)
        MarkovType.cycles = 0
        employed_income_dist = DiscreteDistribution(np.ones(1),
                                                    [np.ones(1),
                                                     np.ones(1)])
        unemployed_income_dist = DiscreteDistribution(np.ones(1),
                                                      [np.ones(1),
                                                       np.zeros(1)])
        MarkovType.IncomeDstn = [[employed_income_dist,
                                  unemployed_income_dist]]

        MarkovType.solve()
        MarkovType.unpackcFunc()

        self.TBSType = TBSType
        self.MarkovType = MarkovType
Example #14
0
 def __init__(self,cycles=1,time_flow=True,**kwds):
     MarkovConsumerType.__init__(self,cycles=1,time_flow=True,**kwds)
     self.solveOnePeriod = solveConsMarkovALT
Example #15
0
def main():
    # Import the HARK library.  The assumption is that this code is in a folder
    # contained in the HARK folder.  Also import the ConsumptionSavingModel
    import numpy as np  # numeric Python
    from HARK.utilities import plotFuncs  # basic plotting tools
    from HARK.ConsumptionSaving.ConsMarkovModel import MarkovConsumerType  # An alternative, much longer way to solve the TBS model
    from time import clock  # timing utility

    do_simulation = True

    # Define the model primitives
    base_primitives = {
        'UnempPrb': .00625,  # Probability of becoming unemployed
        'DiscFac': 0.975,  # Intertemporal discount factor
        'Rfree': 1.01,  # Risk-free interest factor on assets
        'PermGroFac': 1.0025,  # Permanent income growth factor (uncompensated)
        'CRRA': 1.0
    }  # Coefficient of relative risk aversion

    # Define a dictionary to be used in case of simulation
    simulation_values = {
        'aLvlInitMean': 0.0,  # Mean of log initial assets for new agents
        'aLvlInitStd': 1.0,  # Stdev of log initial assets for new agents
        'AgentCount': 10000,  # Number of agents to simulate
        'T_sim': 120,  # Number of periods to simulate
        'T_cycle': 1
    }  # Number of periods in the cycle

    # Make and solve a tractable consumer type
    ExampleType = TractableConsumerType(**base_primitives)
    t_start = clock()
    ExampleType.solve()
    t_end = clock()
    print('Solving a tractable consumption-savings model took ' +
          str(t_end - t_start) + ' seconds.')

    # Plot the consumption function and whatnot
    m_upper = 1.5 * ExampleType.mTarg
    conFunc_PF = lambda m: ExampleType.h * ExampleType.PFMPC + ExampleType.PFMPC * m
    #plotFuncs([ExampleType.solution[0].cFunc,ExampleType.mSSfunc,ExampleType.cSSfunc],0,m_upper)
    plotFuncs([ExampleType.solution[0].cFunc, ExampleType.solution[0].cFunc_U],
              0, m_upper)

    if do_simulation:
        ExampleType(**
                    simulation_values)  # Set attributes needed for simulation
        ExampleType.track_vars = ['mLvlNow']
        ExampleType.makeShockHistory()
        ExampleType.initializeSim()
        ExampleType.simulate()

    # Now solve the same model using backward induction rather than the analytic method of TBS.
    # The TBS model is equivalent to a Markov model with two states, one of them absorbing (permanent unemployment).
    MrkvArray = np.array(
        [[1.0 - base_primitives['UnempPrb'], base_primitives['UnempPrb']],
         [0.0,
          1.0]])  # Define the two state, absorbing unemployment Markov array
    init_consumer_objects = {
        "CRRA":
        base_primitives['CRRA'],
        "Rfree":
        np.array(2 * [base_primitives['Rfree']
                      ]),  # Interest factor (same in both states)
        "PermGroFac": [
            np.array(2 * [
                base_primitives['PermGroFac'] /
                (1.0 - base_primitives['UnempPrb'])
            ])
        ],  # Unemployment-compensated permanent growth factor
        "BoroCnstArt":
        None,  # Artificial borrowing constraint
        "PermShkStd": [0.0],  # Permanent shock standard deviation
        "PermShkCount":
        1,  # Number of shocks in discrete permanent shock distribution
        "TranShkStd": [0.0],  # Transitory shock standard deviation
        "TranShkCount":
        1,  # Number of shocks in discrete permanent shock distribution
        "T_cycle":
        1,  # Number of periods in cycle
        "UnempPrb":
        0.0,  # Unemployment probability (not used, as the unemployment here is *permanent*, not transitory)
        "UnempPrbRet":
        0.0,  # Unemployment probability when retired (irrelevant here)
        "T_retire":
        0,  # Age at retirement (turned off)
        "IncUnemp":
        0.0,  # Income when unemployed (irrelevant)
        "IncUnempRet":
        0.0,  # Income when unemployed and retired (irrelevant)
        "aXtraMin":
        0.001,  # Minimum value of assets above minimum in grid
        "aXtraMax":
        ExampleType.mUpperBnd,  # Maximum value of assets above minimum in grid
        "aXtraCount":
        48,  # Number of points in assets grid
        "aXtraExtra": [None],  # Additional points to include in assets grid
        "aXtraNestFac":
        3,  # Degree of exponential nesting when constructing assets grid
        "LivPrb": [np.array([1.0, 1.0])],  # Survival probability
        "DiscFac":
        base_primitives['DiscFac'],  # Intertemporal discount factor
        'AgentCount':
        1,  # Number of agents in a simulation (irrelevant)
        'tax_rate':
        0.0,  # Tax rate on labor income (irrelevant)
        'vFuncBool':
        False,  # Whether to calculate the value function
        'CubicBool':
        True,  # Whether to use cubic splines (False --> linear splines)
        'MrkvArray': [MrkvArray]  # State transition probabilities
    }
    MarkovType = MarkovConsumerType(
        **init_consumer_objects)  # Make a basic consumer type
    employed_income_dist = [np.ones(1), np.ones(1),
                            np.ones(1)]  # Income distribution when employed
    unemployed_income_dist = [
        np.ones(1), np.ones(1), np.zeros(1)
    ]  # Income distribution when permanently unemployed
    MarkovType.IncomeDstn = [[employed_income_dist, unemployed_income_dist]
                             ]  # set the income distribution in each state
    MarkovType.cycles = 0

    # Solve the "Markov TBS" model
    t_start = clock()
    MarkovType.solve()
    t_end = clock()
    MarkovType.unpackcFunc()

    print('Solving the same model "the long way" took ' +
          str(t_end - t_start) + ' seconds.')
    #plotFuncs([ExampleType.solution[0].cFunc,ExampleType.solution[0].cFunc_U],0,m_upper)
    plotFuncs(MarkovType.cFunc[0], 0, m_upper)
    diffFunc = lambda m: ExampleType.solution[0].cFunc(m) - MarkovType.cFunc[
        0][0](m)
    print('Difference between the (employed) consumption functions:')
    plotFuncs(diffFunc, 0, m_upper)
Example #16
0
 def resetRNG(self):
     MarkovConsumerType.resetRNG(self)
Example #17
0
# %% [markdown]
# One other parameter needs to change: the number of agents in simulation.  We want to increase this, because later on when we vastly increase the variance of the permanent income shock, things get wonky.  (We need to change this value here, before we have used the parameters to initialize the $\texttt{MarkovConsumerType}$, because this parameter is used during initialization.)
#
# Other parameters that are not used during initialization can also be assigned here, by changing the appropriate value in the $\texttt{init_China_parameters_dictionary}$; however, they can also be changed later, by altering the appropriate attribute of the initialized $\texttt{MarkovConsumerType}$.

# %%
init_China_parameters['AgentCount']   = 10000

# %% [markdown]
# ### Import and initialize the Agents
#
# Here, we bring in an agent making a consumption/savings decision every period, subject to transitory and permanent income shocks, AND a Markov shock

# %%
from HARK.ConsumptionSaving.ConsMarkovModel import MarkovConsumerType
ChinaExample = MarkovConsumerType(**init_China_parameters)

# %% [markdown]
# Currently, Markov states can differ in their interest factor, permanent growth factor, survival probability, and income distribution.  Each of these needs to be specifically set.
#
# Do that here, except shock distribution, which will be done later (because we want to examine the consequences of different shock distributions).

# %%
GrowthFastAnn = 1.06 # Six percent annual growth 
GrowthSlowAnn = 1.00 # Stagnation
ChinaExample.assignParameters(PermGroFac = [np.array([GrowthSlowAnn,GrowthFastAnn ** (.25)])], #needs to be a list, with 0th element of shape of shape (StateCount,)
                              Rfree      =  np.array(StateCount*[init_China_parameters['Rfree']]), #needs to be an array, of shape (StateCount,)
                              LivPrb     = [np.array(StateCount*[init_China_parameters['LivPrb']][0])], #needs to be a list, with 0th element of shape of shape (StateCount,)
                              cycles     = 0)

ChinaExample.track_vars = ['aNrmNow','cNrmNow','pLvlNow'] # Names of variables to be tracked
Example #18
0
# 1. Model with serially correlated unemployment
# 2. Model with period of "unemployment immunity"
# 3. Model with serially correlated permanent income growth
# 4. Model with serially correlated interest factor
#
# ### 1. Serial Unemployment 
#
# Let's create a consumer similar to the one in "idiosyncratic shock" model but who faces serially correlated unemployment during boom or bust cycles of the economy.

# %%
# Make a consumer with serially correlated unemployment, subject to boom and bust cycles
init_serial_unemployment = copy(init_idiosyncratic_shocks)
init_serial_unemployment["MrkvArray"] = [MrkvArray]
init_serial_unemployment["UnempPrb"] = 0  # to make income distribution when employed
init_serial_unemployment["global_markov"] = False
SerialUnemploymentExample = MarkovConsumerType(**init_serial_unemployment)
SerialUnemploymentExample.cycles = 0
SerialUnemploymentExample.vFuncBool = False  # for easy toggling here

# %%
# Replace the default (lognormal) income distribution with a custom one
employed_income_dist = DiscreteDistribution(np.ones(1), [np.ones(1), np.ones(1)])  # Definitely get income
unemployed_income_dist = DiscreteDistribution(np.ones(1), [np.ones(1), np.zeros(1)]) # Definitely don't
SerialUnemploymentExample.IncomeDstn = [
    [
        employed_income_dist,
        unemployed_income_dist,
        employed_income_dist,
        unemployed_income_dist,
    ]
]
    "aXtraNestFac":
    3,  # Degree of exponential nesting when constructing assets grid
    "LivPrb": [np.array([1.0, 1.0])],  # Survival probability
    "DiscFac":
    base_primitives["DiscFac"],  # Intertemporal discount factor
    "AgentCount":
    1,  # Number of agents in a simulation (irrelevant)
    "tax_rate":
    0.0,  # Tax rate on labor income (irrelevant)
    "vFuncBool":
    False,  # Whether to calculate the value function
    "CubicBool":
    True,  # Whether to use cubic splines (False --> linear splines)
    "MrkvArray": [MrkvArray],  # State transition probabilities
}
MarkovType = MarkovConsumerType(
    **init_consumer_objects)  # Make a basic consumer type
employed_income_dist = [
    np.ones(1),
    np.ones(1),
    np.ones(1),
]  # Income distribution when employed
unemployed_income_dist = [
    np.ones(1),
    np.ones(1),
    np.zeros(1),
]  # Income distribution when permanently unemployed
MarkovType.IncomeDstn = [[employed_income_dist, unemployed_income_dist]
                         ]  # set the income distribution in each state
MarkovType.cycles = 0

# %%
Example #20
0
    def setUp(self):
        # Set up and solve TBS
        base_primitives = {
            "UnempPrb": 0.015,
            "DiscFac": 0.9,
            "Rfree": 1.1,
            "PermGroFac": 1.05,
            "CRRA": 0.95,
        }
        TBSType = TractableConsumerType(**base_primitives)
        TBSType.solve()

        # Set up and solve Markov
        MrkvArray = [
            np.array([
                [
                    1.0 - base_primitives["UnempPrb"],
                    base_primitives["UnempPrb"]
                ],
                [0.0, 1.0],
            ])
        ]
        Markov_primitives = {
            "CRRA":
            base_primitives["CRRA"],
            "Rfree":
            np.array(2 * [base_primitives["Rfree"]]),
            "PermGroFac": [
                np.array(2 * [
                    base_primitives["PermGroFac"] /
                    (1.0 - base_primitives["UnempPrb"])
                ])
            ],
            "BoroCnstArt":
            None,
            "PermShkStd": [0.0],
            "PermShkCount":
            1,
            "TranShkStd": [0.0],
            "TranShkCount":
            1,
            "T_total":
            1,
            "UnempPrb":
            0.0,
            "UnempPrbRet":
            0.0,
            "T_retire":
            0,
            "IncUnemp":
            0.0,
            "IncUnempRet":
            0.0,
            "aXtraMin":
            0.001,
            "aXtraMax":
            TBSType.mUpperBnd,
            "aXtraCount":
            48,
            "aXtraExtra": [None],
            "aXtraNestFac":
            3,
            "LivPrb": [
                np.array([1.0, 1.0]),
            ],
            "DiscFac":
            base_primitives["DiscFac"],
            "Nagents":
            1,
            "psi_seed":
            0,
            "xi_seed":
            0,
            "unemp_seed":
            0,
            "tax_rate":
            0.0,
            "vFuncBool":
            False,
            "CubicBool":
            True,
            "MrkvArray":
            MrkvArray,
            "T_cycle":
            1,
        }

        MarkovType = MarkovConsumerType(**Markov_primitives)
        MarkovType.cycles = 0
        employed_income_dist = DiscreteDistribution(np.ones(1),
                                                    np.array([[1.0], [1.0]]))
        unemployed_income_dist = DiscreteDistribution(np.ones(1),
                                                      np.array([[1.0], [0.0]]))
        MarkovType.IncShkDstn = [[
            employed_income_dist, unemployed_income_dist
        ]]

        MarkovType.solve()
        MarkovType.unpack("cFunc")

        self.TBSType = TBSType
        self.MarkovType = MarkovType
Example #21
0
 def getShocks(self):
     MarkovConsumerType.getShocks(self)
     if (hasattr(self, 'Mrkv_univ') and self.Mrkv_univ is not None):
         self.MrkvNow = self.MrkvNow_temp  # Make sure real sequence is recorded