def LM_models_shock(Ivar): exogenous = [Ivar] settings = {'save' : False, 'use_saved' : True, 'Fvac_share' : False} settings['endo_destrNO'] = False settings['endo_destrO'] = False settings['vac2w_costs'] = 0.05 settings['Fvac_factor'] = 5 settings['destrO_share'] = 0.5 settings['SAM_model'] = 'Standard' ss_standard = ss_calib('Solve', settings) block_list=[laborMarket1, laborMarket2, firm_labor_standard, wage_func1, ProdFunc] unknowns = ['N', 'S', 'Tight', 'JV', 'JM', 'Y'] targets = [ 'N_res', 'S_res', 'free_entry', 'JV_res', 'JM_res', 'ProdFunc_Res'] Time = 300 G_jac_standard = jac.get_G(block_list, exogenous, unknowns, targets, Time, ss_standard, save=False) settings['SAM_model'] = 'Costly_vac_creation' settings['SAM_model_variant'] = 'simple' ss_costly = ss_calib('Solve', settings) block_list = [laborMarket1_costly_vac, laborMarket2, firm_labor_costly_vac, wage_func1, ProdFunc] unknowns = ['N', 'S', 'Tight', 'V', 'JV', 'JM', 'Y'] targets = [ 'N_res', 'S_res', 'Match_res', 'free_entry', 'JV_res', 'JM_res', 'ProdFunc_Res'] G_jac_costly_vac = jac.get_G(block_list, exogenous, unknowns, targets, Time, ss_costly, save=False) # FR if Ivar == 'destr': exogenous = ['destrO'] settings['SAM_model'] = 'Costly_vac_creation' settings['SAM_model_variant'] = 'FR' ss_costly_FR = ss_calib('Solve', settings) block_list = [laborMarket1_costly_vac_FR, laborMarket2, firm_labor_costly_vac_FR, wage_func1, ProdFunc, destr_rate] unknowns = ['N', 'S', 'Tight', 'V', 'JV', 'JM', 'Y'] targets = [ 'N_res', 'S_res', 'Match_res', 'free_entry', 'JV_res', 'JM_res', 'ProdFunc_Res'] G_jac_costly_vac_FR = jac.get_G(block_list, exogenous, unknowns, targets, Time, ss_costly_FR, save=False) G_jac_costly_vac_FR_destrNO = G_jac_costly_vac_FR if Ivar == 'destr': exogenous = ['destrNO'] G_jac_costly_vac_FR_destrNO = jac.get_G(block_list, exogenous, unknowns, targets, Time, ss_costly_FR, save=False) return ss_standard, ss_costly, ss_costly_FR, G_jac_standard, G_jac_costly_vac, G_jac_costly_vac_FR, G_jac_costly_vac_FR_destrNO
def test_one_block_scalars_only(ss, expected_A, expected_pi): # source block ------------------------------------------------------------ @simple def taylor(pi, phi, i): taylor_eq = phi * pi - i return taylor_eq # Collect data ------------------------------------------------------------ block_list = [taylor] unknowns = ['i'] targets = ['taylor_eq'] T = 300 # Verify A ---------------------------------------------------------------- A = jac.get_H_U(block_list, unknowns, targets, T, ss, asymptotic=True, save=True) assert_that(A).is_not_none().is_instance_of(np.ndarray) assert_that(A.shape).is_equal_to((1199, 1, 1)) assert_that(np.array_equal(A, expected_A)).is_true() # Verify Winding number --------------------------------------------------- wn = det.winding_criterion(A) assert_that(wn).is_zero() # Verify G ---------------------------------------------------------------- G = jac.get_G(block_list=block_list, exogenous=['pi'], unknowns=unknowns, targets=targets, T=300, ss=ss) assert_that(G).described_as("G").is_not_none().is_instance_of( dict).is_not_empty().contains_only("i") i = G["i"] assert_that(i).described_as("i").is_not_none().is_instance_of( dict).is_not_empty().contains_only("pi") pi = i["pi"] assert_that(pi).described_as("pi").is_not_none().is_instance_of(np.ndarray) assert_that(pi.shape).is_equal_to((300, 300)) assert_that(np.array_equal(pi, expected_pi)).is_true()
def New_LR_SS(ss, dZ, Ivar, Time, scenario, t_terminal, HH): @solved(unknowns=['K', 'I'], targets=['inv', 'K_res']) def firm_investment(K, alpha, rstar, delta, kappak, Q, mc, Y, I_s, I, r): rk_plus = alpha * mc(+1) * Y(+1) / K inv = 1 - (rk_plus + (1 - delta)) / (1 + r(+1)) K_res = K - ((1 - delta) * K(-1) + I) return inv, K_res # @solved(unknowns=['K', 'Q', 'I'], targets=['inv', 'val', 'K_res']) # def firm_investment(K, alpha, rstar, delta, kappak, Q, mc, Y, I_s, I, r): # rk_plus = alpha * mc(+1) * Y(+1) / K # inv = Q - (rk_plus + Q(+1) * (1-delta))/(1+r(+1)) # LHS = 1 + kappak/2 * (I/I(-1) -1)**2 + I/I(-1) * kappak * (I/I(-1) -1) # RHS = Q(+1) + kappak * (I(+1)/I -1) * (I(+1)/I)**2 # val = LHS - RHS # K_res = K - ((1 - delta) * K(-1) + I(-1)) # return inv, val, K_res @simple def firm_labor_standard(mc, Y, L, alpha, pMatch, vacK, destr, w, rstar, r, Z, JV, JM): MPL = (1 - alpha) * mc * Y / L free_entry = JV - 0 JV_res = JV - (-vacK + pMatch * JM) JM_res = JM - ((MPL - w) + JM(+1) * (1 - destr) / (1 + r(+1))) return free_entry, JV_res, JM_res @simple def ProdFunc(Y, Z, K, alpha, L): ProdFunc_Res = Y - Z * K(-1)**alpha * L**(1 - alpha) return ProdFunc_Res @solved(unknowns=['w'], targets=['w_res']) def wage_func(Tight, w, wss, Tightss, pi): eta = 0.01 w_res = np.log(w) - (np.log(wss) + eta * np.log(Tight / Tightss)) return w_res @simple def laborMarket1(q, N, destr, S, pMatch, Tight): N_res = N - ((1 - destr) * N(-1) + S * q) S_res = S - (1 - (1 - destr) * N(-1)) V = q * S / pMatch N_ = N(-1) return N_res, S_res, V, N_ @simple def laborMarket2(Tight, ma): q = Tight / ((1 + Tight**ma)**(1 / ma)) pMatch = q / Tight return q, pMatch @simple def MutFund(B, r, ra, div, p): MF_Div = (div + p) + B(-1) * (1 + r) MF_Div_res = 1 + ra - (MF_Div) return MF_Div_res, MF_Div @solved(unknowns=['p'], targets=['equity']) def arbitrage(div, p, r): equity = div(+1) + p(+1) - p * (1 + r(+1)) return equity @simple def dividend(Y, w, N, vacK, V, I, F_cost, T_firms): div = Y - w * N - vacK - I - F_cost - T_firms return div @simple def fiscal_rev(C, VAT, B, taxes, MF_Div, T_firms): G_rev = taxes + B + T_firms return G_rev @simple def fiscal_exp(b, r, G, B, N, lumpsum_T, uT, UINCAGG_count): G_exp = G + UINCAGG_count + (lumpsum_T + uT) + B(-1) * (1 + r) return G_exp @simple def B_res(G_rev, G_exp): B_res = G_rev - G_exp return B_res @simple def HHTransfer(uT): Tuni = uT return Tuni @simple def Asset_mkt_clearing(A_agg, B, p): Asset_mkt = B + p - A_agg return Asset_mkt @simple def Labor_mkt_clearing(N, L): Labor_mkt = L - N return Labor_mkt @solved(unknowns=['i', 'r'], targets=['i_res', 'fisher']) def monetary(rstar, pi, i, r, phi): phi = 1.3 i_res = i - (rstar + phi * pi) fisher = 1 + i(-1) - (1 + r) * (1 + pi) return i_res, fisher @simple def aggregate(CN, CS, AN, AS, N, ssN, TAXN, TAXS, UINCAGG, TINC, CTD, ATD): dN = N / ssN dU = (1 - N) / (1 - ssN) C_agg = CTD A_agg = ATD taxes = TINC UINCAGG_count = UINCAGG * dU return C_agg, A_agg, taxes, UINCAGG_count @simple def goods_mkt_clearing(Y, C_agg, C, I, G, psip, V, vacK, Isip, F_cost, A_DEBT, kappa): goods_mkt = Y - C_agg - I - G - vacK - F_cost + kappa * A_DEBT(-1) return goods_mkt LM = solved(block_list=[ laborMarket1, laborMarket2, firm_labor_standard, wage_func ], unknowns=['N', 'S', 'Tight', 'JV', 'JM'], targets=['N_res', 'S_res', 'free_entry', 'JV_res', 'JM_res']) Asset_block_B = solved( block_list=[fiscal_rev, fiscal_exp, B_res, HH, MutFund, aggregate], unknowns=['B', 'ra'], targets=['B_res', 'MF_Div_res']) Asset_block_only_T = solved(block_list=[ fiscal_rev, fiscal_exp, B_res, HH, MutFund, aggregate, HHTransfer ], unknowns=['uT', 'ra'], targets=['B_res', 'MF_Div_res']) prod_stuff = solved(block_list=[monetary, ProdFunc, firm_investment], unknowns=['Y'], targets=['ProdFunc_Res']) exogenous = [Ivar] unknowns = ['r'] targets = ['Asset_mkt'] # general equilibrium jacobians if scenario == 'T': block_list = [ Asset_block_only_T, Asset_mkt_clearing, dividend, arbitrage, goods_mkt_clearing ] if scenario == 'B': block_list = [ LM, Asset_block_B, prod_stuff, Asset_mkt_clearing, Labor_mkt_clearing, dividend, arbitrage, goods_mkt_clearing ] G_jac = jac.get_G(block_list, exogenous, unknowns, targets, Time, ss, save=True) dMat = FigUtils.Shock_mult(G_jac, dZ, Ivar) New_ss_temp = {} for i in dMat.keys(): if i in ss: New_ss_temp[i] = dMat[i][t_terminal] + ss[i] New_ss_temp[Ivar] = ss[Ivar] + dZ[t_terminal] New_ss = ss.copy() for key in New_ss_temp.keys(): New_ss[key] = New_ss_temp[key] del New_ss_temp fig = FigUtils.FigPlot_newss_asset_vars(ss, dMat, Ivar, t_terminal, dZ, scenario) #fig = FigUtils.FigPlot3x3(ss, dMat, Ivar, t_terminal, dZ) return New_ss, fig
def jac(self, ss, T, shock_list, output_list=None, save=False, use_saved=False): # H_U_factored caching could be helpful here too return jac.get_G(self.block_list, shock_list, self.unknowns, self.targets, T, ss, output_list, save=save, use_saved=use_saved)
def test_one_block_with_vectors(ss, expected_A, expected_i_1_pi, expected_i_2_pi): # source block ------------------------------------------------------------ @SimpleWithVectorArgs({"phi": 2, "i": 2}) def taylor(pi, phi, i): taylor_eq = phi * pi - i return taylor_eq # Collect data ------------------------------------------------------------ block_list = [taylor] unknowns = ["i_1", "i_2"] targets = ["taylor_eq_1", "taylor_eq_2"] T = 300 # Verify A ---------------------------------------------------------------- A = jac.get_H_U(block_list, unknowns, targets, T, ss, asymptotic=True, save=True) assert_that(A).is_not_none().is_instance_of(np.ndarray) assert_that(A.shape).is_equal_to((1199, 2, 2)) assert_that(np.array_equal(A, expected_A)).is_true() # Verify Winding number --------------------------------------------------- wn = det.winding_criterion(A) assert_that(wn).is_zero() # Verify G ---------------------------------------------------------------- G = jac.get_G(block_list=block_list, exogenous=['pi'], unknowns=unknowns, targets=targets, T=300, ss=ss) assert_that(G).described_as("G").is_not_none().is_instance_of( dict).is_not_empty().contains_only("i_1", "i_2") i_1 = G["i_1"] assert_that(i_1).described_as("i_1").is_not_none().is_instance_of( dict).is_not_empty().contains_only("pi") i_1_pi = i_1["pi"] assert_that(i_1_pi).described_as("i_1_pi").is_not_none().is_instance_of( np.ndarray) assert_that(i_1_pi.shape).is_equal_to((300, 300)) assert_that(np.array_equal(i_1_pi, expected_i_1_pi)).is_true() i_2 = G["i_2"] assert_that(i_2).described_as("i_2").is_not_none().is_instance_of( dict).is_not_empty().contains_only("pi") i_2_pi = i_2["pi"] assert_that(i_2_pi).described_as("i_2_pi").is_not_none().is_instance_of( np.ndarray) assert_that(i_2_pi.shape).is_equal_to((300, 300)) assert_that(np.array_equal(i_2_pi, expected_i_2_pi)).is_true()