taxcalc 4.2.1__py3-none-any.whl → 4.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. taxcalc/__init__.py +1 -1
  2. taxcalc/assumptions/ASSUMPTIONS.md +53 -0
  3. taxcalc/assumptions/README.md +17 -0
  4. taxcalc/assumptions/economic_assumptions_template.json +77 -0
  5. taxcalc/calcfunctions.py +7 -4
  6. taxcalc/data.py +10 -5
  7. taxcalc/policy.py +1 -1
  8. taxcalc/policy_current_law.json +4649 -288
  9. taxcalc/records.py +20 -15
  10. taxcalc/reforms/2017_law.json +125 -0
  11. taxcalc/reforms/2017_law.out.csv +10 -0
  12. taxcalc/reforms/ARPA.json +78 -0
  13. taxcalc/reforms/ARPA.out.csv +10 -0
  14. taxcalc/reforms/BrownKhanna.json +23 -0
  15. taxcalc/reforms/BrownKhanna.out.csv +10 -0
  16. taxcalc/reforms/CARES.json +40 -0
  17. taxcalc/reforms/CARES.out.csv +10 -0
  18. taxcalc/reforms/ConsolidatedAppropriationsAct2021.json +15 -0
  19. taxcalc/reforms/ConsolidatedAppropriationsAct2021.out.csv +10 -0
  20. taxcalc/reforms/Larson2019.json +36 -0
  21. taxcalc/reforms/Larson2019.out.csv +10 -0
  22. taxcalc/reforms/README.md +22 -0
  23. taxcalc/reforms/REFORMS.md +92 -0
  24. taxcalc/reforms/Renacci.json +61 -0
  25. taxcalc/reforms/Renacci.out.csv +10 -0
  26. taxcalc/reforms/SandersDeFazio.json +15 -0
  27. taxcalc/reforms/SandersDeFazio.out.csv +10 -0
  28. taxcalc/reforms/TCJA.json +160 -0
  29. taxcalc/reforms/TCJA.md +48 -0
  30. taxcalc/reforms/TCJA.out.csv +10 -0
  31. taxcalc/reforms/Trump2016.json +71 -0
  32. taxcalc/reforms/Trump2016.out.csv +10 -0
  33. taxcalc/reforms/Trump2017.json +51 -0
  34. taxcalc/reforms/Trump2017.out.csv +10 -0
  35. taxcalc/reforms/archive/Clinton2016.json +56 -0
  36. taxcalc/reforms/archive/RyanBrady.json +104 -0
  37. taxcalc/reforms/archive/TCJA_House.json +144 -0
  38. taxcalc/reforms/archive/TCJA_House_Amended.json +152 -0
  39. taxcalc/reforms/archive/TCJA_Reconciliation.json +187 -0
  40. taxcalc/reforms/archive/TCJA_Senate.json +116 -0
  41. taxcalc/reforms/archive/TCJA_Senate_111417.json +169 -0
  42. taxcalc/reforms/archive/TCJA_Senate_120117.json +174 -0
  43. taxcalc/reforms/cases.csv +10 -0
  44. taxcalc/reforms/clp.out.csv +10 -0
  45. taxcalc/reforms/ext.json +59 -0
  46. taxcalc/reforms/growfactors_ext.csv +65 -0
  47. taxcalc/reforms/ptaxes0.json +37 -0
  48. taxcalc/reforms/ptaxes0.out.csv +10 -0
  49. taxcalc/reforms/ptaxes1.json +21 -0
  50. taxcalc/reforms/ptaxes1.out.csv +10 -0
  51. taxcalc/reforms/ptaxes2.json +18 -0
  52. taxcalc/reforms/ptaxes2.out.csv +10 -0
  53. taxcalc/reforms/ptaxes3.json +28 -0
  54. taxcalc/reforms/ptaxes3.out.csv +10 -0
  55. taxcalc/taxcalcio.py +44 -22
  56. taxcalc/tests/benefits_expect.csv +169 -0
  57. taxcalc/tests/cmpi_cps_expect.txt +132 -0
  58. taxcalc/tests/cmpi_puf_expect.txt +132 -0
  59. taxcalc/tests/conftest.py +143 -0
  60. taxcalc/tests/cpscsv_agg_expect.csv +26 -0
  61. taxcalc/tests/puf_var_correl_coeffs_2016.csv +80 -0
  62. taxcalc/tests/puf_var_wght_means_by_year.csv +80 -0
  63. taxcalc/tests/pufcsv_agg_expect.csv +26 -0
  64. taxcalc/tests/pufcsv_mtr_expect.txt +63 -0
  65. taxcalc/tests/reforms.json +649 -0
  66. taxcalc/tests/reforms_expect.csv +65 -0
  67. taxcalc/tests/test_4package.py +67 -0
  68. taxcalc/tests/test_benefits.py +86 -0
  69. taxcalc/tests/test_calcfunctions.py +871 -0
  70. taxcalc/tests/test_calculator.py +1021 -0
  71. taxcalc/tests/test_compare.py +336 -0
  72. taxcalc/tests/test_compatible_data.py +338 -0
  73. taxcalc/tests/test_consumption.py +144 -0
  74. taxcalc/tests/test_cpscsv.py +163 -0
  75. taxcalc/tests/test_data.py +133 -0
  76. taxcalc/tests/test_decorators.py +332 -0
  77. taxcalc/tests/test_growdiff.py +102 -0
  78. taxcalc/tests/test_growfactors.py +94 -0
  79. taxcalc/tests/test_parameters.py +617 -0
  80. taxcalc/tests/test_policy.py +1557 -0
  81. taxcalc/tests/test_puf_var_stats.py +194 -0
  82. taxcalc/tests/test_pufcsv.py +385 -0
  83. taxcalc/tests/test_records.py +234 -0
  84. taxcalc/tests/test_reforms.py +386 -0
  85. taxcalc/tests/test_responses.py +41 -0
  86. taxcalc/tests/test_taxcalcio.py +755 -0
  87. taxcalc/tests/test_utils.py +792 -0
  88. taxcalc/validation/CSV_INPUT_VARS.md +29 -0
  89. taxcalc/validation/CSV_OUTPUT_VARS.md +63 -0
  90. taxcalc/validation/README.md +68 -0
  91. taxcalc/validation/taxsim35/Differences_Explained.md +54 -0
  92. taxcalc/validation/taxsim35/README.md +139 -0
  93. taxcalc/validation/taxsim35/expected_differences/a17-taxdiffs-expect.csv +25 -0
  94. taxcalc/validation/taxsim35/expected_differences/a18-taxdiffs-expect.csv +25 -0
  95. taxcalc/validation/taxsim35/expected_differences/a19-taxdiffs-expect.csv +25 -0
  96. taxcalc/validation/taxsim35/expected_differences/a20-taxdiffs-expect.csv +25 -0
  97. taxcalc/validation/taxsim35/expected_differences/a21-taxdiffs-expect.csv +25 -0
  98. taxcalc/validation/taxsim35/expected_differences/b17-taxdiffs-expect.csv +25 -0
  99. taxcalc/validation/taxsim35/expected_differences/b18-taxdiffs-expect.csv +25 -0
  100. taxcalc/validation/taxsim35/expected_differences/b19-taxdiffs-expect.csv +25 -0
  101. taxcalc/validation/taxsim35/expected_differences/b20-taxdiffs-expect.csv +25 -0
  102. taxcalc/validation/taxsim35/expected_differences/b21-taxdiffs-expect.csv +25 -0
  103. taxcalc/validation/taxsim35/expected_differences/c17-taxdiffs-expect.csv +25 -0
  104. taxcalc/validation/taxsim35/expected_differences/c18-taxdiffs-expect.csv +25 -0
  105. taxcalc/validation/taxsim35/expected_differences/c19-taxdiffs-expect.csv +25 -0
  106. taxcalc/validation/taxsim35/input_setup.py +67 -0
  107. taxcalc/validation/taxsim35/main_comparison.py +183 -0
  108. taxcalc/validation/taxsim35/prepare_taxcalc_input.py +161 -0
  109. taxcalc/validation/taxsim35/process_taxcalc_output.py +140 -0
  110. taxcalc/validation/taxsim35/taxsim_emulation.json +49 -0
  111. taxcalc/validation/taxsim35/taxsim_input.py +321 -0
  112. taxcalc/validation/taxsim35/tc_sims.py +98 -0
  113. taxcalc/validation/taxsim35/tests_35.py +80 -0
  114. taxcalc/validation/tests_35.sh +13 -0
  115. {taxcalc-4.2.1.dist-info → taxcalc-4.3.0.dist-info}/METADATA +3 -4
  116. taxcalc-4.3.0.dist-info/RECORD +139 -0
  117. {taxcalc-4.2.1.dist-info → taxcalc-4.3.0.dist-info}/WHEEL +1 -1
  118. taxcalc/tmd_growfactors.csv +0 -55
  119. taxcalc/tmd_weights.csv.gz +0 -0
  120. taxcalc-4.2.1.dist-info/RECORD +0 -34
  121. {taxcalc-4.2.1.dist-info → taxcalc-4.3.0.dist-info}/LICENSE +0 -0
  122. {taxcalc-4.2.1.dist-info → taxcalc-4.3.0.dist-info}/entry_points.txt +0 -0
  123. {taxcalc-4.2.1.dist-info → taxcalc-4.3.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,1021 @@
1
+ """
2
+ Tests of Calculator class.
3
+ """
4
+ # CODING-STYLE CHECKS:
5
+ # pycodestyle test_calculator.py
6
+ # pylint --disable=locally-disabled test_calculator.py
7
+ #
8
+ # pylint: disable=too-many-lines,invalid-name
9
+
10
+ import os
11
+ from io import StringIO
12
+ import copy
13
+ import pytest
14
+ import numpy as np
15
+ import pandas as pd
16
+ from taxcalc import Policy, Records, Calculator, Consumption
17
+
18
+
19
+ def test_make_calculator(cps_subsample):
20
+ """
21
+ Test Calculator class ctor.
22
+ """
23
+ start_year = Policy.JSON_START_YEAR
24
+ sim_year = 2018
25
+ pol = Policy()
26
+ assert pol.current_year == start_year
27
+ rec = Records.cps_constructor(data=cps_subsample)
28
+ consump = Consumption()
29
+ consump.update_consumption({'MPC_e20400': {sim_year: 0.05}})
30
+ assert consump.current_year == start_year
31
+ calc = Calculator(policy=pol, records=rec,
32
+ consumption=consump, verbose=True)
33
+ assert calc.data_year == Records.CPSCSV_YEAR
34
+ assert calc.current_year == Records.CPSCSV_YEAR
35
+ # test incorrect Calculator instantiation:
36
+ with pytest.raises(ValueError):
37
+ Calculator(policy=None, records=rec)
38
+ with pytest.raises(ValueError):
39
+ Calculator(policy=pol, records=None)
40
+ with pytest.raises(ValueError):
41
+ Calculator(policy=pol, records=rec, consumption=list())
42
+
43
+
44
+ def test_make_calculator_deepcopy(cps_subsample):
45
+ """
46
+ Test deepcopy of Calculator object.
47
+ """
48
+ pol = Policy()
49
+ rec = Records.cps_constructor(data=cps_subsample)
50
+ calc1 = Calculator(policy=pol, records=rec)
51
+ calc2 = copy.deepcopy(calc1)
52
+ assert isinstance(calc2, Calculator)
53
+
54
+
55
+ def test_make_calculator_with_policy_reform(cps_subsample):
56
+ """
57
+ Test Calculator class ctor with policy reform.
58
+ """
59
+ rec = Records.cps_constructor(data=cps_subsample)
60
+ year = rec.current_year
61
+ # create a Policy object and apply a policy reform
62
+ pol = Policy()
63
+ reform = {
64
+ 'II_em': {2013: 4000},
65
+ 'II_em-indexed': {2013: False},
66
+ 'STD_Aged': {2013: [1600, 1300, 1300, 1600, 1600]},
67
+ 'STD_Aged-indexed': {2013: False}
68
+ }
69
+ pol.implement_reform(reform)
70
+ # create a Calculator object using this policy reform
71
+ calc = Calculator(policy=pol, records=rec)
72
+ assert calc.reform_warnings == {}
73
+ # check that Policy object embedded in Calculator object is correct
74
+ assert calc.current_year == year
75
+ assert calc.policy_param('II_em') == 4000
76
+ assert np.allclose(calc.policy_param('_II_em'),
77
+ np.array([4000] * Policy.DEFAULT_NUM_YEARS))
78
+ exp_STD_Aged = [[1600, 1300, 1300,
79
+ 1600, 1600]] * Policy.DEFAULT_NUM_YEARS
80
+ assert np.allclose(calc.policy_param('_STD_Aged'),
81
+ np.array(exp_STD_Aged))
82
+ assert np.allclose(calc.policy_param('STD_Aged'),
83
+ np.array([1600, 1300, 1300, 1600, 1600]))
84
+
85
+
86
+ def test_make_calculator_with_multiyear_reform(cps_subsample):
87
+ """
88
+ Test Calculator class ctor with multi-year policy reform.
89
+ """
90
+ rec = Records.cps_constructor(data=cps_subsample)
91
+ year = rec.current_year
92
+ # create a Policy object and apply a policy reform
93
+ pol = Policy()
94
+ reform = {
95
+ 'II_em': {2015: 5000, 2016: 6000},
96
+ 'II_em-indexed': {2015: False},
97
+ 'STD_Aged': {2016: [1600, 1300, 1600, 1300, 1600]}
98
+ }
99
+ pol.implement_reform(reform)
100
+ # create a Calculator object using this policy-reform
101
+ calc = Calculator(policy=pol, records=rec)
102
+ # check that Policy object embedded in Calculator object is correct
103
+ assert pol.num_years == Policy.DEFAULT_NUM_YEARS
104
+ assert calc.current_year == year
105
+ assert calc.policy_param('II_em') == 3950
106
+ exp_II_em = [3900, 3950, 5000] + [6000] * (Policy.DEFAULT_NUM_YEARS - 3)
107
+ assert np.allclose(calc.policy_param('_II_em'),
108
+ np.array(exp_II_em))
109
+ calc.increment_year()
110
+ calc.increment_year()
111
+ assert calc.current_year == 2016
112
+ assert np.allclose(calc.policy_param('STD_Aged'),
113
+ np.array([1600, 1300, 1600, 1300, 1600]))
114
+
115
+
116
+ def test_calculator_advance_to_year(cps_subsample):
117
+ """
118
+ Test Calculator advance_to_year method.
119
+ """
120
+ rec = Records.cps_constructor(data=cps_subsample)
121
+ pol = Policy()
122
+ calc = Calculator(policy=pol, records=rec)
123
+ calc.advance_to_year(2016)
124
+ assert calc.current_year == 2016
125
+ with pytest.raises(ValueError):
126
+ calc.advance_to_year(2015)
127
+
128
+
129
+ def test_make_calculator_raises_on_no_policy(cps_subsample):
130
+ """
131
+ Test Calculator ctor error with no policy argument.
132
+ """
133
+ rec = Records.cps_constructor(data=cps_subsample)
134
+ with pytest.raises(ValueError):
135
+ Calculator(records=rec)
136
+
137
+
138
+ def test_calculator_mtr(cps_subsample):
139
+ """
140
+ Test Calculator mtr method.
141
+ """
142
+ rec = Records.cps_constructor(data=cps_subsample)
143
+ calcx = Calculator(policy=Policy(), records=rec)
144
+ calcx.calc_all()
145
+ combinedx = calcx.array('combined')
146
+ c00100x = calcx.array('c00100')
147
+ calc = Calculator(policy=Policy(), records=rec)
148
+ recs_pre_e00200p = copy.deepcopy(calc.array('e00200p'))
149
+ (mtr_ptx, mtr_itx, mtr_cmb) = calc.mtr(variable_str='e00200p',
150
+ zero_out_calculated_vars=True)
151
+ recs_post_e00200p = calc.array('e00200p')
152
+ assert np.allclose(recs_post_e00200p, recs_pre_e00200p)
153
+ assert np.allclose(calc.array('combined'), combinedx)
154
+ assert np.allclose(calc.array('c00100'), c00100x)
155
+ assert np.array_equal(mtr_cmb, mtr_ptx) is False
156
+ assert np.array_equal(mtr_ptx, mtr_itx) is False
157
+ with pytest.raises(ValueError):
158
+ calc.mtr(variable_str='bad_income_type')
159
+ (_, _, mtr_combined) = calc.mtr(variable_str='e00200s',
160
+ calc_all_already_called=True)
161
+ assert isinstance(mtr_combined, np.ndarray)
162
+ (_, _, mtr_combined) = calc.mtr(variable_str='e00650',
163
+ negative_finite_diff=True,
164
+ calc_all_already_called=True)
165
+ assert isinstance(mtr_combined, np.ndarray)
166
+ (_, _, mtr_combined) = calc.mtr(variable_str='e00900p',
167
+ calc_all_already_called=True)
168
+ assert isinstance(mtr_combined, np.ndarray)
169
+ (_, _, mtr_combined) = calc.mtr(variable_str='e01700',
170
+ calc_all_already_called=True)
171
+ assert isinstance(mtr_combined, np.ndarray)
172
+ (_, _, mtr_combined) = calc.mtr(variable_str='e26270',
173
+ calc_all_already_called=True)
174
+ assert isinstance(mtr_combined, np.ndarray)
175
+ (_, _, mtr_combined) = calc.mtr(variable_str='k1bx14p',
176
+ calc_all_already_called=True)
177
+ assert isinstance(mtr_combined, np.ndarray)
178
+ (_, _, mtr_combined) = calc.mtr(variable_str='e00200p',
179
+ calc_all_already_called=True)
180
+ assert np.allclose(mtr_combined, mtr_cmb)
181
+ assert np.allclose(calc.array('combined'), combinedx)
182
+ assert np.allclose(calc.array('c00100'), c00100x)
183
+
184
+
185
+ def test_calculator_mtr_when_PT_rates_differ():
186
+ """
187
+ Test Calculator mtr method in special case.
188
+ """
189
+ reform = {
190
+ 'II_rt1': {2013: 0.40},
191
+ 'II_rt2': {2013: 0.40},
192
+ 'II_rt3': {2013: 0.40},
193
+ 'II_rt4': {2013: 0.40},
194
+ 'II_rt5': {2013: 0.40},
195
+ 'II_rt6': {2013: 0.40},
196
+ 'II_rt7': {2013: 0.40},
197
+ 'PT_rt1': {2013: 0.30},
198
+ 'PT_rt2': {2013: 0.30},
199
+ 'PT_rt3': {2013: 0.30},
200
+ 'PT_rt4': {2013: 0.30},
201
+ 'PT_rt5': {2013: 0.30},
202
+ 'PT_rt6': {2013: 0.30},
203
+ 'PT_rt7': {2013: 0.30}
204
+ }
205
+ funit = (
206
+ 'RECID,MARS,FLPDYR,e00200,e00200p,e00900,e00900p,extraneous\n'
207
+ '1, 1, 2009, 200000,200000, 100000,100000, 9999999999\n'
208
+ )
209
+ rec = Records(pd.read_csv(StringIO(funit)))
210
+ pol = Policy()
211
+ calc1 = Calculator(policy=pol, records=rec)
212
+ (_, mtr1, _) = calc1.mtr(variable_str='p23250')
213
+ pol.implement_reform(reform)
214
+ calc2 = Calculator(policy=pol, records=rec)
215
+ (_, mtr2, _) = calc2.mtr(variable_str='p23250')
216
+ assert np.allclose(mtr1, mtr2, rtol=0.0, atol=1e-06)
217
+
218
+
219
+ def test_make_calculator_increment_years_first(cps_subsample):
220
+ """
221
+ Test Calculator inflation indexing of policy parameters.
222
+ """
223
+ # pylint: disable=too-many-locals
224
+ # create Policy object with policy reform
225
+ pol = Policy()
226
+ std5 = 2000
227
+ reform = {
228
+ 'STD_Aged': {2015: [std5, std5, std5, std5, std5]},
229
+ 'II_em': {2015: 5000,
230
+ 2016: 6000},
231
+ 'II_em-indexed': {2016: False}
232
+ }
233
+ pol.implement_reform(reform)
234
+ # create Calculator object with Policy object as modified by reform
235
+ rec = Records.cps_constructor(data=cps_subsample)
236
+ calc = Calculator(policy=pol, records=rec)
237
+ # compare expected policy parameter values with those embedded in calc
238
+ irates = pol.inflation_rates()
239
+ syr = Policy.JSON_START_YEAR
240
+ irate2015 = irates[2015 - syr]
241
+ irate2016 = irates[2016 - syr]
242
+ std6 = std5 * (1.0 + irate2015)
243
+ std7 = std6 * (1.0 + irate2016)
244
+ exp_STD_Aged = np.array([[1500, 1200, 1200, 1500, 1200],
245
+ [1550, 1200, 1200, 1550, 1200],
246
+ [std5, std5, std5, std5, std5],
247
+ [std6, std6, std6, std6, std6],
248
+ [std7, std7, std7, std7, std7]])
249
+ act_STD_Aged = calc.policy_param('_STD_Aged')
250
+ assert np.allclose(act_STD_Aged[:5], exp_STD_Aged)
251
+ exp_II_em = np.array([3900, 3950, 5000, 6000, 6000])
252
+ act_II_em = calc.policy_param('_II_em')
253
+ assert np.allclose(act_II_em[:5], exp_II_em)
254
+
255
+
256
+ def test_ID_HC_vs_BS(cps_subsample):
257
+ """
258
+ Test that complete haircut of itemized deductions produces same
259
+ results as a 100% benefit surtax with no benefit deduction.
260
+ """
261
+ recs = Records.cps_constructor(data=cps_subsample)
262
+ # specify complete-haircut reform policy and Calculator object
263
+ hc_policy = Policy()
264
+ hc_reform = {
265
+ 'ID_Medical_hc': {2013: 1.0},
266
+ 'ID_StateLocalTax_hc': {2013: 1.0},
267
+ 'ID_RealEstate_hc': {2013: 1.0},
268
+ 'ID_Casualty_hc': {2013: 1.0},
269
+ 'ID_Miscellaneous_hc': {2013: 1.0},
270
+ 'ID_InterestPaid_hc': {2013: 1.0},
271
+ 'ID_Charity_hc': {2013: 1.0}
272
+ }
273
+ hc_policy.implement_reform(hc_reform)
274
+ hc_calc = Calculator(policy=hc_policy, records=recs)
275
+ hc_calc.calc_all()
276
+ hc_taxes = hc_calc.dataframe(['iitax', 'payrolltax'])
277
+ del hc_calc
278
+ # specify benefit-surtax reform policy and Calculator object
279
+ bs_policy = Policy()
280
+ bs_reform = {
281
+ 'ID_BenefitSurtax_crt': {2013: 0.0},
282
+ 'ID_BenefitSurtax_trt': {2013: 1.0}
283
+ }
284
+ bs_policy.implement_reform(bs_reform)
285
+ bs_calc = Calculator(policy=bs_policy, records=recs)
286
+ bs_calc.calc_all()
287
+ bs_taxes = bs_calc.dataframe([], all_vars=True)
288
+ del bs_calc
289
+ # compare calculated taxes generated by the two reforms
290
+ assert np.allclose(hc_taxes['payrolltax'], bs_taxes['payrolltax'])
291
+ assert np.allclose(hc_taxes['iitax'], bs_taxes['iitax'])
292
+
293
+
294
+ def test_ID_StateLocal_HC_vs_CRT(cps_subsample):
295
+ """
296
+ Test that a cap on state/local income and sales tax deductions at 0 percent
297
+ of AGI is equivalent to a complete haircut on the same state/local tax
298
+ deductions.
299
+ """
300
+ rec = Records.cps_constructor(data=cps_subsample)
301
+ # specify state/local complete haircut reform policy and Calculator object
302
+ hc_policy = Policy()
303
+ hc_reform = {'ID_StateLocalTax_hc': {2013: 1.0}}
304
+ hc_policy.implement_reform(hc_reform)
305
+ hc_calc = Calculator(policy=hc_policy, records=rec)
306
+ hc_calc.calc_all()
307
+ # specify AGI cap reform policy and Calculator object
308
+ crt_policy = Policy()
309
+ crt_reform = {'ID_StateLocalTax_crt': {2013: 0.0}}
310
+ crt_policy.implement_reform(crt_reform)
311
+ crt_calc = Calculator(policy=crt_policy, records=rec)
312
+ crt_calc.calc_all()
313
+ # compare calculated tax results generated by the two reforms
314
+ assert np.allclose(hc_calc.array('payrolltax'),
315
+ crt_calc.array('payrolltax'))
316
+ assert np.allclose(hc_calc.array('iitax'),
317
+ crt_calc.array('iitax'))
318
+
319
+
320
+ def test_ID_RealEstate_HC_vs_CRT(cps_subsample):
321
+ """
322
+ Test that a cap on all state, local, and foreign real estate tax deductions
323
+ at 0 percent of AGI is equivalent to a complete haircut on the same real
324
+ estate tax deductions.
325
+ """
326
+ rec = Records.cps_constructor(data=cps_subsample)
327
+ # specify real estate complete haircut reform policy and Calculator object
328
+ hc_policy = Policy()
329
+ hc_reform = {'ID_RealEstate_hc': {2013: 1.0}}
330
+ hc_policy.implement_reform(hc_reform)
331
+ hc_calc = Calculator(policy=hc_policy, records=rec)
332
+ hc_calc.calc_all()
333
+ # specify AGI cap reform policy and Calculator object
334
+ crt_policy = Policy()
335
+ crt_reform = {'ID_RealEstate_crt': {2013: 0.0}}
336
+ crt_policy.implement_reform(crt_reform)
337
+ crt_calc = Calculator(policy=crt_policy, records=rec)
338
+ crt_calc.calc_all()
339
+ # compare calculated tax results generated by the two reforms
340
+ assert np.allclose(hc_calc.array('payrolltax'),
341
+ crt_calc.array('payrolltax'))
342
+ assert np.allclose(hc_calc.array('iitax'),
343
+ crt_calc.array('iitax'))
344
+
345
+
346
+ RAWINPUT_FUNITS = 4
347
+ RAWINPUT_YEAR = 2015
348
+ RAWINPUT_CONTENTS = (
349
+ 'RECID,MARS,unknown\n'
350
+ ' 1, 2, 9\n'
351
+ ' 2, 1, 9\n'
352
+ ' 3, 4, 9\n'
353
+ ' 4, 3, 9\n'
354
+ )
355
+
356
+
357
+ def test_calculator_using_nonstd_input():
358
+ """
359
+ Test Calculator using non-standard input records.
360
+ """
361
+ # check Calculator handling of raw, non-standard input data with no aging
362
+ pol = Policy()
363
+ pol.set_year(RAWINPUT_YEAR) # set policy params to input data year
364
+ nonstd = Records(data=pd.read_csv(StringIO(RAWINPUT_CONTENTS)),
365
+ start_year=RAWINPUT_YEAR, # set raw input data year
366
+ gfactors=None, # keeps raw data unchanged
367
+ weights=None)
368
+ assert nonstd.array_length == RAWINPUT_FUNITS
369
+ calc = Calculator(policy=pol, records=nonstd,
370
+ sync_years=False) # keeps raw data unchanged
371
+ assert calc.current_year == RAWINPUT_YEAR
372
+ calc.calc_all()
373
+ assert calc.weighted_total('e00200') == 0
374
+ assert calc.total_weight() == 0
375
+ varlist = ['RECID', 'MARS']
376
+ dframe = calc.dataframe(varlist)
377
+ assert isinstance(dframe, pd.DataFrame)
378
+ assert dframe.shape == (RAWINPUT_FUNITS, len(varlist))
379
+ mars = calc.array('MARS')
380
+ assert isinstance(mars, np.ndarray)
381
+ assert mars.shape == (RAWINPUT_FUNITS,)
382
+ exp_iitax = np.zeros((nonstd.array_length,))
383
+ assert np.allclose(calc.array('iitax'), exp_iitax)
384
+ mtr_ptax, _, _ = calc.mtr(wrt_full_compensation=False)
385
+ exp_mtr_ptax = np.zeros((nonstd.array_length,))
386
+ exp_mtr_ptax.fill(0.153)
387
+ assert np.allclose(mtr_ptax, exp_mtr_ptax)
388
+
389
+
390
+ def test_bad_json_names(tests_path):
391
+ """
392
+ Test that ValueError raised with assump or reform do not end in '.json'
393
+ """
394
+ test_url = (
395
+ 'https://raw.githubusercontent.com/PSLmodels/'
396
+ 'Tax-Calculator/master/taxcalc/reforms/'
397
+ '2017_law.out.csv'
398
+ )
399
+ csvname = os.path.join(tests_path, '..', 'growfactors.csv')
400
+ with pytest.raises(ValueError):
401
+ Calculator.read_json_param_objects(csvname, None)
402
+ with pytest.raises(ValueError):
403
+ Calculator.read_json_param_objects(test_url, None)
404
+ with pytest.raises(ValueError):
405
+ Calculator.read_json_param_objects(None, csvname)
406
+ with pytest.raises(ValueError):
407
+ Calculator.read_json_param_objects(None, test_url)
408
+
409
+
410
+ def test_json_assump_url():
411
+ """
412
+ Test reading JSON assumption file using URL.
413
+ """
414
+ assump_str = """
415
+ {
416
+ "consumption": {
417
+ // all BEN_*_value parameters have a default value of one
418
+ "BEN_housing_value": {"2017": 1.0},
419
+ "BEN_snap_value": {"2017": 1.0},
420
+ "BEN_tanf_value": {"2017": 1.0},
421
+ "BEN_vet_value": {"2017": 1.0},
422
+ "BEN_wic_value": {"2017": 1.0},
423
+ "BEN_mcare_value": {"2017": 1.0},
424
+ "BEN_mcaid_value": {"2017": 1.0},
425
+ "BEN_other_value": {"2017": 1.0},
426
+ // all MPC_* parameters have a default value of zero
427
+ "MPC_e17500": {"2017": 0.0},
428
+ "MPC_e18400": {"2017": 0.0},
429
+ "MPC_e19800": {"2017": 0.0},
430
+ "MPC_e20400": {"2017": 0.0}
431
+ },
432
+ "growdiff_baseline": {
433
+ // all growdiff_baseline parameters have a default value of zero
434
+ "ABOOK": {"2017": 0.0},
435
+ "ACGNS": {"2017": 0.0},
436
+ "ACPIM": {"2017": 0.0},
437
+ "ACPIU": {"2017": 0.0},
438
+ "ADIVS": {"2017": 0.0},
439
+ "AINTS": {"2017": 0.0},
440
+ "AIPD": {"2017": 0.0},
441
+ "ASCHCI": {"2017": 0.0},
442
+ "ASCHCL": {"2017": 0.0},
443
+ "ASCHEI": {"2017": 0.0},
444
+ "ASCHEL": {"2017": 0.0},
445
+ "ASCHF": {"2017": 0.0},
446
+ "ASOCSEC": {"2017": 0.0},
447
+ "ATXPY": {"2017": 0.0},
448
+ "AUCOMP": {"2017": 0.0},
449
+ "AWAGE": {"2017": 0.0},
450
+ "ABENOTHER": {"2017": 0.0},
451
+ "ABENMCARE": {"2017": 0.0},
452
+ "ABENMCAID": {"2017": 0.0},
453
+ "ABENSSI": {"2017": 0.0},
454
+ "ABENSNAP": {"2017": 0.0},
455
+ "ABENWIC": {"2017": 0.0},
456
+ "ABENHOUSING": {"2017": 0.0},
457
+ "ABENTANF": {"2017": 0.0},
458
+ "ABENVET": {"2017": 0.0}
459
+ },
460
+ "growdiff_response": {
461
+ // all growdiff_response parameters have a default value of zero
462
+ "ABOOK": {"2017": 0.0},
463
+ "ACGNS": {"2017": 0.0},
464
+ "ACPIM": {"2017": 0.0},
465
+ "ACPIU": {"2017": 0.0},
466
+ "ADIVS": {"2017": 0.0},
467
+ "AINTS": {"2017": 0.0},
468
+ "AIPD": {"2017": 0.0},
469
+ "ASCHCI": {"2017": 0.0},
470
+ "ASCHCL": {"2017": 0.0},
471
+ "ASCHEI": {"2017": 0.0},
472
+ "ASCHEL": {"2017": 0.0},
473
+ "ASCHF": {"2017": 0.0},
474
+ "ASOCSEC": {"2017": 0.0},
475
+ "ATXPY": {"2017": 0.0},
476
+ "AUCOMP": {"2017": 0.0},
477
+ "AWAGE": {"2017": 0.0},
478
+ "ABENOTHER": {"2017": 0.0},
479
+ "ABENMCARE": {"2017": 0.0},
480
+ "ABENMCAID": {"2017": 0.0},
481
+ "ABENSSI": {"2017": 0.0},
482
+ "ABENSNAP": {"2017": 0.0},
483
+ "ABENWIC": {"2017": 0.0},
484
+ "ABENHOUSING": {"2017": 0.0},
485
+ "ABENTANF": {"2017": 0.0},
486
+ "ABENVET": {"2017": 0.0}
487
+ }
488
+ }
489
+ """
490
+ assump_url = ('https://raw.githubusercontent.com/PSLmodels/'
491
+ 'Tax-Calculator/master/taxcalc/assumptions/'
492
+ 'economic_assumptions_template.json')
493
+ params_str = Calculator.read_json_param_objects(None, assump_str)
494
+ assert params_str
495
+ params_url = Calculator.read_json_param_objects(None, assump_url)
496
+ assert params_url
497
+ assert params_url == params_str
498
+
499
+ assump_gh_url = (
500
+ "github://PSLmodels:Tax-Calculator@master/taxcalc/assumptions/"
501
+ "economic_assumptions_template.json"
502
+ )
503
+ params_gh_url = Calculator.read_json_param_objects(None, assump_gh_url)
504
+ assert params_gh_url
505
+ assert params_gh_url == params_str
506
+
507
+
508
+ def test_read_bad_json_assump_file():
509
+ """
510
+ Test invalid JSON assumption files.
511
+ """
512
+ badassump1 = """
513
+ {
514
+ "consumption": { // example of incorrect JSON because 'x' must be "x"
515
+ 'x': {"2014": 0.25}
516
+ },
517
+ "growdiff_baseline": {},
518
+ "growdiff_response": {}
519
+ }
520
+ """
521
+ with pytest.raises(ValueError):
522
+ Calculator.read_json_param_objects(None, badassump1)
523
+ with pytest.raises(ValueError):
524
+ Calculator.read_json_param_objects(None, 'unknown_file_name')
525
+ with pytest.raises(TypeError):
526
+ Calculator.read_json_param_objects(None, list())
527
+
528
+
529
+ def test_json_doesnt_exist():
530
+ """
531
+ Test JSON file which doesn't exist
532
+ """
533
+ with pytest.raises(ValueError):
534
+ Calculator.read_json_param_objects(None, './reforms/doesnt_exist.json')
535
+ with pytest.raises(ValueError):
536
+ Calculator.read_json_param_objects('./reforms/doesnt_exist.json', None)
537
+
538
+
539
+ def test_calc_all():
540
+ """
541
+ Test calc_all method.
542
+ """
543
+ cyr = 2016
544
+ pol = Policy()
545
+ pol.set_year(cyr)
546
+ nonstd = Records(data=pd.read_csv(StringIO(RAWINPUT_CONTENTS)),
547
+ start_year=cyr, gfactors=None, weights=None)
548
+ assert nonstd.array_length == RAWINPUT_FUNITS
549
+ calc = Calculator(policy=pol, records=nonstd,
550
+ sync_years=False) # keeps raw data unchanged
551
+ assert calc.current_year == cyr
552
+
553
+
554
+ def test_noreform_documentation():
555
+ """
556
+ Test automatic documentation creation.
557
+ """
558
+ reform_json = """
559
+ {
560
+ }
561
+ """
562
+ assump_json = """
563
+ {
564
+ "consumption": {},
565
+ "growdiff_baseline": {},
566
+ "growdiff_response": {}
567
+ }
568
+ """
569
+ params = Calculator.read_json_param_objects(reform_json, assump_json)
570
+ assert isinstance(params, dict)
571
+ actual_doc = Calculator.reform_documentation(params)
572
+ expected_doc = (
573
+ 'REFORM DOCUMENTATION\n'
574
+ 'Baseline Growth-Difference Assumption Values by Year:\n'
575
+ 'none: no baseline GrowDiff assumptions specified\n'
576
+ 'Response Growth-Difference Assumption Values by Year:\n'
577
+ 'none: no response GrowDiff assumptions specified\n'
578
+ 'Policy Reform Parameter Values by Year:\n'
579
+ 'none: using current-law policy parameters\n'
580
+ )
581
+ assert actual_doc == expected_doc
582
+
583
+
584
+ def test_reform_documentation():
585
+ """
586
+ Test automatic documentation creation.
587
+ """
588
+ reform_json = """
589
+ {
590
+ "II_em-indexed": {
591
+ "2016": false,
592
+ "2018": true
593
+ },
594
+ "II_em": {
595
+ "2016": 5000,
596
+ "2018": 6000,
597
+ "2020": 7000
598
+ },
599
+ "EITC_indiv": {
600
+ "2017": true
601
+ },
602
+ "STD_Aged-indexed": {
603
+ "2016": false
604
+ },
605
+ "STD_Aged": {
606
+ "2016": [1600, 1300, 1300, 1600, 1600],
607
+ "2020": [2000, 2000, 2000, 2000, 2000]
608
+ },
609
+ "ID_BenefitCap_Switch": {
610
+ "2020": [false, false, false, false, false, false, false]
611
+ }
612
+ }
613
+ """
614
+ assump_json = """
615
+ {
616
+ "consumption": {},
617
+ // increase baseline inflation rate by one percentage point in 2014+
618
+ // (has no effect on known policy parameter values)
619
+ "growdiff_baseline": {"ACPIU": {"2014": 0.010}},
620
+ "growdiff_response": {"ACPIU": {"2014": 0.015}}
621
+ }
622
+ """
623
+ params = Calculator.read_json_param_objects(reform_json, assump_json)
624
+ assert isinstance(params, dict)
625
+ second_reform = {'II_em': {2019: 6500}}
626
+ doc = Calculator.reform_documentation(params, [second_reform])
627
+ assert isinstance(doc, str)
628
+ dump = False # set to True to print documentation and force test failure
629
+ if dump:
630
+ print(doc)
631
+ assert 1 == 2
632
+
633
+
634
+ def test_distribution_tables(cps_subsample):
635
+ """
636
+ Test distribution_tables method.
637
+ """
638
+ pol = Policy()
639
+ recs = Records.cps_constructor(data=cps_subsample)
640
+ calc1 = Calculator(policy=pol, records=recs)
641
+ assert calc1.current_year == 2014
642
+ calc1.calc_all()
643
+ dt1, dt2 = calc1.distribution_tables(None, 'weighted_deciles')
644
+ assert isinstance(dt1, pd.DataFrame)
645
+ assert dt2 is None
646
+ dt1, dt2 = calc1.distribution_tables(calc1, 'weighted_deciles')
647
+ assert isinstance(dt1, pd.DataFrame)
648
+ assert isinstance(dt2, pd.DataFrame)
649
+ reform = {
650
+ 'UBI_u18': {2014: 1000},
651
+ 'UBI_1820': {2014: 1000},
652
+ 'UBI_21': {2014: 1000}
653
+ }
654
+ pol.implement_reform(reform)
655
+ assert not pol.parameter_errors
656
+ calc2 = Calculator(policy=pol, records=recs)
657
+ calc2.calc_all()
658
+ dt1, dt2 = calc1.distribution_tables(calc2, 'weighted_deciles')
659
+ assert isinstance(dt1, pd.DataFrame)
660
+ assert isinstance(dt2, pd.DataFrame)
661
+
662
+
663
+ def test_difference_table(cps_subsample):
664
+ """
665
+ Test difference_table method.
666
+ """
667
+ cyr = 2014
668
+ pol = Policy()
669
+ recs = Records.cps_constructor(data=cps_subsample)
670
+ calc1 = Calculator(policy=pol, records=recs)
671
+ assert calc1.current_year == cyr
672
+ reform = {'SS_Earnings_c': {cyr: 9e99}}
673
+ pol.implement_reform(reform)
674
+ calc2 = Calculator(policy=pol, records=recs)
675
+ assert calc2.current_year == cyr
676
+ calc1.calc_all()
677
+ calc2.calc_all()
678
+ diff = calc1.difference_table(calc2, 'weighted_deciles', 'iitax')
679
+ assert isinstance(diff, pd.DataFrame)
680
+
681
+
682
+ def test_diagnostic_table(cps_subsample):
683
+ """
684
+ Test diagnostic_table method.
685
+ """
686
+ recs = Records.cps_constructor(data=cps_subsample)
687
+ calc = Calculator(policy=Policy(), records=recs)
688
+ adt = calc.diagnostic_table(3)
689
+ assert isinstance(adt, pd.DataFrame)
690
+
691
+
692
+ def test_mtr_graph(cps_subsample):
693
+ """
694
+ Test mtr_graph method.
695
+ """
696
+ recs = Records.cps_constructor(data=cps_subsample)
697
+ calc = Calculator(policy=Policy(), records=recs)
698
+ fig = calc.mtr_graph(calc,
699
+ mars=2,
700
+ income_measure='wages',
701
+ mtr_measure='ptax',
702
+ pop_quantiles=False)
703
+ assert fig
704
+ fig = calc.mtr_graph(calc,
705
+ income_measure='agi',
706
+ mtr_measure='itax',
707
+ pop_quantiles=True)
708
+ assert fig
709
+
710
+
711
+ def test_atr_graph(cps_subsample):
712
+ """
713
+ Test atr_graph method.
714
+ """
715
+ recs = Records.cps_constructor(data=cps_subsample)
716
+ calc = Calculator(policy=Policy(), records=recs)
717
+ fig = calc.atr_graph(calc, mars=2, atr_measure='itax')
718
+ assert fig
719
+ fig = calc.atr_graph(calc, atr_measure='ptax')
720
+ assert fig
721
+
722
+
723
+ def test_privacy_of_embedded_objects(cps_subsample):
724
+ """
725
+ Test privacy of objects embedded in Calculator object.
726
+ """
727
+ recs = Records.cps_constructor(data=cps_subsample)
728
+ calc = Calculator(policy=Policy(), records=recs)
729
+ var1 = var2 = var3 = 0
730
+ # pylint: disable=protected-access
731
+ with pytest.raises(AttributeError):
732
+ var1 = calc.__policy.current_year
733
+ with pytest.raises(AttributeError):
734
+ var2 = calc.__records.s006
735
+ with pytest.raises(AttributeError):
736
+ var3 = calc.__consumption.current_year
737
+ assert var1 == var2 == var3
738
+
739
+
740
+ def test_n65(cps_subsample):
741
+ """
742
+ Test n65 method.
743
+ """
744
+ recs = Records.cps_constructor(data=cps_subsample)
745
+ calc = Calculator(policy=Policy(), records=recs)
746
+ assert calc.n65().sum() > 600
747
+
748
+
749
+ def test_ce_aftertax_income(cps_subsample):
750
+ """
751
+ Test ce_aftertax_income method.
752
+ """
753
+ rec = Records.cps_constructor(data=cps_subsample)
754
+ pol = Policy()
755
+ calc1 = Calculator(policy=pol, records=rec)
756
+ pol.implement_reform({'SS_Earnings_c': {2013: 9e99}})
757
+ calc2 = Calculator(policy=pol, records=rec)
758
+ res = calc1.ce_aftertax_income(calc2)
759
+ assert isinstance(res, dict)
760
+
761
+
762
+ @pytest.mark.itmded_vars
763
+ @pytest.mark.pre_release
764
+ @pytest.mark.requires_pufcsv
765
+ @pytest.mark.parametrize('year, cvname, hcname',
766
+ [(2018, 'c17000', 'ID_Medical_hc'),
767
+ (2018, 'c18300', 'ID_AllTaxes_hc'),
768
+ (2018, 'c19200', 'ID_InterestPaid_hc'),
769
+ (2018, 'c19700', 'ID_Charity_hc'),
770
+ (2018, 'c20500', 'ID_Casualty_hc'),
771
+ (2018, 'c20800', 'ID_Miscellaneous_hc'),
772
+ (2017, 'c17000', 'ID_Medical_hc'),
773
+ (2017, 'c18300', 'ID_AllTaxes_hc'),
774
+ (2017, 'c19200', 'ID_InterestPaid_hc'),
775
+ (2017, 'c19700', 'ID_Charity_hc'),
776
+ (2017, 'c20500', 'ID_Casualty_hc'),
777
+ (2017, 'c20800', 'ID_Miscellaneous_hc')])
778
+ def test_itemded_component_amounts(year, cvname, hcname, puf_fullsample):
779
+ """
780
+ Check that all c04470 components are adjusted to reflect the filing
781
+ unit's standard-vs-itemized-deduction decision. Check for 2018
782
+ (when current law has no Pease phaseout of itemized deductions and
783
+ already has complete haircuts for Casualty and Miscellaneous deductions)
784
+ and 2017 (when current law has a Pease phaseout of itemized deductions
785
+ and has no haircuts). The calcfunctions.py code makes no attempt to
786
+ adjust the components for the effects of Pease-like phaseout or any other
787
+ type of limitation on total itemized deductions, so the pre-2018 tests
788
+ here use c21060, instead of c04470, as the itemized deductions total.
789
+ """
790
+ # pylint: disable=too-many-locals
791
+ recs = Records(data=puf_fullsample)
792
+ # policy1 such that everybody itemizes deductions and all are allowed
793
+ policy1 = Policy()
794
+ reform1 = {
795
+ 'STD_Aged': {year: [0.0, 0.0, 0.0, 0.0, 0.0]},
796
+ 'STD': {year: [0.0, 0.0, 0.0, 0.0, 0.0]}
797
+ }
798
+ policy1.implement_reform(reform1)
799
+ assert not policy1.parameter_errors
800
+ # policy2 such that everybody itemizes deductions but one is disallowed
801
+ policy2 = Policy()
802
+ reform2 = {
803
+ 'STD_Aged': {year: [0.0, 0.0, 0.0, 0.0, 0.0]},
804
+ 'STD': {year: [0.0, 0.0, 0.0, 0.0, 0.0]},
805
+ hcname: {year: 1.0}
806
+ }
807
+ policy2.implement_reform(reform2)
808
+ assert not policy2.parameter_errors
809
+ # compute tax liability in specified year
810
+ calc1 = Calculator(policy=policy1, records=recs, verbose=False)
811
+ calc1.advance_to_year(year)
812
+ calc1.calc_all()
813
+ calc2 = Calculator(policy=policy2, records=recs, verbose=False)
814
+ calc2.advance_to_year(year)
815
+ calc2.calc_all()
816
+ # confirm that nobody is taking the standard deduction
817
+ assert np.allclose(calc1.array('standard'), 0.)
818
+ assert np.allclose(calc2.array('standard'), 0.)
819
+ # calculate different in total itemized deductions
820
+ if year == 2017:
821
+ # pre-Pease limitation total itemized deductions
822
+ itmded1 = calc1.weighted_total('c21060') * 1e-9
823
+ itmded2 = calc2.weighted_total('c21060') * 1e-9
824
+ elif year == 2018:
825
+ # total itemized deductions (no Pease-like limitation)
826
+ itmded1 = calc1.weighted_total('c04470') * 1e-9
827
+ itmded2 = calc2.weighted_total('c04470') * 1e-9
828
+ else:
829
+ raise ValueError('illegal year value = {}'.format(year))
830
+ difference_in_total_itmded = itmded1 - itmded2
831
+ # calculate itemized component amount
832
+ component_amt = calc1.weighted_total(cvname) * 1e-9
833
+ # confirm that component amount is equal to difference in total deductions
834
+ if year == 2017 and cvname == 'c19700':
835
+ atol = 0.016
836
+ elif year == 2017 and cvname == 'c19200':
837
+ atol = 0.010
838
+ elif year == 2017 and cvname == 'c18300':
839
+ atol = 0.009
840
+ else:
841
+ atol = 0.00001
842
+ if not np.allclose(component_amt, difference_in_total_itmded, atol=atol):
843
+ txt = '\n{}={:.3f} != {:.3f}=difference_in_total_itemized_deductions'
844
+ msg = txt.format(cvname, component_amt, difference_in_total_itmded)
845
+ raise ValueError(msg)
846
+
847
+
848
+ def test_qbid_calculation():
849
+ """
850
+ Test Calculator's QBID calculations using the six example filing units
851
+ specified in Table 1 of this TPC publication: "Navigating the New
852
+ Pass-Through Provisions: A Technical Explanation" by William G. Gale
853
+ and Aaron Krupkin (January 31, 2018), which is available at this URL:
854
+ https://www.taxpolicycenter.org/publications/
855
+ navigating-new-pass-through-provisions-technical-explanation/full
856
+ """
857
+ # In constructing the TPC example filing units, assume that the taxpayer
858
+ # has business income in the form of e26270/e02000 income and no earnings,
859
+ # and that the spouse has no business income and only earnings.
860
+ TPC_YEAR = 2018
861
+ TPC_VARS = (
862
+ 'RECID,MARS,e00200s,e00200,e26270,e02000,PT_SSTB_income,'
863
+ 'PT_binc_w2_wages,PT_ubia_property,pre_qbid_taxinc,qbid\n'
864
+ )
865
+ TPC_FUNITS = (
866
+ '1,2, 99000, 99000,75000,75000,1,20000,90000,150000,15000.00\n'
867
+ '2,2,349000,349000,75000,75000,1,20000,90000,400000, 1612.50\n'
868
+ '3,2,524000,524000,75000,75000,1,20000,90000,575000, 0.00\n'
869
+ '4,2, 99000, 99000,75000,75000,0,20000,90000,150000,15000.00\n'
870
+ '5,2,349000,349000,75000,75000,0,20000,90000,400000,10750.00\n'
871
+ '6,2,524000,524000,75000,75000,0,20000,90000,575000,10000.00\n'
872
+ )
873
+ # generate actual Calculator pre-qbid taxinc and qbid amounts
874
+ tpc_df = pd.read_csv(StringIO(TPC_VARS + TPC_FUNITS))
875
+ recs = Records(data=tpc_df, start_year=TPC_YEAR,
876
+ gfactors=None, weights=None)
877
+ calc = Calculator(policy=Policy(), records=recs)
878
+ assert calc.current_year == TPC_YEAR
879
+ calc.calc_all()
880
+ varlist = ['RECID', 'c00100', 'standard', 'c04470', 'qbided']
881
+ tc_df = calc.dataframe(varlist)
882
+ # compare actual amounts with expected amounts from TPC publication
883
+ act_taxinc = tc_df.c00100 - np.maximum(tc_df.standard, tc_df.c04470)
884
+ exp_taxinc = tpc_df.pre_qbid_taxinc
885
+ assert np.allclose(act_taxinc, exp_taxinc)
886
+ assert np.allclose(tc_df.qbided, tpc_df.qbid)
887
+
888
+
889
+ def test_qbid_limit_switch():
890
+ """
891
+ Test Calculator's switch to implement wage/capital limitations
892
+ on QBI deduction.
893
+ """
894
+ cy = 2019
895
+ ref = {"PT_qbid_limit_switch": {2019: False}}
896
+
897
+ # filing unit has $500,000 in wages and $100,000 in QBI. Since
898
+ # the household is above the taxable income limitation threshold,
899
+ # with full wage/capital limitations, it does not receive a QBI
900
+ # deduction. With sufficent wage/capital to avoid the limitation,
901
+ # the filing unit receives a deduction of:
902
+ # $100,000 * 20% = $20,000.
903
+ VARS = 'RECID,MARS,e00200s,e00200p,e00200,e26270,e02000\n'
904
+ FUNIT = '1,2,250000,250000,500000,100000,100000'
905
+
906
+ funit_df = pd.read_csv(StringIO(VARS + FUNIT))
907
+ recs = Records(data=funit_df, start_year=cy,
908
+ gfactors=None, weights=None)
909
+
910
+ calc_base = Calculator(policy=Policy(), records=recs)
911
+ calc_base.calc_all()
912
+
913
+ qbid_base = calc_base.array('qbided')
914
+ assert np.equal(qbid_base, 0)
915
+
916
+ pol_ref = Policy()
917
+ pol_ref.implement_reform(ref)
918
+ calc_ref = Calculator(policy=pol_ref, records=recs)
919
+ calc_ref.calc_all()
920
+
921
+ qbid_ref = calc_ref.array('qbided')
922
+ assert np.equal(qbid_ref, 20000)
923
+
924
+
925
+ def test_calc_all_benefits_amounts(cps_subsample):
926
+ '''
927
+ Testing how benefits are handled in the calc_all method
928
+ '''
929
+ # set a reform with a positive UBI amount
930
+ ubi_ref = {'UBI_21': {2020: 1000}}
931
+
932
+ # create baseline calculator
933
+ pol = Policy()
934
+ recs = Records.cps_constructor(data=cps_subsample)
935
+ calc_base = Calculator(pol, recs)
936
+ calc_base.advance_to_year(2020)
937
+ calc_base.calc_all()
938
+
939
+ # create reform calculator
940
+ pol_ubi = Policy()
941
+ pol_ubi.implement_reform(ubi_ref)
942
+ calc_ubi = Calculator(pol_ubi, recs)
943
+ calc_ubi.advance_to_year(2020)
944
+ calc_ubi.calc_all()
945
+
946
+ # check that differences in benefits totals are equal to diffs in
947
+ # UBI
948
+ ubi_diff = (calc_ubi.weighted_total('ubi') -
949
+ calc_base.weighted_total('ubi')) / 1e9
950
+ benefit_cost_diff = (
951
+ calc_ubi.weighted_total('benefit_cost_total') -
952
+ calc_base.weighted_total('benefit_cost_total')) / 1e9
953
+ benefit_value_diff = (
954
+ calc_ubi.weighted_total('benefit_cost_total') -
955
+ calc_base.weighted_total('benefit_cost_total')) / 1e9
956
+
957
+ assert np.allclose(ubi_diff, benefit_cost_diff)
958
+ assert np.allclose(ubi_diff, benefit_value_diff)
959
+
960
+
961
+ def test_cg_top_rate():
962
+ """
963
+ Test top CG bracket and rate.
964
+ """
965
+ cy = 2019
966
+
967
+ # set NIIT and STD to zero to isolate CG tax rates
968
+ base = {"NIIT_rt": {2019: 0},
969
+ "STD": {2019: [0, 0, 0, 0, 0]}}
970
+
971
+ # create additional top CG bracket and rate
972
+ ref = {"CG_brk3": {2019: [1000000, 1000000, 1000000, 1000000, 1000000]},
973
+ "CG_rt4": {2019: 0.4},
974
+ "NIIT_rt": {2019: 0},
975
+ "STD": {2019: [0, 0, 0, 0, 0]}}
976
+
977
+ # create one record just below the top CG bracket and one just above
978
+ VARS = 'RECID,MARS,p23250\n'
979
+ FUNITS = '1,2,999999\n2,2,1000001\n'
980
+
981
+ pol_base = Policy()
982
+ pol_base.implement_reform(base)
983
+
984
+ pol_ref = Policy()
985
+ pol_ref.implement_reform(ref)
986
+
987
+ funit_df = pd.read_csv(StringIO(VARS + FUNITS))
988
+ recs = Records(data=funit_df, start_year=cy,
989
+ gfactors=None, weights=None)
990
+
991
+ calc_base = Calculator(policy=pol_base, records=recs)
992
+ calc_base.calc_all()
993
+
994
+ calc_ref = Calculator(policy=pol_ref, records=recs)
995
+ calc_ref.calc_all()
996
+
997
+ # calculate MTRs wrt long term gains
998
+ mtr_base = calc_base.mtr(variable_str='p23250',
999
+ calc_all_already_called=True,
1000
+ wrt_full_compensation=False)
1001
+ mtr_itax_base = mtr_base[1]
1002
+
1003
+ cg_rt3 = pol_base.to_array('CG_rt3', year=2019)
1004
+ # check that MTR for both records is equal to CG_rt3
1005
+ assert np.allclose(mtr_itax_base, cg_rt3)
1006
+
1007
+ # calculate MTRs under reform
1008
+ mtr_ref = calc_ref.mtr(variable_str='p23250',
1009
+ calc_all_already_called=True,
1010
+ wrt_full_compensation=False)
1011
+ mtr_itax_ref = mtr_ref[1]
1012
+
1013
+ cg_rt3_ref = pol_ref.to_array('CG_rt3', year=2019)
1014
+ cg_rt4_ref = pol_ref.to_array(param='CG_rt4', year=2019)
1015
+
1016
+ # check that MTR of houshold below top threshold is equal to
1017
+ # CG_rt3
1018
+ assert np.allclose(mtr_itax_ref[0], cg_rt3_ref)
1019
+ # check that MTR of household above top threshold is equal to
1020
+ # CG_rt4
1021
+ assert np.allclose(mtr_itax_ref[1], cg_rt4_ref)