def read_tax_func_estimate(self, pickle_path, pickle_file): ''' This function reads in tax function parameters from pickle files. Args: pickle_path (str): path to pickle with tax function parameter estimates pickle_file (str): name of pickle file with tax function parameter estimates Returns: dict_params (dict): dictionary containing arrays of tax function parameters ''' if os.path.exists(pickle_path): print('pickle path exists') dict_params = safe_read_pickle(pickle_path) else: path_in_egg = pickle_file pkl_path = os.path.join(os.path.dirname(__file__), 'tests', path_in_egg) dict_params = dict_params = safe_read_pickle(pkl_path) return dict_params
def test_inner_loop(): # Test SS.inner_loop 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', 'inner_loop_inputs.pkl')) (outer_loop_vars_in, params, baseline, baseline_spending) = input_tuple ss_params, income_tax_params, chi_params, small_open_params = params (bssmat, nssmat, r, Y, TR, factor) = outer_loop_vars_in 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.eta = (p.omega_SS.reshape(p.S, 1) * p.lambdas.reshape(1, p.J)).reshape(1, p.S, p.J) p.Z = np.ones(p.T + p.S) * Z p.zeta_D = np.zeros(p.T + p.S) 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.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 BQ = np.ones(p.J) * 0.00019646295986015257 outer_loop_vars = (bssmat, nssmat, r, BQ, Y, TR, factor) (euler_errors, new_bmat, new_nmat, new_r, new_r_gov, new_r_hh, new_w, new_TR, new_Y, new_factor, new_BQ, average_income_model) = SS.inner_loop(outer_loop_vars, p, None) test_tuple = (euler_errors, new_bmat, new_nmat, new_r, new_w, new_TR, new_Y, new_factor, new_BQ, average_income_model) expected_tuple = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'inner_loop_outputs.pkl')) for i, v in enumerate(expected_tuple): assert(np.allclose(test_tuple[i], v, atol=1e-05))
def test_inner_loop(): # Test TPI.inner_loop 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', 'tpi_inner_loop_inputs.pkl')) guesses, outer_loop_vars, params, j = input_tuple income_tax_params, tpi_params, initial_values, ind = params initial_values = initial_values tpi_params = tpi_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, tau_b, delta_tau, tau_payroll, tau_bq, p.rho, p.omega, N_tilde, lambdas, p.imm_rates, p.e, retire, p.mean_income_data, factor, h_wealth, p_wealth, m_wealth, p.b_ellipse, p.upsilon, p.chi_b, p.chi_n, theta, p.baseline) = tpi_params p.eta = p.omega.reshape(p.T + p.S, p.S, 1) * p.lambdas.reshape(1, p.J) p.Z = np.ones(p.T + p.S) * Z p.zeta_D = np.zeros(p.T + p.S) p.initial_foreign_debt_ratio = 0.0 p.initial_debt_ratio = 0.59 p.tau_bq = np.ones(p.T + p.S) * 0.0 p.tau_payroll = np.ones(p.T + p.S) * tau_payroll 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.tax_func_type = 'DEP' p.analytical_mtrs, etr_params, mtrx_params, mtry_params =\ income_tax_params p.etr_params = np.transpose(etr_params, (1, 0, 2))[:p.T, :, :] p.mtrx_params = np.transpose(mtrx_params, (1, 0, 2))[:p.T, :, :] p.mtry_params = np.transpose(mtry_params, (1, 0, 2))[:p.T, :, :] p.lambdas = lambdas.reshape(p.J, 1) p.num_workers = 1 (K0, b_sinit, b_splus1init, factor, initial_b, initial_n, p.omega_S_preTP, initial_debt, D0) = initial_values initial_values_in = (K0, b_sinit, b_splus1init, factor, initial_b, initial_n, D0) (r, K, BQ, TR) = outer_loop_vars wss = firm.get_w_from_r(r[-1], p, 'SS') w = np.ones(p.T + p.S) * wss w[:p.T] = firm.get_w_from_r(r[:p.T], p, 'TPI') outer_loop_vars_in = (r, w, r, BQ, TR, theta) guesses = (guesses[0], guesses[1]) test_tuple = TPI.inner_loop(guesses, outer_loop_vars_in, initial_values_in, j, ind, p) expected_tuple = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'tpi_inner_loop_outputs.pkl')) for i, v in enumerate(expected_tuple): assert (np.allclose(test_tuple[i], v))
def tpi_output(request): ref_idx = request.param if ref_idx == "baseline": return (safe_read_pickle(REG_BASELINE + "/TPI/TPI_vars.pkl"), safe_read_pickle(BASELINE + "/TPI/TPI_vars.pkl")) else: return (safe_read_pickle( REG_REFORM.format(ref_idx=request.param) + "/TPI/TPI_vars.pkl"), safe_read_pickle( REFORM.format(ref_idx=request.param) + "/TPI/TPI_vars.pkl"))
def parameter_read(request): ref_idx = request.param if ref_idx == "baseline": return (safe_read_pickle(REG_BASELINE + "/model_params.pkl"), safe_read_pickle(BASELINE + "/model_params.pkl")) else: return (safe_read_pickle( REG_REFORM.format(ref_idx=request.param) + "/model_params.pkl"), safe_read_pickle( REFORM.format(ref_idx=request.param) + "/model_params.pkl"))
def txfunc_output(request): ref_idx = request.param if ref_idx == "baseline": reg_path = REG_BASELINE + "/TxFuncEst_{idx}.pkl".format(idx=ref_idx) path = BASELINE + "/TxFuncEst_{idx}.pkl".format(idx=ref_idx) return (safe_read_pickle(reg_path), safe_read_pickle(path)) else: reg_path = (REG_REFORM.format(ref_idx=ref_idx) + "/TxFuncEst_policy{idx}.pkl".format(idx=ref_idx)) path = (REFORM.format(ref_idx=ref_idx) + "/TxFuncEst_policy{idx}.pkl".format(idx=ref_idx)) return (safe_read_pickle(reg_path), safe_read_pickle(path))
def test_firstdoughnutring(): # Test TPI.firstdoughnutring 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', 'firstdoughnutring_inputs.pkl')) guesses, r, w, b, BQ, TR, j, params = input_tuple income_tax_params, tpi_params, initial_b = params tpi_params = tpi_params + [True] 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, tau_b, delta_tau, tau_payroll, tau_bq, p.rho, p.omega, N_tilde, lambdas, p.imm_rates, p.e, retire, p.mean_income_data, factor, h_wealth, p_wealth, m_wealth, p.b_ellipse, p.upsilon, p.chi_b, p.chi_n, theta, p.baseline) = tpi_params p.Z = np.ones(p.T + p.S) * Z p.zeta_D = np.zeros(p.T + p.S) p.initial_foreign_debt_ratio = 0.0 p.initial_debt_ratio = 0.59 p.tau_bq = np.ones(p.T + p.S) * 0.0 p.tau_payroll = np.ones(p.T + p.S) * tau_payroll 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.tax_func_type = 'DEP' p.analytical_mtrs, etr_params, mtrx_params, mtry_params =\ income_tax_params p.etr_params = np.transpose(etr_params, (1, 0, 2)) p.mtrx_params = np.transpose(mtrx_params, (1, 0, 2)) p.mtry_params = np.transpose(mtry_params, (1, 0, 2)) p.lambdas = lambdas.reshape(p.J, 1) p.num_workers = 1 bq = BQ / p.lambdas[j] tr = TR test_list = TPI.firstdoughnutring(guesses, r, w, bq, tr, theta, factor, j, initial_b, p) expected_list = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'firstdoughnutring_outputs.pkl')) assert (np.allclose(np.array(test_list), np.array(expected_list)))
def test_twist_doughnut(): # Test TPI.twist_doughnut 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', 'twist_doughnut_inputs.pkl')) guesses, r, w, BQ, TR, j, s, t, params = input_tuple income_tax_params, tpi_params, initial_b = params tpi_params = tpi_params + [True] 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, tau_b, delta_tau, tau_payroll, tau_bq, p.rho, p.omega, N_tilde, lambdas, p.imm_rates, p.e, retire, p.mean_income_data, factor, h_wealth, p_wealth, m_wealth, p.b_ellipse, p.upsilon, p.chi_b, p.chi_n, theta, p.baseline) = tpi_params p.Z = np.ones(p.T + p.S) * Z p.tau_bq = np.ones(p.T + p.S) * 0.0 p.tau_c = np.ones((p.T + p.S, p.S, p.J)) * 0.0 p.tau_payroll = np.ones(p.T + p.S) * tau_payroll 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.tax_func_type = 'DEP' p.analytical_mtrs, etr_params, mtrx_params, mtry_params =\ income_tax_params p.lambdas = lambdas.reshape(p.J, 1) p.num_workers = 1 length = int(len(guesses) / 2) tau_c_to_use = np.diag(p.tau_c[:p.S, :, j], p.S - (s + 2)) bq = BQ[t:t + length] / p.lambdas[j] tr = TR[t:t + length] test_list = TPI.twist_doughnut(guesses, r, w, bq, tr, theta, factor, j, s, t, tau_c_to_use, etr_params, mtrx_params, mtry_params, initial_b, p) expected_list = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'twist_doughnut_outputs.pkl')) assert (np.allclose(np.array(test_list), np.array(expected_list)))
def test_txfunc_est(): # Test txfunc.txfunc_est() function. The test is that given # inputs from previous run, the outputs are unchanged. input_tuple = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data/txfunc_est_inputs.pkl')) (df, s, t, rate_type, output_dir, graph) = input_tuple tax_func_type = 'DEP' numparams = 12 test_tuple = txfunc.txfunc_est(df, s, t, rate_type, tax_func_type, numparams, output_dir, graph) expected_tuple = (np.array([ 6.37000261e-22, 2.73404765e-03, 1.62463424e-08, 1.48147213e-02, 2.32797191e-01, -3.69059719e-02, 1.00000000e-04, -1.01967001e-01, 3.96030035e-02, 1.02987671e-01, -1.30433574e-01, 1.00000000e+00 ]), 19527.162030047846, 3798) for i, v in enumerate(expected_tuple): assert (np.allclose(test_tuple[i], v))
from __future__ import print_function import pytest import numpy as np import os from ogindia import SS, utils, aggregates, household from ogindia.parameters import Specifications CUR_PATH = os.path.abspath(os.path.dirname(__file__)) input_tuple = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'SS_fsolve_inputs.pkl')) guesses_in, params = input_tuple params = params + (None, 1) (bssmat, nssmat, chi_params, ss_params, income_tax_params, iterative_params, small_open_params, client, num_workers) = params p1 = Specifications() (p1.J, p1.S, p1.T, p1.BW, p1.beta, p1.sigma, p1.alpha, p1.gamma, p1.epsilon, Z, p1.delta, p1.ltilde, p1.nu, p1.g_y, p1.g_n_ss, tau_payroll, tau_bq, p1.rho, p1.omega_SS, p1.budget_balance, alpha_T, p1.debt_ratio_ss, tau_b, delta_tau, lambdas, imm_rates, p1.e, retire, p1.mean_income_data, h_wealth, p_wealth, m_wealth, p1.b_ellipse, p1.upsilon) = ss_params p1.eta = (p1.omega_SS.reshape(p1.S, 1) * p1.lambdas.reshape(1, p1.J)).reshape(1, p1.S, p1.J) p1.Z = np.ones(p1.T + p1.S) * Z p1.tau_bq = np.ones(p1.T + p1.S) * 0.0 p1.tau_payroll = np.ones(p1.T + p1.S) * tau_payroll p1.alpha_T = np.ones(p1.T + p1.S) * alpha_T p1.tau_b = np.ones(p1.T + p1.S) * tau_b p1.delta_tau = np.ones(p1.T + p1.S) * delta_tau p1.h_wealth = np.ones(p1.T + p1.S) * h_wealth p1.p_wealth = np.ones(p1.T + p1.S) * p_wealth
''' Tests of parameter_table.py module ''' import pytest import os from ogindia import utils, parameter_tables # Load in test results and parameters CUR_PATH = os.path.abspath(os.path.dirname(__file__)) base_params = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'model_params_baseline.pkl')) base_taxfunctions = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'TxFuncEst_baseline.pkl')) def test_tax_rate_table(): str = parameter_tables.tax_rate_table(base_taxfunctions, base_params) assert str
def test_run_TPI(): # Test TPI.run_TPI 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', 'run_TPI_inputs.pkl')) (income_tax_params, tpi_params, iterative_params, small_open_params, initial_values, SS_values, fiscal_params, biz_tax_params, output_dir, baseline_spending) = input_tuple tpi_params = tpi_params + [True] initial_values = initial_values + (0.0, ) p = Specifications() (J, S, T, BW, p.beta, p.sigma, p.alpha, p.gamma, p.epsilon, Z, p.delta, p.ltilde, p.nu, p.g_y, p.g_n, tau_b, delta_tau, tau_payroll, tau_bq, p.rho, p.omega, N_tilde, lambdas, p.imm_rates, p.e, retire, p.mean_income_data, factor, h_wealth, p_wealth, m_wealth, p.b_ellipse, p.upsilon, p.chi_b, p.chi_n, theta, p.baseline) = tpi_params new_param_values = { 'J': J, 'S': S, 'T': T, 'eta': (np.ones((S, J)) / (S * J)) } # update parameters instance with new values for test p.update_specifications(new_param_values, raise_errors=False) (J, S, T, BW, p.beta, p.sigma, p.alpha, p.gamma, p.epsilon, Z, p.delta, p.ltilde, p.nu, p.g_y, p.g_n, tau_b, delta_tau, tau_payroll, tau_bq, p.rho, p.omega, N_tilde, lambdas, p.imm_rates, p.e, retire, p.mean_income_data, factor, h_wealth, p_wealth, m_wealth, p.b_ellipse, p.upsilon, p.chi_b, p.chi_n, theta, p.baseline) = tpi_params p.omega_SS = p.omega[-1, :] p.eta = p.omega.reshape(T + S, S, 1) * lambdas.reshape(1, J) p.Z = np.ones(p.T + p.S) * Z p.zeta_D = np.zeros(p.T + p.S) p.initial_foreign_debt_ratio = 0.0 p.initial_debt_ratio = 0.59 p.tau_bq = np.ones(p.T + p.S) * 0.0 p.tau_payroll = np.ones(p.T + p.S) * tau_payroll 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.small_open, ss_firm_r, ss_hh_r = small_open_params p.ss_firm_r = np.ones(p.T + p.S) * ss_firm_r p.ss_hh_r = np.ones(p.T + p.S) * ss_hh_r p.maxiter, p.mindist_SS, p.mindist_TPI = iterative_params (p.budget_balance, alpha_T, alpha_G, p.tG1, p.tG2, p.rho_G, p.debt_ratio_ss) = fiscal_params p.alpha_T = np.concatenate((alpha_T, np.ones(40) * alpha_T[-1])) p.alpha_G = np.concatenate((alpha_G, np.ones(40) * alpha_G[-1])) (tau_b, delta_tau) = biz_tax_params p.tau_b = np.ones(p.T + p.S) * tau_b p.delta_tau = np.ones(p.T + p.S) * delta_tau p.analytical_mtrs, etr_params, mtrx_params, mtry_params =\ income_tax_params p.etr_params = np.transpose(etr_params, (1, 0, 2))[:p.T, :, :] p.mtrx_params = np.transpose(mtrx_params, (1, 0, 2))[:p.T, :, :] p.mtry_params = np.transpose(mtry_params, (1, 0, 2))[:p.T, :, :] p.lambdas = lambdas.reshape(p.J, 1) p.output = output_dir p.baseline_spending = baseline_spending p.num_workers = 1 (K0, b_sinit, b_splus1init, factor, initial_b, initial_n, p.omega_S_preTP, initial_debt, D0) = initial_values # Need to run SS first to get results ss_outputs = SS.run_SS(p, None) if p.baseline: utils.mkdirs(os.path.join(p.baseline_dir, "SS")) ss_dir = os.path.join(p.baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) else: utils.mkdirs(os.path.join(p.output_base, "SS")) ss_dir = os.path.join(p.output_base, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) test_dict = TPI.run_TPI(p, None) expected_dict = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'run_TPI_outputs.pkl')) # delete values key-value pairs that are not in both dicts del expected_dict['I_total'] del test_dict['etr_path'], test_dict['mtrx_path'], test_dict['mtry_path'] del test_dict['bmat_s'] test_dict['b_mat'] = test_dict.pop('bmat_splus1') test_dict['REVENUE'] = test_dict.pop('total_revenue') test_dict['T_H'] = test_dict.pop('TR') test_dict['IITpayroll_revenue'] = (test_dict['REVENUE'][:160] - test_dict['business_revenue']) del test_dict['T_P'], test_dict['T_BQ'], test_dict['T_W'] del test_dict['y_before_tax_mat'], test_dict['K_f'], test_dict['K_d'] del test_dict['D_d'], test_dict['D_f'] del test_dict['new_borrowing_f'], test_dict['debt_service_f'] del test_dict['resource_constraint_error'], test_dict['T_C'] del test_dict['r_gov'], test_dict['r_hh'], test_dict['tr_path'] for k, v in expected_dict.items(): try: assert (np.allclose(test_dict[k], v, rtol=1e-04, atol=1e-04)) except ValueError: assert (np.allclose(test_dict[k], v[:p.T, :, :], rtol=1e-04, atol=1e-04))
''' Tests of output_tables.py module ''' import pytest import os import pandas as pd from ogindia import utils, output_tables # Load in test results and parameters CUR_PATH = os.path.abspath(os.path.dirname(__file__)) base_tpi = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'TPI_vars_baseline.pkl')) base_params = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'model_params_baseline.pkl')) reform_tpi = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'TPI_vars_reform.pkl')) reform_params = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'model_params_reform.pkl')) def test_macro_table(): df = output_tables.macro_table(base_tpi, base_params, reform_tpi, reform_params) assert isinstance(df, pd.DataFrame)
def dump_diff_output(baseline_dir, policy_dir): ''' This function reads the pickles with the SS and time path results from the baseline and reform and then calculates the percentage differences between the two for each year in the 10-year budget window, over the entire budget window, and in the SS. Args: baseline_dir (str): path for directory with baseline policy results policy_dir (str): path for directory with reform policy results Returns: baseline_macros (Numpy array): time path for relevant macro variables from baseline equilibrium, order of variables is Y, C, I, L, w, r, total_revenue policy_macros (Numpy array): time path for relevant macro variables from reform equilibrium pct_changes (Numpy array): percentage changes in macro variables from baseline to reform for each year in the time path ''' # read macro output tpi_baseline_dir = os.path.join(baseline_dir, "TPI") tpi_policy_dir = os.path.join(policy_dir, "TPI") if not os.path.exists(tpi_policy_dir): os.mkdir(tpi_policy_dir) tpi_macro_vars_policy_path = os.path.join(tpi_policy_dir, "TPI_vars.pkl") tpi_macro_vars_policy = safe_read_pickle(tpi_macro_vars_policy_path) tpi_macro_vars_baseline_path = os.path.join(tpi_baseline_dir, "TPI_vars.pkl") tpi_macro_vars_baseline = safe_read_pickle(tpi_macro_vars_baseline_path) T = len(tpi_macro_vars_baseline['C']) baseline_macros = np.zeros((7, T)) baseline_macros[0, :] = tpi_macro_vars_baseline['Y'][:T] baseline_macros[1, :] = tpi_macro_vars_baseline['C'][:T] baseline_macros[2, :] = tpi_macro_vars_baseline['I'][:T] baseline_macros[3, :] = tpi_macro_vars_baseline['L'][:T] baseline_macros[4, :] = tpi_macro_vars_baseline['w'][:T] baseline_macros[5, :] = tpi_macro_vars_baseline['r'][:T] baseline_macros[6, :] = tpi_macro_vars_baseline['total_revenue'][:T] policy_macros = np.zeros((7, T)) policy_macros[0, :] = tpi_macro_vars_policy['Y'][:T] policy_macros[1, :] = tpi_macro_vars_policy['C'][:T] policy_macros[2, :] = tpi_macro_vars_policy['I'][:T] policy_macros[3, :] = tpi_macro_vars_policy['L'][:T] policy_macros[4, :] = tpi_macro_vars_policy['w'][:T] policy_macros[5, :] = tpi_macro_vars_policy['r'][:T] policy_macros[6, :] = tpi_macro_vars_policy['total_revenue'][:T] pct_changes = np.zeros((7, 12)) # pct changes for each year in budget window pct_changes[:, :10] = ((policy_macros - baseline_macros) / policy_macros)[:, :10] # pct changes over entire budget window pct_changes[:, 10] = ((policy_macros[:, :10].sum(axis=1) - baseline_macros[:, :10].sum(axis=1)) / policy_macros[:, :10].sum(axis=1)) # Load SS results ss_policy_path = os.path.join(policy_dir, "SS", "SS_vars.pkl") ss_policy = safe_read_pickle(ss_policy_path) ss_baseline_path = os.path.join(baseline_dir, "SS", "SS_vars.pkl") ss_baseline = safe_read_pickle(ss_baseline_path) # pct changes in macro aggregates in SS pct_changes[0, 11] = ((ss_policy['Yss'] - ss_baseline['Yss']) / ss_baseline['Yss']) pct_changes[1, 11] = ((ss_policy['Css'] - ss_baseline['Css']) / ss_baseline['Css']) pct_changes[2, 11] = ((ss_policy['Iss'] - ss_baseline['Iss']) / ss_baseline['Iss']) pct_changes[3, 11] = ((ss_policy['Lss'] - ss_baseline['Lss']) / ss_baseline['Lss']) pct_changes[4, 11] = ((ss_policy['wss'] - ss_baseline['wss']) / ss_baseline['wss']) pct_changes[5, 11] = ((ss_policy['rss'] - ss_baseline['rss']) / ss_baseline['rss']) pct_changes[6, 11] = ( (ss_policy['total_revenue_ss'] - ss_baseline['total_revenue_ss']) / ss_baseline['total_revenue_ss']) return pct_changes, baseline_macros, policy_macros
def test_euler_equation_solver(): # Test SS.inner_loop 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', 'euler_eqn_solver_inputs.pkl')) (guesses, params) = input_tuple p = Specifications() (r, w, TR, factor, j, p.J, p.S, p.beta, p.sigma, p.ltilde, p.g_y, p.g_n_ss, tau_payroll, retire, p.mean_income_data, h_wealth, p_wealth, m_wealth, p.b_ellipse, p.upsilon, j, p.chi_b, p.chi_n, tau_bq, p.rho, lambdas, p.omega_SS, p.e, p.analytical_mtrs, etr_params, mtrx_params, mtry_params) = params p.eta = (p.omega_SS.reshape(p.S, 1) * p.lambdas.reshape(1, p.J)).reshape(1, p.S, p.J) p.tau_bq = np.ones(p.T + p.S) * 0.0 p.tau_payroll = np.ones(p.T + p.S) * tau_payroll 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.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.tax_func_type = 'DEP' p.lambdas = lambdas.reshape(p.J, 1) b_splus1 = np.array(guesses[:p.S]).reshape(p.S, 1) + 0.005 BQ = aggregates.get_BQ(r, b_splus1, j, p, 'SS', False) bq = household.get_bq(BQ, j, p, 'SS') tr = household.get_tr(TR, j, p, 'SS') args = (r, w, bq, tr, factor, j, p) test_list = SS.euler_equation_solver(guesses, *args) expected_list = np.array([ -3.62741663e+00, -6.30068841e+00, -6.76592886e+00, -6.97731223e+00, -7.05777777e+00, -6.57305440e+00, -7.11553046e+00, -7.30569622e+00, -7.45808107e+00, -7.89984062e+00, -8.11466111e+00, -8.28230086e+00, -8.79253862e+00, -8.86994311e+00, -9.31299476e+00, -9.80834199e+00, -9.97333771e+00, -1.08349979e+01, -1.13199826e+01, -1.22890930e+01, -1.31550471e+01, -1.42753713e+01, -1.55721098e+01, -1.73811490e+01, -1.88856303e+01, -2.09570569e+01, -2.30559500e+01, -2.52127149e+01, -2.76119605e+01, -3.03141128e+01, -3.30900203e+01, -3.62799730e+01, -3.91169706e+01, -4.24246421e+01, -4.55740527e+01, -4.92914871e+01, -5.30682805e+01, -5.70043846e+01, -6.06075991e+01, -6.45251018e+01, -6.86128365e+01, -7.35896515e+01, -7.92634608e+01, -8.34733231e+01, -9.29802390e+01, -1.01179788e+02, -1.10437881e+02, -1.20569527e+02, -1.31569973e+02, -1.43633399e+02, -1.57534056e+02, -1.73244610e+02, -1.90066728e+02, -2.07980863e+02, -2.27589046e+02, -2.50241670e+02, -2.76314755e+02, -3.04930986e+02, -3.36196973e+02, -3.70907934e+02, -4.10966644e+02, -4.56684022e+02, -5.06945218e+02, -5.61838645e+02, -6.22617808e+02, -6.90840503e+02, -7.67825713e+02, -8.54436805e+02, -9.51106365e+02, -1.05780305e+03, -1.17435473e+03, -1.30045062e+03, -1.43571221e+03, -1.57971603e+03, -1.73204264e+03, -1.88430524e+03, -2.03403679e+03, -2.17861987e+03, -2.31532884e+03, -8.00654731e+03, -5.21487172e-02, -2.80234170e-01, 4.93894552e-01, 3.11884938e-01, 6.55799607e-01, 5.62182419e-01, 3.86074983e-01, 3.43741491e-01, 4.22461089e-01, 3.63707951e-01, 4.93150010e-01, 4.72813688e-01, 4.07390308e-01, 4.94974186e-01, 4.69900128e-01, 4.37562389e-01, 5.67370182e-01, 4.88965362e-01, 6.40728461e-01, 6.14619979e-01, 4.97173823e-01, 6.19549666e-01, 6.51193557e-01, 4.48906118e-01, 7.93091492e-01, 6.51249363e-01, 6.56307713e-01, 1.12948552e+00, 9.50018058e-01, 6.79613030e-01, 9.51359123e-01, 6.31059147e-01, 7.97896887e-01, 8.44620817e-01, 7.43683837e-01, 1.56693187e+00, 2.75630011e-01, 5.32956891e-01, 1.57110727e+00, 1.22674610e+00, 4.63932928e-01, 1.47225464e+00, 1.16948107e+00, 1.07965795e+00, -3.20557791e-01, -1.17064127e+00, -7.84880649e-01, -7.60851182e-01, -1.61415945e+00, -8.30363975e-01, -1.68459409e+00, -1.49260581e+00, -1.84257084e+00, -1.72143079e+00, -1.43131579e+00, -1.63719219e+00, -1.43874851e+00, -1.57207905e+00, -1.72909159e+00, -1.98778122e+00, -1.80843826e+00, -2.12828312e+00, -2.24768762e+00, -2.36961877e+00, -2.49117258e+00, -2.59914065e+00, -2.82309085e+00, -2.93613362e+00, -3.34446991e+00, -3.45445086e+00, -3.74962140e+00, -3.78113417e+00, -4.55643800e+00, -4.86929016e+00, -5.08657898e+00, -5.22054177e+00, -5.54606515e+00, -5.78478304e+00, -5.93652041e+00, -6.11519786e+00]) assert(np.allclose(np.array(test_list), np.array(expected_list)))
def test_run_SS(input_path, expected_path): # Test SS.run_SS 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', input_path)) (income_tax_params, ss_params, iterative_params, chi_params, small_open_params, baseline, baseline_spending, baseline_dir) =\ input_tuple 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.eta = (p.omega_SS.reshape(p.S, 1) * p.lambdas.reshape(1, p.J)).reshape(1, p.S, p.J) 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.baseline_dir = baseline_dir 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 test_dict = SS.run_SS(p, None) expected_dict = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', expected_path)) # 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['resource_constraint_error'], test_dict['T_Css'] del test_dict['r_gov_ss'], test_dict['r_hh_ss'] 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'], test_dict['Iss_total'] del test_dict['debt_service_f'], test_dict['new_borrowing_f'] test_dict['revenue_ss'] = test_dict.pop('total_revenue_ss') test_dict['T_Hss'] = test_dict.pop('TR_ss') for k, v in expected_dict.items(): assert(np.allclose(test_dict[k], v))
''' Tests of parameter_plots.py module ''' import pytest import os from ogindia import utils, parameter_plots # Load in test results and parameters CUR_PATH = os.path.abspath(os.path.dirname(__file__)) base_params = utils.safe_read_pickle( os.path.join(CUR_PATH, 'test_io_data', 'model_params_baseline.pkl')) def test_plot_imm_rates(): fig = parameter_plots.plot_imm_rates(base_params) assert fig def test_plot_mort_rates(): fig = parameter_plots.plot_mort_rates(base_params) assert fig def test_plot_pop_growth(): fig = parameter_plots.plot_pop_growth(base_params) assert fig def test_plot_ability_profiles():