def test_makedirs(tmp_path): ''' Test of utils.makedirs() function ''' utils.mkdirs(tmp_path) assert os.path.exists(tmp_path)
def test_constant_demographics_TPI(): ''' This tests solves the model under the assumption of constant demographics, a balanced budget, and tax functions that do not vary over time. In this case, given how initial guesss for the time path are made, the time path should be solved for on the first iteration and the values all along the time path should equal their steady-state values. ''' output_base = "./OUTPUT" baseline_dir = "./OUTPUT" user_params = { 'constant_demographics': True, 'budget_balance': True, 'zero_taxes': True, 'maxiter': 2 } # Create output directory structure ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [ss_dir, tpi_dir] for _dir in dirs: try: print("making dir: ", _dir) os.makedirs(_dir) except OSError as oe: pass spec = Specifications(run_micro=False, output_base=output_base, baseline_dir=baseline_dir, test=False, time_path=True, baseline=True, reform={}, guid='') spec.update_specifications(user_params) print('path for tax functions: ', spec.output_base) spec.get_tax_function_parameters(None, False) # Run SS ss_outputs = SS.run_SS(spec, None) # save SS results utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) # Save pickle with parameter values for the run param_dir = os.path.join(baseline_dir, "model_params.pkl") pickle.dump(spec, open(param_dir, "wb")) tpi_output = TPI.run_TPI(spec, None) print( 'Max diff btwn SS and TP bsplus1 = ', np.absolute(tpi_output['bmat_splus1'][:spec.T, :, :] - ss_outputs['bssmat_splus1']).max()) print('Max diff btwn SS and TP Y = ', np.absolute(tpi_output['Y'][:spec.T] - ss_outputs['Yss']).max()) assert (np.allclose(tpi_output['bmat_splus1'][:spec.T, :, :], ss_outputs['bssmat_splus1']))
def test_run_TPI(baseline, param_updates, filename, tmp_path, dask_client): ''' Test TPI.run_TPI function. Provide inputs to function and ensure that output returned matches what it has been before. ''' baseline_dir = os.path.join(CUR_PATH, 'baseline') if baseline: output_base = baseline_dir else: output_base = os.path.join(CUR_PATH, 'reform') p = Specifications(baseline=baseline, baseline_dir=baseline_dir, output_base=output_base, test=True, client=dask_client, num_workers=NUM_WORKERS) p.update_specifications(param_updates) p.maxiter = 2 # this test runs through just two iterations p.get_tax_function_parameters(None, run_micro=False, tax_func_path=os.path.join( CUR_PATH, '..', 'data', 'tax_functions', 'TxFuncEst_baseline_CPS.pkl')) # Need to run SS first to get results SS.ENFORCE_SOLUTION_CHECKS = False 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") with open(ss_dir, "wb") as f: pickle.dump(ss_outputs, f) else: utils.mkdirs(os.path.join(p.output_base, "SS")) ss_dir = os.path.join(p.output_base, "SS", "SS_vars.pkl") with open(ss_dir, "wb") as f: pickle.dump(ss_outputs, f) TPI.ENFORCE_SOLUTION_CHECKS = False test_dict = TPI.run_TPI(p, None) expected_dict = utils.safe_read_pickle(filename) for k, v in expected_dict.items(): try: assert (np.allclose(test_dict[k][:p.T], v[:p.T], rtol=1e-04, atol=1e-04)) except ValueError: assert (np.allclose(test_dict[k][:p.T, :, :], v[:p.T, :, :], rtol=1e-04, atol=1e-04))
def test_constant_demographics_TPI(dask_client): ''' This tests solves the model under the assumption of constant demographics, a balanced budget, and tax functions that do not vary over time. In this case, given how initial guesss for the time path are made, the time path should be solved for on the first iteration and the values all along the time path should equal their steady-state values. ''' # Create output directory structure spec = Specifications(run_micro=False, output_base=OUTPUT_DIR, baseline_dir=OUTPUT_DIR, test=False, time_path=True, baseline=True, iit_reform={}, guid='', client=dask_client, num_workers=NUM_WORKERS) og_spec = { 'constant_demographics': True, 'budget_balance': True, 'zero_taxes': True, 'maxiter': 2, 'r_gov_shift': 0.0, 'zeta_D': [0.0, 0.0], 'zeta_K': [0.0, 0.0], 'debt_ratio_ss': 1.0, 'initial_foreign_debt_ratio': 0.0, 'start_year': 2019, 'cit_rate': [0.0], 'PIA_rate_bkt_1': 0.0, 'PIA_rate_bkt_2': 0.0, 'PIA_rate_bkt_3': 0.0, 'eta': (spec.omega_SS.reshape(spec.S, 1) * spec.lambdas.reshape(1, spec.J)) } spec.update_specifications(og_spec) spec.get_tax_function_parameters(None, False, tax_func_path=TAX_FUNC_PATH) # Run SS ss_outputs = SS.run_SS(spec, None) # save SS results utils.mkdirs(os.path.join(OUTPUT_DIR, "SS")) ss_dir = os.path.join(OUTPUT_DIR, "SS", "SS_vars.pkl") with open(ss_dir, "wb") as f: pickle.dump(ss_outputs, f) # Run TPI tpi_output = TPI.run_TPI(spec, None) assert (np.allclose(tpi_output['bmat_splus1'][:spec.T, :, :], ss_outputs['bssmat_splus1']))
def test_constant_demographics_TPI(): ''' This tests solves the model under the assumption of constant demographics, a balanced budget, and tax functions that do not vary over time. In this case, given how initial guesss for the time path are made, the time path should be solved for on the first iteration and the values all along the time path should equal their steady-state values. ''' output_base = "./OUTPUT" baseline_dir = "./OUTPUT" user_params = {'constant_demographics': True, 'budget_balance': True, 'zero_taxes': True, 'maxiter': 2} # Create output directory structure ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [ss_dir, tpi_dir] for _dir in dirs: try: print("making dir: ", _dir) os.makedirs(_dir) except OSError as oe: pass spec = Specifications(run_micro=False, output_base=output_base, baseline_dir=baseline_dir, test=False, time_path=True, baseline=True, reform={}, guid='') spec.update_specifications(user_params) print('path for tax functions: ', spec.output_base) spec.get_tax_function_parameters(None, False) # Run SS ss_outputs = SS.run_SS(spec, None) # save SS results utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) # Save pickle with parameter values for the run param_dir = os.path.join(baseline_dir, "model_params.pkl") pickle.dump(spec, open(param_dir, "wb")) tpi_output = TPI.run_TPI(spec, None) print('Max diff btwn SS and TP bsplus1 = ', np.absolute(tpi_output['bmat_splus1'][:spec.T, :, :] - ss_outputs['bssmat_splus1']).max()) print('Max diff btwn SS and TP Y = ', np.absolute(tpi_output['Y'][:spec.T] - ss_outputs['Yss']).max()) assert(np.allclose(tpi_output['bmat_splus1'][:spec.T, :, :], ss_outputs['bssmat_splus1']))
def test_constant_demographics_TPI(): ''' This tests solves the model under the assumption of constant demographics, a balanced budget, and tax functions that do not vary over time. In this case, given how initial guesss for the time path are made, the time path should be solved for on the first iteration and the values all along the time path should equal their steady-state values. ''' output_base = os.path.join(CUR_PATH, 'OUTPUT') baseline_dir = output_base # Create output directory structure ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [ss_dir, tpi_dir] for _dir in dirs: try: print("making dir: ", _dir) os.makedirs(_dir) except OSError: pass spec = Specifications(run_micro=False, output_base=output_base, baseline_dir=baseline_dir, test=False, time_path=True, baseline=True, iit_reform={}, guid='') og_spec = { 'constant_demographics': True, 'budget_balance': True, 'zero_taxes': True, 'maxiter': 2, 'eta': (spec.omega_SS.reshape(spec.S, 1) * spec.lambdas.reshape(1, spec.J)) } spec.update_specifications(og_spec) spec.get_tax_function_parameters(None, False) # Run SS ss_outputs = SS.run_SS(spec, None) # save SS results utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) # Run TPI tpi_output = TPI.run_TPI(spec, None) assert (np.allclose(tpi_output['bmat_splus1'][:spec.T, :, :], ss_outputs['bssmat_splus1']))
def test_constant_demographics_TPI_small_open(): ''' This tests solves the model under the assumption of constant demographics, a balanced budget, and tax functions that do not vary over time, as well as with a small open economy assumption. ''' # Create output directory structure spec = Specifications(run_micro=False, output_base=OUTPUT_DIR, baseline_dir=OUTPUT_DIR, test=False, time_path=True, baseline=True, iit_reform={}, guid='') og_spec = { 'constant_demographics': True, 'budget_balance': True, 'zero_taxes': True, 'maxiter': 2, 'r_gov_shift': 0.0, 'zeta_D': [0.0, 0.0], 'zeta_K': [1.0], 'debt_ratio_ss': 1.0, 'initial_foreign_debt_ratio': 0.0, 'start_year': 2019, 'cit_rate': [0.0], 'PIA_rate_bkt_1': 0.0, 'PIA_rate_bkt_2': 0.0, 'PIA_rate_bkt_3': 0.0, 'eta': (spec.omega_SS.reshape(spec.S, 1) * spec.lambdas.reshape(1, spec.J)) } spec.update_specifications(og_spec) spec.get_tax_function_parameters(None, False, tax_func_path=TAX_FUNC_PATH) # Run SS ss_outputs = SS.run_SS(spec, None) # save SS results utils.mkdirs(os.path.join(OUTPUT_DIR, "SS")) ss_dir = os.path.join(OUTPUT_DIR, "SS", "SS_vars.pkl") with open(ss_dir, "wb") as f: pickle.dump(ss_outputs, f) # Run TPI tpi_output = TPI.run_TPI(spec, None) assert (np.allclose(tpi_output['bmat_splus1'][:spec.T, :, :], ss_outputs['bssmat_splus1']))
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 } # 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.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.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 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['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['resource_constraint_error'], test_dict['T_C'] del test_dict['r_gov'], test_dict['r_hh'] 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))
def plot_income_data(ages, abil_midp, abil_pcts, emat, output_dir=None, filesuffix=""): ''' Plot income profiles from models estimated from data. Args: Returns: ''' ''' This function graphs ability matrix in 3D, 2D, log, and nolog Args: ages (Numpy array) ages represented in sample, length S abil_midp (Numpy array): midpoints of income percentile bins in each ability group abil_pcts (Numpy array): percent of population in each lifetime income group, length J emat (Numpy array): effective labor units by age and lifetime income group, size SxJ filesuffix (str): suffix to be added to plot files Returns: None ''' J = abil_midp.shape[0] abil_mesh, age_mesh = np.meshgrid(abil_midp, ages) cmap1 = matplotlib.cm.get_cmap('summer') if output_dir: # Make sure that directory is created utils.mkdirs(output_dir) if J == 1: # Plot of 2D, J=1 in levels plt.figure() plt.plot(ages, emat) filename = "ability_2D_lev" + filesuffix fullpath = os.path.join(output_dir, filename) plt.savefig(fullpath) plt.close() # Plot of 2D, J=1 in logs plt.figure() plt.plot(ages, np.log(emat)) filename = "ability_2D_log" + filesuffix fullpath = os.path.join(output_dir, filename) plt.savefig(fullpath) plt.close() else: # Plot of 3D, J>1 in levels fig10 = plt.figure() ax10 = fig10.gca(projection='3d') ax10.plot_surface(age_mesh, abil_mesh, emat, rstride=8, cstride=1, cmap=cmap1) ax10.set_xlabel(r'age-$s$') ax10.set_ylabel(r'ability type -$j$') ax10.set_zlabel(r'ability $e_{j,s}$') filename = "ability_3D_lev" + filesuffix fullpath = os.path.join(output_dir, filename) plt.savefig(fullpath) plt.close() # Plot of 3D, J>1 in logs fig11 = plt.figure() ax11 = fig11.gca(projection='3d') ax11.plot_surface(age_mesh, abil_mesh, np.log(emat), rstride=8, cstride=1, cmap=cmap1) ax11.set_xlabel(r'age-$s$') ax11.set_ylabel(r'ability type -$j$') ax11.set_zlabel(r'log ability $log(e_{j,s})$') filename = "ability_3D_log" + filesuffix fullpath = os.path.join(output_dir, filename) plt.savefig(fullpath) plt.close() if J <= 10: # Restricted because of line and marker types # Plot of 2D lines from 3D version in logs ax = plt.subplot(111) linestyles = np.array([ "-", "--", "-.", ":", ]) markers = np.array(["x", "v", "o", "d", ">", "|"]) pct_lb = 0 for j in range(J): this_label = ( str(int(np.rint(pct_lb))) + " - " + str(int(np.rint(pct_lb + 100 * abil_pcts[j]))) + "%") pct_lb += 100 * abil_pcts[j] if j <= 3: ax.plot(ages, np.log(emat[:, j]), label=this_label, linestyle=linestyles[j], color='black') elif j > 3: ax.plot(ages, np.log(emat[:, j]), label=this_label, marker=markers[j - 4], color='black') ax.axvline(x=80, color='black', linestyle='--') box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) ax.set_xlabel(r'age-$s$') ax.set_ylabel(r'log ability $log(e_{j,s})$') filename = "ability_2D_log" + filesuffix fullpath = os.path.join(output_dir, filename) plt.savefig(fullpath) plt.close() else: if J <= 10: # Restricted because of line and marker types # Plot of 2D lines from 3D version in logs ax = plt.subplot(111) linestyles = np.array([ "-", "--", "-.", ":", ]) markers = np.array(["x", "v", "o", "d", ">", "|"]) pct_lb = 0 for j in range(J): this_label = (str(int(np.rint(pct_lb))) + " - " + str(int(np.rint(pct_lb + 100 * abil_pcts[j]))) + "%") pct_lb += 100 * abil_pcts[j] if j <= 3: ax.plot(ages, np.log(emat[:, j]), label=this_label, linestyle=linestyles[j], color='black') elif j > 3: ax.plot(ages, np.log(emat[:, j]), label=this_label, marker=markers[j - 4], color='black') ax.axvline(x=80, color='black', linestyle='--') box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) ax.set_xlabel(r'age-$s$') ax.set_ylabel(r'log ability $log(e_{j,s})$') return ax
def runner_SS(output_base, baseline_dir, baseline=False, analytical_mtrs=False, age_specific=False, reform={}, user_params={}, guid='', run_micro=True): from ogusa import parameters, demographics, income, utils from ogusa import txfunc tick = time.time() #Create output directory structure saved_moments_dir = os.path.join(output_base, "Saved_moments") ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [saved_moments_dir, ss_dir, tpi_dir] for _dir in dirs: try: print "making dir: ", _dir os.makedirs(_dir) except OSError as oe: pass if run_micro: txfunc.get_tax_func_estimate(baseline=baseline, analytical_mtrs=analytical_mtrs, age_specific=age_specific, start_year=user_params['start_year'], reform=reform, guid=guid) print ("in runner, baseline is ", baseline) run_params = ogusa.parameters.get_parameters(baseline=baseline, guid=guid) run_params['analytical_mtrs'] = analytical_mtrs # Modify ogusa parameters based on user input if 'frisch' in user_params: print "updating fricsh and associated" b_ellipse, upsilon = ogusa.elliptical_u_est.estimation(user_params['frisch'], run_params['ltilde']) run_params['b_ellipse'] = b_ellipse run_params['upsilon'] = upsilon run_params.update(user_params) # Modify ogusa parameters based on user input if 'g_y_annual' in user_params: print "updating g_y_annual and associated" ending_age = run_params['ending_age'] starting_age = run_params['starting_age'] S = run_params['S'] g_y = (1 + user_params['g_y_annual'])**(float(ending_age - starting_age) / S) - 1 run_params['g_y'] = g_y run_params.update(user_params) from ogusa import SS, TPI # List of parameter names that will not be changing (unless we decide to # change them for a tax experiment) param_names = ['S', 'J', 'T', 'BW', 'lambdas', 'starting_age', 'ending_age', 'beta', 'sigma', 'alpha', 'nu', 'Z', 'delta', 'E', 'ltilde', 'g_y', 'maxiter', 'mindist_SS', 'mindist_TPI', 'analytical_mtrs', 'b_ellipse', 'k_ellipse', 'upsilon', 'chi_b_guess', 'chi_n_guess','etr_params','mtrx_params', 'mtry_params','tau_payroll', 'tau_bq', 'retire', 'mean_income_data', 'g_n_vector', 'h_wealth', 'p_wealth', 'm_wealth', 'omega', 'g_n_ss', 'omega_SS', 'surv_rate', 'imm_rates', 'e', 'rho', 'omega_S_preTP'] ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' sim_params = {} for key in param_names: sim_params[key] = run_params[key] sim_params['output_dir'] = output_base sim_params['run_params'] = run_params income_tax_params, ss_params, iterative_params, chi_params= SS.create_steady_state_parameters(**sim_params) ''' **** CALL CALIBRATION here if boolean flagged **** ''' calibrate_model = False if calibrate_model: chi_params = calibrate.chi_estimate(income_tax_params, ss_params, iterative_params, chi_params, baseline_dir=baseline_dir) ss_outputs = SS.run_SS(income_tax_params, ss_params, iterative_params, chi_params, baseline, baseline_dir=baseline_dir) ''' ------------------------------------------------------------------------ Pickle SS results ------------------------------------------------------------------------ ''' if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) else: utils.mkdirs(os.path.join(output_base, "SS")) ss_dir = os.path.join(output_base, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb"))
def run_TPI(p, client=None): # unpack tuples of parameters initial_values, SS_values, baseline_values = get_initial_SS_values(p) (B0, b_sinit, b_splus1init, factor, initial_b, initial_n, D0) = initial_values (Kss, Bss, Lss, rss, wss, BQss, T_Hss, total_revenue_ss, bssmat_splus1, nssmat, Yss, Gss, theta) = SS_values (T_Hbaseline, Gbaseline) = baseline_values print('Government spending breakpoints are tG1: ', p.tG1, '; and tG2:', p.tG2) # Initialize guesses at time paths # Make array of initial guesses for labor supply and savings domain = np.linspace(0, p.T, p.T) domain2 = np.tile(domain.reshape(p.T, 1, 1), (1, p.S, p.J)) ending_b = bssmat_splus1 guesses_b = (-1 / (domain2 + 1)) * (ending_b - initial_b) + ending_b ending_b_tail = np.tile(ending_b.reshape(1, p.S, p.J), (p.S, 1, 1)) guesses_b = np.append(guesses_b, ending_b_tail, axis=0) domain3 = np.tile(np.linspace(0, 1, p.T).reshape(p.T, 1, 1), (1, p.S, p.J)) guesses_n = domain3 * (nssmat - initial_n) + initial_n ending_n_tail = np.tile(nssmat.reshape(1, p.S, p.J), (p.S, 1, 1)) guesses_n = np.append(guesses_n, ending_n_tail, axis=0) b_mat = guesses_b # np.zeros((p.T + p.S, p.S, p.J)) n_mat = guesses_n # np.zeros((p.T + p.S, p.S, p.J)) ind = np.arange(p.S) L_init = np.ones((p.T + p.S, )) * Lss B_init = np.ones((p.T + p.S, )) * Bss L_init[:p.T] = aggr.get_L(n_mat[:p.T], p, 'TPI') B_init[1:p.T] = aggr.get_K(b_mat[:p.T], p, 'TPI', False)[:p.T - 1] B_init[0] = B0 if not p.small_open: if p.budget_balance: K_init = B_init else: K_init = B_init * Kss / Bss else: K_init = firm.get_K(L_init, p.firm_r, p, 'TPI') K = K_init L = L_init B = B_init Y = np.zeros_like(K) Y[:p.T] = firm.get_Y(K[:p.T], L[:p.T], p, 'TPI') Y[p.T:] = Yss r = np.zeros_like(Y) if not p.small_open: r[:p.T] = firm.get_r(Y[:p.T], K[:p.T], p, 'TPI') r[p.T:] = rss else: r = p.firm_r # compute w w = np.zeros_like(r) w[:p.T] = firm.get_w_from_r(r[:p.T], p, 'TPI') w[p.T:] = wss r_gov = fiscal.get_r_gov(r, p) if p.budget_balance: r_hh = r else: r_hh = aggr.get_r_hh(r, r_gov, K, p.debt_ratio_ss * Y) if p.small_open: r_hh = p.hh_r BQ0 = aggr.get_BQ(r[0], initial_b, None, p, 'SS', True) if not p.use_zeta: BQ = np.zeros((p.T + p.S, p.J)) for j in range(p.J): BQ[:, j] = (list(np.linspace(BQ0[j], BQss[j], p.T)) + [BQss[j]] * p.S) BQ = np.array(BQ) else: BQ = (list(np.linspace(BQ0, BQss, p.T)) + [BQss] * p.S) BQ = np.array(BQ) if p.budget_balance: if np.abs(T_Hss) < 1e-13: T_Hss2 = 0.0 # sometimes SS is very small but not zero, # even if taxes are zero, this get's rid of the approximation # error, which affects the perc changes below else: T_Hss2 = T_Hss T_H = np.ones(p.T + p.S) * T_Hss2 total_revenue = T_H G = np.zeros(p.T + p.S) elif not p.baseline_spending: T_H = p.alpha_T * Y elif p.baseline_spending: T_H = T_Hbaseline T_H_new = p.T_H # Need to set T_H_new for later reference G = Gbaseline G_0 = Gbaseline[0] # Initialize some starting value if p.budget_balance: D = 0.0 * Y else: D = p.debt_ratio_ss * Y TPIiter = 0 TPIdist = 10 euler_errors = np.zeros((p.T, 2 * p.S, p.J)) TPIdist_vec = np.zeros(p.maxiter) print('analytical mtrs in tpi = ', p.analytical_mtrs) print('tax function type in tpi = ', p.tax_func_type) # TPI loop while (TPIiter < p.maxiter) and (TPIdist >= p.mindist_TPI): r_gov[:p.T] = fiscal.get_r_gov(r[:p.T], p) if p.budget_balance: r_hh[:p.T] = r[:p.T] else: K[:p.T] = firm.get_K_from_Y(Y[:p.T], r[:p.T], p, 'TPI') r_hh[:p.T] = aggr.get_r_hh(r[:p.T], r_gov[:p.T], K[:p.T], D[:p.T]) if p.small_open: r_hh[:p.T] = p.hh_r[:p.T] outer_loop_vars = (r, w, r_hh, BQ, T_H, theta) euler_errors = np.zeros((p.T, 2 * p.S, p.J)) lazy_values = [] for j in range(p.J): guesses = (guesses_b[:, :, j], guesses_n[:, :, j]) lazy_values.append( delayed(inner_loop)(guesses, outer_loop_vars, initial_values, j, ind, p)) results = compute(*lazy_values, scheduler=dask.multiprocessing.get, num_workers=p.num_workers) for j, result in enumerate(results): euler_errors[:, :, j], b_mat[:, :, j], n_mat[:, :, j] = result bmat_s = np.zeros((p.T, p.S, p.J)) bmat_s[0, 1:, :] = initial_b[:-1, :] bmat_s[1:, 1:, :] = b_mat[:p.T - 1, :-1, :] bmat_splus1 = np.zeros((p.T, p.S, p.J)) bmat_splus1[:, :, :] = b_mat[:p.T, :, :] L[:p.T] = aggr.get_L(n_mat[:p.T], p, 'TPI') B[1:p.T] = aggr.get_K(bmat_splus1[:p.T], p, 'TPI', False)[:p.T - 1] if np.any(B) < 0: print('B has negative elements. B[0:9]:', B[0:9]) print('B[T-2:T]:', B[p.T - 2, p.T]) etr_params_4D = np.tile( p.etr_params.reshape(p.T, p.S, 1, p.etr_params.shape[2]), (1, 1, p.J, 1)) bqmat = household.get_bq(BQ, None, p, 'TPI') tax_mat = tax.total_taxes(r_hh[:p.T], w[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat[:p.T, :, :], factor, T_H[:p.T], theta, 0, None, False, 'TPI', p.e, etr_params_4D, p) r_hh_path = utils.to_timepath_shape(r_hh, p) wpath = utils.to_timepath_shape(w, p) c_mat = household.get_cons(r_hh_path[:p.T, :, :], wpath[:p.T, :, :], bmat_s, bmat_splus1, n_mat[:p.T, :, :], bqmat[:p.T, :, :], tax_mat, p.e, p.tau_c[:p.T, :, :], p) if not p.small_open: if p.budget_balance: K[:p.T] = B[:p.T] else: if not p.baseline_spending: Y = T_H / p.alpha_T # maybe unecessary (total_rev, T_Ipath, T_Ppath, T_BQpath, T_Wpath, T_Cpath, business_revenue) = aggr.revenue( r_hh[:p.T], w[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat[:p.T, :, :], c_mat[:p.T, :, :], Y[:p.T], L[:p.T], K[:p.T], factor, theta, etr_params_4D, p, 'TPI') total_revenue = np.array( list(total_rev) + [total_revenue_ss] * p.S) # set intial debt value if p.baseline: D_0 = p.initial_debt_ratio * Y[0] else: D_0 = D0 if not p.baseline_spending: G_0 = p.alpha_G[0] * Y[0] dg_fixed_values = (Y, total_revenue, T_H, D_0, G_0) Dnew, G = fiscal.D_G_path(r_gov, dg_fixed_values, Gbaseline, p) K[:p.T] = B[:p.T] - Dnew[:p.T] if np.any(K < 0): print('K has negative elements. Setting them ' + 'positive to prevent NAN.') K[:p.T] = np.fmax(K[:p.T], 0.05 * B[:p.T]) else: K[:p.T] = firm.get_K(L[:p.T], p.firm_r[:p.T], p, 'TPI') Ynew = firm.get_Y(K[:p.T], L[:p.T], p, 'TPI') if not p.small_open: rnew = firm.get_r(Ynew[:p.T], K[:p.T], p, 'TPI') else: rnew = r.copy() r_gov_new = fiscal.get_r_gov(rnew, p) if p.budget_balance: r_hh_new = rnew[:p.T] else: r_hh_new = aggr.get_r_hh(rnew, r_gov_new, K[:p.T], Dnew[:p.T]) if p.small_open: r_hh_new = p.hh_r[:p.T] # compute w wnew = firm.get_w_from_r(rnew[:p.T], p, 'TPI') b_mat_shift = np.append(np.reshape(initial_b, (1, p.S, p.J)), b_mat[:p.T - 1, :, :], axis=0) BQnew = aggr.get_BQ(r_hh_new[:p.T], b_mat_shift, None, p, 'TPI', False) bqmat_new = household.get_bq(BQnew, None, p, 'TPI') (total_rev, T_Ipath, T_Ppath, T_BQpath, T_Wpath, T_Cpath, business_revenue) = aggr.revenue( r_hh_new[:p.T], wnew[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat_new[:p.T, :, :], c_mat[:p.T, :, :], Ynew[:p.T], L[:p.T], K[:p.T], factor, theta, etr_params_4D, p, 'TPI') total_revenue = np.array(list(total_rev) + [total_revenue_ss] * p.S) if p.budget_balance: T_H_new = total_revenue elif not p.baseline_spending: T_H_new = p.alpha_T[:p.T] * Ynew[:p.T] # If baseline_spending==True, no need to update T_H, it's fixed if p.small_open and not p.budget_balance: # Loop through years to calculate debt and gov't spending. # This is done earlier when small_open=False. if p.baseline: D_0 = p.initial_debt_ratio * Y[0] else: D_0 = D0 if not p.baseline_spending: G_0 = p.alpha_G[0] * Ynew[0] dg_fixed_values = (Ynew, total_revenue, T_H, D_0, G_0) Dnew, G = fiscal.D_G_path(r_gov_new, dg_fixed_values, Gbaseline, p) if p.budget_balance: Dnew = D w[:p.T] = wnew[:p.T] r[:p.T] = utils.convex_combo(rnew[:p.T], r[:p.T], p.nu) BQ[:p.T] = utils.convex_combo(BQnew[:p.T], BQ[:p.T], p.nu) D = Dnew Y[:p.T] = utils.convex_combo(Ynew[:p.T], Y[:p.T], p.nu) if not p.baseline_spending: T_H[:p.T] = utils.convex_combo(T_H_new[:p.T], T_H[:p.T], p.nu) guesses_b = utils.convex_combo(b_mat, guesses_b, p.nu) guesses_n = utils.convex_combo(n_mat, guesses_n, p.nu) print('r diff: ', (rnew[:p.T] - r[:p.T]).max(), (rnew[:p.T] - r[:p.T]).min()) print('BQ diff: ', (BQnew[:p.T] - BQ[:p.T]).max(), (BQnew[:p.T] - BQ[:p.T]).min()) print('T_H diff: ', (T_H_new[:p.T] - T_H[:p.T]).max(), (T_H_new[:p.T] - T_H[:p.T]).min()) print('Y diff: ', (Ynew[:p.T] - Y[:p.T]).max(), (Ynew[:p.T] - Y[:p.T]).min()) if not p.baseline_spending: if T_H.all() != 0: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list( utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(utils.pct_diff_func(wnew[:p.T], w[:p.T])) + list(utils.pct_diff_func(T_H_new[:p.T], T_H[:p.T]))).max() else: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list( utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(utils.pct_diff_func(wnew[:p.T], w[:p.T])) + list(np.abs(T_H[:p.T]))).max() else: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list(utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(utils.pct_diff_func(wnew[:p.T], w[:p.T])) + list(utils.pct_diff_func(Ynew[:p.T], Y[:p.T]))).max() TPIdist_vec[TPIiter] = TPIdist # After T=10, if cycling occurs, drop the value of nu # wait til after T=10 or so, because sometimes there is a jump up # in the first couple iterations # if TPIiter > 10: # if TPIdist_vec[TPIiter] - TPIdist_vec[TPIiter - 1] > 0: # nu /= 2 # print 'New Value of nu:', nu TPIiter += 1 print('Iteration:', TPIiter) print('\tDistance:', TPIdist) # Compute effective and marginal tax rates for all agents mtrx_params_4D = np.tile( p.mtrx_params.reshape(p.T, p.S, 1, p.mtrx_params.shape[2]), (1, 1, p.J, 1)) mtry_params_4D = np.tile( p.mtry_params.reshape(p.T, p.S, 1, p.mtry_params.shape[2]), (1, 1, p.J, 1)) e_3D = np.tile(p.e.reshape(1, p.S, p.J), (p.T, 1, 1)) mtry_path = tax.MTR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, True, e_3D, etr_params_4D, mtry_params_4D, p) mtrx_path = tax.MTR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, False, e_3D, etr_params_4D, mtrx_params_4D, p) etr_path = tax.ETR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, e_3D, etr_params_4D, p) C = aggr.get_C(c_mat, p, 'TPI') if not p.small_open: I = aggr.get_I(bmat_splus1[:p.T], K[1:p.T + 1], K[:p.T], p, 'TPI') rc_error = Y[:p.T] - C[:p.T] - I[:p.T] - G[:p.T] else: I = ((1 + np.squeeze(np.hstack( (p.g_n[1:p.T], p.g_n_ss)))) * np.exp(p.g_y) * K[1:p.T + 1] - (1.0 - p.delta) * K[:p.T]) BI = aggr.get_I(bmat_splus1[:p.T], B[1:p.T + 1], B[:p.T], p, 'TPI') new_borrowing = (D[1:p.T] * (1 + p.g_n[1:p.T]) * np.exp(p.g_y) - D[:p.T - 1]) rc_error = (Y[:p.T - 1] + new_borrowing - (C[:p.T - 1] + BI[:p.T - 1] + G[:p.T - 1]) + (p.hh_r[:p.T - 1] * B[:p.T - 1] - (p.delta + p.firm_r[:p.T - 1]) * K[:p.T - 1] - p.hh_r[:p.T - 1] * D[:p.T - 1])) # Compute total investment (not just domestic) I_total = ((1 + p.g_n[:p.T]) * np.exp(p.g_y) * K[1:p.T + 1] - (1.0 - p.delta) * K[:p.T]) rce_max = np.amax(np.abs(rc_error)) print('Max absolute value resource constraint error:', rce_max) print('Checking time path for violations of constraints.') for t in range(p.T): household.constraint_checker_TPI(b_mat[t], n_mat[t], c_mat[t], t, p.ltilde) eul_savings = euler_errors[:, :p.S, :].max(1).max(1) eul_laborleisure = euler_errors[:, p.S:, :].max(1).max(1) print('Max Euler error, savings: ', eul_savings) print('Max Euler error labor supply: ', eul_laborleisure) ''' ------------------------------------------------------------------------ Save variables/values so they can be used in other modules ------------------------------------------------------------------------ ''' output = { 'Y': Y[:p.T], 'B': B, 'K': K, 'L': L, 'C': C, 'I': I, 'I_total': I_total, 'BQ': BQ, 'total_revenue': total_revenue, 'business_revenue': business_revenue, 'IITpayroll_revenue': T_Ipath, 'T_H': T_H, 'T_P': T_Ppath, 'T_BQ': T_BQpath, 'T_W': T_Wpath, 'T_C': T_Cpath, 'G': G, 'D': D, 'r': r, 'r_gov': r_gov, 'r_hh': r_hh, 'w': w, 'bmat_splus1': bmat_splus1, 'bmat_s': bmat_s[:p.T, :, :], 'n_mat': n_mat[:p.T, :, :], 'c_path': c_mat, 'bq_path': bqmat, 'tax_path': tax_mat, 'eul_savings': eul_savings, 'eul_laborleisure': eul_laborleisure, 'resource_constraint_error': rc_error, 'etr_path': etr_path, 'mtrx_path': mtrx_path, 'mtry_path': mtry_path } tpi_dir = os.path.join(p.output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_vars.pkl") pickle.dump(output, open(tpi_vars, "wb")) if np.any(G) < 0: print('Government spending is negative along transition path' + ' to satisfy budget') if (((TPIiter >= p.maxiter) or (np.absolute(TPIdist) > p.mindist_TPI)) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found' + ' (TPIdist)') if ((np.any(np.absolute(rc_error) >= p.mindist_TPI * 10)) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found ' + '(rc_error)') if ((np.any(np.absolute(eul_savings) >= p.mindist_TPI) or (np.any(np.absolute(eul_laborleisure) > p.mindist_TPI))) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found ' + '(eulers)') return output
def run_TPI(p, client=None): ''' Solve for transition path equilibrium of OG-USA. Args: p (OG-USA Specifications object): model parameters client (Dask client object): client Returns: output (dictionary): dictionary with transition path solution results ''' # unpack tuples of parameters initial_values, ss_vars, theta, baseline_values = get_initial_SS_values(p) (B0, b_sinit, b_splus1init, factor, initial_b, initial_n) =\ initial_values (TRbaseline, Gbaseline, D0_baseline) = baseline_values print('Government spending breakpoints are tG1: ', p.tG1, '; and tG2:', p.tG2) # Initialize guesses at time paths # Make array of initial guesses for labor supply and savings guesses_b = utils.get_initial_path(initial_b, ss_vars['bssmat_splus1'], p, 'ratio') guesses_n = utils.get_initial_path(initial_n, ss_vars['nssmat'], p, 'ratio') b_mat = guesses_b n_mat = guesses_n ind = np.arange(p.S) # Get path for aggregate savings and labor supply` L_init = np.ones((p.T + p.S, )) * ss_vars['Lss'] B_init = np.ones((p.T + p.S, )) * ss_vars['Bss'] L_init[:p.T] = aggr.get_L(n_mat[:p.T], p, 'TPI') B_init[1:p.T] = aggr.get_B(b_mat[:p.T], p, 'TPI', False)[:p.T - 1] B_init[0] = B0 K_init = B_init * ss_vars['Kss'] / ss_vars['Bss'] K = K_init K_d = K_init * ss_vars['K_d_ss'] / ss_vars['Kss'] K_f = K_init * ss_vars['K_f_ss'] / ss_vars['Kss'] L = L_init B = B_init Y = np.zeros_like(K) Y[:p.T] = firm.get_Y(K[:p.T], L[:p.T], p, 'TPI') Y[p.T:] = ss_vars['Yss'] r = np.zeros_like(Y) r[:p.T] = firm.get_r(Y[:p.T], K[:p.T], p, 'TPI') r[p.T:] = ss_vars['rss'] # For case where economy is small open econ r[p.zeta_K == 1] = p.world_int_rate[p.zeta_K == 1] # Compute other interest rates r_gov = fiscal.get_r_gov(r, p) r_hh = aggr.get_r_hh(r, r_gov, K, ss_vars['Dss']) # compute w w = np.zeros_like(r) w[:p.T] = firm.get_w_from_r(r[:p.T], p, 'TPI') w[p.T:] = ss_vars['wss'] # initial guesses at fiscal vars if p.budget_balance: if np.abs(ss_vars['TR_ss']) < 1e-13: TR_ss2 = 0.0 # sometimes SS is very small but not zero, # even if taxes are zero, this get's rid of the # approximation error, which affects the pct changes below else: TR_ss2 = ss_vars['TR_ss'] TR = np.ones(p.T + p.S) * TR_ss2 total_tax_revenue = TR - ss_vars['agg_pension_outlays'] G = np.zeros(p.T + p.S) D = np.zeros(p.T + p.S) D_d = np.zeros(p.T + p.S) D_f = np.zeros(p.T + p.S) else: if p.baseline_spending: TR = TRbaseline G = Gbaseline G[p.T:] = ss_vars['Gss'] else: TR = p.alpha_T * Y G = np.ones(p.T + p.S) * ss_vars['Gss'] D = np.ones(p.T + p.S) * ss_vars['Dss'] D_d = D * ss_vars['D_d_ss'] / ss_vars['Dss'] D_f = D * ss_vars['D_f_ss'] / ss_vars['Dss'] total_tax_revenue = np.ones(p.T + p.S) * ss_vars['total_tax_revenue'] # Initialize bequests BQ0 = aggr.get_BQ(r_hh[0], initial_b, None, p, 'SS', True) if not p.use_zeta: BQ = np.zeros((p.T + p.S, p.J)) for j in range(p.J): BQ[:, j] = (list(np.linspace(BQ0[j], ss_vars['BQss'][j], p.T)) + [ss_vars['BQss'][j]] * p.S) BQ = np.array(BQ) else: BQ = (list(np.linspace(BQ0, ss_vars['BQss'], p.T)) + [ss_vars['BQss']] * p.S) BQ = np.array(BQ) TPIiter = 0 TPIdist = 10 euler_errors = np.zeros((p.T, 2 * p.S, p.J)) TPIdist_vec = np.zeros(p.maxiter) # TPI loop while (TPIiter < p.maxiter) and (TPIdist >= p.mindist_TPI): r_gov[:p.T] = fiscal.get_r_gov(r[:p.T], p) if not p.budget_balance: K[:p.T] = firm.get_K_from_Y(Y[:p.T], r[:p.T], p, 'TPI') r_hh[:p.T] = aggr.get_r_hh(r[:p.T], r_gov[:p.T], K[:p.T], D[:p.T]) outer_loop_vars = (r, w, r_hh, BQ, TR, theta) euler_errors = np.zeros((p.T, 2 * p.S, p.J)) lazy_values = [] for j in range(p.J): guesses = (guesses_b[:, :, j], guesses_n[:, :, j]) lazy_values.append( delayed(inner_loop)(guesses, outer_loop_vars, initial_values, j, ind, p)) if client: futures = client.compute(lazy_values, num_workers=p.num_workers) results = client.gather(futures) else: results = results = compute(*lazy_values, scheduler=dask.multiprocessing.get, num_workers=p.num_workers) for j, result in enumerate(results): euler_errors[:, :, j], b_mat[:, :, j], n_mat[:, :, j] = result bmat_s = np.zeros((p.T, p.S, p.J)) bmat_s[0, 1:, :] = initial_b[:-1, :] bmat_s[1:, 1:, :] = b_mat[:p.T - 1, :-1, :] bmat_splus1 = np.zeros((p.T, p.S, p.J)) bmat_splus1[:, :, :] = b_mat[:p.T, :, :] etr_params_4D = np.tile( p.etr_params.reshape(p.T, p.S, 1, p.etr_params.shape[2]), (1, 1, p.J, 1)) bqmat = household.get_bq(BQ, None, p, 'TPI') trmat = household.get_tr(TR, None, p, 'TPI') tax_mat = tax.net_taxes(r_hh[:p.T], w[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat[:p.T, :, :], factor, trmat[:p.T, :, :], theta, 0, None, False, 'TPI', p.e, etr_params_4D, p) r_hh_path = utils.to_timepath_shape(r_hh) wpath = utils.to_timepath_shape(w) c_mat = household.get_cons(r_hh_path[:p.T, :, :], wpath[:p.T, :, :], bmat_s, bmat_splus1, n_mat[:p.T, :, :], bqmat[:p.T, :, :], tax_mat, p.e, p.tau_c[:p.T, :, :], p) y_before_tax_mat = household.get_y(r_hh_path[:p.T, :, :], wpath[:p.T, :, :], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], p) (total_tax_rev, iit_payroll_tax_revenue, agg_pension_outlays, bequest_tax_revenue, wealth_tax_revenue, cons_tax_revenue, business_tax_revenue, payroll_tax_revenue, iit_revenue) = aggr.revenue(r_hh[:p.T], w[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat[:p.T, :, :], c_mat[:p.T, :, :], Y[:p.T], L[:p.T], K[:p.T], factor, theta, etr_params_4D, p, 'TPI') total_tax_revenue[:p.T] = total_tax_rev dg_fixed_values = (Y, total_tax_revenue, agg_pension_outlays, TR, Gbaseline, D0_baseline) (Dnew, G[:p.T], D_d[:p.T], D_f[:p.T], new_borrowing, debt_service, new_borrowing_f) =\ fiscal.D_G_path(r_gov, dg_fixed_values, p) L[:p.T] = aggr.get_L(n_mat[:p.T], p, 'TPI') B[1:p.T] = aggr.get_B(bmat_splus1[:p.T], p, 'TPI', False)[:p.T - 1] K_demand_open = firm.get_K(L[:p.T], p.world_int_rate[:p.T], p, 'TPI') K[:p.T], K_d[:p.T], K_f[:p.T] = aggr.get_K_splits( B[:p.T], K_demand_open, D_d[:p.T], p.zeta_K[:p.T]) Ynew = firm.get_Y(K[:p.T], L[:p.T], p, 'TPI') rnew = r.copy() rnew[:p.T] = firm.get_r(Ynew[:p.T], K[:p.T], p, 'TPI') # For case where economy is small open econ r[p.zeta_K == 1] = p.world_int_rate[p.zeta_K == 1] r_gov_new = fiscal.get_r_gov(rnew, p) r_hh_new = aggr.get_r_hh(rnew[:p.T], r_gov_new[:p.T], K[:p.T], Dnew[:p.T]) # compute w wnew = firm.get_w_from_r(rnew[:p.T], p, 'TPI') b_mat_shift = np.append(np.reshape(initial_b, (1, p.S, p.J)), b_mat[:p.T - 1, :, :], axis=0) BQnew = aggr.get_BQ(r_hh_new[:p.T], b_mat_shift, None, p, 'TPI', False) bqmat_new = household.get_bq(BQnew, None, p, 'TPI') (total_tax_rev, iit_payroll_tax_revenue, agg_pension_outlays, bequest_tax_revenue, wealth_tax_revenue, cons_tax_revenue, business_tax_revenue, payroll_tax_revenue, iit_revenue) = aggr.revenue(r_hh_new[:p.T], wnew[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat_new[:p.T, :, :], c_mat[:p.T, :, :], Ynew[:p.T], L[:p.T], K[:p.T], factor, theta, etr_params_4D, p, 'TPI') total_tax_revenue[:p.T] = total_tax_rev TR_new = fiscal.get_TR(Ynew[:p.T], TR[:p.T], G[:p.T], total_tax_revenue[:p.T], agg_pension_outlays[:p.T], p, 'TPI') # update vars for next iteration w[:p.T] = wnew[:p.T] r[:p.T] = utils.convex_combo(rnew[:p.T], r[:p.T], p.nu) BQ[:p.T] = utils.convex_combo(BQnew[:p.T], BQ[:p.T], p.nu) D[:p.T] = Dnew[:p.T] Y[:p.T] = utils.convex_combo(Ynew[:p.T], Y[:p.T], p.nu) if not p.baseline_spending: TR[:p.T] = utils.convex_combo(TR_new[:p.T], TR[:p.T], p.nu) guesses_b = utils.convex_combo(b_mat, guesses_b, p.nu) guesses_n = utils.convex_combo(n_mat, guesses_n, p.nu) print('r diff: ', (rnew[:p.T] - r[:p.T]).max(), (rnew[:p.T] - r[:p.T]).min()) print('BQ diff: ', (BQnew[:p.T] - BQ[:p.T]).max(), (BQnew[:p.T] - BQ[:p.T]).min()) print('TR diff: ', (TR_new[:p.T] - TR[:p.T]).max(), (TR_new[:p.T] - TR[:p.T]).min()) print('Y diff: ', (Ynew[:p.T] - Y[:p.T]).max(), (Ynew[:p.T] - Y[:p.T]).min()) if not p.baseline_spending: if TR.all() != 0: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list( utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(utils.pct_diff_func(TR_new[:p.T], TR[:p.T]))).max() else: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list( utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(np.abs(TR[:p.T]))).max() else: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list(utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(utils.pct_diff_func(Ynew[:p.T], Y[:p.T]))).max() TPIdist_vec[TPIiter] = TPIdist # After T=10, if cycling occurs, drop the value of nu # wait til after T=10 or so, because sometimes there is a jump up # in the first couple iterations # if TPIiter > 10: # if TPIdist_vec[TPIiter] - TPIdist_vec[TPIiter - 1] > 0: # nu /= 2 # print 'New Value of nu:', nu TPIiter += 1 print('Iteration:', TPIiter) print('\tDistance:', TPIdist) # Compute effective and marginal tax rates for all agents mtrx_params_4D = np.tile( p.mtrx_params.reshape(p.T, p.S, 1, p.mtrx_params.shape[2]), (1, 1, p.J, 1)) mtry_params_4D = np.tile( p.mtry_params.reshape(p.T, p.S, 1, p.mtry_params.shape[2]), (1, 1, p.J, 1)) e_3D = np.tile(p.e.reshape(1, p.S, p.J), (p.T, 1, 1)) mtry_path = tax.MTR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, True, e_3D, etr_params_4D, mtry_params_4D, p) mtrx_path = tax.MTR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, False, e_3D, etr_params_4D, mtrx_params_4D, p) etr_path = tax.ETR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, e_3D, etr_params_4D, p) C = aggr.get_C(c_mat, p, 'TPI') # Note that implicity in this computation is that immigrants' # wealth is all in the form of private capital I_d = aggr.get_I(bmat_splus1[:p.T], K_d[1:p.T + 1], K_d[:p.T], p, 'TPI') I = aggr.get_I(bmat_splus1[:p.T], K[1:p.T + 1], K[:p.T], p, 'TPI') # solve resource constraint # foreign debt service costs debt_service_f = fiscal.get_debt_service_f(r_hh, D_f) RC_error = aggr.resource_constraint(Y[:p.T - 1], C[:p.T - 1], G[:p.T - 1], I_d[:p.T - 1], K_f[:p.T - 1], new_borrowing_f[:p.T - 1], debt_service_f[:p.T - 1], r_hh[:p.T - 1], p) # Compute total investment (not just domestic) I_total = aggr.get_I(None, K[1:p.T + 1], K[:p.T], p, 'total_tpi') # Compute resource constraint error rce_max = np.amax(np.abs(RC_error)) print('Max absolute value resource constraint error:', rce_max) print('Checking time path for violations of constraints.') for t in range(p.T): household.constraint_checker_TPI(b_mat[t], n_mat[t], c_mat[t], t, p.ltilde) eul_savings = euler_errors[:, :p.S, :].max(1).max(1) eul_laborleisure = euler_errors[:, p.S:, :].max(1).max(1) print('Max Euler error, savings: ', eul_savings) print('Max Euler error labor supply: ', eul_laborleisure) ''' ------------------------------------------------------------------------ Save variables/values so they can be used in other modules ------------------------------------------------------------------------ ''' output = { 'Y': Y[:p.T], 'B': B, 'K': K, 'K_f': K_f, 'K_d': K_d, 'L': L, 'C': C, 'I': I, 'I_total': I_total, 'I_d': I_d, 'BQ': BQ, 'total_tax_revenue': total_tax_revenue, 'business_tax_revenue': business_tax_revenue, 'iit_payroll_tax_revenue': iit_payroll_tax_revenue, 'iit_revenue': iit_revenue, 'payroll_tax_revenue': payroll_tax_revenue, 'TR': TR, 'agg_pension_outlays': agg_pension_outlays, 'bequest_tax_revenue': bequest_tax_revenue, 'wealth_tax_revenue': wealth_tax_revenue, 'cons_tax_revenue': cons_tax_revenue, 'G': G, 'D': D, 'D_f': D_f, 'D_d': D_d, 'r': r, 'r_gov': r_gov, 'r_hh': r_hh, 'w': w, 'bmat_splus1': bmat_splus1, 'bmat_s': bmat_s[:p.T, :, :], 'n_mat': n_mat[:p.T, :, :], 'c_path': c_mat, 'bq_path': bqmat, 'tr_path': trmat, 'y_before_tax_mat': y_before_tax_mat, 'tax_path': tax_mat, 'eul_savings': eul_savings, 'eul_laborleisure': eul_laborleisure, 'resource_constraint_error': RC_error, 'new_borrowing_f': new_borrowing_f, 'debt_service_f': debt_service_f, 'etr_path': etr_path, 'mtrx_path': mtrx_path, 'mtry_path': mtry_path } tpi_dir = os.path.join(p.output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_vars.pkl") with open(tpi_vars, "wb") as f: pickle.dump(output, f) if np.any(G) < 0: print('Government spending is negative along transition path' + ' to satisfy budget') if (((TPIiter >= p.maxiter) or (np.absolute(TPIdist) > p.mindist_TPI)) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found' + ' (TPIdist)') if ((np.any(np.absolute(RC_error) >= p.mindist_TPI * 10)) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found ' + '(RC_error)') if ((np.any(np.absolute(eul_savings) >= p.mindist_TPI) or (np.any(np.absolute(eul_laborleisure) > p.mindist_TPI))) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found ' + '(eulers)') return output
def run_TPI(p, client=None): # unpack tuples of parameters initial_values, SS_values, baseline_values = get_initial_SS_values(p) (B0, b_sinit, b_splus1init, factor, initial_b, initial_n, D0) = initial_values (Kss, Bss, Lss, rss, wss, BQss, T_Hss, total_revenue_ss, bssmat_splus1, nssmat, Yss, Gss, theta) = SS_values (T_Hbaseline, Gbaseline) = baseline_values print('Government spending breakpoints are tG1: ', p.tG1, '; and tG2:', p.tG2) # Initialize guesses at time paths # Make array of initial guesses for labor supply and savings domain = np.linspace(0, p.T, p.T) domain2 = np.tile(domain.reshape(p.T, 1, 1), (1, p.S, p.J)) ending_b = bssmat_splus1 guesses_b = (-1 / (domain2 + 1)) * (ending_b - initial_b) + ending_b ending_b_tail = np.tile(ending_b.reshape(1, p.S, p.J), (p.S, 1, 1)) guesses_b = np.append(guesses_b, ending_b_tail, axis=0) domain3 = np.tile(np.linspace(0, 1, p.T).reshape(p.T, 1, 1), (1, p.S, p.J)) guesses_n = domain3 * (nssmat - initial_n) + initial_n ending_n_tail = np.tile(nssmat.reshape(1, p.S, p.J), (p.S, 1, 1)) guesses_n = np.append(guesses_n, ending_n_tail, axis=0) b_mat = guesses_b # np.zeros((p.T + p.S, p.S, p.J)) n_mat = guesses_n # np.zeros((p.T + p.S, p.S, p.J)) ind = np.arange(p.S) L_init = np.ones((p.T + p.S,)) * Lss B_init = np.ones((p.T + p.S,)) * Bss L_init[:p.T] = aggr.get_L(n_mat[:p.T], p, 'TPI') B_init[1:p.T] = aggr.get_K(b_mat[:p.T], p, 'TPI', False)[:p.T - 1] B_init[0] = B0 if not p.small_open: if p.budget_balance: K_init = B_init else: K_init = B_init * Kss / Bss else: K_init = firm.get_K(L_init, p.firm_r, p, 'TPI') K = K_init L = L_init B = B_init Y = np.zeros_like(K) Y[:p.T] = firm.get_Y(K[:p.T], L[:p.T], p, 'TPI') Y[p.T:] = Yss r = np.zeros_like(Y) if not p.small_open: r[:p.T] = firm.get_r(Y[:p.T], K[:p.T], p, 'TPI') r[p.T:] = rss else: r = p.firm_r # compute w w = np.zeros_like(r) w[:p.T] = firm.get_w_from_r(r[:p.T], p, 'TPI') w[p.T:] = wss r_gov = fiscal.get_r_gov(r, p) if p.budget_balance: r_hh = r else: r_hh = aggr.get_r_hh(r, r_gov, K, p.debt_ratio_ss * Y) if p.small_open: r_hh = p.hh_r BQ0 = aggr.get_BQ(r[0], initial_b, None, p, 'SS', True) if not p.use_zeta: BQ = np.zeros((p.T + p.S, p.J)) for j in range(p.J): BQ[:, j] = (list(np.linspace(BQ0[j], BQss[j], p.T)) + [BQss[j]] * p.S) BQ = np.array(BQ) else: BQ = (list(np.linspace(BQ0, BQss, p.T)) + [BQss] * p.S) BQ = np.array(BQ) if p.budget_balance: if np.abs(T_Hss) < 1e-13: T_Hss2 = 0.0 # sometimes SS is very small but not zero, # even if taxes are zero, this get's rid of the approximation # error, which affects the perc changes below else: T_Hss2 = T_Hss T_H = np.ones(p.T + p.S) * T_Hss2 total_revenue = T_H G = np.zeros(p.T + p.S) elif not p.baseline_spending: T_H = p.alpha_T * Y elif p.baseline_spending: T_H = T_Hbaseline T_H_new = p.T_H # Need to set T_H_new for later reference G = Gbaseline G_0 = Gbaseline[0] # Initialize some starting value if p.budget_balance: D = 0.0 * Y else: D = p.debt_ratio_ss * Y TPIiter = 0 TPIdist = 10 euler_errors = np.zeros((p.T, 2 * p.S, p.J)) TPIdist_vec = np.zeros(p.maxiter) print('analytical mtrs in tpi = ', p.analytical_mtrs) print('tax function type in tpi = ', p.tax_func_type) # TPI loop while (TPIiter < p.maxiter) and (TPIdist >= p.mindist_TPI): r_gov[:p.T] = fiscal.get_r_gov(r[:p.T], p) if p.budget_balance: r_hh[:p.T] = r[:p.T] else: K[:p.T] = firm.get_K_from_Y(Y[:p.T], r[:p.T], p, 'TPI') r_hh[:p.T] = aggr.get_r_hh(r[:p.T], r_gov[:p.T], K[:p.T], D[:p.T]) if p.small_open: r_hh[:p.T] = p.hh_r[:p.T] outer_loop_vars = (r, w, r_hh, BQ, T_H, theta) euler_errors = np.zeros((p.T, 2 * p.S, p.J)) lazy_values = [] for j in range(p.J): guesses = (guesses_b[:, :, j], guesses_n[:, :, j]) lazy_values.append( delayed(inner_loop)(guesses, outer_loop_vars, initial_values, j, ind, p)) results = compute(*lazy_values, scheduler=dask.multiprocessing.get, num_workers=p.num_workers) for j, result in enumerate(results): euler_errors[:, :, j], b_mat[:, :, j], n_mat[:, :, j] = result bmat_s = np.zeros((p.T, p.S, p.J)) bmat_s[0, 1:, :] = initial_b[:-1, :] bmat_s[1:, 1:, :] = b_mat[:p.T-1, :-1, :] bmat_splus1 = np.zeros((p.T, p.S, p.J)) bmat_splus1[:, :, :] = b_mat[:p.T, :, :] L[:p.T] = aggr.get_L(n_mat[:p.T], p, 'TPI') B[1:p.T] = aggr.get_K(bmat_splus1[:p.T], p, 'TPI', False)[:p.T - 1] if np.any(B) < 0: print('B has negative elements. B[0:9]:', B[0:9]) print('B[T-2:T]:', B[p.T - 2, p.T]) etr_params_4D = np.tile( p.etr_params.reshape(p.T, p.S, 1, p.etr_params.shape[2]), (1, 1, p.J, 1)) bqmat = household.get_bq(BQ, None, p, 'TPI') tax_mat = tax.total_taxes(r_hh[:p.T], w[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat[:p.T, :, :], factor, T_H[:p.T], theta, 0, None, False, 'TPI', p.e, etr_params_4D, p) r_hh_path = utils.to_timepath_shape(r_hh, p) wpath = utils.to_timepath_shape(w, p) c_mat = household.get_cons(r_hh_path[:p.T, :, :], wpath[:p.T, :, :], bmat_s, bmat_splus1, n_mat[:p.T, :, :], bqmat[:p.T, :, :], tax_mat, p.e, p.tau_c[:p.T, :, :], p) if not p.small_open: if p.budget_balance: K[:p.T] = B[:p.T] else: if not p.baseline_spending: Y = T_H / p.alpha_T # maybe unecessary (total_rev, T_Ipath, T_Ppath, T_BQpath, T_Wpath, T_Cpath, business_revenue) = aggr.revenue( r_hh[:p.T], w[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat[:p.T, :, :], c_mat[:p.T, :, :], Y[:p.T], L[:p.T], K[:p.T], factor, theta, etr_params_4D, p, 'TPI') total_revenue = np.array(list(total_rev) + [total_revenue_ss] * p.S) # set intial debt value if p.baseline: D_0 = p.initial_debt_ratio * Y[0] else: D_0 = D0 if not p.baseline_spending: G_0 = p.alpha_G[0] * Y[0] dg_fixed_values = (Y, total_revenue, T_H, D_0, G_0) Dnew, G = fiscal.D_G_path(r_gov, dg_fixed_values, Gbaseline, p) K[:p.T] = B[:p.T] - Dnew[:p.T] if np.any(K < 0): print('K has negative elements. Setting them ' + 'positive to prevent NAN.') K[:p.T] = np.fmax(K[:p.T], 0.05 * B[:p.T]) else: K[:p.T] = firm.get_K(L[:p.T], p.firm_r[:p.T], p, 'TPI') Ynew = firm.get_Y(K[:p.T], L[:p.T], p, 'TPI') if not p.small_open: rnew = firm.get_r(Ynew[:p.T], K[:p.T], p, 'TPI') else: rnew = r.copy() r_gov_new = fiscal.get_r_gov(rnew, p) if p.budget_balance: r_hh_new = rnew[:p.T] else: r_hh_new = aggr.get_r_hh(rnew, r_gov_new, K[:p.T], Dnew[:p.T]) if p.small_open: r_hh_new = p.hh_r[:p.T] # compute w wnew = firm.get_w_from_r(rnew[:p.T], p, 'TPI') b_mat_shift = np.append(np.reshape(initial_b, (1, p.S, p.J)), b_mat[:p.T - 1, :, :], axis=0) BQnew = aggr.get_BQ(r_hh_new[:p.T], b_mat_shift, None, p, 'TPI', False) bqmat_new = household.get_bq(BQnew, None, p, 'TPI') (total_rev, T_Ipath, T_Ppath, T_BQpath, T_Wpath, T_Cpath, business_revenue) = aggr.revenue( r_hh_new[:p.T], wnew[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat_new[:p.T, :, :], c_mat[:p.T, :, :], Ynew[:p.T], L[:p.T], K[:p.T], factor, theta, etr_params_4D, p, 'TPI') total_revenue = np.array(list(total_rev) + [total_revenue_ss] * p.S) if p.budget_balance: T_H_new = total_revenue elif not p.baseline_spending: T_H_new = p.alpha_T[:p.T] * Ynew[:p.T] # If baseline_spending==True, no need to update T_H, it's fixed if p.small_open and not p.budget_balance: # Loop through years to calculate debt and gov't spending. # This is done earlier when small_open=False. if p.baseline: D_0 = p.initial_debt_ratio * Y[0] else: D_0 = D0 if not p.baseline_spending: G_0 = p.alpha_G[0] * Ynew[0] dg_fixed_values = (Ynew, total_revenue, T_H, D_0, G_0) Dnew, G = fiscal.D_G_path(r_gov_new, dg_fixed_values, Gbaseline, p) if p.budget_balance: Dnew = D w[:p.T] = wnew[:p.T] r[:p.T] = utils.convex_combo(rnew[:p.T], r[:p.T], p.nu) BQ[:p.T] = utils.convex_combo(BQnew[:p.T], BQ[:p.T], p.nu) D = Dnew Y[:p.T] = utils.convex_combo(Ynew[:p.T], Y[:p.T], p.nu) if not p.baseline_spending: T_H[:p.T] = utils.convex_combo(T_H_new[:p.T], T_H[:p.T], p.nu) guesses_b = utils.convex_combo(b_mat, guesses_b, p.nu) guesses_n = utils.convex_combo(n_mat, guesses_n, p.nu) print('r diff: ', (rnew[:p.T] - r[:p.T]).max(), (rnew[:p.T] - r[:p.T]).min()) print('BQ diff: ', (BQnew[:p.T] - BQ[:p.T]).max(), (BQnew[:p.T] - BQ[:p.T]).min()) print('T_H diff: ', (T_H_new[:p.T]-T_H[:p.T]).max(), (T_H_new[:p.T] - T_H[:p.T]).min()) print('Y diff: ', (Ynew[:p.T]-Y[:p.T]).max(), (Ynew[:p.T] - Y[:p.T]).min()) if not p.baseline_spending: if T_H.all() != 0: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list(utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(utils.pct_diff_func(wnew[:p.T], w[:p.T])) + list(utils.pct_diff_func(T_H_new[:p.T], T_H[:p.T]))).max() else: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list(utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(utils.pct_diff_func(wnew[:p.T], w[:p.T])) + list(np.abs(T_H[:p.T]))).max() else: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list(utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(utils.pct_diff_func(wnew[:p.T], w[:p.T])) + list(utils.pct_diff_func(Ynew[:p.T], Y[:p.T]))).max() TPIdist_vec[TPIiter] = TPIdist # After T=10, if cycling occurs, drop the value of nu # wait til after T=10 or so, because sometimes there is a jump up # in the first couple iterations # if TPIiter > 10: # if TPIdist_vec[TPIiter] - TPIdist_vec[TPIiter - 1] > 0: # nu /= 2 # print 'New Value of nu:', nu TPIiter += 1 print('Iteration:', TPIiter) print('\tDistance:', TPIdist) # Compute effective and marginal tax rates for all agents mtrx_params_4D = np.tile( p.mtrx_params.reshape(p.T, p.S, 1, p.mtrx_params.shape[2]), (1, 1, p.J, 1)) mtry_params_4D = np.tile( p.mtry_params.reshape(p.T, p.S, 1, p.mtry_params.shape[2]), (1, 1, p.J, 1)) e_3D = np.tile(p.e.reshape(1, p.S, p.J), (p.T, 1, 1)) mtry_path = tax.MTR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, True, e_3D, etr_params_4D, mtry_params_4D, p) mtrx_path = tax.MTR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, False, e_3D, etr_params_4D, mtrx_params_4D, p) etr_path = tax.ETR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, e_3D, etr_params_4D, p) C = aggr.get_C(c_mat, p, 'TPI') if not p.small_open: I = aggr.get_I(bmat_splus1[:p.T], K[1:p.T + 1], K[:p.T], p, 'TPI') rc_error = Y[:p.T] - C[:p.T] - I[:p.T] - G[:p.T] else: I = ((1 + np.squeeze(np.hstack((p.g_n[1:p.T], p.g_n_ss)))) * np.exp(p.g_y) * K[1:p.T + 1] - (1.0 - p.delta) * K[:p.T]) BI = aggr.get_I(bmat_splus1[:p.T], B[1:p.T + 1], B[:p.T], p, 'TPI') new_borrowing = (D[1:p.T] * (1 + p.g_n[1:p.T]) * np.exp(p.g_y) - D[:p.T - 1]) rc_error = (Y[:p.T - 1] + new_borrowing - ( C[:p.T - 1] + BI[:p.T - 1] + G[:p.T - 1]) + (p.hh_r[:p.T - 1] * B[:p.T - 1] - ( p.delta + p.firm_r[:p.T - 1]) * K[:p.T - 1] - p.hh_r[:p.T - 1] * D[:p.T - 1])) # Compute total investment (not just domestic) I_total = ((1 + p.g_n[:p.T]) * np.exp(p.g_y) * K[1:p.T + 1] - (1.0 - p.delta) * K[:p.T]) rce_max = np.amax(np.abs(rc_error)) print('Max absolute value resource constraint error:', rce_max) print('Checking time path for violations of constraints.') for t in range(p.T): household.constraint_checker_TPI( b_mat[t], n_mat[t], c_mat[t], t, p.ltilde) eul_savings = euler_errors[:, :p.S, :].max(1).max(1) eul_laborleisure = euler_errors[:, p.S:, :].max(1).max(1) print('Max Euler error, savings: ', eul_savings) print('Max Euler error labor supply: ', eul_laborleisure) ''' ------------------------------------------------------------------------ Save variables/values so they can be used in other modules ------------------------------------------------------------------------ ''' output = {'Y': Y[:p.T], 'B': B, 'K': K, 'L': L, 'C': C, 'I': I, 'I_total': I_total, 'BQ': BQ, 'total_revenue': total_revenue, 'business_revenue': business_revenue, 'IITpayroll_revenue': T_Ipath, 'T_H': T_H, 'T_P': T_Ppath, 'T_BQ': T_BQpath, 'T_W': T_Wpath, 'T_C': T_Cpath, 'G': G, 'D': D, 'r': r, 'r_gov': r_gov, 'r_hh': r_hh, 'w': w, 'bmat_splus1': bmat_splus1, 'bmat_s': bmat_s[:p.T, :, :], 'n_mat': n_mat[:p.T, :, :], 'c_path': c_mat, 'bq_path': bqmat, 'tax_path': tax_mat, 'eul_savings': eul_savings, 'eul_laborleisure': eul_laborleisure, 'resource_constraint_error': rc_error, 'etr_path': etr_path, 'mtrx_path': mtrx_path, 'mtry_path': mtry_path} tpi_dir = os.path.join(p.output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_vars.pkl") pickle.dump(output, open(tpi_vars, "wb")) if np.any(G) < 0: print('Government spending is negative along transition path' + ' to satisfy budget') if (((TPIiter >= p.maxiter) or (np.absolute(TPIdist) > p.mindist_TPI)) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found' + ' (TPIdist)') if ((np.any(np.absolute(rc_error) >= p.mindist_TPI * 10)) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found ' + '(rc_error)') if ((np.any(np.absolute(eul_savings) >= p.mindist_TPI) or (np.any(np.absolute(eul_laborleisure) > p.mindist_TPI))) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found ' + '(eulers)') return output
def runner_SS(output_base, baseline_dir, baseline=False, analytical_mtrs=True, age_specific=False, reform=0, fix_transfers=False, user_params={}, guid='', calibrate_model=False, run_micro=True): from ogusa import parameters, demographics, income, utils tick = time.time() #Create output directory structure saved_moments_dir = os.path.join(output_base, "Saved_moments") ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [saved_moments_dir, ss_dir, tpi_dir] for _dir in dirs: try: print "making dir: ", _dir os.makedirs(_dir) except OSError as oe: pass print("in runner, baseline is ", baseline) run_params = ogusa.parameters.get_parameters(baseline=baseline, reform=reform, guid=guid, user_modifiable=True) run_params['analytical_mtrs'] = analytical_mtrs # Modify ogusa parameters based on user input if 'frisch' in user_params: print "updating fricsh and associated" b_ellipse, upsilon = ogusa.elliptical_u_est.estimation( user_params['frisch'], run_params['ltilde']) run_params['b_ellipse'] = b_ellipse run_params['upsilon'] = upsilon run_params.update(user_params) # Modify ogusa parameters based on user input if 'sigma' in user_params: print "updating sigma" run_params['sigma'] = user_params['sigma'] run_params.update(user_params) from ogusa import SS, TPI, SS_alt # List of parameter names that will not be changing (unless we decide to # change them for a tax experiment) param_names = [ 'S', 'J', 'T', 'BW', 'lambdas', 'starting_age', 'ending_age', 'beta', 'sigma', 'alpha', 'nu', 'Z', 'delta', 'E', 'ltilde', 'g_y', 'maxiter', 'mindist_SS', 'mindist_TPI', 'analytical_mtrs', 'b_ellipse', 'k_ellipse', 'upsilon', 'chi_b_guess', 'chi_n_guess', 'etr_params', 'mtrx_params', 'mtry_params', 'tau_payroll', 'tau_bq', 'retire', 'mean_income_data', 'g_n_vector', 'h_wealth', 'p_wealth', 'm_wealth', 'omega', 'g_n_ss', 'omega_SS', 'surv_rate', 'imm_rates', 'e', 'rho', 'omega_S_preTP' ] sim_params = {} for key in param_names: sim_params[key] = run_params[key] sim_params['output_dir'] = output_base sim_params['run_params'] = run_params ''' ------------------------------------------------------------------------ If using income tax reform, need to determine parameters that yield same SS revenue as the wealth tax reform. ------------------------------------------------------------------------ ''' if reform == 1: income_tax_params, ss_params, iterative_params, chi_params = SS.create_steady_state_parameters( **sim_params) # find SS revenue from wealth tax reform reform3_ss_dir = os.path.join( "./OUTPUT_WEALTH_REFORM" + '/sigma' + str(run_params['sigma']), "SS/SS_vars.pkl") reform3_ss_solutions = pickle.load(open(reform3_ss_dir, "rb")) receipts_to_match = reform3_ss_solutions[ 'T_Hss'] + reform3_ss_solutions['Gss'] # create function to match SS revenue def matcher(d_guess, params): income_tax_params, receipts_to_match, ss_params, iterative_params,\ chi_params, baseline, baseline_dir = params analytical_mtrs, etr_params, mtrx_params, mtry_params = income_tax_params etr_params[:, 3] = d_guess mtrx_params[:, 3] = d_guess mtry_params[:, 3] = d_guess income_tax_params = analytical_mtrs, etr_params, mtrx_params, mtry_params ss_outputs = SS.run_SS(income_tax_params, ss_params, iterative_params, chi_params, baseline, fix_transfers=fix_transfers, baseline_dir=baseline_dir) receipts_new = ss_outputs['T_Hss'] + ss_outputs['Gss'] error = abs(receipts_to_match - receipts_new) if d_guess <= 0: error = 1e14 print 'Error in taxes:', error return error print 'Computing new income tax to match wealth tax' # d_guess= .452 # initial guess 0.452 works for sigma = 2, frisch 1.5 # new_d_inc = d_guess # import scipy.optimize as opt # params = [income_tax_params, receipts_to_match, ss_params, iterative_params, # chi_params, baseline, baseline_dir] # new_d_inc = opt.fsolve(matcher, d_guess, args=params, xtol=1e-8) # print '\tOld income tax:', d_guess # print '\tNew income tax:', new_d_inc def samesign(a, b): return a * b > 0 def bisect_method(func, params, low, high): 'Find root of continuous function where f(low) and f(high) have opposite signs' #assert not samesign(func(params,low), func(params,high)) for i in range(54): midpoint = (low + high) / 2.0 if samesign(func(params, low), func(params, midpoint)): low = midpoint else: high = midpoint return midpoint def solve_model(params, d): income_tax_params, ss_params, iterative_params,\ chi_params, baseline ,baseline_dir = params analytical_mtrs, etr_params, mtrx_params, mtry_params = income_tax_params etr_params[:, 3] = d mtrx_params[:, 3] = d mtry_params[:, 3] = d income_tax_params = analytical_mtrs, etr_params, mtrx_params, mtry_params ss_outputs = SS.run_SS(income_tax_params, ss_params, iterative_params, chi_params, baseline, fix_transfers=fix_transfers, baseline_dir=baseline_dir) ss_dir = os.path.join("./OUTPUT_INCOME_REFORM/sigma2.0", "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) receipts_new = ss_outputs['T_Hss'] + ss_outputs['Gss'] new_error = receipts_to_match - receipts_new print 'Error in taxes:', error print 'New income tax:', d return new_error # print 'Computing new income tax to match wealth tax' # d_guess= 0.5025 # initial guess # # income_tax_params, receipts_to_match, ss_params, iterative_params,\ # # chi_params, baseline, baseline_dir = params # analytical_mtrs, etr_params, mtrx_params, mtry_params = income_tax_params # etr_params[:,3] = d_guess # mtrx_params[:,3] = d_guess # mtry_params[:,3] = d_guess # income_tax_params = analytical_mtrs, etr_params, mtrx_params, mtry_params # ss_outputs = SS.run_SS(income_tax_params, ss_params, iterative_params, # chi_params, baseline, fix_transfers=fix_transfers, # baseline_dir=baseline_dir) # ss_dir = os.path.join("./OUTPUT_INCOME_REFORM/sigma2.0", "SS/SS_vars.pkl") # pickle.dump(ss_outputs, open(ss_dir, "wb")) # receipts_new = ss_outputs['T_Hss'] + ss_outputs['Gss'] # error = receipts_to_match - receipts_new # new_error = error # print "ERROR: ", error # max_loop_iter = 1 # output_list = np.zeros((max_loop_iter,3)) # loop_iter = 0 # bisect = 0 # d_guess_old = d_guess # # while np.abs(new_error) > 1e-8 and loop_iter < max_loop_iter: # while loop_iter < max_loop_iter: # # if new_error > 0 and new_error > 0 and bisect == 0: # # d_guess_old = d_guess # # d_guess+=0.001 # # elif new_error < 0 and new_error < 0 and bisect == 0: # # d_guess_old = d_guess # # d_guess-=0.001 # # d_guess = max(0.0,d_guess) # constrain so not negative # # else: # # bisect = 1 # # print 'Entering bisection method' # # params = income_tax_params, ss_params, iterative_params,\ # # chi_params, baseline ,baseline_dir # # high = max(d_guess,d_guess_old) # # low = min(d_guess,d_guess_old) # # d_guess = bisect_method(solve_model, params, low, high) # # loop_iter = max_loop_iter # d_guess_old = d_guess # d_guess+=0.0005 # # error = new_error # etr_params[:,3] = d_guess # mtrx_params[:,3] = d_guess # mtry_params[:,3] = d_guess # income_tax_params = analytical_mtrs, etr_params, mtrx_params, mtry_params # print 'now here$$$' # ss_outputs = SS.run_SS(income_tax_params, ss_params, iterative_params, # chi_params, baseline, fix_transfers=fix_transfers, # baseline_dir=baseline_dir) # ss_dir = os.path.join("./OUTPUT_INCOME_REFORM/sigma2.0", "SS/SS_vars.pkl") # pickle.dump(ss_outputs, open(ss_dir, "wb")) # receipts_new = ss_outputs['T_Hss'] + ss_outputs['Gss'] # new_error = (receipts_to_match - receipts_new) # print "ERROR: ", new_error # output_list[loop_iter,0]=new_error # output_list[loop_iter,1]=d_guess # output_list[loop_iter,2]=ss_outputs['Yss']-ss_outputs['Iss']-ss_outputs['Css']-ss_outputs['Gss'] # np.savetxt('inc_tax_out.csv',output_list, delimiter=",") # pickle.dump(output_list, open("output_list.pkl", "wb")) # print 'Error in taxes:', error # print 'Old income tax:', d_guess_old # print 'New income tax:', d_guess # print 'iteration: ', loop_iter # loop_iter += 1 analytical_mtrs, etr_params, mtrx_params, mtry_params = income_tax_params new_d_inc = 0.5025 # this is 0.453 if fix_transfers=False, 0.503 if True etr_params[:, 3] = new_d_inc mtrx_params[:, 3] = new_d_inc mtry_params[:, 3] = new_d_inc sim_params['etr_params'] = np.tile( np.reshape(etr_params, (run_params['S'], 1, etr_params.shape[1])), (1, run_params['BW'], 1)) sim_params['mtrx_params'] = np.tile( np.reshape(mtrx_params, (run_params['S'], 1, mtrx_params.shape[1])), (1, run_params['BW'], 1)) sim_params['mtry_params'] = np.tile( np.reshape(mtry_params, (run_params['S'], 1, mtry_params.shape[1])), (1, run_params['BW'], 1)) ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' income_tax_params, ss_params, iterative_params, chi_params = SS.create_steady_state_parameters( **sim_params) analytical_mtrs, etr_params, mtrx_params, mtry_params = income_tax_params ''' **** CALL CALIBRATION here if boolean flagged **** ''' if calibrate_model: chi_params = calibrate.chi_estimate(income_tax_params, ss_params, iterative_params, chi_params, baseline_dir=baseline_dir) # ss_outputs = SS_alt.run_SS(income_tax_params, ss_params, iterative_params, # chi_params, baseline, baseline_dir=baseline_dir) print 'Fix transfers = ', fix_transfers ss_outputs = SS.run_SS(income_tax_params, ss_params, iterative_params, chi_params, baseline, fix_transfers=fix_transfers, baseline_dir=baseline_dir) model_moments = ogusa.calibrate.calc_moments(ss_outputs, sim_params['omega_SS'], sim_params['lambdas'], sim_params['S'], sim_params['J']) scf, data = ogusa.wealth.get_wealth_data() wealth_moments = ogusa.wealth.compute_wealth_moments( scf, sim_params['lambdas'], sim_params['J']) print 'model moments: ', model_moments[:sim_params['J'] + 2] print 'data moments: ', wealth_moments ''' ------------------------------------------------------------------------ Pickle SS results and parameters ------------------------------------------------------------------------ ''' if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) param_dir = os.path.join(baseline_dir, "run_parameters.pkl") pickle.dump(sim_params, open(param_dir, "wb")) else: utils.mkdirs(os.path.join(output_base, "SS")) ss_dir = os.path.join(output_base, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) param_dir = os.path.join(output_base, "run_parameters.pkl") pickle.dump(sim_params, open(param_dir, "wb"))
def runner(output_base, baseline_dir, test=False, time_path=True, baseline=True, iit_reform={}, og_spec={}, guid='', run_micro=True, data=None, client=None, num_workers=1): ''' This function runs the OG-USA model, solving for the steady-state and (optionally) the time path equilibrium. Args: output_base (str): path to save output to baseline_dir (str): path where baseline model results are saved test (bool): whether to run model in test mode (which has a smaller state space and higher tolerances for solution) time_path (bool): whether to solve for the time path equlibrium baseline (bool): whether the model run is the baseline run iit_reform (dict): Tax-Calculator policy dictionary og_spec (dict): dictionary with updates to default parameters in OG-USA guid (str): id for OG-USA run run_micro (bool): whether to estimate tax functions from micro data or load saved parameters from pickle file data (str or Pandas DataFrame): path to or data to use in Tax-Calculator client (Dask client object): client num_workers (int): number of workers to use for parallelization with Dask Returns: None ''' tick = time.time() # Create output directory structure ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [ss_dir, tpi_dir] for _dir in dirs: try: print("making dir: ", _dir) os.makedirs(_dir) except OSError: pass print('In runner, baseline is ', baseline) # Get parameter class # Note - set run_micro false when initially load class # Update later with call to spec.get_tax_function_parameters() spec = Specifications(run_micro=False, output_base=output_base, baseline_dir=baseline_dir, test=test, time_path=time_path, baseline=baseline, iit_reform=iit_reform, guid=guid, data=data, client=client, num_workers=num_workers) spec.update_specifications(og_spec) print('path for tax functions: ', spec.output_base) spec.get_tax_function_parameters(client, run_micro) ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' ss_outputs = SS.run_SS(spec, client=client) ''' ------------------------------------------------------------------------ Pickle SS results ------------------------------------------------------------------------ ''' if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS", "SS_vars.pkl") with open(ss_dir, "wb") as f: pickle.dump(ss_outputs, f) print('JUST SAVED SS output to ', ss_dir) # Save pickle with parameter values for the run param_dir = os.path.join(baseline_dir, "model_params.pkl") with open(param_dir, "wb") as f: cloudpickle.dump((spec), f) else: utils.mkdirs(os.path.join(output_base, "SS")) ss_dir = os.path.join(output_base, "SS", "SS_vars.pkl") with open(ss_dir, "wb") as f: pickle.dump(ss_outputs, f) # Save pickle with parameter values for the run param_dir = os.path.join(output_base, "model_params.pkl") with open(param_dir, "wb") as f: cloudpickle.dump((spec), f) if time_path: ''' ------------------------------------------------------------------------ Run the TPI simulation ------------------------------------------------------------------------ ''' tpi_output = TPI.run_TPI(spec, client=client) ''' ------------------------------------------------------------------------ Pickle TPI results ------------------------------------------------------------------------ ''' tpi_dir = os.path.join(output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_vars.pkl") with open(tpi_vars, "wb") as f: pickle.dump(tpi_output, f) print("Time path iteration complete.") print("It took {0} seconds to get that part done.".format(time.time() - tick))
def runner(output_base, baseline_dir, baseline=False, analytical_mtrs=True, age_specific=False, reform={}, user_params={}, guid='', run_micro=True): #from ogusa import parameters, wealth, labor, demographics, income from ogusa import parameters, wealth, labor, demog, income, utils from ogusa import txfunc tick = time.time() #Create output directory structure saved_moments_dir = os.path.join(output_base, "Saved_moments") ssinit_dir = os.path.join(output_base, "SSinit") tpiinit_dir = os.path.join(output_base, "TPIinit") dirs = [saved_moments_dir, ssinit_dir, tpiinit_dir] for _dir in dirs: try: print "making dir: ", _dir os.makedirs(_dir) except OSError as oe: pass if run_micro: txfunc.get_tax_func_estimate(baseline=baseline, analytical_mtrs=analytical_mtrs, age_specific=age_specific, start_year=user_params['start_year'], reform=reform, guid=guid) print("in runner, baseline is ", baseline) run_params = ogusa.parameters.get_parameters(baseline=baseline, guid=guid) run_params['analytical_mtrs'] = analytical_mtrs # Modify ogusa parameters based on user input if 'frisch' in user_params: print "updating fricsh and associated" b_ellipse, upsilon = ogusa.elliptical_u_est.estimation( user_params['frisch'], run_params['ltilde']) run_params['b_ellipse'] = b_ellipse run_params['upsilon'] = upsilon run_params.update(user_params) # Modify ogusa parameters based on user input if 'g_y_annual' in user_params: print "updating g_y_annual and associated" g_y = (1 + user_params['g_y_annual'])**( float(ending_age - starting_age) / S) - 1 run_params['g_y'] = g_y run_params.update(user_params) from ogusa import SS, TPI # Generate Wealth data moments wealth.get_wealth_data(run_params['lambdas'], run_params['J'], run_params['flag_graphs'], output_dir=output_base) # Generate labor data moments labor.labor_data_moments(run_params['flag_graphs'], output_dir=output_base) calibrate_model = False # List of parameter names that will not be changing (unless we decide to # change them for a tax experiment) param_names = [ 'S', 'J', 'T', 'BW', 'lambdas', 'starting_age', 'ending_age', 'beta', 'sigma', 'alpha', 'nu', 'Z', 'delta', 'E', 'ltilde', 'g_y', 'maxiter', 'mindist_SS', 'mindist_TPI', 'analytical_mtrs', 'b_ellipse', 'k_ellipse', 'upsilon', 'chi_b_guess', 'chi_n_guess', 'etr_params', 'mtrx_params', 'mtry_params', 'tau_payroll', 'tau_bq', 'retire', 'mean_income_data', 'g_n_vector', 'h_wealth', 'p_wealth', 'm_wealth', 'omega', 'g_n_ss', 'omega_SS', 'surv_rate', 'e', 'rho' ] ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' sim_params = {} for key in param_names: sim_params[key] = run_params[key] sim_params['output_dir'] = output_base sim_params['run_params'] = run_params income_tax_params, ss_parameters, iterative_params, chi_params = SS.create_steady_state_parameters( **sim_params) ss_outputs = SS.run_SS(income_tax_params, ss_parameters, iterative_params, chi_params, baseline, baseline_dir=baseline_dir) ''' ------------------------------------------------------------------------ Pickle SS results ------------------------------------------------------------------------ ''' if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/ss_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) else: utils.mkdirs(os.path.join(output_dir, "SS")) ss_dir = os.path.join(output_dir, "SS/ss_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) ''' ------------------------------------------------------------------------ Run the baseline TPI simulation ------------------------------------------------------------------------ ''' sim_params['input_dir'] = output_base sim_params['baseline_dir'] = baseline_dir income_tax_params, tpi_params, iterative_params, initial_values, SS_values = TPI.create_tpi_params( **sim_params) # ss_outputs['income_tax_params'] = income_tax_params # ss_outputs['wealth_tax_params'] = wealth_tax_params # ss_outputs['ellipse_params'] = ellipse_params # ss_outputs['parameters'] = parameters # ss_outputs['N_tilde'] = N_tilde # ss_outputs['omega_stationary'] = omega_stationary # ss_outputs['K0'] = K0 # ss_outputs['b_sinit'] = b_sinit # ss_outputs['b_splus1init'] = b_splus1init # ss_outputs['L0'] = L0 # ss_outputs['Y0'] = Y0 # ss_outputs['r0'] = r0 # ss_outputs['BQ0'] = BQ0 # ss_outputs['T_H_0'] = T_H_0 # ss_outputs['factor_ss'] = factor # ss_outputs['tax0'] = tax0 # ss_outputs['c0'] = c0 # ss_outputs['initial_b'] = initial_b # ss_outputs['initial_n'] = initial_n # ss_outputs['tau_bq'] = tau_bq # ss_outputs['g_n_vector'] = g_n_vector # ss_outputs['output_dir'] = output_base # with open("ss_outputs.pkl", 'wb') as fp: # pickle.dump(ss_outputs, fp) w_path, r_path, T_H_path, BQ_path, Y_path = TPI.run_TPI( income_tax_params, tpi_params, iterative_params, initial_values, SS_values, output_dir=output_base) print "getting to here...." TPI.TP_solutions(w_path, r_path, T_H_path, BQ_path, **ss_outputs) print "took {0} seconds to get that part done.".format(time.time() - tick)
def runner_SS( output_base, baseline_dir, baseline=False, analytical_mtrs=False, age_specific=False, reform={}, user_params={}, guid="", run_micro=True, ): from ogusa import parameters, demographics, income, utils from ogusa import txfunc tick = time.time() # Create output directory structure saved_moments_dir = os.path.join(output_base, "Saved_moments") ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [saved_moments_dir, ss_dir, tpi_dir] for _dir in dirs: try: print "making dir: ", _dir os.makedirs(_dir) except OSError as oe: pass if run_micro: txfunc.get_tax_func_estimate( baseline=baseline, analytical_mtrs=analytical_mtrs, age_specific=age_specific, start_year=user_params["start_year"], reform=reform, guid=guid, ) print ("in runner, baseline is ", baseline) run_params = ogusa.parameters.get_parameters(baseline=baseline, guid=guid) run_params["analytical_mtrs"] = analytical_mtrs # Modify ogusa parameters based on user input if "frisch" in user_params: print "updating fricsh and associated" b_ellipse, upsilon = ogusa.elliptical_u_est.estimation(user_params["frisch"], run_params["ltilde"]) run_params["b_ellipse"] = b_ellipse run_params["upsilon"] = upsilon run_params.update(user_params) # Modify ogusa parameters based on user input if "g_y_annual" in user_params: print "updating g_y_annual and associated" ending_age = run_params["ending_age"] starting_age = run_params["starting_age"] S = run_params["S"] g_y = (1 + user_params["g_y_annual"]) ** (float(ending_age - starting_age) / S) - 1 run_params["g_y"] = g_y run_params.update(user_params) from ogusa import SS, TPI """ **** CALL CALIBRATION here if boolean flagged **** """ calibrate_model = False # if calibrate_model: # chi_b, chi_n = calibrate.(income_tax_params, ss_params, iterative_params, chi_params, baseline, # calibrate_model, output_dir=output_base, baseline_dir=baseline_dir) # List of parameter names that will not be changing (unless we decide to # change them for a tax experiment) param_names = [ "S", "J", "T", "BW", "lambdas", "starting_age", "ending_age", "beta", "sigma", "alpha", "nu", "Z", "delta", "E", "ltilde", "g_y", "maxiter", "mindist_SS", "mindist_TPI", "analytical_mtrs", "b_ellipse", "k_ellipse", "upsilon", "chi_b_guess", "chi_n_guess", "etr_params", "mtrx_params", "mtry_params", "tau_payroll", "tau_bq", "retire", "mean_income_data", "g_n_vector", "h_wealth", "p_wealth", "m_wealth", "omega", "g_n_ss", "omega_SS", "surv_rate", "imm_rates", "e", "rho", "omega_S_preTP", ] """ ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ """ sim_params = {} for key in param_names: sim_params[key] = run_params[key] sim_params["output_dir"] = output_base sim_params["run_params"] = run_params income_tax_params, ss_params, iterative_params, chi_params = SS.create_steady_state_parameters(**sim_params) ss_outputs = SS.run_SS( income_tax_params, ss_params, iterative_params, chi_params, baseline, baseline_dir=baseline_dir ) """ ------------------------------------------------------------------------ Pickle SS results ------------------------------------------------------------------------ """ if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) else: utils.mkdirs(os.path.join(output_base, "SS")) ss_dir = os.path.join(output_base, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb"))
def run_model(meta_param_dict, adjustment): ''' Initializes classes from OG-USA that compute the model under different policies. Then calls function get output objects. ''' meta_params = MetaParams() meta_params.adjust(meta_param_dict) if meta_params.data_source == "PUF": data = retrieve_puf(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) else: data = "cps" # Create output directory structure base_dir = os.path.join(CUR_DIR, BASELINE_DIR) reform_dir = os.path.join(CUR_DIR, REFORM_DIR) dirs = [base_dir, reform_dir] for _dir in dirs: utils.mkdirs(_dir) # Dask parmeters client = None num_workers = 1 # whether to estimate tax functions from microdata run_micro = True # Solve baseline model base_spec = { 'start_year': meta_param_dict['year'], 'debt_ratio_ss': 2.0, 'r_gov_scale': 1.0, 'r_gov_shift': 0.02, 'zeta_D': [0.4], 'zeta_K': [0.1], 'initial_debt_ratio': 0.78, 'initial_foreign_debt_ratio': 0.4, 'tax_func_type': 'linear', 'age_specific': False } base_params = Specifications(run_micro=False, output_base=base_dir, baseline_dir=base_dir, test=False, time_path=False, baseline=True, iit_reform={}, guid='', data=data, client=client, num_workers=num_workers) base_params.update_specifications(base_spec) base_params.get_tax_function_parameters(client, run_micro) base_ss = SS.run_SS(base_params, client=client) utils.mkdirs(os.path.join(base_dir, "SS")) ss_dir = os.path.join(base_dir, "SS", "SS_vars.pkl") with open(ss_dir, "wb") as f: pickle.dump(base_ss, f) # Solve reform model reform_spec = base_spec reform_spec.update(adjustment['ogusa']) reform_params = Specifications(run_micro=False, output_base=reform_dir, baseline_dir=base_dir, test=False, time_path=False, baseline=False, iit_reform={}, guid='', data=data, client=client, num_workers=num_workers) reform_params.update_specifications(reform_spec) reform_params.get_tax_function_parameters(client, run_micro) reform_ss = SS.run_SS(reform_params, client=client) comp_dict = comp_output(base_ss, base_params, reform_ss, reform_params) return comp_dict
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.eta = p.omega.reshape(T + S, S, 1) * lambdas.reshape(1, 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.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.frac_tax_payroll = 0.5 * np.ones(p.T + p.S) 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") with open(ss_dir, "wb") as f: pickle.dump(ss_outputs, f) else: utils.mkdirs(os.path.join(p.output_base, "SS")) ss_dir = os.path.join(p.output_base, "SS/SS_vars.pkl") with open(ss_dir, "wb") as f: pickle.dump(ss_outputs, f) 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['iit_revenue'], test_dict['payroll_tax_revenue'] 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))
def run_TPI(p, client=None): # unpack tuples of parameters initial_values, ss_vars, theta, baseline_values = get_initial_SS_values(p) (B0, b_sinit, b_splus1init, factor, initial_b, initial_n, D0) = initial_values (T_Hbaseline, Gbaseline) = baseline_values print('Government spending breakpoints are tG1: ', p.tG1, '; and tG2:', p.tG2) # Initialize guesses at time paths # Make array of initial guesses for labor supply and savings domain = np.linspace(0, p.T, p.T) domain2 = np.tile(domain.reshape(p.T, 1, 1), (1, p.S, p.J)) ending_b = ss_vars['bssmat_splus1'] guesses_b = (-1 / (domain2 + 1)) * (ending_b - initial_b) + ending_b ending_b_tail = np.tile(ending_b.reshape(1, p.S, p.J), (p.S, 1, 1)) guesses_b = np.append(guesses_b, ending_b_tail, axis=0) domain3 = np.tile(np.linspace(0, 1, p.T).reshape(p.T, 1, 1), (1, p.S, p.J)) guesses_n = domain3 * (ss_vars['nssmat'] - initial_n) + initial_n ending_n_tail = np.tile(ss_vars['nssmat'].reshape(1, p.S, p.J), (p.S, 1, 1)) guesses_n = np.append(guesses_n, ending_n_tail, axis=0) b_mat = guesses_b n_mat = guesses_n ind = np.arange(p.S) L_init = np.ones((p.T + p.S, )) * ss_vars['Lss'] B_init = np.ones((p.T + p.S, )) * ss_vars['Bss'] L_init[:p.T] = aggr.get_L(n_mat[:p.T], p, 'TPI') B_init[1:p.T] = aggr.get_K(b_mat[:p.T], p, 'TPI', False)[:p.T - 1] B_init[0] = B0 if not p.small_open: if p.budget_balance: K_init = B_init else: K_init = B_init * ss_vars['Kss'] / ss_vars['Bss'] else: K_init = firm.get_K(L_init, p.firm_r, p, 'TPI') K = K_init K_d = K_init * ss_vars['K_d_ss'] / ss_vars['Kss'] K_f = K_init * ss_vars['K_f_ss'] / ss_vars['Kss'] L = L_init B = B_init Y = np.zeros_like(K) Y[:p.T] = firm.get_Y(K[:p.T], L[:p.T], p, 'TPI') Y[p.T:] = ss_vars['Yss'] r = np.zeros_like(Y) if not p.small_open: r[:p.T] = firm.get_r(Y[:p.T], K[:p.T], p, 'TPI') r[p.T:] = ss_vars['rss'] else: r = p.firm_r # compute w w = np.zeros_like(r) w[:p.T] = firm.get_w_from_r(r[:p.T], p, 'TPI') w[p.T:] = ss_vars['wss'] r_gov = fiscal.get_r_gov(r, p) if p.budget_balance: r_hh = r else: r_hh = aggr.get_r_hh(r, r_gov, K, ss_vars['Dss']) if p.small_open: r_hh = p.hh_r BQ0 = aggr.get_BQ(r[0], initial_b, None, p, 'SS', True) if not p.use_zeta: BQ = np.zeros((p.T + p.S, p.J)) for j in range(p.J): BQ[:, j] = (list(np.linspace(BQ0[j], ss_vars['BQss'][j], p.T)) + [ss_vars['BQss'][j]] * p.S) BQ = np.array(BQ) else: BQ = (list(np.linspace(BQ0, ss_vars['BQss'], p.T)) + [ss_vars['BQss']] * p.S) BQ = np.array(BQ) if p.budget_balance: if np.abs(ss_vars['T_Hss']) < 1e-13: T_Hss2 = 0.0 # sometimes SS is very small but not zero, # even if taxes are zero, this get's rid of the approximation # error, which affects the perc changes below else: T_Hss2 = ss_vars['T_Hss'] T_H = np.ones(p.T + p.S) * T_Hss2 total_revenue = T_H G = np.zeros(p.T + p.S) elif not p.baseline_spending: T_H = p.alpha_T * Y G = np.ones(p.T + p.S) * ss_vars['Gss'] elif p.baseline_spending: T_H = T_Hbaseline T_H_new = p.T_H # Need to set T_H_new for later reference G = Gbaseline G_0 = Gbaseline[0] # Initialize some starting values if p.budget_balance: D = np.zeros(p.T + p.S) else: D = np.ones(p.T + p.S) * ss_vars['Dss'] if ss_vars['Dss'] == 0: D_d = np.zeros(p.T + p.S) D_f = np.zeros(p.T + p.S) else: D_d = D * ss_vars['D_d_ss'] / ss_vars['Dss'] D_f = D * ss_vars['D_f_ss'] / ss_vars['Dss'] total_revenue = np.ones(p.T + p.S) * ss_vars['total_revenue_ss'] TPIiter = 0 TPIdist = 10 euler_errors = np.zeros((p.T, 2 * p.S, p.J)) TPIdist_vec = np.zeros(p.maxiter) # TPI loop while (TPIiter < p.maxiter) and (TPIdist >= p.mindist_TPI): r_gov[:p.T] = fiscal.get_r_gov(r[:p.T], p) if p.budget_balance: r_hh[:p.T] = r[:p.T] else: K[:p.T] = firm.get_K_from_Y(Y[:p.T], r[:p.T], p, 'TPI') r_hh[:p.T] = aggr.get_r_hh(r[:p.T], r_gov[:p.T], K[:p.T], D[:p.T]) if p.small_open: r_hh[:p.T] = p.hh_r[:p.T] outer_loop_vars = (r, w, r_hh, BQ, T_H, theta) euler_errors = np.zeros((p.T, 2 * p.S, p.J)) lazy_values = [] for j in range(p.J): guesses = (guesses_b[:, :, j], guesses_n[:, :, j]) lazy_values.append( delayed(inner_loop)(guesses, outer_loop_vars, initial_values, j, ind, p)) results = compute(*lazy_values, scheduler=dask.multiprocessing.get, num_workers=p.num_workers) for j, result in enumerate(results): euler_errors[:, :, j], b_mat[:, :, j], n_mat[:, :, j] = result bmat_s = np.zeros((p.T, p.S, p.J)) bmat_s[0, 1:, :] = initial_b[:-1, :] bmat_s[1:, 1:, :] = b_mat[:p.T - 1, :-1, :] bmat_splus1 = np.zeros((p.T, p.S, p.J)) bmat_splus1[:, :, :] = b_mat[:p.T, :, :] etr_params_4D = np.tile( p.etr_params.reshape(p.T, p.S, 1, p.etr_params.shape[2]), (1, 1, p.J, 1)) bqmat = household.get_bq(BQ, None, p, 'TPI') tax_mat = tax.total_taxes(r_hh[:p.T], w[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat[:p.T, :, :], factor, T_H[:p.T], theta, 0, None, False, 'TPI', p.e, etr_params_4D, p) r_hh_path = utils.to_timepath_shape(r_hh, p) wpath = utils.to_timepath_shape(w, p) c_mat = household.get_cons(r_hh_path[:p.T, :, :], wpath[:p.T, :, :], bmat_s, bmat_splus1, n_mat[:p.T, :, :], bqmat[:p.T, :, :], tax_mat, p.e, p.tau_c[:p.T, :, :], p) y_before_tax_mat = (r_hh_path[:p.T, :, :] * bmat_s[:p.T, :, :] + wpath[:p.T, :, :] * p.e * n_mat[:p.T, :, :]) if not p.baseline_spending and not p.budget_balance: Y[:p.T] = T_H[:p.T] / p.alpha_T[:p.T] # maybe unecessary (total_rev, T_Ipath, T_Ppath, T_BQpath, T_Wpath, T_Cpath, business_revenue) = aggr.revenue( r_hh[:p.T], w[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat[:p.T, :, :], c_mat[:p.T, :, :], Y[:p.T], L[:p.T], K[:p.T], factor, theta, etr_params_4D, p, 'TPI') total_revenue[:p.T] = total_rev # set intial debt value if p.baseline: D0 = p.initial_debt_ratio * Y[0] if not p.baseline_spending: G_0 = p.alpha_G[0] * Y[0] dg_fixed_values = (Y, total_revenue, T_H, D0, G_0) Dnew, G[:p.T] = fiscal.D_G_path(r_gov, dg_fixed_values, Gbaseline, p) # Fix initial amount of foreign debt holding D_f[0] = p.initial_foreign_debt_ratio * Dnew[0] for t in range(1, p.T): D_f[t + 1] = (D_f[t] / (np.exp(p.g_y) * (1 + p.g_n[t + 1])) + p.zeta_D[t] * (Dnew[t + 1] - (Dnew[t] / (np.exp(p.g_y) * (1 + p.g_n[t + 1]))))) D_d[:p.T] = Dnew[:p.T] - D_f[:p.T] else: # if budget balance Dnew = np.zeros(p.T + 1) G[:p.T] = np.zeros(p.T) D_f[:p.T] = np.zeros(p.T) D_d[:p.T] = np.zeros(p.T) L[:p.T] = aggr.get_L(n_mat[:p.T], p, 'TPI') B[1:p.T] = aggr.get_K(bmat_splus1[:p.T], p, 'TPI', False)[:p.T - 1] K_demand_open = firm.get_K(L[:p.T], p.firm_r[:p.T], p, 'TPI') K_d[:p.T] = B[:p.T] - D_d[:p.T] if np.any(K_d < 0): print('K_d has negative elements. Setting them ' + 'positive to prevent NAN.') K_d[:p.T] = np.fmax(K_d[:p.T], 0.05 * B[:p.T]) K_f[:p.T] = p.zeta_K[:p.T] * (K_demand_open - B[:p.T] + D_d[:p.T]) K = K_f + K_d if np.any(B) < 0: print('B has negative elements. B[0:9]:', B[0:9]) print('B[T-2:T]:', B[p.T - 2, p.T]) if p.small_open: K[:p.T] = K_demand_open Ynew = firm.get_Y(K[:p.T], L[:p.T], p, 'TPI') rnew = r.copy() if not p.small_open: rnew[:p.T] = firm.get_r(Ynew[:p.T], K[:p.T], p, 'TPI') else: rnew[:p.T] = r[:p.T].copy() r_gov_new = fiscal.get_r_gov(rnew, p) if p.budget_balance: r_hh_new = rnew[:p.T] else: r_hh_new = aggr.get_r_hh(rnew[:p.T], r_gov_new[:p.T], K[:p.T], Dnew[:p.T]) if p.small_open: r_hh_new = p.hh_r[:p.T] # compute w wnew = firm.get_w_from_r(rnew[:p.T], p, 'TPI') b_mat_shift = np.append(np.reshape(initial_b, (1, p.S, p.J)), b_mat[:p.T - 1, :, :], axis=0) BQnew = aggr.get_BQ(r_hh_new[:p.T], b_mat_shift, None, p, 'TPI', False) bqmat_new = household.get_bq(BQnew, None, p, 'TPI') (total_rev, T_Ipath, T_Ppath, T_BQpath, T_Wpath, T_Cpath, business_revenue) = aggr.revenue( r_hh_new[:p.T], wnew[:p.T], bmat_s, n_mat[:p.T, :, :], bqmat_new[:p.T, :, :], c_mat[:p.T, :, :], Ynew[:p.T], L[:p.T], K[:p.T], factor, theta, etr_params_4D, p, 'TPI') total_revenue[:p.T] = total_rev if p.budget_balance: T_H_new = total_revenue elif not p.baseline_spending: T_H_new = p.alpha_T[:p.T] * Ynew[:p.T] # If baseline_spending==True, no need to update T_H, it's fixed # update vars for next iteration w[:p.T] = wnew[:p.T] r[:p.T] = utils.convex_combo(rnew[:p.T], r[:p.T], p.nu) BQ[:p.T] = utils.convex_combo(BQnew[:p.T], BQ[:p.T], p.nu) D[:p.T] = Dnew[:p.T] Y[:p.T] = utils.convex_combo(Ynew[:p.T], Y[:p.T], p.nu) if not p.baseline_spending: T_H[:p.T] = utils.convex_combo(T_H_new[:p.T], T_H[:p.T], p.nu) guesses_b = utils.convex_combo(b_mat, guesses_b, p.nu) guesses_n = utils.convex_combo(n_mat, guesses_n, p.nu) print('r diff: ', (rnew[:p.T] - r[:p.T]).max(), (rnew[:p.T] - r[:p.T]).min()) print('BQ diff: ', (BQnew[:p.T] - BQ[:p.T]).max(), (BQnew[:p.T] - BQ[:p.T]).min()) print('T_H diff: ', (T_H_new[:p.T] - T_H[:p.T]).max(), (T_H_new[:p.T] - T_H[:p.T]).min()) print('Y diff: ', (Ynew[:p.T] - Y[:p.T]).max(), (Ynew[:p.T] - Y[:p.T]).min()) if not p.baseline_spending: if T_H.all() != 0: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list( utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(utils.pct_diff_func(T_H_new[:p.T], T_H[:p.T]))).max() else: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list( utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(np.abs(T_H[:p.T]))).max() else: TPIdist = np.array( list(utils.pct_diff_func(rnew[:p.T], r[:p.T])) + list(utils.pct_diff_func(BQnew[:p.T], BQ[:p.T]).flatten()) + list(utils.pct_diff_func(Ynew[:p.T], Y[:p.T]))).max() TPIdist_vec[TPIiter] = TPIdist # After T=10, if cycling occurs, drop the value of nu # wait til after T=10 or so, because sometimes there is a jump up # in the first couple iterations # if TPIiter > 10: # if TPIdist_vec[TPIiter] - TPIdist_vec[TPIiter - 1] > 0: # nu /= 2 # print 'New Value of nu:', nu TPIiter += 1 print('Iteration:', TPIiter) print('\tDistance:', TPIdist) # Compute effective and marginal tax rates for all agents mtrx_params_4D = np.tile( p.mtrx_params.reshape(p.T, p.S, 1, p.mtrx_params.shape[2]), (1, 1, p.J, 1)) mtry_params_4D = np.tile( p.mtry_params.reshape(p.T, p.S, 1, p.mtry_params.shape[2]), (1, 1, p.J, 1)) e_3D = np.tile(p.e.reshape(1, p.S, p.J), (p.T, 1, 1)) mtry_path = tax.MTR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, True, e_3D, etr_params_4D, mtry_params_4D, p) mtrx_path = tax.MTR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, False, e_3D, etr_params_4D, mtrx_params_4D, p) etr_path = tax.ETR_income(r_hh_path[:p.T], wpath[:p.T], bmat_s[:p.T, :, :], n_mat[:p.T, :, :], factor, e_3D, etr_params_4D, p) C = aggr.get_C(c_mat, p, 'TPI') # Note that implicity in this computation is that immigrants' # wealth is all in the form of private capital I_d = aggr.get_I(bmat_splus1[:p.T], K_d[1:p.T + 1], K_d[:p.T], p, 'TPI') I = aggr.get_I(bmat_splus1[:p.T], K[1:p.T + 1], K[:p.T], p, 'TPI') # solve resource constraint # net foreign borrowing new_borrowing_f = (D_f[1:p.T + 1] * np.exp(p.g_y) * (1 + p.g_n[1:p.T + 1]) - D_f[:p.T]) debt_service_f = D_f * r_hh RC_error = aggr.resource_constraint(Y[:p.T - 1], C[:p.T - 1], G[:p.T - 1], I_d[:p.T - 1], K_f[:p.T - 1], new_borrowing_f[:p.T - 1], debt_service_f[:p.T - 1], r_hh[:p.T - 1], p) # Compute total investment (not just domestic) I_total = ((1 + p.g_n[:p.T]) * np.exp(p.g_y) * K[1:p.T + 1] - (1.0 - p.delta) * K[:p.T]) rce_max = np.amax(np.abs(RC_error)) print('Max absolute value resource constraint error:', rce_max) print('Checking time path for violations of constraints.') for t in range(p.T): household.constraint_checker_TPI(b_mat[t], n_mat[t], c_mat[t], t, p.ltilde) eul_savings = euler_errors[:, :p.S, :].max(1).max(1) eul_laborleisure = euler_errors[:, p.S:, :].max(1).max(1) print('Max Euler error, savings: ', eul_savings) print('Max Euler error labor supply: ', eul_laborleisure) ''' ------------------------------------------------------------------------ Save variables/values so they can be used in other modules ------------------------------------------------------------------------ ''' output = { 'Y': Y[:p.T], 'B': B, 'K': K, 'K_f': K_f, 'K_d': K_d, 'L': L, 'C': C, 'I': I, 'I_total': I_total, 'I_d': I_d, 'BQ': BQ, 'total_revenue': total_revenue, 'business_revenue': business_revenue, 'IITpayroll_revenue': T_Ipath, 'T_H': T_H, 'T_P': T_Ppath, 'T_BQ': T_BQpath, 'T_W': T_Wpath, 'T_C': T_Cpath, 'G': G, 'D': D, 'D_f': D_f, 'D_d': D_d, 'r': r, 'r_gov': r_gov, 'r_hh': r_hh, 'w': w, 'bmat_splus1': bmat_splus1, 'bmat_s': bmat_s[:p.T, :, :], 'n_mat': n_mat[:p.T, :, :], 'c_path': c_mat, 'bq_path': bqmat, 'y_before_tax_mat': y_before_tax_mat, 'tax_path': tax_mat, 'eul_savings': eul_savings, 'eul_laborleisure': eul_laborleisure, 'resource_constraint_error': RC_error, 'new_borrowing_f': new_borrowing_f, 'debt_service_f': debt_service_f, 'etr_path': etr_path, 'mtrx_path': mtrx_path, 'mtry_path': mtry_path } tpi_dir = os.path.join(p.output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_vars.pkl") pickle.dump(output, open(tpi_vars, "wb")) if np.any(G) < 0: print('Government spending is negative along transition path' + ' to satisfy budget') if (((TPIiter >= p.maxiter) or (np.absolute(TPIdist) > p.mindist_TPI)) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found' + ' (TPIdist)') if ((np.any(np.absolute(RC_error) >= p.mindist_TPI * 10)) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found ' + '(RC_error)') if ((np.any(np.absolute(eul_savings) >= p.mindist_TPI) or (np.any(np.absolute(eul_laborleisure) > p.mindist_TPI))) and ENFORCE_SOLUTION_CHECKS): raise RuntimeError('Transition path equlibrium not found ' + '(eulers)') return output
def runner_SS(output_base, baseline_dir, baseline=False, analytical_mtrs=True, age_specific=False, reform={}, user_params={}, guid='', run_micro=True): from ogusa import parameters, demographics, income, utils from ogusa import txfunc tick = time.time() #Create output directory structure saved_moments_dir = os.path.join(output_base, "Saved_moments") ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [saved_moments_dir, ss_dir, tpi_dir] for _dir in dirs: try: print "making dir: ", _dir os.makedirs(_dir) except OSError as oe: pass if run_micro: txfunc.get_tax_func_estimate(baseline=baseline, analytical_mtrs=analytical_mtrs, age_specific=age_specific, start_year=user_params['start_year'], reform=reform, guid=guid) print("in runner, baseline is ", baseline) run_params = ogusa.parameters.get_parameters(baseline=baseline, guid=guid) run_params['analytical_mtrs'] = analytical_mtrs # Modify ogusa parameters based on user input if 'frisch' in user_params: print "updating fricsh and associated" b_ellipse, upsilon = ogusa.elliptical_u_est.estimation( user_params['frisch'], run_params['ltilde']) run_params['b_ellipse'] = b_ellipse run_params['upsilon'] = upsilon run_params.update(user_params) # Modify ogusa parameters based on user input if 'g_y_annual' in user_params: print "updating g_y_annual and associated" g_y = (1 + user_params['g_y_annual'])**( float(ending_age - starting_age) / S) - 1 run_params['g_y'] = g_y run_params.update(user_params) from ogusa import SS, TPI ''' **** CALL CALIBRATION here if boolean flagged **** ''' calibrate_model = False # if calibrate_model: # chi_b, chi_n = calibrate.(income_tax_params, ss_params, iterative_params, chi_params, baseline, # calibrate_model, output_dir=output_base, baseline_dir=baseline_dir) # List of parameter names that will not be changing (unless we decide to # change them for a tax experiment) param_names = [ 'S', 'J', 'T', 'BW', 'lambdas', 'starting_age', 'ending_age', 'beta', 'sigma', 'alpha', 'nu', 'Z', 'delta', 'E', 'ltilde', 'g_y', 'maxiter', 'mindist_SS', 'mindist_TPI', 'analytical_mtrs', 'b_ellipse', 'k_ellipse', 'upsilon', 'chi_b_guess', 'chi_n_guess', 'etr_params', 'mtrx_params', 'mtry_params', 'tau_payroll', 'tau_bq', 'retire', 'mean_income_data', 'g_n_vector', 'h_wealth', 'p_wealth', 'm_wealth', 'omega', 'g_n_ss', 'omega_SS', 'surv_rate', 'e', 'rho' ] ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' sim_params = {} for key in param_names: sim_params[key] = run_params[key] sim_params['output_dir'] = output_base sim_params['run_params'] = run_params income_tax_params, ss_params, iterative_params, chi_params = SS.create_steady_state_parameters( **sim_params) ss_outputs = SS.run_SS(income_tax_params, ss_params, iterative_params, chi_params, baseline, baseline_dir=baseline_dir) ''' ------------------------------------------------------------------------ Pickle SS results ------------------------------------------------------------------------ ''' if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) else: utils.mkdirs(os.path.join(output_base, "SS")) ss_dir = os.path.join(output_base, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb"))
def runner(output_base, baseline_dir, test=False, time_path=True, baseline=True, reform={}, user_params={}, guid='', run_micro=True, data=None, client=None, num_workers=1): tick = time.time() # Create output directory structure ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [ss_dir, tpi_dir] for _dir in dirs: try: print("making dir: ", _dir) os.makedirs(_dir) except OSError: pass print('In runner, baseline is ', baseline) # Get parameter class # Note - set run_micro false when initially load class # Update later with call to spec.get_tax_function_parameters() spec = Specifications(run_micro=False, output_base=output_base, baseline_dir=baseline_dir, test=test, time_path=time_path, baseline=baseline, reform=reform, guid=guid, data=data, client=client, num_workers=num_workers) spec.update_specifications(user_params) print('path for tax functions: ', spec.output_base) spec.get_tax_function_parameters(client, run_micro) ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' ss_outputs = SS.run_SS(spec, client=client) ''' ------------------------------------------------------------------------ Pickle SS results ------------------------------------------------------------------------ ''' if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) # Save pickle with parameter values for the run param_dir = os.path.join(baseline_dir, "model_params.pkl") pickle.dump(spec, open(param_dir, "wb")) else: utils.mkdirs(os.path.join(output_base, "SS")) ss_dir = os.path.join(output_base, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) # Save pickle with parameter values for the run param_dir = os.path.join(output_base, "model_params.pkl") pickle.dump(spec, open(param_dir, "wb")) if time_path: ''' ------------------------------------------------------------------------ Run the TPI simulation ------------------------------------------------------------------------ ''' tpi_output = TPI.run_TPI(spec, client=client) ''' ------------------------------------------------------------------------ Pickle TPI results ------------------------------------------------------------------------ ''' tpi_dir = os.path.join(output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_vars.pkl") pickle.dump(tpi_output, open(tpi_vars, "wb")) print("Time path iteration complete.") print("It took {0} seconds to get that part done.".format(time.time() - tick))
def runner(output_base, baseline_dir, test=False, time_path=True, baseline=True, reform={}, user_params={}, guid='', run_micro=True, data=None, client=None, num_workers=1): tick = time.time() # Create output directory structure ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [ss_dir, tpi_dir] for _dir in dirs: try: print("making dir: ", _dir) os.makedirs(_dir) except OSError: pass print('In runner, baseline is ', baseline) # Get parameter class # Note - set run_micro false when initially load class # Update later with call to spec.get_tax_function_parameters() spec = Specifications(run_micro=False, output_base=output_base, baseline_dir=baseline_dir, test=test, time_path=time_path, baseline=baseline, reform=reform, guid=guid, data=data, client=client, num_workers=num_workers) spec.update_specifications(user_params) print('path for tax functions: ', spec.output_base) spec.get_tax_function_parameters(client, run_micro) ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' ss_outputs = SS.run_SS(spec, client=client) ''' ------------------------------------------------------------------------ Pickle SS results ------------------------------------------------------------------------ ''' if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) # Save pickle with parameter values for the run param_dir = os.path.join(baseline_dir, "model_params.pkl") pickle.dump(spec, open(param_dir, "wb")) else: utils.mkdirs(os.path.join(output_base, "SS")) ss_dir = os.path.join(output_base, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) # Save pickle with parameter values for the run param_dir = os.path.join(output_base, "model_params.pkl") pickle.dump(spec, open(param_dir, "wb")) if time_path: ''' ------------------------------------------------------------------------ Run the TPI simulation ------------------------------------------------------------------------ ''' tpi_output = TPI.run_TPI(spec, client=client) ''' ------------------------------------------------------------------------ Pickle TPI results ------------------------------------------------------------------------ ''' tpi_dir = os.path.join(output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_vars.pkl") pickle.dump(tpi_output, open(tpi_vars, "wb")) print("Time path iteration complete.") print("It took {0} seconds to get that part done.".format( time.time() - tick))
def get_tax_function_parameters( self, p, iit_reform={}, guid="", data="", client=None, num_workers=1, run_micro=False, tax_func_path=None, ): """ Reads pickle file of tax function parameters or estimates the parameters from microsimulation model output. Args: client (Dask client object): client run_micro (bool): whether to estimate parameters from microsimulation model tax_func_path (string): path where find or save tax function parameter estimates Returns: None """ # set paths if none given if tax_func_path is None: if p.baseline: pckl = "TxFuncEst_baseline{}.pkl".format(guid) tax_func_path = os.path.join(CUR_PATH, pckl) print("Using baseline tax parameters from ", tax_func_path) else: pckl = "TxFuncEst_policy{}.pkl".format(guid) tax_func_path = os.path.join(CUR_PATH, pckl) print( "Using reform policy tax parameters from ", tax_func_path ) # create directory for tax function pickles to be saved to mkdirs(os.path.split(tax_func_path)[0]) # If run_micro is false, check to see if parameters file exists # and if it is consistent with Specifications instance if not run_micro: dict_params, run_micro = self.read_tax_func_estimate(tax_func_path) if run_micro: txfunc.get_tax_func_estimate( # pragma: no cover p.BW, p.S, p.starting_age, p.ending_age, p.baseline, p.analytical_mtrs, p.tax_func_type, p.age_specific, p.start_year, iit_reform, guid, tax_func_path, data, client, num_workers, ) dict_params, _ = self.read_tax_func_estimate(p, tax_func_path) mean_income_data = dict_params["tfunc_avginc"][0] try: frac_tax_payroll = np.append( dict_params["tfunc_frac_tax_payroll"], np.ones(p.T + p.S - p.BW) * dict_params["tfunc_frac_tax_payroll"][-1], ) except KeyError: pass try: taxcalc_version = dict_params["taxcalc_version"] except KeyError: taxcalc_version = "No version recorded" # Reorder indices of tax function and tile for all years after # budget window ends num_etr_params = dict_params["tfunc_etr_params_S"].shape[2] # First check to see if tax parameters that are used were # estimated with a budget window and ages that are as long as # the those implied based on the start year and model age. # N.B. the tax parameters dictionary does not save the years # that correspond to the parameter estimates, so the start year # used there may name match what is used in a run that reads in # some cached tax function parameters. Likewise for age. params_list = ["etr", "mtrx", "mtry"] BW_in_tax_params = dict_params["tfunc_etr_params_S"].shape[1] S_in_tax_params = dict_params["tfunc_etr_params_S"].shape[0] if p.BW != BW_in_tax_params: print( "Warning: There is a discrepency between the start" + " year of the model and that of the tax functions!!" ) # After printing warning, make it work by tiling if p.BW > BW_in_tax_params: for item in params_list: dict_params["tfunc_" + item + "_params_S"] = np.concatenate( ( dict_params["tfunc_" + item + "_params_S"], np.tile( dict_params["tfunc_" + item + "_params_S"][ :, -1, : ].reshape(S_in_tax_params, 1, num_etr_params), (1, p.BW - BW_in_tax_params, 1), ), ), axis=1, ) dict_params["tfunc_avg_" + item] = np.append( dict_params["tfunc_avg_" + item], np.tile( dict_params["tfunc_avg_" + item][-1], (p.BW - BW_in_tax_params), ), ) if p.S != S_in_tax_params: print( "Warning: There is a discrepency between the ages" + " used in the model and those in the tax functions!!" ) # After printing warning, make it work by tiling if p.S > S_in_tax_params: for item in params_list: dict_params["tfunc_" + item + "_params_S"] = np.concatenate( ( dict_params["tfunc_" + item + "_params_S"], np.tile( dict_params["tfunc_" + item + "_params_S"][ -1, :, : ].reshape(1, p.BW, num_etr_params), (p.S - S_in_tax_params, 1, 1), ), ), axis=0, ) tax_param_dict = { "etr_params": dict_params["tfunc_etr_params_S"], "mtrx_params": dict_params["tfunc_mtrx_params_S"], "mtry_params": dict_params["tfunc_mtry_params_S"], "taxcalc_version": taxcalc_version, "mean_income_data": mean_income_data, "frac_tax_payroll": frac_tax_payroll, } return tax_param_dict
def runner(output_base, baseline_dir, baseline=False, analytical_mtrs=True, age_specific=False, reform=0, fix_transfers=False, user_params={}, guid='', run_micro=True, calibrate_model=False): from ogusa import parameters, demographics, income, utils tick = time.time() #Create output directory structure saved_moments_dir = os.path.join(output_base, "Saved_moments") ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [saved_moments_dir, ss_dir, tpi_dir] for _dir in dirs: try: print "making dir: ", _dir os.makedirs(_dir) except OSError as oe: pass print("in runner, baseline is ", baseline) run_params = ogusa.parameters.get_parameters(baseline=baseline, reform=reform, guid=guid, user_modifiable=True) run_params['analytical_mtrs'] = analytical_mtrs # Modify ogusa parameters based on user input if 'frisch' in user_params: print "updating fricsh and associated" b_ellipse, upsilon = ogusa.elliptical_u_est.estimation( user_params['frisch'], run_params['ltilde']) run_params['b_ellipse'] = b_ellipse run_params['upsilon'] = upsilon run_params.update(user_params) # Modify ogusa parameters based on user input if 'sigma' in user_params: print "updating sigma" run_params['sigma'] = user_params['sigma'] run_params.update(user_params) from ogusa import SS, TPI calibrate_model = False # List of parameter names that will not be changing (unless we decide to # change them for a tax experiment) param_names = [ 'S', 'J', 'T', 'BW', 'lambdas', 'starting_age', 'ending_age', 'beta', 'sigma', 'alpha', 'nu', 'Z', 'delta', 'E', 'ltilde', 'g_y', 'maxiter', 'mindist_SS', 'mindist_TPI', 'analytical_mtrs', 'b_ellipse', 'k_ellipse', 'upsilon', 'chi_b_guess', 'chi_n_guess', 'etr_params', 'mtrx_params', 'mtry_params', 'tau_payroll', 'tau_bq', 'retire', 'mean_income_data', 'g_n_vector', 'h_wealth', 'p_wealth', 'm_wealth', 'omega', 'g_n_ss', 'omega_SS', 'surv_rate', 'imm_rates', 'e', 'rho', 'omega_S_preTP' ] ''' ------------------------------------------------------------------------ If using income tax reform, need to determine parameters that yield same SS revenue as the wealth tax reform. ------------------------------------------------------------------------ ''' if reform == 1: sim_params = {} for key in param_names: sim_params[key] = run_params[key] sim_params['output_dir'] = output_base sim_params['run_params'] = run_params income_tax_params, ss_params, iterative_params, chi_params = SS.create_steady_state_parameters( **sim_params) # find SS revenue from wealth tax reform reform3_ss_dir = os.path.join( "./OUTPUT_WEALTH_REFORM" + '/sigma' + str(run_params['sigma']), "SS/SS_vars.pkl") reform3_ss_solutions = pickle.load(open(reform3_ss_dir, "rb")) receipts_to_match = reform3_ss_solutions['net_tax_receipts'] # create function to match SS revenue # def matcher(d_guess, params): # income_tax_params, receipts_to_match, ss_params, iterative_params,\ # chi_params, baseline, baseline_dir = params # analytical_mtrs, etr_params, mtrx_params, mtry_params = income_tax_params # etr_params[:,3] = d_guess # mtrx_params[:,3] = d_guess # mtry_params[:,3] = d_guess # income_tax_params = analytical_mtrs, etr_params, mtrx_params, mtry_params # ss_outputs = SS.run_SS(income_tax_params, ss_params, iterative_params, # chi_params, baseline ,baseline_dir=baseline_dir, output_base=output_base) # # receipts_new = ss_outputs['T_Hss'] + ss_outputs['Gss'] # error = abs(receipts_to_match - receipts_new) # if d_guess <= 0: # error = 1e14 # print 'Error in taxes:', error # return error # print 'Computing new income tax to match wealth tax' d_guess = 0.413 # initial guess # import scipy.optimize as opt # params = [income_tax_params, receipts_to_match, ss_params, iterative_params, # chi_params, baseline, baseline_dir] # new_d_inc = opt.fsolve(matcher, d_guess, args=params, xtol=1e-6) new_d_inc = 0.413 # this value comes out given default parameter values (if fix_transfers=True this is 0.503 if False then 0.453) print '\tOld income tax:', d_guess print '\tNew income tax:', new_d_inc analytical_mtrs, etr_params, mtrx_params, mtry_params = income_tax_params etr_params[:, 3] = new_d_inc mtrx_params[:, 3] = new_d_inc mtry_params[:, 3] = new_d_inc run_params['etr_params'] = np.tile( np.reshape(etr_params, (run_params['S'], 1, etr_params.shape[1])), (1, run_params['BW'], 1)) run_params['mtrx_params'] = np.tile( np.reshape(mtrx_params, (run_params['S'], 1, mtrx_params.shape[1])), (1, run_params['BW'], 1)) run_params['mtry_params'] = np.tile( np.reshape(mtry_params, (run_params['S'], 1, mtry_params.shape[1])), (1, run_params['BW'], 1)) ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' sim_params = {} for key in param_names: sim_params[key] = run_params[key] sim_params['output_dir'] = output_base sim_params['run_params'] = run_params income_tax_params, ss_parameters, iterative_params, chi_params = SS.create_steady_state_parameters( **sim_params) analytical_mtrs, etr_params, mtrx_params, mtry_params = income_tax_params print('ETR param shape = ', etr_params.shape) ss_outputs = SS.run_SS(income_tax_params, ss_parameters, iterative_params, chi_params, baseline, fix_transfers=fix_transfers, baseline_dir=baseline_dir) ''' ------------------------------------------------------------------------ Pickle SS results and parameters of run ------------------------------------------------------------------------ ''' if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) param_dir = os.path.join(baseline_dir, "run_parameters.pkl") pickle.dump(sim_params, open(param_dir, "wb")) else: utils.mkdirs(os.path.join(output_base, "SS")) ss_dir = os.path.join(output_base, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) param_dir = os.path.join(output_base, "run_parameters.pkl") pickle.dump(sim_params, open(param_dir, "wb")) ''' ------------------------------------------------------------------------ Run the TPI simulation ------------------------------------------------------------------------ ''' sim_params['baseline'] = baseline sim_params['input_dir'] = output_base sim_params['baseline_dir'] = baseline_dir income_tax_params, tpi_params, iterative_params, initial_values, SS_values = TPI.create_tpi_params( **sim_params) tpi_output, macro_output = TPI.run_TPI(income_tax_params, tpi_params, iterative_params, initial_values, SS_values, fix_transfers=fix_transfers, output_dir=output_base) ''' ------------------------------------------------------------------------ Pickle TPI results ------------------------------------------------------------------------ ''' tpi_dir = os.path.join(output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_vars.pkl") pickle.dump(tpi_output, open(tpi_vars, "wb")) tpi_dir = os.path.join(output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_macro_vars.pkl") pickle.dump(macro_output, open(tpi_vars, "wb")) print "Time path iteration complete. It" print "took {0} seconds to get that part done.".format(time.time() - tick)
def runner(output_base, baseline_dir, test=False, time_path=True, baseline=False, analytical_mtrs=False, age_specific=False, reform={}, user_params={}, guid='', run_micro=True, small_open=False, budget_balance=False, baseline_spending=False): #from ogusa import parameters, wealth, labor, demographics, income from ogusa import parameters, demographics, income, utils from ogusa import txfunc tick = time.time() # Make sure options are internally consistent if baseline == True and baseline_spending == True: print( 'Inconsistent options. Setting <baseline_spending> to False, leaving <baseline> True.' ) baseline_spending = False if budget_balance == True and baseline_spending == True: print( 'Inconsistent options. Setting <baseline_spending> to False, leaving <budget_balance> True.' ) baseline_spending = False #Create output directory structure saved_moments_dir = os.path.join(output_base, "Saved_moments") ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [saved_moments_dir, ss_dir, tpi_dir] for _dir in dirs: try: print("making dir: ", _dir) os.makedirs(_dir) except OSError as oe: pass if run_micro: txfunc.get_tax_func_estimate(baseline=baseline, analytical_mtrs=analytical_mtrs, age_specific=age_specific, start_year=user_params['start_year'], reform=reform, guid=guid) print('In runner, baseline is ', baseline) run_params = ogusa.parameters.get_parameters(test=test, baseline=baseline, guid=guid) run_params['analytical_mtrs'] = analytical_mtrs run_params['small_open'] = small_open run_params['budget_balance'] = budget_balance # Modify ogusa parameters based on user input if 'frisch' in user_params: print("updating frisch and associated") b_ellipse, upsilon = ogusa.elliptical_u_est.estimation( user_params['frisch'], run_params['ltilde']) run_params['b_ellipse'] = b_ellipse run_params['upsilon'] = upsilon run_params.update(user_params) if 'debt_ratio_ss' in user_params: run_params['debt_ratio_ss'] = user_params['debt_ratio_ss'] # Modify ogusa parameters based on user input if 'g_y_annual' in user_params: print("updating g_y_annual and associated") ending_age = run_params['ending_age'] starting_age = run_params['starting_age'] S = run_params['S'] g_y = (1 + user_params['g_y_annual'])**( float(ending_age - starting_age) / S) - 1 run_params['g_y'] = g_y run_params.update(user_params) # Modify transfer & spending ratios based on user input. if 'T_shifts' in user_params: if baseline_spending == False: print('updating ALPHA_T with T_shifts in first', user_params['T_shifts'].size, 'periods.') T_shifts = np.concatenate((user_params['T_shifts'], np.zeros(run_params['ALPHA_T'].size - user_params['T_shifts'].size)), axis=0) run_params['ALPHA_T'] = run_params['ALPHA_T'] + T_shifts if 'G_shifts' in user_params: if baseline_spending == False: print('updating ALPHA_G with G_shifts in first', user_params['G_shifts'].size, 'periods.') G_shifts = np.concatenate((user_params['G_shifts'], np.zeros(run_params['ALPHA_G'].size - user_params['G_shifts'].size)), axis=0) run_params['ALPHA_G'] = run_params['ALPHA_G'] + G_shifts from ogusa import SS, TPI calibrate_model = False # List of parameter names that will not be changing (unless we decide to # change them for a tax experiment) param_names = [ 'S', 'J', 'T', 'BW', 'lambdas', 'starting_age', 'ending_age', 'beta', 'sigma', 'alpha', 'gamma', 'epsilon', 'nu', 'Z', 'delta', 'E', 'ltilde', 'g_y', 'maxiter', 'mindist_SS', 'mindist_TPI', 'analytical_mtrs', 'b_ellipse', 'k_ellipse', 'upsilon', 'small_open', 'budget_balance', 'ss_firm_r', 'ss_hh_r', 'tpi_firm_r', 'tpi_hh_r', 'tG1', 'tG2', 'alpha_T', 'alpha_G', 'ALPHA_T', 'ALPHA_G', 'rho_G', 'debt_ratio_ss', 'tau_b', 'delta_tau', 'chi_b_guess', 'chi_n_guess', 'etr_params', 'mtrx_params', 'mtry_params', 'tau_payroll', 'tau_bq', 'retire', 'mean_income_data', 'g_n_vector', 'h_wealth', 'p_wealth', 'm_wealth', 'omega', 'g_n_ss', 'omega_SS', 'surv_rate', 'imm_rates', 'e', 'rho', 'initial_debt', 'omega_S_preTP' ] ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' sim_params = {} for key in param_names: sim_params[key] = run_params[key] sim_params['output_dir'] = output_base sim_params['run_params'] = run_params income_tax_params, ss_parameters, iterative_params, chi_params, small_open_params = SS.create_steady_state_parameters( **sim_params) ss_outputs = SS.run_SS(income_tax_params, ss_parameters, iterative_params, chi_params, small_open_params, baseline, baseline_spending, baseline_dir=baseline_dir) ''' ------------------------------------------------------------------------ Pickle SS results ------------------------------------------------------------------------ ''' if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) else: utils.mkdirs(os.path.join(output_base, "SS")) ss_dir = os.path.join(output_base, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) if time_path: ''' ------------------------------------------------------------------------ Run the TPI simulation ------------------------------------------------------------------------ ''' sim_params['baseline'] = baseline sim_params['baseline_spending'] = baseline_spending sim_params['input_dir'] = output_base sim_params['baseline_dir'] = baseline_dir income_tax_params, tpi_params, iterative_params, small_open_params, initial_values, SS_values, fiscal_params, biz_tax_params = TPI.create_tpi_params( **sim_params) tpi_output, macro_output = TPI.run_TPI( income_tax_params, tpi_params, iterative_params, small_open_params, initial_values, SS_values, fiscal_params, biz_tax_params, output_dir=output_base, baseline_spending=baseline_spending) ''' ------------------------------------------------------------------------ Pickle TPI results ------------------------------------------------------------------------ ''' tpi_dir = os.path.join(output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_vars.pkl") pickle.dump(tpi_output, open(tpi_vars, "wb")) tpi_dir = os.path.join(output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_macro_vars.pkl") pickle.dump(macro_output, open(tpi_vars, "wb")) print("Time path iteration complete.") print("It took {0} seconds to get that part done.".format(time.time() - tick))
def run_model(meta_param_dict, adjustment): ''' Initializes classes from OG-USA that compute the model under different policies. Then calls function get output objects. ''' print('Meta_param_dict = ', meta_param_dict) print('adjustment dict = ', adjustment) meta_params = MetaParams() meta_params.adjust(meta_param_dict) if meta_params.data_source == "PUF": data = retrieve_puf(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) # set name of cached baseline file in case use below cached_pickle = 'TxFuncEst_baseline_PUF.pkl' else: data = "cps" # set name of cached baseline file in case use below cached_pickle = 'TxFuncEst_baseline_CPS.pkl' # Get TC params adjustments iit_mods = convert_policy_adjustment( adjustment["Tax-Calculator Parameters"]) # Create output directory structure base_dir = os.path.join(CUR_DIR, BASELINE_DIR) reform_dir = os.path.join(CUR_DIR, REFORM_DIR) dirs = [base_dir, reform_dir] for _dir in dirs: utils.mkdirs(_dir) # Dask parmeters client = Client() num_workers = 5 # TODO: Swap to these parameters when able to specify tax function # and model workers separately # num_workers_txf = 5 # num_workers_mod = 6 # whether to estimate tax functions from microdata run_micro = True time_path = meta_param_dict['time_path'][0]['value'] # filter out OG-USA params that will not change between baseline and # reform runs (these are the non-policy parameters) filtered_ogusa_params = {} constant_param_set = { 'frisch', 'beta_annual', 'sigma', 'g_y_annual', 'gamma', 'epsilon', 'Z', 'delta_annual', 'small_open', 'world_int_rate', 'initial_foreign_debt_ratio', 'zeta_D', 'zeta_K', 'tG1', 'tG2', 'rho_G', 'debt_ratio_ss', 'budget_balance' } filtered_ogusa_params = OrderedDict() for k, v in adjustment['OG-USA Parameters'].items(): if k in constant_param_set: filtered_ogusa_params[k] = v # Solve baseline model start_year = meta_param_dict['year'][0]['value'] if start_year == 2020: OGPATH = inspect.getfile(SS) OGDIR = os.path.dirname(OGPATH) tax_func_path = None #os.path.join(OGDIR, 'data', 'tax_functions', # cached_pickle) run_micro_baseline = False else: tax_func_path = None run_micro_baseline = True base_spec = { **{ 'start_year': start_year, 'tax_func_type': 'DEP', 'age_specific': False }, **filtered_ogusa_params } base_params = Specifications(run_micro=False, output_base=base_dir, baseline_dir=base_dir, test=False, time_path=False, baseline=True, iit_reform={}, guid='', data=data, client=client, num_workers=num_workers) base_params.update_specifications(base_spec) base_params.get_tax_function_parameters(client, run_micro_baseline, tax_func_path=tax_func_path) base_ss = SS.run_SS(base_params, client=client) utils.mkdirs(os.path.join(base_dir, "SS")) base_ss_dir = os.path.join(base_dir, "SS", "SS_vars.pkl") with open(base_ss_dir, "wb") as f: pickle.dump(base_ss, f) if time_path: base_tpi = TPI.run_TPI(base_params, client=client) tpi_dir = os.path.join(base_dir, "TPI", "TPI_vars.pkl") with open(tpi_dir, "wb") as f: pickle.dump(base_tpi, f) else: base_tpi = None # Solve reform model reform_spec = base_spec reform_spec.update(adjustment["OG-USA Parameters"]) reform_params = Specifications(run_micro=False, output_base=reform_dir, baseline_dir=base_dir, test=False, time_path=time_path, baseline=False, iit_reform=iit_mods, guid='', data=data, client=client, num_workers=num_workers) reform_params.update_specifications(reform_spec) reform_params.get_tax_function_parameters(client, run_micro) reform_ss = SS.run_SS(reform_params, client=client) utils.mkdirs(os.path.join(reform_dir, "SS")) reform_ss_dir = os.path.join(reform_dir, "SS", "SS_vars.pkl") with open(reform_ss_dir, "wb") as f: pickle.dump(reform_ss, f) if time_path: reform_tpi = TPI.run_TPI(reform_params, client=client) else: reform_tpi = None comp_dict = comp_output(base_params, base_ss, reform_params, reform_ss, time_path, base_tpi, reform_tpi) # Shut down client and make sure all of its references are # cleaned up. client.close() del client return comp_dict
def plot_all(base_output_path, reform_output_path, save_path): ''' Function to plot all default output plots. Args: base_output_path (str): path to baseline results reform_output_path (str): path to reform results save_path (str): path to save plots to Returns: None: All output figures saved to disk. ''' # Make directory in case it doesn't exist utils.mkdirs(save_path) # Read in data # Read in TPI output and parameters base_tpi = utils.safe_read_pickle( os.path.join(base_output_path, 'TPI', 'TPI_vars.pkl') ) base_ss = utils.safe_read_pickle( os.path.join(base_output_path, 'SS', 'SS_vars.pkl') ) base_params = utils.safe_read_pickle( os.path.join(base_output_path, 'model_params.pkl') ) reform_tpi = utils.safe_read_pickle( os.path.join(reform_output_path, 'TPI', 'TPI_vars.pkl') ) reform_ss = utils.safe_read_pickle( os.path.join(reform_output_path, 'SS', 'SS_vars.pkl') ) reform_params = utils.safe_read_pickle( os.path.join(reform_output_path, 'model_params.pkl') ) # Percentage changes in macro vars (Y, K, L, C) plot_aggregates(base_tpi, base_params, reform_tpi=reform_tpi, reform_params=reform_params, var_list=['Y', 'K', 'L', 'C'], plot_type='pct_diff', num_years_to_plot=150, start_year=base_params.start_year, vertical_line_years=[ base_params.start_year + base_params.tG1, base_params.start_year + base_params.tG2], plot_title='Percentage Changes in Macro Aggregates', path=os.path.join(save_path, 'MacroAgg_PctChange.png')) # Percentage change in fiscal vars (D, G, TR, Rev) plot_aggregates(base_tpi, base_params, reform_tpi=reform_tpi, reform_params=reform_params, var_list=['D', 'G', 'TR', 'total_tax_revenue'], plot_type='pct_diff', num_years_to_plot=150, start_year=base_params.start_year, vertical_line_years=[ base_params.start_year + base_params.tG1, base_params.start_year + base_params.tG2], plot_title='Percentage Changes in Fiscal Variables', path=os.path.join(save_path, 'Fiscal_PctChange.png')) # r and w in baseline and reform -- vertical lines at tG1, tG2 plot_aggregates(base_tpi, base_params, reform_tpi=reform_tpi, reform_params=reform_params, var_list=['r'], plot_type='levels', num_years_to_plot=150, start_year=base_params.start_year, vertical_line_years=[ base_params.start_year + base_params.tG1, base_params.start_year + base_params.tG2], plot_title='Real Interest Rates Under Baseline and Reform', path=os.path.join(save_path, 'InterestRates.png')) plot_aggregates(base_tpi, base_params, reform_tpi=reform_tpi, reform_params=reform_params, var_list=['w'], plot_type='levels', num_years_to_plot=150, start_year=base_params.start_year, vertical_line_years=[ base_params.start_year + base_params.tG1, base_params.start_year + base_params.tG2], plot_title='Wage Rates Under Baseline and Reform', path=os.path.join(save_path, 'WageRates.png')) # Debt-GDP in base and reform-- vertical lines at tG1, tG2 plot_gdp_ratio(base_tpi, base_params, reform_tpi, reform_params, var_list=['D'], num_years_to_plot=150, start_year=base_params.start_year, vertical_line_years=[ base_params.start_year + base_params.tG1, base_params.start_year + base_params.tG2], plot_title='Debt-to-GDP', path=os.path.join(save_path, 'DebtGDPratio.png')) # Tax revenue to GDP in base and reform-- vertical lines at tG1, tG2 plot_gdp_ratio(base_tpi, base_params, reform_tpi, reform_params, var_list=['total_tax_revenue'], num_years_to_plot=150, start_year=base_params.start_year, vertical_line_years=[ base_params.start_year + base_params.tG1, base_params.start_year + base_params.tG2], plot_title='Tax Revenue to GDP', path=os.path.join(save_path, 'RevenueGDPratio.png')) # Pct change in c, n, b, y, etr, mtrx, mtry by ability group over 10 years var_list = ['c_path', 'n_mat', 'bmat_splus1', 'etr_path', 'mtrx_path', 'mtry_path', 'y_before_tax_mat'] title_list = ['consumption', 'labor supply', 'savings', 'effective tax rates', 'marginal tax rates on labor income', 'marginal tax rates on capital income', 'before tax income'] path_list = ['Cons', 'Labor', 'Save', 'ETR', 'MTRx', 'MTRy', 'Income'] for i, v in enumerate(var_list): ability_bar(base_tpi, base_params, reform_tpi, reform_params, var=v, num_years=10, start_year=base_params.start_year, plot_title='Percentage changes in ' + title_list[i], path=os.path.join(save_path, 'PctChange_' + path_list[i] + '.png')) # lifetime profiles, base vs reform, SS for c, n, b, y - not by j var_list = ['cssmat', 'nssmat', 'bssmat_splus1', 'etr_ss', 'mtrx_ss', 'mtry_ss'] for i, v in enumerate(var_list): ss_profiles(base_ss, base_params, reform_ss, reform_params, by_j=False, var=v, plot_title='Lifecycle Profile of ' + title_list[i], path=os.path.join(save_path, 'SSLifecycleProfile_' + path_list[i] + '.png')) # lifetime profiles, c, n , b, y by j, separately for base and reform for i, v in enumerate(var_list): ss_profiles(base_ss, base_params, by_j=True, var=v, plot_title='Lifecycle Profile of ' + title_list[i], path=os.path.join(save_path, 'SSLifecycleProfile_' + path_list[i] + '_Baseline.png')) ss_profiles(reform_ss, reform_params, by_j=True, var=v, plot_title='Lifecycle Profile of ' + title_list[i], path=os.path.join(save_path, 'SSLifecycleProfile_' + path_list[i] + '_Reform.png'))
def runner(output_base, baseline_dir, baseline=False, analytical_mtrs=True, age_specific=False, reform={}, user_params={}, guid='', run_micro=True): #from ogusa import parameters, wealth, labor, demographics, income from ogusa import parameters, wealth, labor, demog, income, utils from ogusa import txfunc tick = time.time() #Create output directory structure saved_moments_dir = os.path.join(output_base, "Saved_moments") ssinit_dir = os.path.join(output_base, "SSinit") tpiinit_dir = os.path.join(output_base, "TPIinit") dirs = [saved_moments_dir, ssinit_dir, tpiinit_dir] for _dir in dirs: try: print "making dir: ", _dir os.makedirs(_dir) except OSError as oe: pass if run_micro: txfunc.get_tax_func_estimate(baseline=baseline, analytical_mtrs=analytical_mtrs, age_specific=age_specific, start_year=user_params['start_year'], reform=reform, guid=guid) print ("in runner, baseline is ", baseline) run_params = ogusa.parameters.get_parameters(baseline=baseline, guid=guid) run_params['analytical_mtrs'] = analytical_mtrs # Modify ogusa parameters based on user input if 'frisch' in user_params: print "updating fricsh and associated" b_ellipse, upsilon = ogusa.elliptical_u_est.estimation(user_params['frisch'], run_params['ltilde']) run_params['b_ellipse'] = b_ellipse run_params['upsilon'] = upsilon run_params.update(user_params) # Modify ogusa parameters based on user input if 'g_y_annual' in user_params: print "updating g_y_annual and associated" g_y = (1 + user_params['g_y_annual'])**(float(ending_age - starting_age) / S) - 1 run_params['g_y'] = g_y run_params.update(user_params) from ogusa import SS, TPI # Generate Wealth data moments wealth.get_wealth_data(run_params['lambdas'], run_params['J'], run_params['flag_graphs'], output_dir=output_base) # Generate labor data moments labor.labor_data_moments(run_params['flag_graphs'], output_dir=output_base) calibrate_model = False # List of parameter names that will not be changing (unless we decide to # change them for a tax experiment) param_names = ['S', 'J', 'T', 'BW', 'lambdas', 'starting_age', 'ending_age', 'beta', 'sigma', 'alpha', 'nu', 'Z', 'delta', 'E', 'ltilde', 'g_y', 'maxiter', 'mindist_SS', 'mindist_TPI', 'analytical_mtrs', 'b_ellipse', 'k_ellipse', 'upsilon', 'chi_b_guess', 'chi_n_guess','etr_params','mtrx_params', 'mtry_params','tau_payroll', 'tau_bq', 'retire', 'mean_income_data', 'g_n_vector', 'h_wealth', 'p_wealth', 'm_wealth', 'omega', 'g_n_ss', 'omega_SS', 'surv_rate', 'e', 'rho'] ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' sim_params = {} for key in param_names: sim_params[key] = run_params[key] sim_params['output_dir'] = output_base sim_params['run_params'] = run_params income_tax_params, ss_parameters, iterative_params, chi_params = SS.create_steady_state_parameters(**sim_params) ss_outputs = SS.run_SS(income_tax_params, ss_parameters, iterative_params, chi_params, baseline, baseline_dir=baseline_dir) ''' ------------------------------------------------------------------------ Pickle SS results ------------------------------------------------------------------------ ''' if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/ss_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) else: utils.mkdirs(os.path.join(output_dir, "SS")) ss_dir = os.path.join(output_dir, "SS/ss_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) ''' ------------------------------------------------------------------------ Run the baseline TPI simulation ------------------------------------------------------------------------ ''' sim_params['input_dir'] = output_base sim_params['baseline_dir'] = baseline_dir income_tax_params, tpi_params, iterative_params, initial_values, SS_values = TPI.create_tpi_params(**sim_params) # ss_outputs['income_tax_params'] = income_tax_params # ss_outputs['wealth_tax_params'] = wealth_tax_params # ss_outputs['ellipse_params'] = ellipse_params # ss_outputs['parameters'] = parameters # ss_outputs['N_tilde'] = N_tilde # ss_outputs['omega_stationary'] = omega_stationary # ss_outputs['K0'] = K0 # ss_outputs['b_sinit'] = b_sinit # ss_outputs['b_splus1init'] = b_splus1init # ss_outputs['L0'] = L0 # ss_outputs['Y0'] = Y0 # ss_outputs['r0'] = r0 # ss_outputs['BQ0'] = BQ0 # ss_outputs['T_H_0'] = T_H_0 # ss_outputs['factor_ss'] = factor # ss_outputs['tax0'] = tax0 # ss_outputs['c0'] = c0 # ss_outputs['initial_b'] = initial_b # ss_outputs['initial_n'] = initial_n # ss_outputs['tau_bq'] = tau_bq # ss_outputs['g_n_vector'] = g_n_vector # ss_outputs['output_dir'] = output_base # with open("ss_outputs.pkl", 'wb') as fp: # pickle.dump(ss_outputs, fp) w_path, r_path, T_H_path, BQ_path, Y_path = TPI.run_TPI(income_tax_params, tpi_params, iterative_params, initial_values, SS_values, output_dir=output_base) print "getting to here...." TPI.TP_solutions(w_path, r_path, T_H_path, BQ_path, **ss_outputs) print "took {0} seconds to get that part done.".format(time.time() - tick)
def runner(output_base, baseline_dir, test=False, time_path=True, baseline=False, constant_rates=True, tax_func_type='DEP', analytical_mtrs=False, age_specific=False, reform={}, user_params={}, guid='', run_micro=True, small_open=False, budget_balance=False, baseline_spending=False, data=None, client=None, num_workers=1): from ogusa import parameters, demographics, income, utils tick = time.time() start_year = user_params.get('start_year', DEFAULT_START_YEAR) if start_year > TC_LAST_YEAR: raise RuntimeError("Start year is beyond data extrapolation.") # Make sure options are internally consistent if baseline and baseline_spending: print("Inconsistent options. Setting <baseline_spending> to False, " "leaving <baseline> True.'") baseline_spending = False if budget_balance and baseline_spending: print("Inconsistent options. Setting <baseline_spending> to False, " "leaving <budget_balance> True.") baseline_spending = False # Create output directory structure ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [ss_dir, tpi_dir] for _dir in dirs: try: print("making dir: ", _dir) os.makedirs(_dir) except OSError as oe: pass print('In runner, baseline is ', baseline) if small_open and (not isinstance(small_open, dict)): raise ValueError( 'small_open must be False/None or a dict with keys: {}'.format( SMALL_OPEN_KEYS)) small_open = small_open or {} run_params = ogusa.parameters.get_parameters( output_base, reform=reform, test=test, baseline=baseline, guid=guid, run_micro=run_micro, constant_rates=constant_rates, analytical_mtrs=analytical_mtrs, tax_func_type=tax_func_type, age_specific=age_specific, start_year=start_year, data=data, client=client, num_workers=num_workers, **small_open) run_params['analytical_mtrs'] = analytical_mtrs run_params['small_open'] = bool(small_open) run_params['budget_balance'] = budget_balance run_params['world_int_rate'] = small_open.get('world_int_rate', DEFAULT_WORLD_INT_RATE) # Modify ogusa parameters based on user input if 'frisch' in user_params: print("updating frisch and associated") b_ellipse, upsilon = ogusa.elliptical_u_est.estimation( user_params['frisch'], run_params['ltilde']) run_params['b_ellipse'] = b_ellipse run_params['upsilon'] = upsilon run_params.update(user_params) if 'debt_ratio_ss' in user_params: run_params['debt_ratio_ss'] = user_params['debt_ratio_ss'] if 'tau_b' in user_params: run_params['tau_b'] = user_params['tau_b'] # Modify ogusa parameters based on user input if 'g_y_annual' in user_params: print("updating g_y_annual and associated") ending_age = run_params['ending_age'] starting_age = run_params['starting_age'] S = run_params['S'] g_y = ((1 + user_params['g_y_annual']) **(float(ending_age - starting_age) / S) - 1) run_params['g_y'] = g_y run_params.update(user_params) # Modify transfer & spending ratios based on user input. if 'T_shifts' in user_params: if not baseline_spending: print('updating ALPHA_T with T_shifts in first', user_params['T_shifts'].size, 'periods.') T_shifts = np.concatenate((user_params['T_shifts'], np.zeros(run_params['ALPHA_T'].size - user_params['T_shifts'].size)), axis=0) run_params['ALPHA_T'] = run_params['ALPHA_T'] + T_shifts if 'G_shifts' in user_params: if not baseline_spending: print('updating ALPHA_G with G_shifts in first', user_params['G_shifts'].size, 'periods.') G_shifts = np.concatenate((user_params['G_shifts'], np.zeros(run_params['ALPHA_G'].size - user_params['G_shifts'].size)), axis=0) run_params['ALPHA_G'] = run_params['ALPHA_G'] + G_shifts from ogusa import SS, TPI calibrate_model = False # List of parameter names that will not be changing (unless we decide to # change them for a tax experiment) param_names = [ 'S', 'J', 'T', 'BW', 'lambdas', 'starting_age', 'ending_age', 'beta', 'sigma', 'alpha', 'gamma', 'epsilon', 'nu', 'Z', 'delta', 'E', 'ltilde', 'g_y', 'maxiter', 'mindist_SS', 'mindist_TPI', 'analytical_mtrs', 'b_ellipse', 'k_ellipse', 'upsilon', 'small_open', 'budget_balance', 'ss_firm_r', 'ss_hh_r', 'tpi_firm_r', 'tpi_hh_r', 'tG1', 'tG2', 'alpha_T', 'alpha_G', 'ALPHA_T', 'ALPHA_G', 'rho_G', 'debt_ratio_ss', 'tau_b', 'delta_tau', 'chi_b_guess', 'chi_n_guess', 'etr_params', 'mtrx_params', 'mtry_params', 'tau_payroll', 'tau_bq', 'retire', 'mean_income_data', 'g_n_vector', 'h_wealth', 'p_wealth', 'm_wealth', 'omega', 'g_n_ss', 'omega_SS', 'surv_rate', 'imm_rates', 'e', 'rho', 'initial_debt', 'omega_S_preTP' ] ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' sim_params = {} for key in param_names: sim_params[key] = run_params[key] sim_params['output_dir'] = output_base sim_params['run_params'] = run_params sim_params['tax_func_type'] = tax_func_type (income_tax_params, ss_parameters, iterative_params, chi_params, small_open_params) = SS.create_steady_state_parameters(**sim_params) ss_outputs = SS.run_SS(income_tax_params, ss_parameters, iterative_params, chi_params, small_open_params, baseline, baseline_spending, baseline_dir=baseline_dir, client=client, num_workers=num_workers) ''' ------------------------------------------------------------------------ Pickle SS results ------------------------------------------------------------------------ ''' model_params = {} for key in param_names: model_params[key] = sim_params[key] if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) # Save pickle with parameter values for the run param_dir = os.path.join(baseline_dir, "model_params.pkl") pickle.dump(model_params, open(param_dir, "wb")) else: utils.mkdirs(os.path.join(output_base, "SS")) ss_dir = os.path.join(output_base, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) # Save pickle with parameter values for the run param_dir = os.path.join(output_base, "model_params.pkl") pickle.dump(model_params, open(param_dir, "wb")) if time_path: ''' ------------------------------------------------------------------------ Run the TPI simulation ------------------------------------------------------------------------ ''' sim_params['baseline'] = baseline sim_params['baseline_spending'] = baseline_spending sim_params['input_dir'] = output_base sim_params['baseline_dir'] = baseline_dir (income_tax_params, tpi_params, iterative_params, small_open_params, initial_values, SS_values, fiscal_params, biz_tax_params) =\ TPI.create_tpi_params(**sim_params) tpi_output = TPI.run_TPI(income_tax_params, tpi_params, iterative_params, small_open_params, initial_values, SS_values, fiscal_params, biz_tax_params, output_dir=output_base, baseline_spending=baseline_spending, client=client, num_workers=num_workers) ''' ------------------------------------------------------------------------ Pickle TPI results ------------------------------------------------------------------------ ''' tpi_dir = os.path.join(output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_vars.pkl") pickle.dump(tpi_output, open(tpi_vars, "wb")) print("Time path iteration complete.") print("It took {0} seconds to get that part done.".format(time.time() - tick))
def runner(output_base, baseline_dir, test=False, time_path=True, baseline=False, analytical_mtrs=False, age_specific=False, reform={}, user_params={}, guid='', run_micro=True, small_open=False, budget_balance=False, baseline_spending=False): #from ogusa import parameters, wealth, labor, demographics, income from ogusa import parameters, demographics, income, utils from ogusa import txfunc tick = time.time() # Make sure options are internally consistent if baseline==True and baseline_spending==True: print 'Inconsistent options. Setting <baseline_spending> to False, leaving <baseline> True.' baseline_spending = False if budget_balance==True and baseline_spending==True: print 'Inconsistent options. Setting <baseline_spending> to False, leaving <budget_balance> True.' baseline_spending = False #Create output directory structure saved_moments_dir = os.path.join(output_base, "Saved_moments") ss_dir = os.path.join(output_base, "SS") tpi_dir = os.path.join(output_base, "TPI") dirs = [saved_moments_dir, ss_dir, tpi_dir] for _dir in dirs: try: print "making dir: ", _dir os.makedirs(_dir) except OSError as oe: pass if run_micro: txfunc.get_tax_func_estimate(baseline=baseline, analytical_mtrs=analytical_mtrs, age_specific=age_specific, start_year=user_params['start_year'], reform=reform, guid=guid) print 'In runner, baseline is ', baseline run_params = ogusa.parameters.get_parameters(test=test, baseline=baseline, guid=guid) run_params['analytical_mtrs'] = analytical_mtrs run_params['small_open'] = small_open run_params['budget_balance'] = budget_balance # Modify ogusa parameters based on user input if 'frisch' in user_params: print "updating frisch and associated" b_ellipse, upsilon = ogusa.elliptical_u_est.estimation(user_params['frisch'], run_params['ltilde']) run_params['b_ellipse'] = b_ellipse run_params['upsilon'] = upsilon run_params.update(user_params) if 'debt_ratio_ss' in user_params: run_params['debt_ratio_ss']=user_params['debt_ratio_ss'] # Modify ogusa parameters based on user input if 'g_y_annual' in user_params: print "updating g_y_annual and associated" ending_age = run_params['ending_age'] starting_age = run_params['starting_age'] S = run_params['S'] g_y = (1 + user_params['g_y_annual'])**(float(ending_age - starting_age) / S) - 1 run_params['g_y'] = g_y run_params.update(user_params) # Modify transfer & spending ratios based on user input. if 'T_shifts' in user_params: if baseline_spending==False: print 'updating ALPHA_T with T_shifts in first', user_params['T_shifts'].size, 'periods.' T_shifts = np.concatenate((user_params['T_shifts'], np.zeros(run_params['ALPHA_T'].size - user_params['T_shifts'].size)), axis=0) run_params['ALPHA_T'] = run_params['ALPHA_T'] + T_shifts if 'G_shifts' in user_params: if baseline_spending==False: print 'updating ALPHA_G with G_shifts in first', user_params['G_shifts'].size, 'periods.' G_shifts = np.concatenate((user_params['G_shifts'], np.zeros(run_params['ALPHA_G'].size - user_params['G_shifts'].size)), axis=0) run_params['ALPHA_G'] = run_params['ALPHA_G'] + G_shifts from ogusa import SS, TPI calibrate_model = False # List of parameter names that will not be changing (unless we decide to # change them for a tax experiment) param_names = ['S', 'J', 'T', 'BW', 'lambdas', 'starting_age', 'ending_age', 'beta', 'sigma', 'alpha', 'gamma', 'epsilon', 'nu', 'Z', 'delta', 'E', 'ltilde', 'g_y', 'maxiter', 'mindist_SS', 'mindist_TPI', 'analytical_mtrs', 'b_ellipse', 'k_ellipse', 'upsilon', 'small_open', 'budget_balance', 'ss_firm_r', 'ss_hh_r', 'tpi_firm_r', 'tpi_hh_r', 'tG1', 'tG2', 'alpha_T', 'alpha_G', 'ALPHA_T', 'ALPHA_G', 'rho_G', 'debt_ratio_ss', 'tau_b', 'delta_tau', 'chi_b_guess', 'chi_n_guess','etr_params','mtrx_params', 'mtry_params','tau_payroll', 'tau_bq', 'retire', 'mean_income_data', 'g_n_vector', 'h_wealth', 'p_wealth', 'm_wealth', 'omega', 'g_n_ss', 'omega_SS', 'surv_rate', 'imm_rates','e', 'rho', 'initial_debt','omega_S_preTP'] ''' ------------------------------------------------------------------------ Run SS ------------------------------------------------------------------------ ''' sim_params = {} for key in param_names: sim_params[key] = run_params[key] sim_params['output_dir'] = output_base sim_params['run_params'] = run_params income_tax_params, ss_parameters, iterative_params, chi_params, small_open_params = SS.create_steady_state_parameters(**sim_params) ss_outputs = SS.run_SS(income_tax_params, ss_parameters, iterative_params, chi_params, small_open_params, baseline, baseline_spending, baseline_dir=baseline_dir) ''' ------------------------------------------------------------------------ Pickle SS results ------------------------------------------------------------------------ ''' if baseline: utils.mkdirs(os.path.join(baseline_dir, "SS")) ss_dir = os.path.join(baseline_dir, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) else: utils.mkdirs(os.path.join(output_base, "SS")) ss_dir = os.path.join(output_base, "SS/SS_vars.pkl") pickle.dump(ss_outputs, open(ss_dir, "wb")) if time_path: ''' ------------------------------------------------------------------------ Run the TPI simulation ------------------------------------------------------------------------ ''' sim_params['baseline'] = baseline sim_params['baseline_spending'] = baseline_spending sim_params['input_dir'] = output_base sim_params['baseline_dir'] = baseline_dir income_tax_params, tpi_params, iterative_params, small_open_params, initial_values, SS_values, fiscal_params, biz_tax_params = TPI.create_tpi_params(**sim_params) tpi_output, macro_output = TPI.run_TPI(income_tax_params, tpi_params, iterative_params, small_open_params, initial_values, SS_values, fiscal_params, biz_tax_params, output_dir=output_base, baseline_spending=baseline_spending) ''' ------------------------------------------------------------------------ Pickle TPI results ------------------------------------------------------------------------ ''' tpi_dir = os.path.join(output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_vars.pkl") pickle.dump(tpi_output, open(tpi_vars, "wb")) tpi_dir = os.path.join(output_base, "TPI") utils.mkdirs(tpi_dir) tpi_vars = os.path.join(tpi_dir, "TPI_macro_vars.pkl") pickle.dump(macro_output, open(tpi_vars, "wb")) print "Time path iteration complete." print "It took {0} seconds to get that part done.".format(time.time() - tick)