Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests #259

Merged
merged 3 commits into from
Oct 26, 2024
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
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ markers =
taxexp
qbid
taxexpdiffs
itax
33 changes: 33 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Miscellaneous tests of tmd.csv variable weighted totals.
"""

import pytest
import taxcalc as tc
from tmd.storage import STORAGE_FOLDER


def test_no_negative_weights(tmd_variables):
assert tmd_variables.s006.min() >= 0, "Negative weights found"
Expand All @@ -22,3 +26,32 @@ def test_population(tmd_variables):
assert (
abs(population / 1e6 / 334.18 - 1) < 0.01
), "Population not within 1% of 334.18 million"


@pytest.mark.itax
def test_income_tax():

def compare(name, act, exp, tol):
assert (
abs(act / exp - 1) < tol
), f"{name}:act,exp,tol= {act} {exp} {tol}"

# use national tmd files to compute various 2021 income tax statistics
rec = tc.Records.tmd_constructor(
data_path=(STORAGE_FOLDER / "output" / "tmd.csv.gz"),
weights_path=(STORAGE_FOLDER / "output" / "tmd_weights.csv.gz"),
growfactors_path=(STORAGE_FOLDER / "output" / "tmd_growfactors.csv"),
exact_calculations=True,
)
sim = tc.Calculator(policy=tc.Policy(), records=rec)
sim.advance_to_year(2021)
sim.calc_all()
wght = sim.array("s006")
agi = sim.array("c00100")
itax = sim.array("iitax")
# check various income tax statistics
compare("wght_sum", wght.sum(), 184e6, 0.01)
hiagi = agi >= 1e6
compare("wght_sum_hiagi", (wght * hiagi).sum(), 0.875e6, 0.01)
compare("wght_itax_sum", (wght * itax).sum(), 1591e9, 0.01)
compare("wght_itax_sum_hiagi", ((wght * itax) * hiagi).sum(), 902e9, 0.01)
14 changes: 9 additions & 5 deletions tests/test_tax_revenue.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import taxcalc as tc


FIRST_CYR = 2023
FIRST_CYR = 2021
LAST_CYR = 2033

RELTOL_ITAX = 0.10
RELTOL_PTAX = 0.02
DEFAULT_RELTOL_ITAX = 0.10
RELTOL_ITAX = {}
DEFAULT_RELTOL_PTAX = 0.02
RELTOL_PTAX = {
2021: 0.05,
}


DUMP = False # True implies test always fails with complete output
Expand Down Expand Up @@ -60,15 +64,15 @@ def test_tax_revenue(
emsg = ""
for year in range(FIRST_CYR, LAST_CYR + 1):
reldiff = act_itax[year] / exp_itax[year] - 1
same = abs(reldiff) < RELTOL_ITAX
same = abs(reldiff) < RELTOL_ITAX.get(year, DEFAULT_RELTOL_ITAX)
if not same or DUMP:
msg = (
f"\nITAX:cyr,act,exp,rdiff= {year} "
f"{act_itax[year]:9.3f} {exp_itax[year]:9.3f} {reldiff:7.4f}"
)
emsg += msg
reldiff = act_ptax[year] / exp_ptax[year] - 1
same = abs(reldiff) < RELTOL_PTAX
same = abs(reldiff) < RELTOL_PTAX.get(year, DEFAULT_RELTOL_PTAX)
if not same or DUMP:
msg = (
f"\nPTAX:cyr,act,exp,rdiff= {year} "
Expand Down
Loading