def test_incorrect_Records_instantiation(gst_sample): with pytest.raises(ValueError): recs = GSTRecords(data=list()) with pytest.raises(ValueError): recs = GSTRecords(data=gst_sample, gfactors=list()) with pytest.raises(ValueError): recs = GSTRecords(data=gst_sample, gfactors=None, weights=list()) with pytest.raises(ValueError): recs = GSTRecords(data=gst_sample, gfactors=None, weights=None, start_year=list())
def test_correct_Records_instantiation(gst_sample): rec1 = GSTRecords(data=gst_sample) assert rec1 assert rec1.current_year == rec1.data_year rec1.set_current_year(rec1.data_year + 1) wghts_path = os.path.join(GSTRecords.CUR_PATH, GSTRecords.GST_WEIGHTS_FILENAME) wghts_df = pd.read_csv(wghts_path) rec2 = GSTRecords(data=gst_sample, gfactors=GrowFactors(), weights=wghts_df, start_year=GSTRecords.GSTCSV_YEAR) assert rec2 assert rec2.current_year == rec2.data_year
def test_correct_Calculator_instantiation(pit_fullsample, pit_subsample, gst_sample, cit_crosssample): syr = Policy.JSON_START_YEAR pol = Policy() assert pol.current_year == syr # specify expected number of filers and aggregate PIT liability expect_weight = 35.241e6 expect_pitax = 1812.601e9 # expect_corpweight = ??? # expect_citax = ??? # create full-sample Calculator object rec_full = Records(data=pit_fullsample) grec = GSTRecords(data=gst_sample) crec = CorpRecords(data=cit_crosssample) calc_full = Calculator(policy=pol, records=rec_full, gstrecords=grec, corprecords=crec) assert isinstance(calc_full, Calculator) assert calc_full.current_year == syr assert calc_full.records_current_year() == syr calc_full.calc_all() actual_full_weight = calc_full.total_weight() actual_full_pitax = calc_full.weighted_total('pitax') assert np.allclose([actual_full_weight], [expect_weight]) assert np.allclose([actual_full_pitax], [expect_pitax]) # TODO: test for corporate results # create sub-sample Calculator object """
def test_incorrect_Calculator_instantiation(pit_subsample, gst_sample, cit_crosssample): pol = Policy() rec = Records(data=pit_subsample) grec = GSTRecords(data=gst_sample) crec = CorpRecords(data=cit_crosssample) with pytest.raises(ValueError): Calculator(policy=None, records=rec, gstrecords=grec, corprecords=crec) with pytest.raises(ValueError): Calculator(policy=pol, records=None, gstrecords=grec, corprecords=crec) with pytest.raises(ValueError): Policy(num_years=0)
def test_dist_table_sum_row(pit_subsample, gst_sample, cit_crosssample): rec = Records(data=pit_subsample) grec = GSTRecords(data=gst_sample) crec = CorpRecords(data=cit_crosssample) calc = Calculator(policy=Policy(), records=rec, gstrecords=grec, corprecords=crec) calc.calc_all() tb1 = create_distribution_table(calc.distribution_table_dataframe(), 'standard_income_bins', 'GTI') tb2 = create_distribution_table(calc.distribution_table_dataframe(), 'weighted_deciles', 'GTI') allrow1 = tb1[-1:] allrow2 = tb2[-4:-3] assert np.allclose(allrow1, allrow2)
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_calculator_mtr(pit_fullsample, gst_sample, cit_crosssample): """ Test Calculator mtr method. """ recs = Records(data=pit_fullsample) grecs = GSTRecords(data=gst_sample) crecs = CorpRecords(data=cit_crosssample) calcx = Calculator(policy=Policy(), records=recs, gstrecords=grecs, corprecords=crecs) calcx.calc_all() pitax_x = calcx.array('pitax') GTIx = calcx.array('GTI') calc = Calculator(policy=Policy(), records=recs, gstrecords=grecs, corprecords=crecs) recs_pre_SALARIES = copy.deepcopy(calc.array('SALARIES')) mtr_pitax = calc.mtr(variable_str='SALARIES', zero_out_calculated_vars=True) recs_post_SALARIES = calc.array('SALARIES') assert np.allclose(recs_post_SALARIES, recs_pre_SALARIES) assert np.allclose(calc.array('pitax'), pitax_x) assert np.allclose(calc.array('GTI'), GTIx) with pytest.raises(ValueError): calc.mtr(variable_str='bad_income_type') mtr_pit = calc.mtr(variable_str='SALARIES', calc_all_already_called=True) assert isinstance(mtr_pit, np.ndarray) mtr_pit = calc.mtr(variable_str='PRFT_GAIN_BP_OTHR_SPECLTV_BUS', calc_all_already_called=True) assert isinstance(mtr_pit, np.ndarray) mtr_pit = calc.mtr(variable_str='SALARIES', calc_all_already_called=True) assert np.allclose(mtr_pitax, mtr_pit) assert np.allclose(calc.array('pitax'), pitax_x) assert np.allclose(calc.array('GTI'), GTIx)
def test_create_distribution_tables(pit_fullsample, gst_sample, cit_crosssample): # pylint: disable=too-many-statements,too-many-branches # create a current-law Policy object and Calculator object calc1 rec = Records(data=pit_fullsample) grec = GSTRecords(data=gst_sample) crec = CorpRecords(data=cit_crosssample) pol = Policy() calc1 = Calculator(policy=pol, records=rec, gstrecords=grec, corprecords=crec) 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, gstrecords=grec, corprecords=crec) calc2.calc_all() test_failure = False # test creating various distribution tables dist, _ = calc2.distribution_tables(None, 'weighted_deciles') assert isinstance(dist, pd.DataFrame) tabcol = 'pitax' expected = [ 0.000, 0.000, 0.000, 0.000, 0.000, 1.962, 5.711, 14.602, 45.503, 163.177, 397.795, 1018.520, 1647.270, 331.218, 384.399, 302.903 ] if not np.allclose(dist[tabcol].values, expected): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.3f},'.format(val)) tabcol = 'GTI' expected = [ 0.000, 0.000, 688.359, 893.687, 1107.005, 1332.670, 1605.580, 1824.545, 2327.660, 2818.092, 3848.954, 6071.569, 22518.121, 2490.655, 2119.235, 1461.678 ] if not np.allclose(dist[tabcol].tolist(), expected): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.3f},'.format(val)) dist, _ = calc2.distribution_tables(None, 'standard_income_bins') assert isinstance(dist, pd.DataFrame) tabcol = 'pitax' expected = [ 0.000, 0.000, 8.334, 279.113, 542.762, 401.310, 415.751, 0.000, 0.000, 0.000, 0.000, 1647.270 ] if not np.allclose(dist[tabcol], expected): test_failure = True print('dist xbin', tabcol) for val in dist[tabcol].values: print('{:.3f},'.format(val)) tabcol = 'GTI' expected = [ 0.000, 0.000, 5884.790, 7399.792, 4810.526, 2392.643, 2030.370, 0.000, 0.000, 0.000, 0.000, 22518.121 ] if not np.allclose(dist[tabcol].tolist(), expected): test_failure = True print('dist xdec', tabcol) for val in dist[tabcol].values: print('{:.3f},'.format(val)) """
def test_increment_year(gst_sample): recs = GSTRecords(data=gst_sample) assert recs.current_year == recs.data_year recs.increment_year() assert recs.current_year == recs.data_year + 1