Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions tests/test_imputed_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def actual_results(rdf, bdf):
"OTM": { # new OBBBA overtime income deduction
"reform_dict": {"OvertimeIncomeDed_c": {simyear: [0, 0, 0, 0, 0]}},
"exp_totben": 23.95,
"exp_affpct": 8.9,
"exp_affpct": 8.90,
"exp_affben": 1401,
# The OTM imputation calibration parameters used in the
# create_taxcalc_imputed_variables.py module were
Expand Down Expand Up @@ -89,9 +89,9 @@ def actual_results(rdf, bdf):
"exp_totben": 54.86,
"exp_affpct": 28.06,
"exp_affben": 1018,
# The affpct statistic of 28.04% and the affben statistic
# of $1020 are reasonably close to the Tax Policy Center
# estimates of 29.6% and $1081, respectively, as reported at
# The affpct statistic and the affben statistic are
# reasonably close to the Tax Policy Center estimates
# of 29.6% and $1081, respectively, as reported at
# https://taxpolicycenter.org/model-estimates/T25-0257
# Note that the $1081 TPC estimate is derived by dividing
# the all-unit average of $320 by the 0.296 affpct.
Expand All @@ -115,6 +115,11 @@ def actual_results(rdf, bdf):
bdf = baseline_sim.dataframe(output_variables)
# estimate effects of each new OBBBA deduction
diffs = [] # list of act-vs-exp differences
abs_tolerance = {
"totben": 0.01, # to handle round(x, 2) logic
"affpct": 0.01, # to handle round(x, 2) logic
"affben": 1.00, # to handle round(x, 0) logic
}
for ded, info in deductions.items():
# create reform Calculator object for simyear
reform_policy = tc.Policy()
Expand All @@ -130,8 +135,9 @@ def actual_results(rdf, bdf):
for stat in ["totben", "affpct", "affben"]:
act = act_res[stat]
exp = info[f"exp_{stat}"]
if not np.allclose([act], [exp]):
diff = f"DIFF:{ded},{stat},act,exp= {act} {exp}"
a_tol = abs_tolerance[stat]
if not np.allclose([act], [exp], atol=a_tol):
diff = f"DIFF:{ded},{stat},act,exp,atol= {act} {exp} {a_tol}"
diffs.append(diff)
# delete reform Policy and Calculator objects
del reform_policy
Expand Down
36 changes: 18 additions & 18 deletions tests/test_tax_expenditures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
against expected tax expenditure values in the tests folder.
"""

import difflib
import pytest
import numpy as np
import pandas as pd
from tmd.storage import STORAGE_FOLDER
from tmd.create_taxcalc_input_variables import TAXYEAR
from tmd.utils.taxcalc_utils import get_tax_expenditure_results
Expand All @@ -27,26 +25,28 @@ def test_tax_exp_diffs(
tmd_growfactors_path,
)
act_path = STORAGE_FOLDER / "output" / "tax_expenditures"
exp_path = tests_folder / "expected_tax_expenditures"
actdf = pd.read_csv(act_path, sep=" ", header=None)
expdf = pd.read_csv(exp_path, sep=" ", header=None)
assert actdf.shape == expdf.shape, "actdf and expdf are not the same shape"
# compare actdf and expdf rows
same = True
actval = actdf.iloc[:, 3].to_numpy(dtype=np.float64)
expval = expdf.iloc[:, 3].to_numpy(dtype=np.float64)
if not np.allclose(actval, expval):
same = False
if same:
return
# if same is False
with open(act_path, "r", encoding="utf-8") as actfile:
act = actfile.readlines()
exp_path = tests_folder / "expected_tax_expenditures"
with open(exp_path, "r", encoding="utf-8") as expfile:
exp = expfile.readlines()
diffs = list(
difflib.context_diff(act, exp, fromfile="actual", tofile="expect", n=0)
)
assert len(act) == len(exp), "number of act and exp rows differ"
a_tol = 0.1 # handles :.1f rounding of tax expenditures
r_tol = 5e-5 # larger than the np.allclose default value of 1e-5
diffs = []
for rowidx, act_row in enumerate(act):
atok = act_row.split()
etok = exp[rowidx].split()
for tokidx in range(3):
assert atok[tokidx] == etok[tokidx], "act vs exp tokens differ"
act_val = float(atok[3])
exp_val = float(etok[3])
if not np.allclose([act_val], [exp_val], atol=a_tol, rtol=r_tol):
msg = (
f"{atok[2]},act,exp,atol,rtol= "
f"{act_val} {exp_val} {a_tol} {r_tol}\n"
)
diffs.append(msg)
if len(diffs) > 0:
emsg = "\nACT-vs-EXP TAX EXPENDITURE DIFFERENCES:\n" + "".join(diffs)
raise ValueError(emsg)
5 changes: 3 additions & 2 deletions tests/test_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ def test_weights(tmd_variables):
wght = tmd_variables["s006"].to_numpy()
actual = {"mean": wght.mean(), "sdev": wght.std()}
expect = {"mean": 815.5521277934885, "sdev": 961.7270821801824}
r_tol = 7e-5 # larger than the np.allclose default value of 1e-5
diffs = []
for stat in ["mean", "sdev"]:
act = actual[stat]
exp = expect[stat]
if not np.allclose([act], [exp]):
diff = f"WEIGHT_DIFF:{stat},act,exp= {act} {exp}"
if not np.allclose([act], [exp], rtol=r_tol):
diff = f"WEIGHT_DIFF:{stat},act,exp,rtol= {act} {exp} {r_tol}"
diffs.append(diff)
if diffs:
emsg = "\nWEIGHT VARIABLE ACT-vs-EXP DIFFS:"
Expand Down