From 23afb3a34db10ee0e0355d3f9a850c4d611377e9 Mon Sep 17 00:00:00 2001 From: martinholmer Date: Fri, 13 Oct 2017 14:01:32 -0400 Subject: [PATCH] Modernize Calculator usage in tests --- taxcalc/calculate.py | 12 +- taxcalc/tests/test_calculate.py | 180 ++++++++++++------------- taxcalc/tests/test_consumption.py | 25 ++-- taxcalc/tests/test_macro_elasticity.py | 11 +- taxcalc/tests/test_pufcsv.py | 26 ++-- taxcalc/tests/test_reforms.py | 16 +-- taxcalc/tests/test_utils.py | 100 +++++++------- 7 files changed, 171 insertions(+), 199 deletions(-) diff --git a/taxcalc/calculate.py b/taxcalc/calculate.py index 20e9eac60..bdfc7a3b4 100644 --- a/taxcalc/calculate.py +++ b/taxcalc/calculate.py @@ -345,13 +345,11 @@ def current_law_version(self): """ Return Calculator object same as self except with current-law policy. """ - clp = self.policy.current_law_version() - recs = copy.deepcopy(self.records) - cons = copy.deepcopy(self.consumption) - behv = copy.deepcopy(self.behavior) - calc = Calculator(policy=clp, records=recs, sync_years=False, - consumption=cons, behavior=behv) - return calc + return Calculator(policy=self.policy.current_law_version(), + records=copy.deepcopy(self.records), + sync_years=False, + consumption=copy.deepcopy(self.consumption), + behavior=copy.deepcopy(self.behavior)) @staticmethod def read_json_param_objects(reform, assump): diff --git a/taxcalc/tests/test_calculate.py b/taxcalc/tests/test_calculate.py index af2b77988..a6c7084db 100644 --- a/taxcalc/tests/test_calculate.py +++ b/taxcalc/tests/test_calculate.py @@ -55,38 +55,38 @@ def fixture_policyfile(): os.remove(f.name) -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) +def test_make_calculator(cps_subsample): + pol = Policy(start_year=2014, num_years=9) + assert pol.current_year == 2014 + rec = 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()) + calc = Calculator(policy=pol, records=rec, + consumption=consump, behavior=Behavior()) assert calc.current_year == 2014 # test incorrect Calculator instantiation: with pytest.raises(ValueError): - calc = Calculator(policy=None, records=recs) + Calculator(policy=None, records=rec) with pytest.raises(ValueError): - calc = Calculator(policy=parm, records=None) + Calculator(policy=pol, records=None) with pytest.raises(ValueError): - calc = Calculator(policy=parm, records=recs, behavior=list()) + Calculator(policy=pol, records=rec, behavior=list()) with pytest.raises(ValueError): - calc = Calculator(policy=parm, records=recs, consumption=list()) + Calculator(policy=pol, records=rec, consumption=list()) -def test_make_Calculator_deepcopy(cps_subsample): - parm = Policy() - recs = Records.cps_constructor(data=cps_subsample) - calc1 = Calculator(policy=parm, records=recs) +def test_make_calculator_deepcopy(cps_subsample): + 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_make_Calculator_with_policy_reform(cps_subsample): - recs = Records.cps_constructor(data=cps_subsample) - year = recs.current_year +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': np.array([4000]), '_II_em_cpi': False, @@ -94,7 +94,7 @@ def test_make_Calculator_with_policy_reform(cps_subsample): "_STD_Aged_cpi": False}} pol.implement_reform(reform) # create a Calculator object using this policy reform - calc = Calculator(policy=pol, records=recs) + 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 == 4000 @@ -108,9 +108,9 @@ def test_make_Calculator_with_policy_reform(cps_subsample): np.array([1600, 1300, 1300, 1600, 1600])) -def test_make_Calculator_with_multiyear_reform(cps_subsample): - recs = Records.cps_constructor(data=cps_subsample) - year = recs.current_year +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: {}} @@ -119,7 +119,7 @@ def test_make_Calculator_with_multiyear_reform(cps_subsample): 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=recs) + 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 @@ -134,7 +134,7 @@ def test_make_Calculator_with_multiyear_reform(cps_subsample): np.array([1600, 1300, 1600, 1300, 1600])) -def test_Calculator_advance_to_year(cps_subsample): +def test_calculator_advance_to_year(cps_subsample): rec = Records.cps_constructor(data=cps_subsample) pol = Policy() calc = Calculator(policy=pol, records=rec) @@ -144,21 +144,21 @@ def test_Calculator_advance_to_year(cps_subsample): calc.advance_to_year(2015) -def test_make_Calculator_raises_on_no_policy(cps_subsample): +def test_make_calculator_raises_on_no_policy(cps_subsample): rec = Records.cps_constructor(data=cps_subsample) with pytest.raises(ValueError): - calc = Calculator(records=rec) + Calculator(records=rec) -def test_Calculator_attr_access_to_policy(cps_subsample): - recs = Records.cps_constructor(data=cps_subsample) - calc = Calculator(policy=Policy(), records=recs) +def test_calculator_attr_access_to_policy(cps_subsample): + rec = Records.cps_constructor(data=cps_subsample) + calc = Calculator(policy=Policy(), records=rec) assert hasattr(calc.records, 'c01000') assert hasattr(calc.policy, '_AMT_Child_em') assert hasattr(calc, 'policy') -def test_Calculator_current_law_version(cps_subsample): +def test_calculator_current_law_version(cps_subsample): rec = Records.cps_constructor(data=cps_subsample) pol = Policy() reform = {2013: {'_II_rt7': [0.45]}} @@ -170,9 +170,9 @@ def test_Calculator_current_law_version(cps_subsample): assert calc.policy.II_rt7 != calc_clp.policy.II_rt7 -def test_Calculator_create_distribution_table(cps_subsample): - recs = Records.cps_constructor(data=cps_subsample) - calc = Calculator(policy=Policy(), records=recs) +def test_calculator_create_distribution_table(cps_subsample): + rec = Records.cps_constructor(data=cps_subsample) + calc = Calculator(policy=Policy(), records=rec) calc.calc_all() dist_labels = ['Returns', 'AGI', 'Standard Deduction Filers', 'Standard Deduction', 'Itemizers', @@ -196,9 +196,9 @@ def test_Calculator_create_distribution_table(cps_subsample): assert isinstance(dt2, pd.DataFrame) -def test_Calculator_mtr(cps_subsample): - recs = Records.cps_constructor(data=cps_subsample) - calc = Calculator(policy=Policy(), records=recs) +def test_calculator_mtr(cps_subsample): + rec = Records.cps_constructor(data=cps_subsample) + calc = Calculator(policy=Policy(), records=rec) recs_pre_e00200p = copy.deepcopy(calc.records.e00200p) (mtr_ptx, mtr_itx, mtr_combined) = calc.mtr(variable_str='e00200p', zero_out_calculated_vars=True) @@ -208,7 +208,7 @@ def test_Calculator_mtr(cps_subsample): assert np.array_equal(mtr_combined, mtr_ptx) is False assert np.array_equal(mtr_ptx, mtr_itx) is False with pytest.raises(ValueError): - (_, _, mtr_combined) = calc.mtr(variable_str='bad_income_type') + calc.mtr(variable_str='bad_income_type') (_, _, mtr_combined) = calc.mtr(variable_str='e00200s') assert type(mtr_combined) == np.ndarray (_, _, mtr_combined) = calc.mtr(variable_str='e00650', @@ -222,7 +222,7 @@ def test_Calculator_mtr(cps_subsample): assert type(mtr_combined) == np.ndarray -def test_Calculator_mtr_when_PT_rates_differ(): +def test_calculator_mtr_when_PT_rates_differ(): reform = {2013: {'_II_rt1': [0.40], '_II_rt2': [0.40], '_II_rt3': [0.40], @@ -241,30 +241,27 @@ def test_Calculator_mtr_when_PT_rates_differ(): u'RECID,MARS,FLPDYR,e00200,e00200p,e00900,e00900p,extraneous\n' u'1, 1, 2009, 200000,200000, 100000,100000, 9999999999\n' ) - pol1 = Policy() - rec1 = Records(pd.read_csv(StringIO(funit))) - calc1 = Calculator(policy=pol1, records=rec1) + rec = Records(pd.read_csv(StringIO(funit))) + pol = Policy() + calc1 = Calculator(policy=pol, records=rec) (_, mtr1, _) = calc1.mtr(variable_str='p23250') - pol2 = Policy() - pol2.implement_reform(reform) - rec2 = Records(pd.read_csv(StringIO(funit))) - calc2 = Calculator(policy=pol2, records=rec2) + pol.implement_reform(reform) + calc2 = Calculator(policy=pol, records=rec) (_, mtr2, _) = calc2.mtr(variable_str='p23250') assert np.allclose(mtr1, mtr2, rtol=0.0, atol=1e-06) -def test_Calculator_create_difference_table(cps_subsample): +def test_calculator_create_difference_table(cps_subsample): # create current-law Policy object and use to create Calculator calc1 - cps1 = Records.cps_constructor(data=cps_subsample) - year = cps1.current_year - calc1 = Calculator(policy=Policy(), records=cps1) + rec = Records.cps_constructor(data=cps_subsample) + year = rec.current_year + pol = Policy() + calc1 = Calculator(policy=pol, records=rec) calc1.calc_all() # create policy-reform Policy object and use to create Calculator calc2 - policy2 = Policy() reform = {year: {'_II_rt7': [0.45]}} - policy2.implement_reform(reform) - cps2 = Records.cps_constructor(data=cps_subsample) - calc2 = Calculator(policy=policy2, records=cps2) + pol.implement_reform(reform) + calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() # create difference table and check that it is a Pandas DataFrame dtable = create_difference_table(calc1.records, calc2.records, @@ -274,15 +271,15 @@ def test_Calculator_create_difference_table(cps_subsample): assert isinstance(dtable, pd.DataFrame) -def test_Calculator_create_diagnostic_table(cps_subsample): - recs = Records.cps_constructor(data=cps_subsample) - calc = Calculator(policy=Policy(), records=recs) +def test_calculator_create_diagnostic_table(cps_subsample): + rec = Records.cps_constructor(data=cps_subsample) + calc = Calculator(policy=Policy(), records=rec) calc.calc_all() adt = create_diagnostic_table(calc) assert isinstance(adt, pd.DataFrame) -def test_make_Calculator_increment_years_first(cps_subsample): +def test_make_calculator_increment_years_first(cps_subsample): # create Policy object with policy reform syr = 2013 pol = Policy(start_year=syr, num_years=5) @@ -294,8 +291,8 @@ def test_make_Calculator_increment_years_first(cps_subsample): reform[2016]['_II_em_cpi'] = False pol.implement_reform(reform) # create Calculator object with Policy object as modified by reform - recs = Records.cps_constructor(data=cps_subsample) - calc = Calculator(policy=pol, records=recs) + 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() irate2015 = irates[2015 - syr] @@ -317,6 +314,7 @@ 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. """ + rec = 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], @@ -327,42 +325,39 @@ def test_ID_HC_vs_BS(cps_subsample): '_ID_Charity_hc': [1.0]}} hc_policy = Policy() hc_policy.implement_reform(hc_reform) - hc_recs = Records.cps_constructor(data=cps_subsample) - hc_calc = Calculator(policy=hc_policy, records=hc_recs) + hc_calc = Calculator(policy=hc_policy, records=rec) + hc_calc.calc_all() # 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_recs = Records.cps_constructor(data=cps_subsample) - bs_calc = Calculator(policy=bs_policy, records=bs_recs) - # compare calculated tax results generated by the two reforms - hc_calc.calc_all() + bs_calc = Calculator(policy=bs_policy, records=rec) bs_calc.calc_all() + # compare calculated tax results generated by the two reforms assert np.allclose(hc_calc.records.payrolltax, bs_calc.records.payrolltax) assert np.allclose(hc_calc.records.iitax, bs_calc.records.iitax) -def test_Calculator_using_nonstd_input(rawinputfile): +def test_calculator_using_nonstd_input(rawinputfile): # check Calculator handling of raw, non-standard input data with no aging - policy = Policy() - policy.set_year(RAWINPUTFILE_YEAR) # set policy params to input data year - nonpuf = Records(data=rawinputfile.name, + 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 nonpuf.dim == RAWINPUTFILE_FUNITS - calc = Calculator(policy=policy, - records=nonpuf, + 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() - exp_iitax = np.zeros((nonpuf.dim,)) - assert np.allclose(nonpuf.iitax, exp_iitax) + 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((nonpuf.dim,)) + exp_mtr_ptax = np.zeros((nonstd.dim,)) exp_mtr_ptax.fill(0.153) assert np.allclose(mtr_ptax, exp_mtr_ptax) @@ -470,26 +465,26 @@ def test_read_json_reform_file_and_implement_reform(reform_file, that is then used to call implement_reform method and Calculate.calc_all() NOTE: implement_reform called when policy.current_year == policy.start_year """ - policy = Policy() + pol = Policy() if set_year: - policy.set_year(2015) + pol.set_year(2015) param_dict = Calculator.read_json_param_objects(reform_file.name, assump_file.name) - policy.implement_reform(param_dict['policy']) - syr = policy.start_year - amt_brk1 = policy._AMT_brk1 + pol.implement_reform(param_dict['policy']) + syr = pol.start_year + amt_brk1 = pol._AMT_brk1 assert amt_brk1[2015 - syr] == 200000 assert amt_brk1[2016 - syr] > 200000 assert amt_brk1[2017 - syr] == 300000 assert amt_brk1[2018 - syr] > 300000 - ii_em = policy._II_em + ii_em = pol._II_em assert ii_em[2016 - syr] == 6000 assert ii_em[2017 - syr] == 6000 assert ii_em[2018 - syr] == 7500 assert ii_em[2019 - syr] > 7500 assert ii_em[2020 - syr] == 9000 assert ii_em[2021 - syr] > 9000 - amt_em = policy._AMT_em + amt_em = pol._AMT_em assert amt_em[2016 - syr, 0] > amt_em[2015 - syr, 0] assert amt_em[2017 - syr, 0] > amt_em[2016 - syr, 0] assert amt_em[2018 - syr, 0] == amt_em[2017 - syr, 0] @@ -497,7 +492,7 @@ def test_read_json_reform_file_and_implement_reform(reform_file, assert amt_em[2020 - syr, 0] == amt_em[2017 - syr, 0] assert amt_em[2021 - syr, 0] > amt_em[2020 - syr, 0] assert amt_em[2022 - syr, 0] > amt_em[2021 - syr, 0] - add4aged = policy._ID_Medical_frt_add4aged + add4aged = pol._ID_Medical_frt_add4aged assert add4aged[2015 - syr] == -0.025 assert add4aged[2016 - syr] == -0.025 assert add4aged[2017 - syr] == 0.0 @@ -652,28 +647,27 @@ def test_read_bad_json_assump_file(bad1assumpfile, bad2assumpfile, def test_convert_parameter_dict(): with pytest.raises(ValueError): - rdict = Calculator._convert_parameter_dict({2013: {'2013': [40000]}}) + Calculator._convert_parameter_dict({2013: {'2013': [40000]}}) with pytest.raises(ValueError): - rdict = Calculator._convert_parameter_dict({'_II_em': {2013: [40000]}}) + Calculator._convert_parameter_dict({'_II_em': {2013: [40000]}}) with pytest.raises(ValueError): - rdict = Calculator._convert_parameter_dict({4567: {2013: [40000]}}) + Calculator._convert_parameter_dict({4567: {2013: [40000]}}) with pytest.raises(ValueError): - rdict = Calculator._convert_parameter_dict({'_II_em': 40000}) + Calculator._convert_parameter_dict({'_II_em': 40000}) rdict = Calculator._convert_parameter_dict({'_II_em': {'2013': [40000]}}) assert isinstance(rdict, dict) def test_calc_all(reform_file, rawinputfile): cyr = 2016 - policy = Policy() + pol = Policy() param_dict = Calculator.read_json_param_objects(reform_file.name, None) - policy.implement_reform(param_dict['policy']) - policy.set_year(cyr) - nonpuf = Records(data=rawinputfile.name, gfactors=None, + pol.implement_reform(param_dict['policy']) + pol.set_year(cyr) + nonstd = Records(data=rawinputfile.name, gfactors=None, weights=None, start_year=cyr) - assert nonpuf.dim == RAWINPUTFILE_FUNITS - calc = Calculator(policy=policy, - records=nonpuf, + assert nonstd.dim == RAWINPUTFILE_FUNITS + calc = Calculator(policy=pol, records=nonstd, sync_years=False) # keeps raw data unchanged assert calc.current_year == cyr calc.calc_all() diff --git a/taxcalc/tests/test_consumption.py b/taxcalc/tests/test_consumption.py index a06869dfc..a34172d9e 100644 --- a/taxcalc/tests/test_consumption.py +++ b/taxcalc/tests/test_consumption.py @@ -67,27 +67,24 @@ def test_consumption_response(cps_subsample): with pytest.raises(ValueError): consump.response(list(), 1) # test correct call to response method - recs = Records.cps_constructor(data=cps_subsample) - pre = copy.deepcopy(recs.e20400) - consump.response(recs, 1.0) - post = recs.e20400 + 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(recs.dim) * mpc + expected_diff = np.ones(rec.dim) * mpc assert np.allclose(actual_diff, expected_diff) # compute earnings mtr with no consumption response - recs0 = Records.cps_constructor(data=cps_subsample) - calc0 = Calculator(policy=Policy(), records=recs0, consumption=None) - ided0 = copy.deepcopy(recs0.e20400) + 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.records.e20400, ided0) # compute earnings mtr with consumption response - recs1 = Records.cps_constructor(data=cps_subsample) - calc1 = Calculator(policy=Policy(), records=recs1, consumption=None) - assert np.allclose(calc1.records.e20400, ided0) - calc1.consumption.update_consumption(consumption_response) - (mtr1_ptax, mtr1_itax, _) = calc1.mtr(variable_str='e00200p', - wrt_full_compensation=False) + 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.records.e20400, ided0) # confirm that payroll mtr values are no different assert np.allclose(mtr1_ptax, mtr0_ptax) diff --git a/taxcalc/tests/test_macro_elasticity.py b/taxcalc/tests/test_macro_elasticity.py index b4b15ed28..65bc61190 100644 --- a/taxcalc/tests/test_macro_elasticity.py +++ b/taxcalc/tests/test_macro_elasticity.py @@ -2,13 +2,12 @@ def test_proportional_change_gdp(cps_subsample): - rec1 = Records.cps_constructor(data=cps_subsample) - calc1 = Calculator(policy=Policy(), records=rec1) - rec2 = Records.cps_constructor(data=cps_subsample) - pol2 = Policy() + rec = Records.cps_constructor(data=cps_subsample) + pol = Policy() + calc1 = Calculator(policy=pol, records=rec) reform = {2013: {'_II_em': [0.0]}} # reform increases taxes and MTRs - pol2.implement_reform(reform) - calc2 = Calculator(policy=pol2, records=rec2) + pol.implement_reform(reform) + calc2 = Calculator(policy=pol, records=rec) calc1.advance_to_year(2014) calc2.advance_to_year(2014) gdp_pchg = 100.0 * proportional_change_gdp(calc1, calc2, elasticity=0.36) diff --git a/taxcalc/tests/test_pufcsv.py b/taxcalc/tests/test_pufcsv.py index baa455bed..558f9ab41 100644 --- a/taxcalc/tests/test_pufcsv.py +++ b/taxcalc/tests/test_pufcsv.py @@ -235,36 +235,30 @@ def test_mtr(tests_path, puf_path): @pytest.mark.requires_pufcsv -def test_credit_reforms(puf_path): +def test_credit_reforms(puf_subsample): """ - Test personal credit reforms using small puf.csv sub-sample + Test personal credit reforms using puf.csv subsample """ - # pylint: disable=too-many-locals + rec = Records(data=puf_subsample) reform_year = 2017 - fullsample = pd.read_csv(puf_path) - subsample = fullsample.sample(frac=0.05, # pylint: disable=no-member - random_state=180) # create current-law Calculator object, calc1 - recs1 = Records(data=subsample) - calc1 = Calculator(policy=Policy(), records=recs1) + pol = Policy() + calc1 = Calculator(policy=pol, records=rec) calc1.advance_to_year(reform_year) calc1.calc_all() itax1 = (calc1.records.iitax * calc1.records.s006).sum() # create personal-refundable-credit-reform Calculator object, calc2 - recs2 = Records(data=subsample) - policy2 = Policy() reform = {reform_year: {'_II_credit': [[1000, 1000, 1000, 1000, 1000]]}} - policy2.implement_reform(reform) - calc2 = Calculator(policy=policy2, records=recs2) + pol.implement_reform(reform) + calc2 = Calculator(policy=pol, records=rec) calc2.advance_to_year(reform_year) calc2.calc_all() itax2 = (calc2.records.iitax * calc2.records.s006).sum() # create personal-nonrefundable-credit-reform Calculator object, calc3 - recs3 = Records(data=subsample) - policy3 = Policy() reform = {reform_year: {'_II_credit_nr': [[1000, 1000, 1000, 1000, 1000]]}} - policy3.implement_reform(reform) - calc3 = Calculator(policy=policy3, records=recs3) + pol = Policy() + pol.implement_reform(reform) + calc3 = Calculator(policy=pol, records=rec) calc3.advance_to_year(reform_year) calc3.calc_all() itax3 = (calc3.records.iitax * calc3.records.s006).sum() diff --git a/taxcalc/tests/test_reforms.py b/taxcalc/tests/test_reforms.py index e9fa19873..9293bb741 100644 --- a/taxcalc/tests/test_reforms.py +++ b/taxcalc/tests/test_reforms.py @@ -46,22 +46,20 @@ def reform_results(reform_dict, puf_data): """ # pylint: disable=too-many-locals # create current-law-policy Calculator object - pol1 = Policy() - rec1 = Records(data=puf_data) - calc1 = Calculator(policy=pol1, records=rec1, verbose=False, behavior=None) + pol = Policy() + rec = Records(data=puf_data) + calc1 = Calculator(policy=pol, records=rec, verbose=False, behavior=None) # create reform Calculator object with possible behavioral responses start_year = reform_dict['start_year'] - beh2 = Behavior() + beh = Behavior() if '_BE_cg' in reform_dict['value']: elasticity = reform_dict['value']['_BE_cg'] del reform_dict['value']['_BE_cg'] # in order to have a valid reform beh_assump = {start_year: {'_BE_cg': elasticity}} - beh2.update_behavior(beh_assump) + beh.update_behavior(beh_assump) reform = {start_year: reform_dict['value']} - pol2 = Policy() - pol2.implement_reform(reform) - rec2 = Records(data=puf_data) - calc2 = Calculator(policy=pol2, records=rec2, verbose=False, behavior=beh2) + pol.implement_reform(reform) + calc2 = Calculator(policy=pol, records=rec, verbose=False, behavior=beh) # increment both calculators to reform's start_year calc1.advance_to_year(start_year) calc2.advance_to_year(start_year) diff --git a/taxcalc/tests/test_utils.py b/taxcalc/tests/test_utils.py index 9fe2d7478..b0e784ff1 100644 --- a/taxcalc/tests/test_utils.py +++ b/taxcalc/tests/test_utils.py @@ -60,19 +60,17 @@ def test_validity_of_name_lists(): def test_create_tables(cps_subsample): # create a current-law Policy object and Calculator object calc1 - policy1 = Policy() - records1 = Records.cps_constructor(data=cps_subsample) - calc1 = Calculator(policy=policy1, records=records1) + 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]}} - policy2 = Policy() - policy2.implement_reform(reform) - records2 = Records.cps_constructor(data=cps_subsample) - calc2 = Calculator(policy=policy2, records=records2) + pol.implement_reform(reform) + calc2 = Calculator(policy=pol, records=rec) calc2.calc_all() - # test creating various difference tables + diff = create_difference_table(calc1.records, calc2.records, groupby='large_income_bins', income_measure='expanded_income', @@ -574,8 +572,8 @@ def test_add_quantile_bins(): def test_dist_table_sum_row(cps_subsample): - recs = Records.cps_constructor(data=cps_subsample) - calc = Calculator(policy=Policy(), records=recs) + 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', @@ -594,17 +592,15 @@ def test_dist_table_sum_row(cps_subsample): def test_diff_table_sum_row(cps_subsample): + rec = Records.cps_constructor(data=cps_subsample) # create a current-law Policy object and Calculator calc1 - policy1 = Policy() - records1 = Records.cps_constructor(data=cps_subsample) - calc1 = Calculator(policy=policy1, records=records1) + 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]}} - policy2 = Policy() - policy2.implement_reform(reform) - records2 = Records.cps_constructor(data=cps_subsample) - calc2 = Calculator(policy=policy2, records=records2) + 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.records, calc2.records, @@ -628,21 +624,21 @@ def test_mtr_graph_data(cps_subsample): calc = Calculator(policy=Policy(), records=Records.cps_constructor(data=cps_subsample)) with pytest.raises(ValueError): - gdata = mtr_graph_data(calc, calc, mars='bad', - income_measure='agi', - dollar_weighting=True) + mtr_graph_data(calc, calc, mars='bad', + income_measure='agi', + dollar_weighting=True) with pytest.raises(ValueError): - gdata = mtr_graph_data(calc, calc, mars=0, - income_measure='expanded_income', - dollar_weighting=True) + mtr_graph_data(calc, calc, mars=0, + income_measure='expanded_income', + dollar_weighting=True) with pytest.raises(ValueError): - gdata = mtr_graph_data(calc, calc, mars=list()) + mtr_graph_data(calc, calc, mars=list()) with pytest.raises(ValueError): - gdata = mtr_graph_data(calc, calc, mars='ALL', mtr_variable='e00200s') + mtr_graph_data(calc, calc, mars='ALL', mtr_variable='e00200s') with pytest.raises(ValueError): - gdata = mtr_graph_data(calc, calc, mtr_measure='badtax') + mtr_graph_data(calc, calc, mtr_measure='badtax') with pytest.raises(ValueError): - gdata = mtr_graph_data(calc, calc, income_measure='badincome') + mtr_graph_data(calc, calc, income_measure='badincome') gdata = mtr_graph_data(calc, calc, mars=1, mtr_wrt_full_compen=True, income_measure='wages', @@ -651,25 +647,25 @@ def test_mtr_graph_data(cps_subsample): def test_atr_graph_data(cps_subsample): - calc = Calculator(policy=Policy(), - records=Records.cps_constructor(data=cps_subsample)) + pol = Policy() + rec = Records.cps_constructor(data=cps_subsample) + calc = Calculator(policy=pol, records=rec) with pytest.raises(ValueError): - gdata = atr_graph_data(calc, calc, mars='bad') + atr_graph_data(calc, calc, mars='bad') with pytest.raises(ValueError): - gdata = atr_graph_data(calc, calc, mars=0) + atr_graph_data(calc, calc, mars=0) with pytest.raises(ValueError): - gdata = atr_graph_data(calc, calc, mars=list()) + atr_graph_data(calc, calc, mars=list()) with pytest.raises(ValueError): - gdata = atr_graph_data(calc, calc, atr_measure='badtax') + 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=Policy(), - records=Records.cps_constructor(data=cps_subsample)) + calcx = Calculator(policy=pol, records=rec) calcx.advance_to_year(2020) - gdata = atr_graph_data(calcx, calc) + atr_graph_data(calcx, calc) def test_xtr_graph_plot(cps_subsample): @@ -721,32 +717,30 @@ def test_write_graph_file(cps_subsample): def test_multiyear_diagnostic_table(cps_subsample): - behv = Behavior() - calc = Calculator(policy=Policy(), - records=Records.cps_constructor(data=cps_subsample), - behavior=behv) + rec = Records.cps_constructor(data=cps_subsample) + pol = Policy() + beh = Behavior() + calc = Calculator(policy=pol, records=rec, behavior=beh) with pytest.raises(ValueError): - adt = multiyear_diagnostic_table(calc, 0) + multiyear_diagnostic_table(calc, 0) with pytest.raises(ValueError): - adt = multiyear_diagnostic_table(calc, 20) + multiyear_diagnostic_table(calc, 20) adt = multiyear_diagnostic_table(calc, 3) assert isinstance(adt, pd.DataFrame) - behv.update_behavior({2013: {'_BE_sub': [0.3]}}) - calc = Calculator(policy=Policy(), - records=Records.cps_constructor(data=cps_subsample), - behavior=behv) + beh.update_behavior({2013: {'_BE_sub': [0.3]}}) + calc = Calculator(policy=pol, records=rec, behavior=beh) assert calc.behavior.has_response() adt = multiyear_diagnostic_table(calc, 3) assert isinstance(adt, pd.DataFrame) def test_myr_diag_table_wo_behv(cps_subsample): - pol = Policy() 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)) @@ -794,19 +788,17 @@ def test_ce_aftertax_income(cps_subsample): 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 - pol1 = Policy() - rec1 = Records.cps_constructor(data=cps_subsample) - calc1 = Calculator(policy=pol1, records=rec1) + pol = Policy() + calc1 = Calculator(policy=pol, records=rec) calc1.advance_to_year(cyr) calc1.calc_all() # specify calc2 and calc_all() for cyr - pol2 = Policy() reform = {2018: {'_II_em': [0.0]}} - pol2.implement_reform(reform) - rec2 = Records.cps_constructor(data=cps_subsample) - calc2 = Calculator(policy=pol2, records=rec2) + pol.implement_reform(reform) + calc2 = Calculator(policy=pol, records=rec) calc2.advance_to_year(cyr) calc2.calc_all() cedict = ce_aftertax_income(calc1, calc2, require_no_agg_tax_change=False)