Skip to content

Commit

Permalink
Merge pull request #259 from PSLmodels/more-tests
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
martinholmer authored Oct 26, 2024
2 parents 4560a35 + b528c05 commit 9d9d53d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
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

0 comments on commit 9d9d53d

Please sign in to comment.