def test_consumption_response(cps_subsample): consump = Consumption() mpc = 0.5 consumption_response = {'MPC_e20400': {2013: mpc}} consump.update_consumption(consumption_response) # test incorrect call to response method with pytest.raises(ValueError): consump.response(list(), 1) # test correct call to response method rec = Records.cps_constructor(data=cps_subsample) pre = copy.deepcopy(rec.e20400) consump.response(rec, 1.0) post = rec.e20400 actual_diff = post - pre expected_diff = np.ones(rec.array_length) * mpc assert np.allclose(actual_diff, expected_diff) # compute earnings mtr with no consumption response rec = Records.cps_constructor(data=cps_subsample) ided0 = copy.deepcopy(rec.e20400) calc0 = Calculator(policy=Policy(), records=rec, consumption=None) (mtr0_ptax, mtr0_itax, _) = calc0.mtr(variable_str='e00200p', wrt_full_compensation=False) assert np.allclose(calc0.array('e20400'), ided0) # compute earnings mtr with consumption response calc1 = Calculator(policy=Policy(), records=rec, consumption=consump) mtr1_ptax, mtr1_itax, _ = calc1.mtr(variable_str='e00200p', wrt_full_compensation=False) assert np.allclose(calc1.array('e20400'), ided0) # confirm that payroll mtr values are no different assert np.allclose(mtr1_ptax, mtr0_ptax) # confirm that all mtr with cons-resp are no greater than without cons-resp assert np.all(np.less_equal(np.around(mtr1_itax, decimals=5), np.around(mtr0_itax, decimals=5))) # confirm that some mtr with cons-resp are less than without cons-resp assert np.any(np.less(mtr1_itax, mtr0_itax))
def test_dec_graph_plots(cps_subsample): pol = Policy() rec = Records.cps_constructor(data=cps_subsample) calc1 = Calculator(policy=pol, records=rec) year = 2020 calc1.advance_to_year(year) reform = { 'SS_Earnings_c': {year: 9e99}, # OASDI FICA tax on all earnings 'FICA_ss_trt': {year: 0.107484} # lower rate to keep revenue unchanged } pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.advance_to_year(year) assert calc1.current_year == calc2.current_year calc1.calc_all() calc2.calc_all() fig = calc1.decile_graph(calc2) assert fig dt1, dt2 = calc1.distribution_tables(calc2, 'weighted_deciles') dta = dec_graph_data(dt1, dt2, year, include_zero_incomes=True, include_negative_incomes=False) assert isinstance(dta, dict) dta = dec_graph_data(dt1, dt2, year, include_zero_incomes=False, include_negative_incomes=True) assert isinstance(dta, dict) dta = dec_graph_data(dt1, dt2, year, include_zero_incomes=False, include_negative_incomes=False) assert isinstance(dta, dict)
def test_make_calculator_with_policy_reform(cps_subsample): """ Test Calculator class ctor with policy reform. """ rec = Records.cps_constructor(data=cps_subsample) year = rec.current_year # create a Policy object and apply a policy reform pol = Policy() reform = {2013: {'_II_em': [4000], '_II_em_cpi': False, '_STD_Aged': [[1600, 1300, 1300, 1600, 1600]], '_STD_Aged_cpi': False}} pol.implement_reform(reform) # create a Calculator object using this policy reform calc = Calculator(policy=pol, records=rec) # check that Policy object embedded in Calculator object is correct assert calc.current_year == year assert calc.policy_param('II_em') == 4000 assert np.allclose(calc.policy_param('_II_em'), np.array([4000] * Policy.DEFAULT_NUM_YEARS)) exp_STD_Aged = [[1600, 1300, 1300, 1600, 1600]] * Policy.DEFAULT_NUM_YEARS assert np.allclose(calc.policy_param('_STD_Aged'), np.array(exp_STD_Aged)) assert np.allclose(calc.policy_param('STD_Aged'), np.array([1600, 1300, 1300, 1600, 1600]))
def test_make_calculator_raises_on_no_policy(cps_subsample): """ Test Calculator ctor error with no policy argument. """ rec = Records.cps_constructor(data=cps_subsample) with pytest.raises(ValueError): Calculator(records=rec)
def test_n65(cps_subsample): """ Test n65 method. """ recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) assert calc.n65().sum() > 1500
def test_make_calculator_with_multiyear_reform(cps_subsample): """ Test Calculator class ctor with multi-year policy reform. """ rec = Records.cps_constructor(data=cps_subsample) year = rec.current_year # create a Policy object and apply a policy reform pol = Policy() reform = {2015: {}, 2016: {}} reform[2015]['_II_em'] = [5000, 6000] # reform values for 2015 and 2016 reform[2015]['_II_em_cpi'] = False reform[2016]['_STD_Aged'] = [[1600, 1300, 1600, 1300, 1600]] pol.implement_reform(reform) # create a Calculator object using this policy-reform calc = Calculator(policy=pol, records=rec) # check that Policy object embedded in Calculator object is correct assert pol.num_years == Policy.DEFAULT_NUM_YEARS assert calc.current_year == year assert calc.policy_param('II_em') == 3950 exp_II_em = [3900, 3950, 5000] + [6000] * (Policy.DEFAULT_NUM_YEARS - 3) assert np.allclose(calc.policy_param('_II_em'), np.array(exp_II_em)) calc.increment_year() calc.increment_year() assert calc.current_year == 2016 assert np.allclose(calc.policy_param('STD_Aged'), np.array([1600, 1300, 1600, 1300, 1600]))
def test_write_graph_file(cps_subsample): recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) mtr = 0.20 * np.ones_like(cps_subsample['e00200']) vdf = calc.dataframe(['s006', 'e00200', 'c00100']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr gdata = mtr_graph_data(vdf, calc.current_year, mtr_measure='ptax', alt_e00200p_text='Taxpayer Earnings', income_measure='agi', dollar_weighting=False) gplot = xtr_graph_plot(gdata) assert gplot htmlfname = temporary_filename(suffix='.html') try: write_graph_file(gplot, htmlfname, 'title') except Exception: # pylint: disable=broad-except if os.path.isfile(htmlfname): try: os.remove(htmlfname) except OSError: pass # sometimes we can't remove a generated temporary file assert 'write_graph_file()_ok' == 'no' # if try was successful, try to remove the file if os.path.isfile(htmlfname): try: os.remove(htmlfname) except OSError: pass # sometimes we can't remove a generated temporary file
def test_mtr_graph_data(cps_subsample): recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) year = calc.current_year with pytest.raises(ValueError): mtr_graph_data(None, year, mars='bad', income_measure='agi', dollar_weighting=True) with pytest.raises(ValueError): mtr_graph_data(None, year, mars=0, income_measure='expanded_income', dollar_weighting=True) with pytest.raises(ValueError): mtr_graph_data(None, year, mars=list()) with pytest.raises(ValueError): mtr_graph_data(None, year, mars='ALL', mtr_variable='e00200s') with pytest.raises(ValueError): mtr_graph_data(None, year, mtr_measure='badtax') with pytest.raises(ValueError): mtr_graph_data(None, year, income_measure='badincome') mtr = 0.20 * np.ones_like(cps_subsample['e00200']) vdf = calc.dataframe(['s006', 'MARS', 'e00200']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr vdf = vdf[vdf['MARS'] == 1] gdata = mtr_graph_data(vdf, year, mars=1, mtr_wrt_full_compen=True, income_measure='wages', dollar_weighting=True) assert isinstance(gdata, dict)
def test_itax_compare(tests_path, using_puf, puf_fullsample, cps_fullsample): """ Conduct income tax comparisons using ITAX data. """ using_puf_adjust_ratios = True # generate 2015 estimates by AGI category using Tax-Calculator if using_puf: if using_puf_adjust_ratios: recs = Records(data=puf_fullsample) else: recs = Records(data=puf_fullsample, adjust_ratios=None) else: recs = Records.cps_constructor(data=cps_fullsample) calc = Calculator(policy=Policy(), records=recs, verbose=False) calc.advance_to_year(2015) calc.calc_all() # open actual output file if using_puf: afilename = os.path.join(tests_path, 'cmpi_puf_actual.txt') else: afilename = os.path.join(tests_path, 'cmpi_cps_actual.txt') afile = open(afilename, 'w') # write compare results to afile for cname in sorted(ITAX.keys()): comparison(cname, calc, ITAX, afile) # close actual output file afile.close() # check for differences between actual and expect output files efilename = afilename.replace('actual', 'expect') differences(afilename, efilename)
def test_distribution_tables(cps_subsample): """ Test distribution_tables method. """ pol = Policy() recs = Records.cps_constructor(data=cps_subsample) calc1 = Calculator(policy=pol, records=recs) assert calc1.current_year == 2014 calc1.calc_all() dt1, dt2 = calc1.distribution_tables(None, 'weighted_deciles') assert isinstance(dt1, pd.DataFrame) assert dt2 is None dt1, dt2 = calc1.distribution_tables(calc1, 'weighted_deciles') assert isinstance(dt1, pd.DataFrame) assert isinstance(dt2, pd.DataFrame) reform = {2014: {'_UBI_u18': [1000], '_UBI_1820': [1000], '_UBI_21': [1000]}} pol.implement_reform(reform) assert not pol.parameter_errors calc2 = Calculator(policy=pol, records=recs) calc2.calc_all() dt1, dt2 = calc1.distribution_tables(calc2, 'weighted_deciles') assert isinstance(dt1, pd.DataFrame) assert isinstance(dt2, pd.DataFrame)
def test_make_calculator_increment_years_first(cps_subsample): """ Test Calculator inflation indexing of policy parameters. """ # pylint: disable=too-many-locals # create Policy object with policy reform pol = Policy() reform = {2015: {}, 2016: {}} std5 = 2000 reform[2015]['_STD_Aged'] = [[std5, std5, std5, std5, std5]] reform[2015]['_II_em'] = [5000] reform[2016]['_II_em'] = [6000] reform[2016]['_II_em_cpi'] = False pol.implement_reform(reform) # create Calculator object with Policy object as modified by reform rec = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=pol, records=rec) # compare expected policy parameter values with those embedded in calc irates = pol.inflation_rates() syr = Policy.JSON_START_YEAR irate2015 = irates[2015 - syr] irate2016 = irates[2016 - syr] std6 = std5 * (1.0 + irate2015) std7 = std6 * (1.0 + irate2016) exp_STD_Aged = np.array([[1500, 1200, 1200, 1500, 1500], [1550, 1200, 1200, 1550, 1550], [std5, std5, std5, std5, std5], [std6, std6, std6, std6, std6], [std7, std7, std7, std7, std7]]) act_STD_Aged = calc.policy_param('_STD_Aged') assert np.allclose(act_STD_Aged[:5], exp_STD_Aged) exp_II_em = np.array([3900, 3950, 5000, 6000, 6000]) act_II_em = calc.policy_param('_II_em') assert np.allclose(act_II_em[:5], exp_II_em)
def test_ID_HC_vs_BS(cps_subsample): """ Test that complete haircut of itemized deductions produces same results as a 100% benefit surtax with no benefit deduction. """ recs = Records.cps_constructor(data=cps_subsample) # specify complete-haircut reform policy and Calculator object hc_reform = {2013: {'_ID_Medical_hc': [1.0], '_ID_StateLocalTax_hc': [1.0], '_ID_RealEstate_hc': [1.0], '_ID_Casualty_hc': [1.0], '_ID_Miscellaneous_hc': [1.0], '_ID_InterestPaid_hc': [1.0], '_ID_Charity_hc': [1.0]}} hc_policy = Policy() hc_policy.implement_reform(hc_reform) hc_calc = Calculator(policy=hc_policy, records=recs) hc_calc.calc_all() hc_taxes = hc_calc.dataframe(['iitax', 'payrolltax']) del hc_calc # specify benefit-surtax reform policy and Calculator object bs_reform = {2013: {'_ID_BenefitSurtax_crt': [0.0], '_ID_BenefitSurtax_trt': [1.0]}} bs_policy = Policy() bs_policy.implement_reform(bs_reform) bs_calc = Calculator(policy=bs_policy, records=recs) bs_calc.calc_all() bs_taxes = bs_calc.dataframe(['iitax', 'payrolltax']) del bs_calc # compare calculated taxes generated by the two reforms assert np.allclose(hc_taxes['payrolltax'], bs_taxes['payrolltax']) assert np.allclose(hc_taxes['iitax'], bs_taxes['iitax'])
def test_diagnostic_table(cps_subsample): """ Test diagnostic_table method. """ recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) adt = calc.diagnostic_table(3) assert isinstance(adt, pd.DataFrame)
def test_dist_table_sum_row(cps_subsample): rec = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=rec) calc.calc_all() tb1 = create_distribution_table(calc.distribution_table_dataframe(), 'standard_income_bins', 'expanded_income') tb2 = create_distribution_table(calc.distribution_table_dataframe(), 'soi_agi_bins', 'expanded_income') assert np.allclose(tb1[-1:], tb2[-1:])
def test_make_calculator_deepcopy(cps_subsample): """ Test deepcopy of Calculator object. """ pol = Policy() rec = Records.cps_constructor(data=cps_subsample) calc1 = Calculator(policy=pol, records=rec) calc2 = copy.deepcopy(calc1) assert isinstance(calc2, Calculator)
def test_atr_graph(cps_subsample): """ Test atr_graph method. """ recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) fig = calc.atr_graph(calc, mars=2, atr_measure='itax') assert fig fig = calc.atr_graph(calc, atr_measure='ptax') assert fig
def test_ce_aftertax_income(cps_subsample): """ Test ce_aftertax_income method. """ rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc1 = Calculator(policy=pol, records=rec) pol.implement_reform({2013: {'_SS_Earnings_c': [9e99]}}) calc2 = Calculator(policy=pol, records=rec) res = calc1.ce_aftertax_income(calc2) assert isinstance(res, dict)
def test_calculator_advance_to_year(cps_subsample): """ Test Calculator advance_to_year method. """ rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc = Calculator(policy=pol, records=rec) calc.advance_to_year(2016) assert calc.current_year == 2016 with pytest.raises(ValueError): calc.advance_to_year(2015)
def test_difference_table(cps_subsample): cyr = 2014 pol = Policy() recs = Records.cps_constructor(data=cps_subsample) calc1 = Calculator(policy=pol, records=recs) assert calc1.current_year == cyr reform = {cyr: {'_SS_Earnings_c': [9e99]}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=recs) assert calc2.current_year == cyr calc1.calc_all() calc2.calc_all() diff = calc1.difference_table(calc2, 'weighted_deciles', 'iitax') assert isinstance(diff, pd.DataFrame)
def test_consumption_response(cps_subsample): consump = Consumption() mpc = 0.5 consumption_response = {2013: {'_MPC_e20400': [mpc]}} consump.update_consumption(consumption_response) # test incorrect call to response method with pytest.raises(ValueError): consump.response(list(), 1) # test correct call to response method rec = Records.cps_constructor(data=cps_subsample) pre = copy.deepcopy(rec.e20400) consump.response(rec, 1.0) post = rec.e20400 actual_diff = post - pre expected_diff = np.ones(rec.array_length) * mpc assert np.allclose(actual_diff, expected_diff) # compute earnings mtr with no consumption response rec = Records.cps_constructor(data=cps_subsample) ided0 = copy.deepcopy(rec.e20400) calc0 = Calculator(policy=Policy(), records=rec, consumption=None) (mtr0_ptax, mtr0_itax, _) = calc0.mtr(variable_str='e00200p', wrt_full_compensation=False) assert np.allclose(calc0.array('e20400'), ided0) # compute earnings mtr with consumption response calc1 = Calculator(policy=Policy(), records=rec, consumption=consump) mtr1_ptax, mtr1_itax, _ = calc1.mtr(variable_str='e00200p', wrt_full_compensation=False) assert np.allclose(calc1.array('e20400'), ided0) # confirm that payroll mtr values are no different assert np.allclose(mtr1_ptax, mtr0_ptax) # confirm that all mtr with cons-resp are no greater than without cons-resp assert np.all( np.less_equal(np.around(mtr1_itax, decimals=5), np.around(mtr0_itax, decimals=5))) # confirm that some mtr with cons-resp are less than without cons-resp assert np.any(np.less(mtr1_itax, mtr0_itax))
def test_privacy_of_embedded_objects(cps_subsample): """ Test privacy of objects embedded in Calculator object. """ recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) var1 = var2 = var3 = 0 # pylint: disable=protected-access with pytest.raises(AttributeError): var1 = calc.__policy.current_year with pytest.raises(AttributeError): var2 = calc.__records.s006 with pytest.raises(AttributeError): var3 = calc.__consumption.current_year assert var1 == var2 == var3
def test_mtr_graph(cps_subsample): """ Test mtr_graph method. """ recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) fig = calc.mtr_graph(calc, mars=2, income_measure='wages', mtr_measure='ptax') assert fig fig = calc.mtr_graph(calc, income_measure='agi', mtr_measure='itax') assert fig
def test_multiyear_diagnostic_table(cps_subsample): behv = Behavior() calc = Calculator(policy=Policy(), records=Records.cps_constructor(data=cps_subsample), behavior=behv) with pytest.raises(ValueError): adt = multiyear_diagnostic_table(calc, 0) with pytest.raises(ValueError): adt = multiyear_diagnostic_table(calc, 20) adt = multiyear_diagnostic_table(calc, 3) assert isinstance(adt, pd.DataFrame) behv.update_behavior({2013: {'_BE_sub': [0.3]}}) assert calc.behavior.has_response() adt = multiyear_diagnostic_table(calc, 3) assert isinstance(adt, pd.DataFrame)
def fixture_tc_objs(request, reform_xx, puf_subsample, cps_subsample): """ Fixture for creating Tax-Calculator objects that use the PUF and use the CPS (called only twice: once for PUF and once for CPS) """ puftest = request.param p_xx = Policy() p_xx.implement_reform(reform_xx, raise_errors=False) if puftest: rec_xx = Records(data=puf_subsample) else: rec_xx = Records.cps_constructor(data=cps_subsample) c_xx = Calculator(policy=p_xx, records=rec_xx) c_xx.advance_to_year(TEST_YEAR) c_xx.calc_all() return rec_xx, c_xx, puftest
def test_ce_aftertax_income(cps_subsample): # test certainty_equivalent() function with con>cmin con = 5000 cmin = 1000 assert con == round(certainty_equivalent(con, 0, cmin), 6) assert con > round(certainty_equivalent((math.log(con) - 0.1), 1, cmin), 6) # test certainty_equivalent() function with con<cmin con = 500 cmin = 1000 assert con == round(certainty_equivalent(con, 0, cmin), 6) # test with require_no_agg_tax_change equal to False rec = Records.cps_constructor(data=cps_subsample, no_benefits=True) cyr = 2020 # specify calc1 and calc_all() for cyr pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.advance_to_year(cyr) calc1.calc_all() # specify calc2 and calc_all() for cyr reform = {2019: {'_II_em': [1000]}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.advance_to_year(cyr) calc2.calc_all() df1 = calc1.dataframe(['s006', 'combined', 'expanded_income']) df2 = calc2.dataframe(['s006', 'combined', 'expanded_income']) cedict = ce_aftertax_expanded_income(df1, df2, require_no_agg_tax_change=False) assert isinstance(cedict, dict) np.allclose(cedict['ceeu1'], [55641, 27167, 5726, 2229, 1565], atol=0.5, rtol=0.0) np.allclose(cedict['ceeu2'], [54629, 26698, 5710, 2229, 1565], atol=0.5, rtol=0.0) # test with require_no_agg_tax_change equal to True with pytest.raises(ValueError): ce_aftertax_expanded_income(df1, df2, require_no_agg_tax_change=True) # test with require_no_agg_tax_change equal to False and custom_params params = {'crra_list': [0, 2], 'cmin_value': 2000} with pytest.raises(ValueError): ce_aftertax_expanded_income(df1, df2, require_no_agg_tax_change=True, custom_params=params)
def test_mtr_graph(cps_subsample): """ Test mtr_graph method. """ recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) fig = calc.mtr_graph(calc, mars=2, income_measure='wages', mtr_measure='ptax', pop_quantiles=False) assert fig fig = calc.mtr_graph(calc, income_measure='agi', mtr_measure='itax', pop_quantiles=True) assert fig
def test_difference_table(cps_subsample): """ Test difference_table method. """ cyr = 2014 pol = Policy() recs = Records.cps_constructor(data=cps_subsample) calc1 = Calculator(policy=pol, records=recs) assert calc1.current_year == cyr reform = {cyr: {'_SS_Earnings_c': [9e99]}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=recs) assert calc2.current_year == cyr calc1.calc_all() calc2.calc_all() diff = calc1.difference_table(calc2, 'weighted_deciles', 'iitax') assert isinstance(diff, pd.DataFrame)
def test_calculator_mtr(cps_subsample): """ Test Calculator mtr method. """ rec = Records.cps_constructor(data=cps_subsample) calcx = Calculator(policy=Policy(), records=rec) calcx.calc_all() combinedx = calcx.array('combined') c00100x = calcx.array('c00100') calc = Calculator(policy=Policy(), records=rec) recs_pre_e00200p = copy.deepcopy(calc.array('e00200p')) (mtr_ptx, mtr_itx, mtr_cmb) = calc.mtr(variable_str='e00200p', zero_out_calculated_vars=True) recs_post_e00200p = calc.array('e00200p') assert np.allclose(recs_post_e00200p, recs_pre_e00200p) assert np.allclose(calc.array('combined'), combinedx) assert np.allclose(calc.array('c00100'), c00100x) assert np.array_equal(mtr_cmb, mtr_ptx) is False assert np.array_equal(mtr_ptx, mtr_itx) is False with pytest.raises(ValueError): calc.mtr(variable_str='bad_income_type') (_, _, mtr_combined) = calc.mtr(variable_str='e00200s', calc_all_already_called=True) assert isinstance(mtr_combined, np.ndarray) (_, _, mtr_combined) = calc.mtr(variable_str='e00650', negative_finite_diff=True, calc_all_already_called=True) assert isinstance(mtr_combined, np.ndarray) (_, _, mtr_combined) = calc.mtr(variable_str='e00900p', calc_all_already_called=True) assert isinstance(mtr_combined, np.ndarray) (_, _, mtr_combined) = calc.mtr(variable_str='e01700', calc_all_already_called=True) assert isinstance(mtr_combined, np.ndarray) (_, _, mtr_combined) = calc.mtr(variable_str='e26270', calc_all_already_called=True) assert isinstance(mtr_combined, np.ndarray) (_, _, mtr_combined) = calc.mtr(variable_str='k1bx14p', calc_all_already_called=True) assert isinstance(mtr_combined, np.ndarray) (_, _, mtr_combined) = calc.mtr(variable_str='e00200p', calc_all_already_called=True) assert np.allclose(mtr_combined, mtr_cmb) assert np.allclose(calc.array('combined'), combinedx) assert np.allclose(calc.array('c00100'), c00100x)
def reform_results(rid, reform_dict, reform_2017_law): """ Return actual results of the reform specified by rid and reform_dict. """ # pylint: disable=too-many-locals rec = Records.cps_constructor() # create baseline Calculator object, calc1 pol = Policy() if reform_dict['baseline'] == '2017_law.json': pol.implement_reform(reform_2017_law) elif reform_dict['baseline'] == 'policy_current_law.json': pass else: msg = 'illegal baseline value {}' raise ValueError(msg.format(reform_dict['baseline'])) calc1 = Calculator(policy=pol, records=rec, verbose=False) # create reform Calculator object, calc2 start_year = reform_dict['start_year'] reform = dict() for name, value in reform_dict['value'].items(): reform[name] = {start_year: value} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec, verbose=False) # increment both Calculator objects to reform's start_year calc1.advance_to_year(start_year) calc2.advance_to_year(start_year) # calculate baseline and reform output for several years output_type = reform_dict['output_type'] num_years = 4 results = list() for _ in range(0, num_years): calc1.calc_all() baseline = calc1.array(output_type) calc2.calc_all() reform = calc2.array(output_type) diff = reform - baseline weighted_sum_diff = (diff * calc1.array('s006')).sum() * 1.0e-9 results.append(weighted_sum_diff) calc1.increment_year() calc2.increment_year() # write actual results to actual_str actual_str = '{}'.format(rid) for iyr in range(0, num_years): actual_str += ',{:.1f}'.format(results[iyr]) return actual_str
def test_dist_table_sum_row(cps_subsample): rec = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=rec) calc.calc_all() tb1 = create_distribution_table(calc.records, groupby='small_income_bins', income_measure='expanded_income', result_type='weighted_sum') tb2 = create_distribution_table(calc.records, groupby='large_income_bins', income_measure='expanded_income', result_type='weighted_sum') assert np.allclose(tb1[-1:], tb2[-1:]) tb3 = create_distribution_table(calc.records, groupby='small_income_bins', income_measure='expanded_income', result_type='weighted_avg') assert isinstance(tb3, pd.DataFrame)
def test_xtr_graph_plot(cps_subsample): calc = Calculator(policy=Policy(), records=Records.cps_constructor(data=cps_subsample), behavior=Behavior()) gdata = mtr_graph_data(calc, calc, mtr_measure='ptax', income_measure='agi', dollar_weighting=False) gplot = xtr_graph_plot(gdata) assert gplot gdata = mtr_graph_data(calc, calc, mtr_measure='itax', alt_e00200p_text='Taxpayer Earnings', income_measure='expanded_income', dollar_weighting=False) assert isinstance(gdata, dict)
def test_myr_diag_table_wo_behv(cps_subsample): reform = { 2013: { '_II_rt7': [0.33], '_PT_rt7': [0.33], } } pol = Policy() pol.implement_reform(reform) calc = Calculator(policy=pol, records=Records.cps_constructor(data=cps_subsample)) calc.calc_all() liabilities_x = (calc.records.combined * calc.records.s006).sum() adt = multiyear_diagnostic_table(calc, 1) # extract combined liabilities as a float and # adopt units of the raw calculator data in liabilities_x liabilities_y = adt.iloc[19].tolist()[0] * 1e9 assert np.allclose(liabilities_x, liabilities_y, atol=0.01, rtol=0.0)
def test_make_Calculator(cps_subsample): parm = Policy(start_year=2014, num_years=9) assert parm.current_year == 2014 recs = Records.cps_constructor(data=cps_subsample) consump = Consumption() consump.update_consumption({2014: {'_MPC_e20400': [0.05]}}) assert consump.current_year == 2013 calc = Calculator(policy=parm, records=recs, consumption=consump, behavior=Behavior()) assert calc.current_year == 2014 # test incorrect Calculator instantiation: with pytest.raises(ValueError): calc = Calculator(policy=None, records=recs) with pytest.raises(ValueError): calc = Calculator(policy=parm, records=None) with pytest.raises(ValueError): calc = Calculator(policy=parm, records=recs, behavior=list()) with pytest.raises(ValueError): calc = Calculator(policy=parm, records=recs, consumption=list())
def test_correct_Records_instantiation(cps_subsample): rec1 = Records.cps_constructor(data=cps_subsample) assert rec1 assert np.all(rec1.MARS != 0) assert rec1.current_year == rec1.data_year sum_e00200_in_cps_year = rec1.e00200.sum() rec1.set_current_year(rec1.data_year + 1) sum_e00200_in_cps_year_plus_one = rec1.e00200.sum() assert sum_e00200_in_cps_year_plus_one == sum_e00200_in_cps_year wghts_path = os.path.join(Records.CUR_PATH, Records.CPS_WEIGHTS_FILENAME) wghts_df = pd.read_csv(wghts_path) rec2 = Records(data=cps_subsample, exact_calculations=False, gfactors=GrowFactors(), weights=wghts_df, start_year=Records.CPSCSV_YEAR) assert rec2 assert np.all(rec2.MARS != 0) assert rec2.current_year == rec2.data_year
def test_calculator_mtr(cps_subsample): """ Test Calculator mtr method. """ rec = Records.cps_constructor(data=cps_subsample) calcx = Calculator(policy=Policy(), records=rec) calcx.calc_all() combinedx = calcx.array('combined') c00100x = calcx.array('c00100') calc = Calculator(policy=Policy(), records=rec) recs_pre_e00200p = copy.deepcopy(calc.array('e00200p')) (mtr_ptx, mtr_itx, mtr_cmb) = calc.mtr(variable_str='e00200p', zero_out_calculated_vars=True) recs_post_e00200p = calc.array('e00200p') assert np.allclose(recs_post_e00200p, recs_pre_e00200p) assert np.allclose(calc.array('combined'), combinedx) assert np.allclose(calc.array('c00100'), c00100x) assert np.array_equal(mtr_cmb, mtr_ptx) is False assert np.array_equal(mtr_ptx, mtr_itx) is False with pytest.raises(ValueError): calc.mtr(variable_str='bad_income_type') (_, _, mtr_combined) = calc.mtr(variable_str='e00200s', calc_all_already_called=True) assert isinstance(mtr_combined, np.ndarray) (_, _, mtr_combined) = calc.mtr(variable_str='e00650', negative_finite_diff=True, calc_all_already_called=True) assert isinstance(mtr_combined, np.ndarray) (_, _, mtr_combined) = calc.mtr(variable_str='e00900p', calc_all_already_called=True) assert isinstance(mtr_combined, np.ndarray) (_, _, mtr_combined) = calc.mtr(variable_str='e01700', calc_all_already_called=True) assert isinstance(mtr_combined, np.ndarray) (_, _, mtr_combined) = calc.mtr(variable_str='e26270', calc_all_already_called=True) assert isinstance(mtr_combined, np.ndarray) (_, _, mtr_combined) = calc.mtr(variable_str='e00200p', calc_all_already_called=True) assert np.allclose(mtr_combined, mtr_cmb) assert np.allclose(calc.array('combined'), combinedx) assert np.allclose(calc.array('c00100'), c00100x)
def test_distribution_tables(cps_subsample): pol = Policy() recs = Records.cps_constructor(data=cps_subsample) calc1 = Calculator(policy=pol, records=recs) assert calc1.current_year == 2014 calc1.calc_all() dt1, dt2 = calc1.distribution_tables(None) assert isinstance(dt1, pd.DataFrame) assert dt2 is None dt1, dt2 = calc1.distribution_tables(calc1) assert isinstance(dt1, pd.DataFrame) assert isinstance(dt2, pd.DataFrame) reform = {2014: {'_UBI1': [1000], '_UBI2': [1000], '_UBI3': [1000]}} pol.implement_reform(reform) assert not pol.reform_errors calc2 = Calculator(policy=pol, records=recs) calc2.calc_all() dt1, dt2 = calc1.distribution_tables(calc2) assert isinstance(dt1, pd.DataFrame) assert isinstance(dt2, pd.DataFrame)
def test_dec_graph_plot(cps_subsample): pol = Policy() rec = Records.cps_constructor(data=cps_subsample) calc1 = Calculator(policy=pol, records=rec) year = 2020 calc1.advance_to_year(year) reform = { year: { '_SS_Earnings_c': [9e99], # OASDI FICA tax on all earnings '_FICA_ss_trt': [0.107484] # lower rate to keep revenue unchanged } } pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.advance_to_year(year) assert calc1.current_year == calc2.current_year calc1.calc_all() calc2.calc_all() fig = calc1.decile_graph(calc2) assert fig
def test_xtr_graph_plot(cps_subsample): recs = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=Policy(), records=recs) mtr = 0.20 * np.ones_like(cps_subsample['e00200']) vdf = calc.dataframe(['s006', 'MARS', 'c00100']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr gdata = mtr_graph_data(vdf, calc.current_year, mtr_measure='ptax', income_measure='agi', dollar_weighting=False) gplot = xtr_graph_plot(gdata) assert gplot vdf = calc.dataframe(['s006', 'expanded_income']) vdf['mtr1'] = mtr vdf['mtr2'] = mtr gdata = mtr_graph_data(vdf, calc.current_year, mtr_measure='itax', alt_e00200p_text='Taxpayer Earnings', income_measure='expanded_income', dollar_weighting=False) assert isinstance(gdata, dict)
def test_myr_diag_table_w_behv(cps_subsample): pol = Policy() rec = Records.cps_constructor(data=cps_subsample) year = rec.current_year beh = Behavior() calc = Calculator(policy=pol, records=rec, behavior=beh) assert calc.current_year == year reform = {year: {'_II_rt7': [0.33], '_PT_rt7': [0.33]}} pol.implement_reform(reform) reform_behav = {year: {'_BE_sub': [0.4], '_BE_cg': [-3.67]}} beh.update_behavior(reform_behav) calc_clp = calc.current_law_version() calc_beh = Behavior.response(calc_clp, calc) calc_beh.calc_all() liabilities_x = (calc_beh.records.combined * calc_beh.records.s006).sum() adt = multiyear_diagnostic_table(calc_beh, 1) # extract combined liabilities as a float and # adopt units of the raw calculator data in liabilities_x liabilities_y = adt.iloc[19].tolist()[0] * 1e9 assert np.allclose(liabilities_x, liabilities_y, atol=0.01, rtol=0.0)
def test_atr_graph_data(cps_subsample): pol = Policy() rec = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=pol, records=rec) with pytest.raises(ValueError): atr_graph_data(calc, calc, mars='bad') with pytest.raises(ValueError): atr_graph_data(calc, calc, mars=0) with pytest.raises(ValueError): atr_graph_data(calc, calc, mars=list()) with pytest.raises(ValueError): atr_graph_data(calc, calc, atr_measure='badtax') gdata = atr_graph_data(calc, calc, mars=1, atr_measure='combined') gdata = atr_graph_data(calc, calc, atr_measure='itax') gdata = atr_graph_data(calc, calc, atr_measure='ptax') assert isinstance(gdata, dict) with pytest.raises(ValueError): calcx = Calculator(policy=pol, records=rec) calcx.advance_to_year(2020) atr_graph_data(calcx, calc)
def test_make_calculator_increment_years_first(cps_subsample): """ Test Calculator inflation indexing of policy parameters. """ # pylint: disable=too-many-locals # create Policy object with policy reform pol = Policy() std5 = 2000 reform = { 'STD_Aged': { 2015: [std5, std5, std5, std5, std5] }, 'II_em': { 2015: 5000, 2016: 6000 }, 'II_em-indexed': { 2016: False } } pol.implement_reform(reform) # create Calculator object with Policy object as modified by reform rec = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=pol, records=rec) # compare expected policy parameter values with those embedded in calc irates = pol.inflation_rates() syr = Policy.JSON_START_YEAR irate2015 = irates[2015 - syr] irate2016 = irates[2016 - syr] std6 = std5 * (1.0 + irate2015) std7 = std6 * (1.0 + irate2016) exp_STD_Aged = np.array([[1500, 1200, 1200, 1500, 1500], [1550, 1200, 1200, 1550, 1550], [std5, std5, std5, std5, std5], [std6, std6, std6, std6, std6], [std7, std7, std7, std7, std7]]) act_STD_Aged = calc.policy_param('_STD_Aged') assert np.allclose(act_STD_Aged[:5], exp_STD_Aged) exp_II_em = np.array([3900, 3950, 5000, 6000, 6000]) act_II_em = calc.policy_param('_II_em') assert np.allclose(act_II_em[:5], exp_II_em)
def test_ce_aftertax_income(cps_subsample): # test certainty_equivalent() function with con>cmin con = 5000 cmin = 1000 assert con == round(certainty_equivalent(con, 0, cmin), 6) assert con > round(certainty_equivalent((math.log(con) - 0.1), 1, cmin), 6) # test certainty_equivalent() function with con<cmin con = 500 cmin = 1000 assert con == round(certainty_equivalent(con, 0, cmin), 6) # test with require_no_agg_tax_change equal to False rec = Records.cps_constructor(data=cps_subsample) cyr = 2020 # specify calc1 and calc_all() for cyr pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.advance_to_year(cyr) calc1.calc_all() # specify calc2 and calc_all() for cyr reform = {'II_em': {2019: 1000}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.advance_to_year(cyr) calc2.calc_all() df1 = calc1.dataframe(['s006', 'combined', 'expanded_income']) df2 = calc2.dataframe(['s006', 'combined', 'expanded_income']) cedict = ce_aftertax_expanded_income(df1, df2, require_no_agg_tax_change=False) assert isinstance(cedict, dict) np.allclose(cedict['ceeu1'], [55641, 27167, 5726, 2229, 1565], atol=0.5, rtol=0.0) np.allclose(cedict['ceeu2'], [54629, 26698, 5710, 2229, 1565], atol=0.5, rtol=0.0) # test with require_no_agg_tax_change equal to True with pytest.raises(ValueError): ce_aftertax_expanded_income(df1, df2, require_no_agg_tax_change=True) # test with require_no_agg_tax_change equal to False and custom_params params = {'crra_list': [0, 2], 'cmin_value': 2000} with pytest.raises(ValueError): ce_aftertax_expanded_income(df1, df2, require_no_agg_tax_change=True, custom_params=params)
def test_make_calculator(cps_subsample): syr = 2014 pol = Policy(start_year=syr, num_years=9) assert pol.current_year == syr rec = Records.cps_constructor(data=cps_subsample) consump = Consumption() consump.update_consumption({syr: {'_MPC_e20400': [0.05]}}) assert consump.current_year == Consumption.JSON_START_YEAR calc = Calculator(policy=pol, records=rec, consumption=consump, behavior=Behavior()) assert calc.current_year == syr assert calc.records_current_year() == syr # test incorrect Calculator instantiation: with pytest.raises(ValueError): Calculator(policy=None, records=rec) with pytest.raises(ValueError): Calculator(policy=pol, records=None) with pytest.raises(ValueError): Calculator(policy=pol, records=rec, behavior=list()) with pytest.raises(ValueError): Calculator(policy=pol, records=rec, consumption=list())
def test_ID_HC_vs_BS(cps_subsample): """ Test that complete haircut of itemized deductions produces same results as a 100% benefit surtax with no benefit deduction. """ recs = Records.cps_constructor(data=cps_subsample) # specify complete-haircut reform policy and Calculator object hc_reform = { 2013: { '_ID_Medical_hc': [1.0], '_ID_StateLocalTax_hc': [1.0], '_ID_RealEstate_hc': [1.0], '_ID_Casualty_hc': [1.0], '_ID_Miscellaneous_hc': [1.0], '_ID_InterestPaid_hc': [1.0], '_ID_Charity_hc': [1.0] } } hc_policy = Policy() hc_policy.implement_reform(hc_reform) hc_calc = Calculator(policy=hc_policy, records=recs) hc_calc.calc_all() hc_taxes = hc_calc.dataframe(['iitax', 'payrolltax']) del hc_calc # specify benefit-surtax reform policy and Calculator object bs_reform = { 2013: { '_ID_BenefitSurtax_crt': [0.0], '_ID_BenefitSurtax_trt': [1.0] } } bs_policy = Policy() bs_policy.implement_reform(bs_reform) bs_calc = Calculator(policy=bs_policy, records=recs) bs_calc.calc_all() bs_taxes = bs_calc.dataframe(['iitax', 'payrolltax']) del bs_calc # compare calculated taxes generated by the two reforms assert np.allclose(hc_taxes['payrolltax'], bs_taxes['payrolltax']) assert np.allclose(hc_taxes['iitax'], bs_taxes['iitax'])
def test_atr_graph_data(cps_subsample): pol = Policy() rec = Records.cps_constructor(data=cps_subsample) calc = Calculator(policy=pol, records=rec) year = calc.current_year with pytest.raises(ValueError): atr_graph_data(None, year, mars='bad') with pytest.raises(ValueError): atr_graph_data(None, year, mars=0) with pytest.raises(ValueError): atr_graph_data(None, year, mars=list()) with pytest.raises(ValueError): atr_graph_data(None, year, atr_measure='badtax') calc.calc_all() vdf = calc.dataframe(['s006', 'MARS', 'expanded_income']) tax = 0.20 * np.ones_like(vdf['expanded_income']) vdf['tax1'] = tax vdf['tax2'] = tax gdata = atr_graph_data(vdf, year, mars=1, atr_measure='combined') gdata = atr_graph_data(vdf, year, atr_measure='itax') gdata = atr_graph_data(vdf, year, atr_measure='ptax') assert isinstance(gdata, dict)
def test_correct_Records_instantiation(cps_subsample): rec1 = Records.cps_constructor(data=cps_subsample) assert rec1 assert np.all(rec1.MARS != 0) assert rec1.current_year == rec1.data_year sum_e00200_in_cps_year = rec1.e00200.sum() rec1.set_current_year(rec1.data_year + 1) sum_e00200_in_cps_year_plus_one = rec1.e00200.sum() assert sum_e00200_in_cps_year_plus_one == sum_e00200_in_cps_year wghts_path = os.path.join(Records.CUR_PATH, Records.CPS_WEIGHTS_FILENAME) wghts_df = pd.read_csv(wghts_path) ratios_path = os.path.join(Records.CUR_PATH, Records.PUF_RATIOS_FILENAME) ratios_df = pd.read_csv(ratios_path, index_col=0).transpose() rec2 = Records(data=cps_subsample, exact_calculations=False, gfactors=GrowFactors(), weights=wghts_df, adjust_ratios=ratios_df, start_year=Records.CPSCSV_YEAR) assert rec2 assert np.all(rec2.MARS != 0) assert rec2.current_year == rec2.data_year
def test_atr_graph_data(cps_subsample): pol = Policy() rec = Records.cps_constructor(data=cps_subsample, no_benefits=True) calc = Calculator(policy=pol, records=rec) year = calc.current_year with pytest.raises(ValueError): atr_graph_data(None, year, mars='bad') with pytest.raises(ValueError): atr_graph_data(None, year, mars=0) with pytest.raises(ValueError): atr_graph_data(None, year, mars=list()) with pytest.raises(ValueError): atr_graph_data(None, year, atr_measure='badtax') calc.calc_all() vdf = calc.dataframe(['s006', 'MARS', 'expanded_income']) tax = 0.20 * np.ones_like(vdf['expanded_income']) vdf['tax1'] = tax vdf['tax2'] = tax gdata = atr_graph_data(vdf, year, mars=1, atr_measure='combined') gdata = atr_graph_data(vdf, year, atr_measure='itax') gdata = atr_graph_data(vdf, year, atr_measure='ptax') assert isinstance(gdata, dict)
def test_correct_Records_instantiation(cps_subsample): rec1 = Records.cps_constructor(data=cps_subsample, gfactors=None) assert rec1 assert np.all(rec1.MARS != 0) assert rec1.current_year == rec1.data_year sum_e00200_in_cps_year = rec1.e00200.sum() rec1.increment_year() sum_e00200_in_cps_year_plus_one = rec1.e00200.sum() assert sum_e00200_in_cps_year_plus_one == sum_e00200_in_cps_year wghts_path = os.path.join(Records.CODE_PATH, Records.CPS_WEIGHTS_FILENAME) wghts_df = pd.read_csv(wghts_path) ratios_path = os.path.join(Records.CODE_PATH, Records.PUF_RATIOS_FILENAME) ratios_df = pd.read_csv(ratios_path, index_col=0).transpose() rec2 = Records(data=cps_subsample, start_year=Records.CPSCSV_YEAR, gfactors=GrowFactors(), weights=wghts_df, adjust_ratios=ratios_df, exact_calculations=False) assert rec2 assert np.all(rec2.MARS != 0) assert rec2.current_year == rec2.data_year
def test_dec_graph_plots(cps_subsample): pol = Policy() rec = Records.cps_constructor(data=cps_subsample, no_benefits=True) calc1 = Calculator(policy=pol, records=rec) year = 2020 calc1.advance_to_year(year) reform = { year: { '_SS_Earnings_c': [9e99], # OASDI FICA tax on all earnings '_FICA_ss_trt': [0.107484] # lower rate to keep revenue unchanged } } pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.advance_to_year(year) assert calc1.current_year == calc2.current_year calc1.calc_all() calc2.calc_all() fig = calc1.decile_graph(calc2) assert fig dt1, dt2 = calc1.distribution_tables(calc2, 'weighted_deciles') dta = dec_graph_data(dt1, dt2, year, include_zero_incomes=True, include_negative_incomes=False) assert isinstance(dta, dict) dta = dec_graph_data(dt1, dt2, year, include_zero_incomes=False, include_negative_incomes=True) assert isinstance(dta, dict) dta = dec_graph_data(dt1, dt2, year, include_zero_incomes=False, include_negative_incomes=False) assert isinstance(dta, dict)
def test_diff_table_sum_row(cps_subsample): rec = Records.cps_constructor(data=cps_subsample) # create a current-law Policy object and Calculator calc1 pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator calc2 reform = {2013: {'_II_rt4': [0.56]}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() # create two difference tables and compare their content tdiff1 = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'standard_income_bins', 'iitax') tdiff2 = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'soi_agi_bins', 'iitax') non_digit_cols = ['perc_inc', 'perc_cut'] digit_cols = [c for c in list(tdiff1) if c not in non_digit_cols] assert np.allclose(tdiff1[digit_cols][-1:], tdiff2[digit_cols][-1:]) np.allclose(tdiff1[non_digit_cols][-1:], tdiff2[non_digit_cols][-1:])
def test_ID_RealEstate_HC_vs_CRT(cps_subsample): """ Test that a cap on all state, local, and foreign real estate tax deductions at 0 percent of AGI is equivalent to a complete haircut on the same real estate tax deductions. """ rec = Records.cps_constructor(data=cps_subsample) # specify real estate complete haircut reform policy and Calculator object hc_reform = {2013: {'_ID_RealEstate_hc': [1.0]}} hc_policy = Policy() hc_policy.implement_reform(hc_reform) hc_calc = Calculator(policy=hc_policy, records=rec) hc_calc.calc_all() # specify AGI cap reform policy and Calculator object crt_reform = {2013: {'_ID_RealEstate_crt': [0.0]}} crt_policy = Policy() crt_policy.implement_reform(crt_reform) crt_calc = Calculator(policy=crt_policy, records=rec) crt_calc.calc_all() # compare calculated tax results generated by the two reforms assert np.allclose(hc_calc.records.payrolltax, crt_calc.records.payrolltax) assert np.allclose(hc_calc.records.iitax, crt_calc.records.iitax)
def test_make_calculator(cps_subsample): """ Test Calculator class ctor. """ start_year = Policy.JSON_START_YEAR sim_year = 2018 pol = Policy() assert pol.current_year == start_year rec = Records.cps_constructor(data=cps_subsample) consump = Consumption() consump.update_consumption({sim_year: {'_MPC_e20400': [0.05]}}) assert consump.current_year == start_year calc = Calculator(policy=pol, records=rec, consumption=consump) assert calc.data_year == Records.CPSCSV_YEAR assert calc.current_year == Records.CPSCSV_YEAR # test incorrect Calculator instantiation: with pytest.raises(ValueError): Calculator(policy=None, records=rec) with pytest.raises(ValueError): Calculator(policy=pol, records=None) with pytest.raises(ValueError): Calculator(policy=pol, records=rec, consumption=list())
def test_make_calculator_with_policy_reform(cps_subsample): rec = Records.cps_constructor(data=cps_subsample) year = rec.current_year # create a Policy object and apply a policy reform pol = Policy() reform = {2013: {'_II_em': [4000], '_II_em_cpi': False, '_STD_Aged': [[1600, 1300, 1300, 1600, 1600]], '_STD_Aged_cpi': False}} pol.implement_reform(reform) # create a Calculator object using this policy reform calc = Calculator(policy=pol, records=rec) # check that Policy object embedded in Calculator object is correct assert calc.current_year == year assert calc.param('II_em') == 4000 assert np.allclose(calc.param('_II_em'), np.array([4000] * Policy.DEFAULT_NUM_YEARS)) exp_STD_Aged = [[1600, 1300, 1300, 1600, 1600]] * Policy.DEFAULT_NUM_YEARS assert np.allclose(calc.param('_STD_Aged'), np.array(exp_STD_Aged)) assert np.allclose(calc.param('STD_Aged'), np.array([1600, 1300, 1300, 1600, 1600]))
def test_ID_StateLocal_HC_vs_CRT(cps_subsample): """ Test that a cap on state/local income and sales tax deductions at 0 percent of AGI is equivalent to a complete haircut on the same state/local tax deductions. """ rec = Records.cps_constructor(data=cps_subsample) # specify state/local complete haircut reform policy and Calculator object hc_policy = Policy() hc_reform = {'ID_StateLocalTax_hc': {2013: 1.0}} hc_policy.implement_reform(hc_reform) hc_calc = Calculator(policy=hc_policy, records=rec) hc_calc.calc_all() # specify AGI cap reform policy and Calculator object crt_policy = Policy() crt_reform = {'ID_StateLocalTax_crt': {2013: 0.0}} crt_policy.implement_reform(crt_reform) crt_calc = Calculator(policy=crt_policy, records=rec) crt_calc.calc_all() # compare calculated tax results generated by the two reforms assert np.allclose(hc_calc.array('payrolltax'), crt_calc.array('payrolltax')) assert np.allclose(hc_calc.array('iitax'), crt_calc.array('iitax'))
def test_make_calculator_with_multiyear_reform(cps_subsample): rec = Records.cps_constructor(data=cps_subsample) year = rec.current_year # create a Policy object and apply a policy reform pol = Policy() reform = {2015: {}, 2016: {}} reform[2015]['_II_em'] = [5000, 6000] # reform values for 2015 and 2016 reform[2015]['_II_em_cpi'] = False reform[2016]['_STD_Aged'] = [[1600, 1300, 1600, 1300, 1600]] pol.implement_reform(reform) # create a Calculator object using this policy-reform calc = Calculator(policy=pol, records=rec) # check that Policy object embedded in Calculator object is correct assert calc.current_year == year assert calc.policy.II_em == 3950 assert calc.policy.num_years == Policy.DEFAULT_NUM_YEARS exp_II_em = [3900, 3950, 5000] + [6000] * (Policy.DEFAULT_NUM_YEARS - 3) assert np.allclose(calc.policy._II_em, np.array(exp_II_em)) calc.increment_year() calc.increment_year() assert calc.current_year == 2016 assert np.allclose(calc.policy.STD_Aged, np.array([1600, 1300, 1600, 1300, 1600]))
def test_diff_table_sum_row(cps_subsample): rec = Records.cps_constructor(data=cps_subsample) # create a current-law Policy object and Calculator calc1 pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator calc2 reform = {'II_rt4': {2013: 0.56}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() # create three difference tables and compare their content dv1 = calc1.dataframe(DIFF_VARIABLES) dv2 = calc2.dataframe(DIFF_VARIABLES) dt1 = create_difference_table(dv1, dv2, 'standard_income_bins', 'iitax') dt2 = create_difference_table(dv1, dv2, 'soi_agi_bins', 'iitax') dt3 = create_difference_table(dv1, dv2, 'weighted_deciles', 'iitax', pop_quantiles=False) dt4 = create_difference_table(dv1, dv2, 'weighted_deciles', 'iitax', pop_quantiles=True) assert np.allclose(dt1.loc['ALL'], dt2.loc['ALL']) assert np.allclose(dt1.loc['ALL'], dt3.loc['ALL']) # make sure population count is larger than filing-unit count assert dt4.at['ALL', 'count'] > dt1.at['ALL', 'count']
def test_ID_RealEstate_HC_vs_CRT(cps_subsample): """ Test that a cap on all state, local, and foreign real estate tax deductions at 0 percent of AGI is equivalent to a complete haircut on the same real estate tax deductions. """ rec = Records.cps_constructor(data=cps_subsample) # specify real estate complete haircut reform policy and Calculator object hc_reform = {2013: {'_ID_RealEstate_hc': [1.0]}} hc_policy = Policy() hc_policy.implement_reform(hc_reform) hc_calc = Calculator(policy=hc_policy, records=rec) hc_calc.calc_all() # specify AGI cap reform policy and Calculator object crt_reform = {2013: {'_ID_RealEstate_crt': [0.0]}} crt_policy = Policy() crt_policy.implement_reform(crt_reform) crt_calc = Calculator(policy=crt_policy, records=rec) crt_calc.calc_all() # compare calculated tax results generated by the two reforms assert np.allclose(hc_calc.array('payrolltax'), crt_calc.array('payrolltax')) assert np.allclose(hc_calc.array('iitax'), crt_calc.array('iitax'))