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_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_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), groupby='small_income_bins', income_measure='expanded_income', tax_to_diff='iitax') tdiff2 = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='large_income_bins', income_measure='expanded_income', tax_to_diff='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.testing.assert_array_equal(tdiff1[non_digit_cols][-1:], tdiff2[non_digit_cols][-1:])
def test_xtr_graph_plot(cps_subsample): calc = Calculator(policy=Policy(), records=Records.cps_constructor(data=cps_subsample), behavior=Behavior()) 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_calculator_using_nonstd_input(): """ Test Calculator using non-standard input records. """ # check Calculator handling of raw, non-standard input data with no aging pol = Policy() pol.set_year(RAWINPUT_YEAR) # set policy params to input data year nonstd = Records( data=pd.read_csv(StringIO(RAWINPUT_CONTENTS)), start_year=RAWINPUT_YEAR, # set raw input data year gfactors=None, # keeps raw data unchanged weights=None) assert nonstd.array_length == RAWINPUT_FUNITS calc = Calculator(policy=pol, records=nonstd, sync_years=False) # keeps raw data unchanged assert calc.current_year == RAWINPUT_YEAR calc.calc_all() assert calc.weighted_total('e00200') == 0 assert calc.total_weight() == 0 varlist = ['RECID', 'MARS'] dframe = calc.dataframe(varlist) assert isinstance(dframe, pd.DataFrame) assert dframe.shape == (RAWINPUT_FUNITS, len(varlist)) mars = calc.array('MARS') assert isinstance(mars, np.ndarray) assert mars.shape == (RAWINPUT_FUNITS, ) exp_iitax = np.zeros((nonstd.array_length, )) assert np.allclose(calc.array('iitax'), exp_iitax) mtr_ptax, _, _ = calc.mtr(wrt_full_compensation=False) exp_mtr_ptax = np.zeros((nonstd.array_length, )) exp_mtr_ptax.fill(0.153) assert np.allclose(mtr_ptax, exp_mtr_ptax)
def test_mtr_graph_data(cps_subsample): calc = Calculator(policy=Policy(), records=Records.cps_constructor(data=cps_subsample)) 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_calculator_using_nonstd_input(rawinputfile): """ Test Calculator using non-standard input records. """ # check Calculator handling of raw, non-standard input data with no aging pol = Policy() pol.set_year(RAWINPUTFILE_YEAR) # set policy params to input data year nonstd = Records(data=rawinputfile.name, gfactors=None, # keeps raw data unchanged weights=None, start_year=RAWINPUTFILE_YEAR) # set raw input data year assert nonstd.array_length == RAWINPUTFILE_FUNITS calc = Calculator(policy=pol, records=nonstd, sync_years=False) # keeps raw data unchanged assert calc.current_year == RAWINPUTFILE_YEAR calc.calc_all() assert calc.weighted_total('e00200') == 0 assert calc.total_weight() == 0 varlist = ['RECID', 'MARS'] dframe = calc.dataframe(varlist) assert isinstance(dframe, pd.DataFrame) assert dframe.shape == (RAWINPUTFILE_FUNITS, len(varlist)) mars = calc.array('MARS') assert isinstance(mars, np.ndarray) assert mars.shape == (RAWINPUTFILE_FUNITS,) exp_iitax = np.zeros((nonstd.array_length,)) assert np.allclose(calc.array('iitax'), exp_iitax) mtr_ptax, _, _ = calc.mtr(wrt_full_compensation=False) exp_mtr_ptax = np.zeros((nonstd.array_length,)) exp_mtr_ptax.fill(0.153) assert np.allclose(mtr_ptax, exp_mtr_ptax)
def test_calculator_using_nonstd_input(rawinputfile): # check Calculator handling of raw, non-standard input data with no aging pol = Policy() pol.set_year(RAWINPUTFILE_YEAR) # set policy params to input data year nonstd = Records( data=rawinputfile.name, gfactors=None, # keeps raw data unchanged weights=None, start_year=RAWINPUTFILE_YEAR) # set raw input data year assert nonstd.dim == RAWINPUTFILE_FUNITS calc = Calculator(policy=pol, records=nonstd, sync_years=False) # keeps raw data unchanged assert calc.current_year == RAWINPUTFILE_YEAR calc.calc_all() assert calc.weighted_total('e00200') == 0 assert calc.total_weight() == 0 varlist = ['RECID', 'MARS'] pdf = calc.dataframe(varlist) assert isinstance(pdf, pd.DataFrame) assert pdf.shape == (RAWINPUTFILE_FUNITS, len(varlist)) mars = calc.array('MARS') assert isinstance(mars, np.ndarray) assert mars.shape == (RAWINPUTFILE_FUNITS, ) exp_iitax = np.zeros((nonstd.dim, )) assert np.allclose(calc.records.iitax, exp_iitax) mtr_ptax, _, _ = calc.mtr(wrt_full_compensation=False) exp_mtr_ptax = np.zeros((nonstd.dim, )) exp_mtr_ptax.fill(0.153) assert np.allclose(mtr_ptax, exp_mtr_ptax)
def test_write_graph_file(cps_subsample): recs = Records.cps_constructor(data=cps_subsample, no_benefits=True) 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_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_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_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.dataframe(DIST_VARIABLES), groupby='small_income_bins', income_measure='expanded_income', result_type='weighted_sum') tb2 = create_distribution_table(calc.dataframe(DIST_VARIABLES), groupby='large_income_bins', income_measure='expanded_income', result_type='weighted_sum') assert np.allclose(tb1[-1:], tb2[-1:]) tb3 = create_distribution_table(calc.dataframe(DIST_VARIABLES), groupby='small_income_bins', income_measure='expanded_income', result_type='weighted_avg') assert isinstance(tb3, pd.DataFrame)
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_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 xtest_diff_table_sum_row(pit_subsample): rec = Records(data=pit_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 = {2017: {'_rate2': [0.06]}} 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_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 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_Calculator_results_consistency(pit_fullsample, gst_sample, cit_crosssample): # generate calculated-variable dataframe for full sample in second year recs = Records(data=pit_fullsample) grecs = GSTRecords(data=gst_sample) crecs = CorpRecords(data=cit_crosssample) calc = Calculator(policy=Policy(), records=recs, gstrecords=grecs, corprecords=crecs) assert isinstance(calc, Calculator) assert calc.current_year == Policy.JSON_START_YEAR calc.advance_to_year(Policy.JSON_START_YEAR + 1) assert calc.current_year == Policy.JSON_START_YEAR + 1 calc.calc_all() varlist = list(Records.CALCULATED_VARS) vdf = calc.dataframe(varlist) assert isinstance(vdf, pd.DataFrame) # check consistency of calculated results individual by individual assert np.allclose(vdf['TTI'], vdf['GTI'] - vdf['deductions']) assert np.allclose(vdf['Aggregate_Income'], np.maximum(0., vdf['TTI'] - vdf['TI_special_rates'])) assert np.all(vdf['Tax_ST_CG_RATE1'] >= 0.) assert np.all(vdf['Tax_ST_CG_RATE2'] >= 0.) assert np.all(vdf['Tax_ST_CG_APPRATE'] == 0.) assert np.allclose(vdf['Total_Tax_STCG'], (vdf['Tax_ST_CG_RATE1'] + vdf['Tax_ST_CG_RATE2'] + vdf['Tax_ST_CG_APPRATE'])) assert np.all(vdf['Tax_LT_CG_RATE1'] >= 0.) assert np.all(vdf['Tax_LT_CG_RATE2'] >= 0.) assert np.allclose(vdf['Total_Tax_LTCG'], vdf['Tax_LT_CG_RATE1'] + vdf['Tax_LT_CG_RATE2']) assert np.allclose(vdf['Total_Tax_Cap_Gains'], vdf['Total_Tax_STCG'] + vdf['Total_Tax_LTCG']) assert np.all(vdf['tax_Aggregate_Income'] >= 0.) assert np.all(vdf['tax_TI_special_rates'] >= 0.) assert np.all(vdf['rebate_agri'] >= 0.) exp = vdf['tax_Aggregate_Income'] + vdf['tax_TI_special_rates'] exp -= vdf['rebate_agri'] assert np.allclose(vdf['tax_TTI'], exp) assert np.all(vdf['rebate'] >= 0.) assert np.all(vdf['surcharge'] >= 0.) assert np.all(vdf['cess'] >= 0.) assert np.all(vdf['pitax'] >= 0.) exp = vdf['tax_TTI'] - vdf['rebate'] + vdf['surcharge'] + vdf['cess'] assert np.allclose(vdf['pitax'], exp)
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_qbid_calculation(): """ Test Calculator's QBID calculations using the six example filing units specified in Table 1 of this TPC publication: "Navigating the New Pass-Through Provisions: A Technical Explanation" by William G. Gale and Aaron Krupkin (January 31, 2018), which is available at this URL: https://www.taxpolicycenter.org/publications/ navigating-new-pass-through-provisions-technical-explanation/full """ # In constructing the TPC example filing units, assume that the taxpayer # has business income in the form of e26270/e02000 income and no earnings, # and that the spouse has no business income and only earnings. TPC_YEAR = 2018 TPC_VARS = ( 'RECID,MARS,e00200s,e00200,e26270,e02000,PT_SSTB_income,' 'PT_binc_w2_wages,PT_ubia_property,pre_qbid_taxinc,qbid\n' ) TPC_FUNITS = ( '1,2, 99000, 99000,75000,75000,1,20000,90000,150000,15000.00\n' '2,2,349000,349000,75000,75000,1,20000,90000,400000, 1612.50\n' '3,2,524000,524000,75000,75000,1,20000,90000,575000, 0.00\n' '4,2, 99000, 99000,75000,75000,0,20000,90000,150000,15000.00\n' '5,2,349000,349000,75000,75000,0,20000,90000,400000,10750.00\n' '6,2,524000,524000,75000,75000,0,20000,90000,575000,10000.00\n' ) # generate actual Calculator pre-qbid taxinc and qbid amounts tpc_df = pd.read_csv(StringIO(TPC_VARS + TPC_FUNITS)) recs = Records(data=tpc_df, start_year=TPC_YEAR, gfactors=None, weights=None) calc = Calculator(policy=Policy(), records=recs) assert calc.current_year == TPC_YEAR calc.calc_all() varlist = ['RECID', 'c00100', 'standard', 'c04470', 'qbided'] tc_df = calc.dataframe(varlist) # compare actual amounts with expected amounts from TPC publication act_taxinc = tc_df.c00100 - np.maximum(tc_df.standard, tc_df.c04470) exp_taxinc = tpc_df.pre_qbid_taxinc assert np.allclose(act_taxinc, exp_taxinc) assert np.allclose(tc_df.qbided, tpc_df.qbid)
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 xtest_create_tables(pit_subsample): # pylint: disable=too-many-statements,too-many-branches # create a current-law Policy object and Calculator object calc1 rec = Records(data=pit_subsample) pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator object calc2 reform = {2017: {'_rate2': [0.06]}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() test_failure = False # test creating various difference tables diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'standard_income_bins', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'pc_aftertaxinc' expected = [ np.nan, np.nan, -0.24, -0.79, -0.81, -0.55, -0.77, -0.69, -0.70, -0.67, -0.27, -0.11, -0.06, -0.58 ] if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff xbin', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'weighted_deciles', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'tot_change' expected = [ 0, 0, 254095629, 2544151690, 2826173357, 2539574809, 4426339426, 5178198524, 6277367974, 8069273960, 10572653961, 10269542170, 52957371499, 6188055374, 3497187245, 584299551 ] if not np.allclose(diff[tabcol].values, expected, atol=0.51, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'share_of_change' expected = [ 0.00, 0.00, 0.48, 4.80, 5.34, 4.80, 8.36, 9.78, 11.85, 15.24, 19.96, 19.39, 100.00, 11.68, 6.60, 1.10 ] if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) tabcol = 'pc_aftertaxinc' expected = [ np.nan, np.nan, -0.26, -0.96, -0.74, -0.52, -0.75, -0.71, -0.68, -0.71, -0.71, -0.34, -0.58, -0.61, -0.30, -0.07 ] if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) # test creating various distribution tables dist, _ = calc2.distribution_tables(None, 'weighted_deciles') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [ 0, 0, -1818680093, 1971755805, 5010676892, 6746034269, 17979713134, 26281107130, 35678824858, 82705314943, 148818900147, 734130905355, 1057504552440, 152370476198, 234667184101, 347093245055 ] if not np.allclose(dist[tabcol].values, expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'num_returns_ItemDed' expected = [ 0, 0, 357019, 1523252, 2463769, 2571339, 4513934, 5278763, 6299826, 7713038, 11001450, 13125085, 54847474, 6188702, 5498415, 1437967 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'expanded_income' expected = [ 0, 0, 105927980655, 294023675202, 417068113194, 528376852001, 658853731628, 818158430558, 1037838578149, 1324689584778, 1788101751565, 4047642990187, 11020681687917, 1286581399758, 1511884268254, 1249177322176 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'aftertax_income' expected = [ 0, 0, 97735000432, 261911925318, 378049091828, 485619641327, 586208937799, 722979233740, 919208533243, 1130705156084, 1473967098213, 2994484618211, 9050869236196, 1002450732282, 1147596725957, 844437159972 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) dist, _ = calc2.distribution_tables(None, 'standard_income_bins') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [ 0, 0, -544908512, -140959365, 3354006293, 9339571323, 18473567840, 55206201916, 89276157367, 297973932010, 290155554334, 99528466942, 194882962290, 1057504552440 ] if not np.allclose(dist[tabcol], expected, atol=0.5, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'num_returns_ItemDed' expected = [ 0, 0, 60455, 1107780, 2366845, 3460607, 4837397, 10090391, 8850784, 17041135, 6187168, 532925, 311987, 54847474 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) if test_failure: assert 1 == 2
def calculate(year_n, start_year, use_puf_not_cps, use_full_sample, user_mods, behavior_allowed): """ The calculate function assumes the specified user_mods is a dictionary returned by the Calculator.read_json_param_objects() function. The function returns (calc1, calc2, mask) where calc1 is pre-reform Calculator object calculated for year_n, calc2 is post-reform Calculator object calculated for year_n, and mask is boolean array marking records with reform-induced iitax diffs Set behavior_allowed to False when generating static results or set behavior_allowed to True when generating dynamic results. """ # pylint: disable=too-many-arguments,too-many-locals # pylint: disable=too-many-branches,too-many-statements check_user_mods(user_mods) # specify Consumption instance consump = Consumption() consump_assumptions = user_mods['consumption'] consump.update_consumption(consump_assumptions) # specify growdiff_baseline and growdiff_response growdiff_baseline = Growdiff() growdiff_response = Growdiff() growdiff_base_assumps = user_mods['growdiff_baseline'] growdiff_resp_assumps = user_mods['growdiff_response'] growdiff_baseline.update_growdiff(growdiff_base_assumps) growdiff_response.update_growdiff(growdiff_resp_assumps) # create pre-reform and post-reform Growfactors instances growfactors_pre = Growfactors() growdiff_baseline.apply_to(growfactors_pre) growfactors_post = Growfactors() growdiff_baseline.apply_to(growfactors_post) growdiff_response.apply_to(growfactors_post) # create sample pd.DataFrame from specified input file and sampling scheme stime = time.time() tbi_path = os.path.abspath(os.path.dirname(__file__)) if use_puf_not_cps: # first try TaxBrain deployment path input_path = 'puf.csv.gz' if not os.path.isfile(input_path): # otherwise try local Tax-Calculator deployment path input_path = os.path.join(tbi_path, '..', '..', 'puf.csv') sampling_frac = 0.05 sampling_seed = 180 else: # if using cps input not puf input # first try Tax-Calculator code path input_path = os.path.join(tbi_path, '..', 'cps.csv.gz') if not os.path.isfile(input_path): # otherwise read from taxcalc package "egg" input_path = None # pragma: no cover full_sample = read_egg_csv('cps.csv.gz') # pragma: no cover sampling_frac = 0.03 sampling_seed = 180 if input_path: full_sample = pd.read_csv(input_path) if use_full_sample: sample = full_sample else: sample = full_sample.sample( # pylint: disable=no-member frac=sampling_frac, random_state=sampling_seed) if use_puf_not_cps: print('puf-read-time= {:.1f}'.format(time.time() - stime)) else: print('cps-read-time= {:.1f}'.format(time.time() - stime)) # create pre-reform Calculator instance if use_puf_not_cps: recs1 = Records(data=copy.deepcopy(sample), gfactors=growfactors_pre) else: recs1 = Records.cps_constructor(data=copy.deepcopy(sample), gfactors=growfactors_pre) policy1 = Policy(gfactors=growfactors_pre) calc1 = Calculator(policy=policy1, records=recs1, consumption=consump) while calc1.current_year < start_year: calc1.increment_year() calc1.calc_all() assert calc1.current_year == start_year # compute mask array res1 = calc1.dataframe(DIST_VARIABLES) if use_puf_not_cps: # create pre-reform Calculator instance with extra income recs1p = Records(data=copy.deepcopy(sample), gfactors=growfactors_pre) # add one dollar to the income of each filing unit to determine # which filing units undergo a resulting change in tax liability recs1p.e00200 += 1.0 # pylint: disable=no-member recs1p.e00200p += 1.0 # pylint: disable=no-member policy1p = Policy(gfactors=growfactors_pre) # create Calculator with recs1p and calculate for start_year calc1p = Calculator(policy=policy1p, records=recs1p, consumption=consump) while calc1p.current_year < start_year: calc1p.increment_year() calc1p.calc_all() assert calc1p.current_year == start_year # compute mask showing which of the calc1 and calc1p results differ; # mask is true if a filing unit's income tax liability changed after # a dollar was added to the filing unit's wage and salary income res1p = calc1p.dataframe(DIST_VARIABLES) mask = np.logical_not( # pylint: disable=no-member np.isclose(res1.iitax, res1p.iitax, atol=0.001, rtol=0.0)) assert np.any(mask) else: # if use_cps_not_cps is False # indicate that no fuzzing of reform results is required mask = np.zeros(res1.shape[0], dtype=np.int8) # specify Behavior instance behv = Behavior() behavior_assumps = user_mods['behavior'] behv.update_behavior(behavior_assumps) # always prevent both behavioral response and growdiff response if behv.has_any_response() and growdiff_response.has_any_response(): msg = 'BOTH behavior AND growdiff_response HAVE RESPONSE' raise ValueError(msg) # optionally prevent behavioral response if behv.has_any_response() and not behavior_allowed: msg = 'A behavior RESPONSE IS NOT ALLOWED' raise ValueError(msg) # create post-reform Calculator instance if use_puf_not_cps: recs2 = Records(data=copy.deepcopy(sample), gfactors=growfactors_post) else: recs2 = Records.cps_constructor(data=copy.deepcopy(sample), gfactors=growfactors_post) policy2 = Policy(gfactors=growfactors_post) policy_reform = user_mods['policy'] policy2.implement_reform(policy_reform) calc2 = Calculator(policy=policy2, records=recs2, consumption=consump, behavior=behv) while calc2.current_year < start_year: calc2.increment_year() calc2.calc_all() assert calc2.current_year == start_year # increment Calculator objects for year_n years and calculate for _ in range(0, year_n): calc1.increment_year() calc2.increment_year() calc1.calc_all() if calc2.behavior_has_response(): calc2 = Behavior.response(calc1, calc2) else: calc2.calc_all() # return calculated Calculator objects and mask return (calc1, calc2, mask)
def test_create_tables(cps_subsample): # pylint: disable=too-many-statements,too-many-branches # create a current-law Policy object and Calculator object calc1 rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator object calc2 reform = {2013: {'_II_rt1': [0.15]}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() test_failure = False # test creating various difference tables diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'standard_income_bins', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'pc_aftertaxinc' expected = [ np.nan, np.nan, -0.16, -0.57, -0.72, -0.69, -0.82, -0.80, -0.75, -0.65, -0.23, -0.09, -0.06, -0.59 ] if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff xbin', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'weighted_deciles', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'tot_change' expected = [ 0, 0, 1219678, 15503037, 25922077, 35000592, 48336897, 62637728, 79750078, 93136108, 116996252, 102458801, 580961247, 63156380, 33664610, 5637811 ] if not np.allclose(diff[tabcol].values, expected, atol=0.51, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'share_of_change' expected = [ 0.00, 0.00, 0.21, 2.67, 4.46, 6.02, 8.32, 10.78, 13.73, 16.03, 20.14, 17.64, 100.00, 10.87, 5.79, 0.97 ] if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) tabcol = 'pc_aftertaxinc' expected = [ np.nan, np.nan, -0.15, -0.62, -0.70, -0.73, -0.78, -0.80, -0.80, -0.74, -0.71, -0.30, -0.59, -0.55, -0.25, -0.06 ] if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) tabcol = 'pc_aftertaxinc' expected = [ np.nan, np.nan, -0.15, -0.62, -0.70, -0.73, -0.78, -0.80, -0.80, -0.74, -0.71, -0.30, -0.59, -0.55, -0.25, -0.06 ] if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) # test creating various distribution tables dist, _ = calc2.distribution_tables(None, 'weighted_deciles') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [ 0, 0, -53644343, -65258622, -57617119, 37391333, 200879230, 329784586, 553827330, 1015854407, 1731283600, 7090603505, 10783103907, 1638192777, 2213960052, 3238450675 ] if not np.allclose(dist[tabcol].values, expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'num_returns_ItemDed' expected = [ 0, 0, 2561, 12610, 21936, 29172, 50890, 61563, 78247, 91823, 118523, 128886, 596211, 63986, 51634, 13266 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'expanded_income' expected = [ 0, 0, 835224673, 2639667638, 3940559051, 5286856071, 6972849344, 8881099529, 11467767759, 14761195525, 19832126806, 44213000235, 118830346631, 14399218059, 16868648076, 12945134101 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'aftertax_income' expected = [ 0, 0, 818813684, 2466000535, 3671150517, 4790979126, 6173998985, 7754183496, 9907604744, 12510477225, 16273592612, 33915377411, 98282178334, 11345456373, 13400757263, 9169163776 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) dist, _ = calc2.distribution_tables(None, 'standard_income_bins') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [ 0, 0, -42244205, -76727831, -62581860, 53797887, 217016689, 723516183, 1108097059, 3272479928, 2818979541, 950296405, 1820474110, 10783103907 ] if not np.allclose(dist[tabcol], expected, atol=0.5, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'num_returns_ItemDed' expected = [ 0, 0, 1202, 13614, 27272, 34407, 48265, 117225, 103319, 181885, 61014, 5126, 2882, 596211 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) if test_failure: assert 1 == 2
def test_create_tables(cps_subsample): # pylint: disable=too-many-statements,too-many-branches # create a current-law Policy object and Calculator object calc1 rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator object calc2 reform = {2013: {'_II_rt1': [0.15]}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() test_failure = False # test creating various difference tables diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='large_income_bins', income_measure='expanded_income', tax_to_diff='combined') assert isinstance(diff, pd.DataFrame) expected = [np.nan, np.nan, -0.14, -0.58, -0.71, -0.70, -0.83, -0.81, -0.73, -0.65, -0.18, -0.59] tabcol = 'pc_aftertaxinc' if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='standard_income_bins', income_measure='expanded_income', tax_to_diff='iitax') assert isinstance(diff, pd.DataFrame) expected = [np.nan, np.nan, -0.14, -0.58, -0.71, -0.70, -0.83, -0.81, -0.73, -0.65, -0.23, -0.09, -0.06, -0.59] tabcol = 'pc_aftertaxinc' if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='small_income_bins', income_measure='expanded_income', tax_to_diff='iitax') assert isinstance(diff, pd.DataFrame) expected = [np.nan, np.nan, -0.29, -0.07, -0.23, -0.78, -0.66, -0.74, -0.70, -0.83, -0.81, -0.73, -0.65, -0.23, -0.09, -0.08, -0.07, -0.05, -0.02, np.nan, -0.59] tabcol = 'pc_aftertaxinc' if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='weighted_deciles', income_measure='expanded_income', tax_to_diff='combined') assert isinstance(diff, pd.DataFrame) expected = [0, 0, 1037894, 16199646, 25518793, 34455230, 49661093, 62344194, 82290396, 90006817, 117415735, 101818106, 580747904, 62408600, 33771695, 5637811] tabcol = 'tot_change' if not np.allclose(diff[tabcol].values, expected, atol=0.51, rtol=0.0): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.0f},'.format(val)) expected = [0.00, 0.00, 0.18, 2.79, 4.39, 5.93, 8.55, 10.74, 14.17, 15.50, 20.22, 17.53, 100.00, 10.75, 5.82, 0.97] tabcol = 'share_of_change' if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) expected = [np.nan, np.nan, -0.13, -0.65, -0.68, -0.71, -0.79, -0.80, -0.82, -0.71, -0.71, -0.30, -0.59, -0.55, -0.25, -0.06] tabcol = 'pc_aftertaxinc' if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) expected = [np.nan, np.nan, -0.13, -0.65, -0.68, -0.71, -0.79, -0.80, -0.82, -0.71, -0.71, -0.30, -0.59, -0.55, -0.25, -0.06] tabcol = 'pc_aftertaxinc' if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) # test creating various distribution tables dist = create_distribution_table(calc2.dataframe(DIST_VARIABLES), groupby='weighted_deciles', income_measure='expanded_income', result_type='weighted_sum') assert isinstance(dist, pd.DataFrame) expected = [0, 0, -54678669, -64005792, -64426464, 32739840, 207396898, 317535861, 575238615, 984782596, 1731373913, 7082515174, 10748471972, 1622921432, 2217477146, 3242116596] tabcol = 'iitax' if not np.allclose(dist[tabcol].values, expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) expected = [0, 0, 2561, 13268, 21368, 28377, 53186, 60433, 79779, 91010, 117445, 128784, 596211, 63766, 51681, 13337] tabcol = 'num_returns_ItemDed' if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) expected = [0, 0, 836765692, 2661991174, 3978757611, 5306258004, 7022134388, 8871843614, 11530190180, 14721635194, 19860290487, 44177752076, 118967618420, 14296456955, 16895894429, 12985400692] tabcol = 'expanded_income' if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) expected = [0, 0, 821526457, 2483359936, 3714540881, 4821394144, 6200512981, 7763298300, 9921184240, 12527297334, 16314596486, 33886371300, 98454082058, 11265497052, 13416447851, 9204426396] tabcol = 'aftertax_income' if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) dist = create_distribution_table(calc2.dataframe(DIST_VARIABLES), groupby='standard_income_bins', income_measure='expanded_income', result_type='weighted_sum') assert isinstance(dist, pd.DataFrame) expected = [0, 0, -43150804, -77526808, -64845122, 43303823, 225370761, 723847940, 1098042284, 3264499170, 2808160213, 950296405, 1820474110, 10748471972] tabcol = 'iitax' if not np.allclose(dist[tabcol], expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) expected = [0, 0, 1202, 13614, 27319, 33655, 50186, 116612, 103896, 181192, 60527, 5126, 2882, 596211] tabcol = 'num_returns_ItemDed' if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) if test_failure: assert 1 == 2
def test_create_tables(cps_subsample): # pylint: disable=too-many-statements,too-many-branches # create a current-law Policy object and Calculator object calc1 rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator object calc2 reform = {2013: {'_II_rt1': [0.15]}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() test_failure = False # test creating various difference tables diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'standard_income_bins', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'pc_aftertaxinc' expected = [ np.nan, np.nan, -0.22, -0.77, -0.80, -0.56, -0.77, -0.69, -0.71, -0.67, -0.27, -0.11, -0.06, -0.58 ] if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff xbin', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'weighted_deciles', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'tot_change' expected = [ 0, 0, 241418460, 2474292614, 2770584237, 2535721686, 4444363117, 5111483934, 6321945100, 8225913647, 10597074824, 10234573879, 52957371499, 6137031947, 3513242382, 584299551 ] if not np.allclose(diff[tabcol].values, expected, atol=0.51, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'share_of_change' expected = [ 0.00, 0.00, 0.46, 4.67, 5.23, 4.79, 8.39, 9.65, 11.94, 15.53, 20.01, 19.33, 100.00, 11.59, 6.63, 1.10 ] if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) tabcol = 'pc_aftertaxinc' expected = [ np.nan, np.nan, -0.25, -0.95, -0.73, -0.53, -0.75, -0.71, -0.68, -0.72, -0.71, -0.34, -0.58, -0.61, -0.30, -0.07 ] if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) tabcol = 'pc_aftertaxinc' expected = [ np.nan, np.nan, -0.25, -0.95, -0.73, -0.53, -0.75, -0.71, -0.68, -0.72, -0.71, -0.34, -0.58, -0.61, -0.30, -0.07 ] if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) # test creating various distribution tables dist, _ = calc2.distribution_tables(None, 'weighted_deciles') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [ 0, 0, -1962728575, 1725493747, 4396953820, 6605728718, 16774691083, 23860454276, 38350836962, 83963523110, 150930070726, 732859528574, 1057504552440, 151607017873, 234865455600, 346387055100 ] if not np.allclose(dist[tabcol].values, expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'num_returns_ItemDed' expected = [ 0, 0, 357019, 1448655, 2559613, 2513429, 4419624, 5275374, 6222375, 7880642, 11147728, 13023015, 54847474, 6118072, 5478575, 1426368 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'expanded_income' expected = [ 0, 0, 105133510325, 290616204980, 413576297349, 517828725223, 659857915773, 803218163892, 1042123266101, 1326558509787, 1805622773921, 4048576203396, 11013111570748, 1281956155093, 1515893182747, 1250726865556 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'aftertax_income' expected = [ 0, 0, 97194414402, 259232286042, 375389146785, 475719938941, 588002960320, 710495874184, 921012587826, 1129166044052, 1488817328688, 2998268537784, 9043299119027, 999335257953, 1151930187406, 847003092425 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) dist, _ = calc2.distribution_tables(None, 'standard_income_bins') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [ 0, 0, -544908512, -27720182, 2473153905, 9469043966, 17806661306, 56292468689, 88558244888, 298427035609, 290639143539, 99528466942, 194882962290, 1057504552440 ] if not np.allclose(dist[tabcol], expected, atol=0.5, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) tabcol = 'num_returns_ItemDed' expected = [ 0, 0, 60455, 1161281, 2323042, 3613216, 4759193, 10006287, 8785946, 17093586, 6199555, 532925, 311987, 54847474 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) if test_failure: assert 1 == 2
def test_create_tables(cps_subsample): # pylint: disable=too-many-statements # create a current-law Policy object and Calculator object calc1 rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator object calc2 reform = {2013: {'_II_rt1': [0.15]}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() # test creating various difference tables diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='large_income_bins', income_measure='expanded_income', tax_to_diff='combined') assert isinstance(diff, pd.DataFrame) expected = [0.00, 0.01, 0.41, 0.76, 0.85, 1.06, 1.14, 1.04, 0.76, 0.19, 0.70] assert np.allclose(diff['perc_aftertax'].values, expected, atol=0.005, rtol=0.0, equal_nan=True) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='webapp_income_bins', income_measure='expanded_income', tax_to_diff='iitax') assert isinstance(diff, pd.DataFrame) expected = [0.00, 0.01, 0.41, 0.76, 0.85, 1.06, 1.14, 1.04, 0.76, 0.26, 0.08, 0.06, 0.70] assert np.allclose(diff['perc_aftertax'].values, expected, atol=0.005, rtol=0.0, equal_nan=True) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='small_income_bins', income_measure='expanded_income', tax_to_diff='iitax') assert isinstance(diff, pd.DataFrame) expected = [0.00, 0.01, 0.02, 0.15, 0.58, 0.73, 0.78, 0.85, 1.06, 1.14, 1.04, 0.76, 0.26, 0.08, 0.08, 0.07, 0.04, 0.02, np.nan, 0.70] assert np.allclose(diff['perc_aftertax'].values, expected, atol=0.005, rtol=0.0, equal_nan=True) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='weighted_deciles', income_measure='expanded_income', tax_to_diff='combined') assert isinstance(diff, pd.DataFrame) expected = [14931, 276555, 7728872, 22552703, 34008512, 50233787, 76811377, 111167087, 123226970, 111414038, 537434832, 66560891, 39571078, 5282069] assert np.allclose(diff['tot_change'].values, expected, atol=0.5, rtol=0.0) expected = [0.00, 0.05, 1.44, 4.20, 6.33, 9.35, 14.29, 20.68, 22.93, 20.73, 100.00, 12.38, 7.36, 0.98] assert np.allclose(diff['share_of_change'].values, expected, atol=0.005, rtol=0.0) expected = [0.01, 0.02, 0.33, 0.70, 0.81, 0.91, 1.07, 1.18, 0.91, 0.37, 0.70, 0.69, 0.34, 0.06] assert np.allclose(diff['perc_aftertax'].values, expected, atol=0.005, rtol=0.0, equal_nan=True) expected = [-0.01, -0.02, -0.33, -0.70, -0.81, -0.91, -1.07, -1.18, -0.91, -0.37, -0.70, -0.69, -0.34, -0.06] assert np.allclose(diff['pc_aftertaxinc'].values, expected, atol=0.005, rtol=0.0, equal_nan=True) # test creating various distribution tables dist = create_distribution_table(calc2.dataframe(DIST_VARIABLES), groupby='weighted_deciles', income_measure='expanded_income', result_type='weighted_sum') assert isinstance(dist, pd.DataFrame) expected = [-8851215, -99666120, -123316561, -85895787, -47357458, 207462144, 443391189, 978487989, 1709504845, 7631268907, 10605027933, 1655597977, 2537684742, 3437986189] assert np.allclose(dist['iitax'].values, expected, atol=0.5, rtol=0.0) expected = [1202, 1688, 13506, 18019, 30130, 48244, 80994, 112788, 131260, 146001, 583832, 70258, 59834, 15909] assert np.allclose(dist['num_returns_ItemDed'].tolist(), expected, atol=0.5, rtol=0.0) expected = [158456013, 1351981790, 2383726863, 3408544081, 4569232020, 6321944661, 8520304098, 11817197884, 17299173380, 41117720202, 96948280992, 12723790026, 15769741079, 12624189098] assert np.allclose(dist['expanded_income'].tolist(), expected, atol=0.5, rtol=0.0) expected = [147367698, 1354827269, 2351611947, 3192405234, 4157431713, 5454468907, 7125788590, 9335613303, 13417244946, 29691084873, 76227844481, 9546216325, 11603328920, 8541539628] assert np.allclose(dist['aftertax_income'].tolist(), expected, atol=0.5, rtol=0.0) dist = create_distribution_table(calc2.dataframe(DIST_VARIABLES), groupby='webapp_income_bins', income_measure='expanded_income', result_type='weighted_sum') assert isinstance(dist, pd.DataFrame) expected = [-103274, -83144506, -152523834, -129881470, 85802556, 255480678, 832529135, 1066963515, 3023956558, 2876331264, 1008672459, 1820944852, 10605027933] assert np.allclose(dist['iitax'], expected, atol=0.5, rtol=0.0) expected = [0, 1202, 22654, 31665, 30547, 49851, 124786, 97349, 160147, 56806, 5803, 3023, 583832] assert np.allclose(dist['num_returns_ItemDed'].tolist(), expected, atol=0.5, rtol=0.0)
def test_create_tables(cps_subsample): # pylint: disable=too-many-statements,too-many-branches # create a current-law Policy object and Calculator object calc1 rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator object calc2 reform = {'II_rt1': {2013: 0.15}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() test_failure = False # test creating various difference tables diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'standard_income_bins', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'pc_aftertaxinc' expected = [0.0, np.nan, -0.1, -0.5, -0.7, -0.7, -0.8, -0.7, -0.7, -0.7, -0.3, -0.1, -0.0, -0.6] if not np.allclose(diff[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0, equal_nan=True): test_failure = True print('diff xbin', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'weighted_deciles', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'tot_change' expected = [0.0, 0.0, 0.0, 0.6, 2.9, 3.5, 4.4, 6.1, 6.5, 8.7, 12.0, 13.3, 58.0, 7.7, 4.8, 0.8] if not np.allclose(diff[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'share_of_change' expected = [0.0, 0.0, 0.0, 1.0, 5.0, 6.0, 7.6, 10.6, 11.1, 15.1, 20.7, 22.9, 100.0, 13.2, 8.3, 1.4,] if not np.allclose(diff[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'pc_aftertaxinc' expected = [np.nan, 0.0, -0.0, -0.3, -0.8, -0.7, -0.7, -0.8, -0.7, -0.7, -0.7, -0.3, -0.6, -0.7, -0.4, -0.1] if not np.allclose(diff[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0, equal_nan=True): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) # test creating various distribution tables dist, _ = calc2.distribution_tables(None, 'weighted_deciles') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [0.0, 0.0, -0.4, -4.1, -5.9, 8.0, 16.9, 29.0, 27.0, 71.4, 153.4, 910.1, 1205.5, 159.4, 268.1, 482.7] if not np.allclose(dist[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'count_ItemDed' expected = [0.0, 0.0, 0.0, 1.1, 2.6, 3.9, 4.7, 6.3, 6.5, 7.4, 11.3, 16.3, 60.3, 7.4, 7.2, 1.7] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'expanded_income' expected = [0.0, -1.4, 30.7, 209.8, 388.8, 541.2, 679.1, 847.6, 1097.1, 1430.7, 1978.3, 5007.6, 12209.4, 1410.9, 1765.5, 1831.2] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'aftertax_income' expected = [0.0, -1.4, 29.0, 195.5, 363.0, 491.0, 612.2, 747.1, 980.6, 1248.0, 1630.2, 3741.3, 10036.6, 1100.9, 1339.0, 1301.4] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) dist, _ = calc2.distribution_tables(None, 'standard_income_bins') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [0.0, 0.0, -1.3, -7.6, -1.2, 20.7, 26.3, 47.2, 95.5, 321.9, 324.0, 64.8, 315.2, 1205.5] if not np.allclose(dist[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'count_ItemDed' expected = [0.0, 0.0, 0.2, 1.8, 3.6, 5.9, 5.7, 10.2, 8.1, 17.7, 6.7, 0.3, 0.1, 60.3] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) if test_failure: assert 1 == 2
def test_create_tables(cps_subsample): # pylint: disable=too-many-statements,too-many-branches # create a current-law Policy object and Calculator object calc1 rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator object calc2 reform = {'II_rt1': {2013: 0.15}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() test_failure = False # test creating various difference tables diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'standard_income_bins', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'pc_aftertaxinc' expected = [ np.nan, np.nan, -0.2, -0.8, -0.8, -0.5, -0.8, -0.7, -0.7, -0.7, -0.3, -0.1, -0.1, -0.6 ] if not np.allclose( diff[tabcol].values, expected, atol=0.1, rtol=0.0, equal_nan=True): test_failure = True print('diff xbin', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'weighted_deciles', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'tot_change' expected = [ 0.0, 0.0, 0.3, 2.5, 2.8, 2.5, 4.4, 5.2, 6.3, 8.1, 10.6, 10.3, 53.0, 6.2, 3.5, 0.6 ] if not np.allclose(diff[tabcol].values, expected, atol=0.1, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'share_of_change' expected = [ 0.0, 0.0, 0.5, 4.8, 5.3, 4.8, 8.3, 9.9, 11.8, 15.2, 19.9, 19.4, 100.0, 11.7, 6.6, 1.1 ] if not np.allclose(diff[tabcol].values, expected, atol=0.1, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'pc_aftertaxinc' expected = [ np.nan, np.nan, -0.3, -1.0, -0.7, -0.5, -0.7, -0.7, -0.7, -0.7, -0.7, -0.3, -0.6, -0.6, -0.3, -0.1 ] if not np.allclose( diff[tabcol].values, expected, atol=0.1, rtol=0.0, equal_nan=True): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) # test creating various distribution tables dist, _ = calc2.distribution_tables(None, 'weighted_deciles') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [ 0.0, 0.0, -1.8, 2.0, 5.0, 6.7, 18.0, 26.1, 35.2, 82.7, 148.8, 734.1, 1056.9, 152.4, 234.7, 347.1 ] if not np.allclose(dist[tabcol].values, expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'count_ItemDed' expected = [ 0.0, 0.0, 0.4, 1.5, 2.5, 2.6, 4.5, 5.3, 6.3, 7.7, 11.0, 13.1, 54.9, 6.2, 5.5, 1.4 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'expanded_income' expected = [ 0.0, 0.0, 105.9, 294.0, 417.1, 528.4, 658.9, 818.2, 1037.8, 1324.7, 1788.1, 4047.6, 11020.7, 1286.6, 1511.9, 1249.2 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'aftertax_income' expected = [ 0.0, 0.0, 97.7, 261.9, 378.1, 485.6, 586.2, 723.2, 919.6, 1130.7, 1474.0, 2994.5, 9051.5, 1002.5, 1147.6, 844.4 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) dist, _ = calc2.distribution_tables(None, 'standard_income_bins') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [ 0.0, 0.0, -0.5, -0.1, 3.4, 9.3, 18.5, 54.6, 89.3, 298.0, 290.2, 99.5, 194.9, 1056.9 ] if not np.allclose(dist[tabcol], expected, atol=0.1, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'count_ItemDed' expected = [ 0.0, 0.0, 0.1, 1.1, 2.4, 3.5, 4.8, 10.1, 8.8, 17.0, 6.2, 0.5, 0.3, 54.9 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) if test_failure: assert 1 == 2
def test_create_tables(cps_subsample): # pylint: disable=too-many-statements,too-many-branches # create a current-law Policy object and Calculator object calc1 rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator object calc2 reform = {2013: {'_II_rt1': [0.15]}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() test_failure = False # test creating various difference tables diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='large_income_bins', income_measure='expanded_income', tax_to_diff='combined') assert isinstance(diff, pd.DataFrame) expected = [ 0.00, -0.14, -0.58, -0.70, -0.71, -0.81, -0.83, -0.74, -0.65, -0.18, -0.59 ] tabcol = 'pc_aftertaxinc' if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='standard_income_bins', income_measure='expanded_income', tax_to_diff='iitax') assert isinstance(diff, pd.DataFrame) expected = [ 0.00, -0.14, -0.58, -0.70, -0.71, -0.81, -0.83, -0.74, -0.65, -0.23, -0.09, -0.06, -0.59 ] tabcol = 'pc_aftertaxinc' if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='small_income_bins', income_measure='expanded_income', tax_to_diff='iitax') assert isinstance(diff, pd.DataFrame) expected = [ 0.00, -0.29, -0.07, -0.22, -0.80, -0.65, -0.74, -0.71, -0.81, -0.83, -0.74, -0.65, -0.23, -0.09, -0.08, -0.07, -0.05, -0.02, 0.00, -0.59 ] tabcol = 'pc_aftertaxinc' if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), groupby='weighted_deciles', income_measure='expanded_income', tax_to_diff='combined') assert isinstance(diff, pd.DataFrame) expected = [ 0, 855188, 15425829, 26212078, 33369237, 50208703, 63312937, 82312360, 90711899, 117518598, 101779164, 581705993, 62142547, 33919755, 5716862 ] tabcol = 'tot_change' if not np.allclose(diff[tabcol].values, expected, atol=0.51, rtol=0.0): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.0f},'.format(val)) expected = [ 0.00, 0.15, 2.65, 4.51, 5.74, 8.63, 10.88, 14.15, 15.59, 20.20, 17.50, 100.00, 10.68, 5.83, 0.98 ] tabcol = 'share_of_change' if not np.allclose(diff[tabcol].values, expected, atol=0.005, rtol=0.0): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) expected = [ 0.00, -0.11, -0.62, -0.71, -0.69, -0.81, -0.82, -0.82, -0.72, -0.71, -0.30, -0.59, -0.55, -0.25, -0.06 ] tabcol = 'pc_aftertaxinc' if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) expected = [ 0.00, -0.11, -0.62, -0.71, -0.69, -0.81, -0.82, -0.82, -0.72, -0.71, -0.30, -0.59, -0.55, -0.25, -0.06 ] tabcol = 'pc_aftertaxinc' if not np.allclose( diff[tabcol].values, expected, atol=0.005, rtol=0.0, equal_nan=True): test_failure = True print('diff', tabcol) for val in diff[tabcol].values: print('{:.2f},'.format(val)) # test creating various distribution tables dist = create_distribution_table(calc2.dataframe(DIST_VARIABLES), groupby='weighted_deciles', income_measure='expanded_income', result_type='weighted_sum') assert isinstance(dist, pd.DataFrame) expected = [ 0, -56140397, -67237556, -58897159, 17222017, 212673684, 328116256, 573255089, 992965515, 1730626734, 7142993526, 10815577709, 1625179635, 2241659962, 3276153930 ] tabcol = 'iitax' if not np.allclose(dist[tabcol].values, expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) expected = [ 0, 1202, 13981, 21932, 27445, 52318, 62509, 79749, 91861, 117068, 129463, 597527, 63940, 52137, 13387 ] tabcol = 'num_returns_ItemDed' if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) expected = [ 0, 812766585, 2639118220, 3940557055, 5243088362, 6988752253, 8827238879, 11605062543, 14729565181, 19894042635, 44374875397, 119055067109, 14255277238, 17039539254, 13080058905 ] tabcol = 'expanded_income' if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) expected = [ 0, 801755209, 2466382489, 3674186760, 4779876836, 6150380331, 7701226391, 10000914935, 12515316309, 16352910962, 34006973974, 98449924197, 11219604941, 13525917494, 9261451538 ] tabcol = 'aftertax_income' if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) dist = create_distribution_table(calc2.dataframe(DIST_VARIABLES), groupby='standard_income_bins', income_measure='expanded_income', result_type='weighted_sum') assert isinstance(dist, pd.DataFrame) expected = [ 0, -44670465, -79534586, -61791623, 34666275, 216487136, 742113595, 1099657851, 3270948526, 2826393721, 962881064, 1848426216, 10815577709 ] tabcol = 'iitax' if not np.allclose(dist[tabcol], expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) expected = [ 0, 1202, 13625, 27355, 33694, 50236, 116751, 104035, 181572, 60936, 5196, 2924, 597527 ] tabcol = 'num_returns_ItemDed' if not np.allclose(dist[tabcol].tolist(), expected, atol=0.5, rtol=0.0): test_failure = True print('dist', tabcol) for val in dist[tabcol].values: print('{:.0f},'.format(val)) if test_failure: assert 1 == 2
def test_create_tables(cps_subsample): # pylint: disable=too-many-statements,too-many-branches # create a current-law Policy object and Calculator object calc1 rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator object calc2 reform = {'II_rt1': {2013: 0.15}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() test_failure = False # test creating various difference tables diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'standard_income_bins', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'pc_aftertaxinc' expected = [np.nan, np.nan, -0.2, -0.8, -0.8, -0.5, -0.8, -0.7, -0.7, -0.7, -0.3, -0.1, -0.1, -0.6] if not np.allclose(diff[tabcol].values, expected, atol=0.1, rtol=0.0, equal_nan=True): test_failure = True print('diff xbin', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'weighted_deciles', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'tot_change' expected = [0.0, 0.0, 0.3, 2.5, 2.8, 2.5, 4.4, 5.2, 6.3, 8.1, 10.6, 10.3, 53.0, 6.2, 3.5, 0.6] if not np.allclose(diff[tabcol].values, expected, atol=0.1, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'share_of_change' expected = [0.0, 0.0, 0.5, 4.8, 5.3, 4.8, 8.3, 9.9, 11.8, 15.2, 19.9, 19.4, 100.0, 11.7, 6.6, 1.1] if not np.allclose(diff[tabcol].values, expected, atol=0.1, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'pc_aftertaxinc' expected = [np.nan, np.nan, -0.3, -1.0, -0.7, -0.5, -0.7, -0.7, -0.7, -0.7, -0.7, -0.3, -0.6, -0.6, -0.3, -0.1] if not np.allclose(diff[tabcol].values, expected, atol=0.1, rtol=0.0, equal_nan=True): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) # test creating various distribution tables dist, _ = calc2.distribution_tables(None, 'weighted_deciles') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [0.0, 0.0, -1.8, 2.0, 5.0, 6.7, 18.0, 26.1, 35.2, 82.7, 148.8, 734.1, 1056.9, 152.4, 234.7, 347.1] if not np.allclose(dist[tabcol].values, expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'count_ItemDed' expected = [0.0, 0.0, 0.4, 1.5, 2.5, 2.6, 4.5, 5.3, 6.3, 7.7, 11.0, 13.1, 54.9, 6.2, 5.5, 1.4] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'expanded_income' expected = [0.0, 0.0, 105.9, 294.0, 417.1, 528.4, 658.9, 818.2, 1037.8, 1324.7, 1788.1, 4047.6, 11020.7, 1286.6, 1511.9, 1249.2] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'aftertax_income' expected = [0.0, 0.0, 97.7, 261.9, 378.1, 485.6, 586.2, 723.2, 919.6, 1130.7, 1474.0, 2994.5, 9051.5, 1002.5, 1147.6, 844.4] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) dist, _ = calc2.distribution_tables(None, 'standard_income_bins') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [0.0, 0.0, -0.5, -0.1, 3.4, 9.3, 18.5, 54.6, 89.3, 298.0, 290.2, 99.5, 194.9, 1056.9] if not np.allclose(dist[tabcol], expected, atol=0.1, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'count_ItemDed' expected = [0.0, 0.0, 0.1, 1.1, 2.4, 3.5, 4.8, 10.1, 8.8, 17.0, 6.2, 0.5, 0.3, 54.9] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) if test_failure: assert 1 == 2
def test_create_tables(cps_subsample): # pylint: disable=too-many-statements,too-many-branches # create a current-law Policy object and Calculator object calc1 rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create a policy-reform Policy object and Calculator object calc2 reform = {'II_rt1': {2013: 0.15}} pol.implement_reform(reform) calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() test_failure = False # test creating various difference tables diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'standard_income_bins', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'pc_aftertaxinc' expected = [ 0.0, np.nan, -0.1, -0.5, -0.7, -0.7, -0.8, -0.7, -0.7, -0.7, -0.3, -0.1, -0.0, -0.6 ] if not np.allclose(diff[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0, equal_nan=True): test_failure = True print('diff xbin', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) diff = create_difference_table(calc1.dataframe(DIFF_VARIABLES), calc2.dataframe(DIFF_VARIABLES), 'weighted_deciles', 'combined') assert isinstance(diff, pd.DataFrame) tabcol = 'tot_change' expected = [ 0.0, 0.0, 0.0, 0.6, 2.7, 3.4, 4.2, 5.8, 6.3, 8.1, 11.5, 12.7, 55.2, 7.2, 4.7, 0.8 ] if not np.allclose( diff[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'share_of_change' expected = [ 0.0, 0.0, 0.0, 1.0, 4.9, 6.1, 7.6, 10.5, 11.4, 14.7, 20.7, 23.0, 100.0, 13.0, 8.5, 1.5 ] if not np.allclose( diff[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'pc_aftertaxinc' expected = [ np.nan, 0.0, -0.0, -0.3, -0.8, -0.7, -0.7, -0.8, -0.7, -0.7, -0.7, -0.3, -0.6, -0.7, -0.4, -0.1 ] if not np.allclose(diff[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0, equal_nan=True): test_failure = True print('diff xdec', tabcol) for val in diff[tabcol].values: print('{:.1f},'.format(val)) # test creating various distribution tables dist, _ = calc2.distribution_tables(None, 'weighted_deciles') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [ 0.0, 0.0, -0.2, -3.5, -5.8, 9.0, 16.6, 28.6, 30.3, 70.2, 153.0, 893.7, 1191.8, 145.6, 269.7, 478.4 ] if not np.allclose( dist[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'count_ItemDed' expected = [ 0.0, 0.0, 0.0, 1.0, 2.9, 4.2, 4.5, 6.0, 6.0, 7.5, 11.3, 15.6, 59.0, 6.7, 7.2, 1.8 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'expanded_income' expected = [ 0.0, -1.4, 28.1, 201.5, 377.6, 536.2, 662.2, 841.1, 1053.0, 1400.7, 1923.1, 4956.7, 11978.6, 1374.6, 1743.5, 1838.6 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'aftertax_income' expected = [ 0.0, -1.4, 26.3, 187.5, 353.1, 483.4, 596.6, 743.0, 934.6, 1221.8, 1579.5, 3721.8, 9846.3, 1096.0, 1317.8, 1308.0 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) dist, _ = calc2.distribution_tables(None, 'standard_income_bins') assert isinstance(dist, pd.DataFrame) tabcol = 'iitax' expected = [ 0.0, 0.0, -1.2, -7.0, 0.2, 23.4, 26.9, 52.7, 95.5, 305.5, 321.1, 58.6, 316.2, 1191.8 ] if not np.allclose( dist[tabcol].values.astype('float'), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) tabcol = 'count_ItemDed' expected = [ 0.0, 0.0, 0.2, 2.1, 3.5, 6.4, 5.8, 9.9, 7.8, 16.4, 6.5, 0.4, 0.1, 59.0 ] if not np.allclose(dist[tabcol].tolist(), expected, atol=0.1, rtol=0.0): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.1f},'.format(val)) if test_failure: assert 1 == 2