def test_SS_solver(baseline, param_updates, filename, dask_client): # Test SS.SS_solver function. Provide inputs to function and # ensure that output returned matches what it has been before. p = Specifications(baseline=baseline, client=dask_client, num_workers=NUM_WORKERS) p.update_specifications(param_updates) p.output_base = CUR_PATH p.get_tax_function_parameters(None, run_micro=False) b_guess = np.ones((p.S, p.J)) * 0.07 n_guess = np.ones((p.S, p.J)) * .35 * p.ltilde if p.zeta_K[-1] == 1.0: rguess = p.world_int_rate[-1] else: rguess = 0.06483431412921253 TRguess = 0.05738932081035772 factorguess = 139355.1547340256 BQguess = aggregates.get_BQ(rguess, b_guess, None, p, 'SS', False) Yguess = 0.6376591201150815 test_dict = SS.SS_solver(b_guess, n_guess, rguess, BQguess, TRguess, factorguess, Yguess, p, None, False) expected_dict = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', filename)) for k, v in expected_dict.items(): print('Testing ', k) assert (np.allclose(test_dict[k], v, atol=1e-07, equal_nan=True))
def find_moments(p, client): b_guess = np.ones((p.S, p.J)) * 0.07 n_guess = np.ones((p.S, p.J)) * .4 * p.ltilde rguess = 0.08961277823002804 # 0.09 T_Hguess = 0.12 factorguess = 12.73047710050195 # 7.7 #70000 # Modified BQguess = aggr.get_BQ(rguess, b_guess, None, p, 'SS', False) exit_early = [0, -1] # 2nd value gives number of valid labor moments to consider before exiting SS_fsolve # Put -1 to run to SS ss_params_baseline = (b_guess, n_guess, None, None, p, client, exit_early) guesses = [rguess] + list(BQguess) + [T_Hguess, factorguess] [solutions_fsolve, infodict, ier, message] =\ opt.fsolve(SS.SS_fsolve, guesses, args=ss_params_baseline, xtol=p.mindist_SS, full_output=True) rss = solutions_fsolve[0] BQss = solutions_fsolve[1:-2] T_Hss = solutions_fsolve[-2] factor_ss = solutions_fsolve[-1] Yss = T_Hss/p.alpha_T[-1] fsolve_flag = True try: output = SS.SS_solver(b_guess, n_guess, rss, BQss, T_Hss, factor_ss, Yss, p, client, fsolve_flag) except: print('RuntimeError: Steady state aggregate resource constraint not satisfied') print('Luckily we caught the error, so minstat_init_calibrate will continue') return 1e10 model_moments = np.array(output['nssmat'].mean(axis=1)[:45]) # calc_moments(output, p.omega_SS, p.lambdas, p.S, p.J) return model_moments
def minstat_init_calibrate(params, *args): a0, a1, a2, a3, a4 = params p, client, data_moments, W, ages = args chi_n = np.ones(p.S) chi_n[:p.S // 2 + 5] = chebyshev_func(ages, a0, a1, a2, a3, a4) slope = chi_n[p.S // 2 + 5 - 1] - chi_n[p.S // 2 + 5 - 2] chi_n[p.S // 2 + 5 - 1:] = (np.linspace(65, 100, 36) - 65) * slope + chi_n[p.S // 2 + 5 - 1] chi_n[chi_n < 0.5] = 0.5 p.chi_n = chi_n print("-----------------------------------------------------") print('PARAMS AT START' + str(params)) print("-----------------------------------------------------") b_guess = np.ones((p.S, p.J)) * 0.07 n_guess = np.ones((p.S, p.J)) * .4 * p.ltilde rguess = 0.09 T_Hguess = 0.12 factorguess = 7.7 #70000 # Modified BQguess = aggr.get_BQ(rguess, b_guess, None, p, 'SS', False) exit_early = [ 0, 2 ] # 2nd value gives number of valid labor moments to consider before exiting SS_fsolve ss_params_baseline = (b_guess, n_guess, None, None, p, client, exit_early) guesses = [rguess] + list(BQguess) + [T_Hguess, factorguess] [solutions_fsolve, infodict, ier, message] =\ opt.fsolve(SS.SS_fsolve, guesses, args=ss_params_baseline, xtol=p.mindist_SS, full_output=True) rss = solutions_fsolve[0] BQss = solutions_fsolve[1:-2] T_Hss = solutions_fsolve[-2] factor_ss = solutions_fsolve[-1] Yss = T_Hss / p.alpha_T[-1] fsolve_flag = True try: output = SS.SS_solver(b_guess, n_guess, rss, BQss, T_Hss, factor_ss, Yss, p, client, fsolve_flag) except: print( 'RuntimeError: Steady state aggregate resource constraint not satisfied' ) print( 'Luckily we caught the error, so minstat_init_calibrate will continue' ) return 1e10 model_moments = calc_moments(output, p.omega_SS, p.lambdas, p.S, p.J) print('Model moments:', model_moments) print("-----------------------------------------------------") distance = np.dot( np.dot((np.array(model_moments[:9]) - np.array(data_moments)).T, W), np.array(model_moments[:9]) - np.array(data_moments)) print('DATA and MODEL DISTANCE: ', distance) return distance
def test_SS_solver(): # Test SS.SS_solver function. Provide inputs to function and # ensure that output returned matches what it has been before. input_tuple = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data/SS_solver_inputs.pkl')) (b_guess_init, n_guess_init, rss, T_Hss, factor_ss, Yss, params, baseline, fsolve_flag, baseline_spending) = input_tuple (bssmat, nssmat, chi_params, ss_params, income_tax_params, iterative_params, small_open_params) = params income_tax_params = ('DEP', ) + income_tax_params params = (bssmat, nssmat, chi_params, ss_params, income_tax_params, iterative_params, small_open_params) test_dict = SS.SS_solver(b_guess_init, n_guess_init, rss, T_Hss, factor_ss, Yss, params, baseline, fsolve_flag, baseline_spending) expected_dict = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data/SS_solver_outputs.pkl')) for k, v in expected_dict.items(): assert (np.allclose(test_dict[k], v))
def test_SS_solver(): # Test SS.SS_solver function. Provide inputs to function and # ensure that output returned matches what it has been before. input_tuple = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data/SS_solver_inputs.pkl')) (b_guess_init, n_guess_init, rss, T_Hss, factor_ss, Yss, params, baseline, fsolve_flag, baseline_spending) = input_tuple (bssmat, nssmat, chi_params, ss_params, income_tax_params, iterative_params, small_open_params) = params p = Specifications() (p.J, p.S, p.T, p.BW, p.beta, p.sigma, p.alpha, p.gamma, p.epsilon, Z, p.delta, p.ltilde, p.nu, p.g_y, p.g_n_ss, tau_payroll, tau_bq, p.rho, p.omega_SS, p.budget_balance, alpha_T, p.debt_ratio_ss, tau_b, delta_tau, lambdas, imm_rates, p.e, retire, p.mean_income_data, h_wealth, p_wealth, m_wealth, p.b_ellipse, p.upsilon) = ss_params p.Z = np.ones(p.T + p.S) * Z p.tau_bq = np.ones(p.T + p.S) * 0.0 p.tau_payroll = np.ones(p.T + p.S) * tau_payroll p.alpha_T = np.ones(p.T + p.S) * alpha_T p.tau_b = np.ones(p.T + p.S) * tau_b p.delta_tau = np.ones(p.T + p.S) * delta_tau p.h_wealth = np.ones(p.T + p.S) * h_wealth p.p_wealth = np.ones(p.T + p.S) * p_wealth p.m_wealth = np.ones(p.T + p.S) * m_wealth p.retire = (np.ones(p.T + p.S) * retire).astype(int) p.lambdas = lambdas.reshape(p.J, 1) p.imm_rates = imm_rates.reshape(1, p.S) p.tax_func_type = 'DEP' p.baseline = baseline p.baseline_spending = baseline_spending p.analytical_mtrs, etr_params, mtrx_params, mtry_params =\ income_tax_params p.etr_params = np.transpose(etr_params.reshape( p.S, 1, etr_params.shape[-1]), (1, 0, 2)) p.mtrx_params = np.transpose(mtrx_params.reshape( p.S, 1, mtrx_params.shape[-1]), (1, 0, 2)) p.mtry_params = np.transpose(mtry_params.reshape( p.S, 1, mtry_params.shape[-1]), (1, 0, 2)) p.maxiter, p.mindist_SS = iterative_params p.chi_b, p.chi_n = chi_params p.small_open, firm_r, hh_r = small_open_params p.firm_r = np.ones(p.T + p.S) * firm_r p.hh_r = np.ones(p.T + p.S) * hh_r p.num_workers = 1 expected_dict = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data/SS_solver_outputs.pkl')) BQss = expected_dict['BQss'] test_dict = SS.SS_solver(b_guess_init, n_guess_init, rss, BQss, T_Hss, factor_ss, Yss, p, None, fsolve_flag) # delete values key-value pairs that are not in both dicts del expected_dict['bssmat'], expected_dict['chi_n'], expected_dict['chi_b'] del expected_dict['Iss_total'] del test_dict['etr_ss'], test_dict['mtrx_ss'], test_dict['mtry_ss'] test_dict['IITpayroll_revenue'] = (test_dict['total_revenue_ss'] - test_dict['business_revenue']) del test_dict['T_Pss'], test_dict['T_BQss'], test_dict['T_Wss'] del test_dict['K_d_ss'], test_dict['K_f_ss'], test_dict['D_d_ss'] del test_dict['D_f_ss'], test_dict['I_d_ss'] del test_dict['debt_service_f'], test_dict['new_borrowing_f'] del test_dict['bqssmat'], test_dict['T_Css'], test_dict['Iss_total'] test_dict['revenue_ss'] = test_dict.pop('total_revenue_ss') for k, v in expected_dict.items(): print('Testing ', k) assert(np.allclose(test_dict[k], v))