class testTractableBufferStock(unittest.TestCase): def setUp(self): # Create portfolio choice consumer type self.tct = TractableConsumerType() # Solve the model under the given parameters self.tct.solve() def test_simulation(self): self.tct.T_sim = 30 # Number of periods to simulate self.tct.AgentCount = 10 # Number of agents to simulate self.tct.aLvlInitMean = 0.0 # Mean of log initial assets for new agents self.tct.aLvlInitStd = 1.0 # stdev of log initial assets for new agents self.tct.T_cycle = 1 self.tct.track_vars += ["mLvl"] self.tct.initialize_sim() self.tct.simulate() self.assertAlmostEqual(self.tct.history["mLvl"][15][0], 5.820630251772332)
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
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
# %% # 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 = process_time() ExampleType.solve() t_end = process_time() 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
cE = (1 - R**(-1)) * mE + R**(-1) return cE def cEDelEqZero( R, k, Pi, mE): # Function that gives us locus of where change in cE is zero cE = (R * k * mE * Pi) / (1 + R * k * Pi) return cE # %% {"code_folding": []} # Define and solve the model MyTBStype = TractableConsumerType(**TBS_dictionary) MyTBStype.solve() UnempPrb = MyTBStype.UnempPrb DiscFac = MyTBStype.DiscFac R = MyTBStype.Rfree PermGroFacCmp = MyTBStype.PermGroFacCmp k = MyTBStype.PFMPC CRRA = MyTBStype.CRRA Rnrm = MyTBStype.Rnrm Pi = PiFunc(R, DiscFac, CRRA, UnempPrb, PermGroFacCmp) # Define the range of mE to plot over mE_range = np.linspace(0, 8, 1000) cE = MyTBStype.solution[0].cFunc(mE_range) # %% {"code_folding": []}