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
1 change: 1 addition & 0 deletions changelog.d/spouse-ignores-dependent-separation.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Limited the inferred spouse's separation check to the head and selected spouse candidate.
13 changes: 5 additions & 8 deletions docs/usage/microsimulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,14 @@ partner in their marital unit. A head without one takes the next-oldest such
adult, except an adult who meets the qualifying child age test against the head
(26 USC 152(c)(3)): under 19, or under 24 if a full-time student, and younger
than the head, or permanently and totally disabled at any age. That adult stays
a dependent. No spouse is inferred when any member of the unit is separated,
and a separated member doesn't count toward a couple. When a situation omits
a dependent. PolicyEngine infers no spouse when the head or selected spouse
candidate is separated. It selects the candidate before checking separation,
without promoting a younger adult; member order breaks equal-age ties. A
separated member doesn't count toward a couple. When a situation omits
marital units, Core puts everyone in one marital unit, so plain age order
applies. A minor living without a parent is left with no head.

This rule has four known limitations. Supply `is_tax_unit_head`,
This rule has three known limitations. Supply `is_tax_unit_head`,
`is_tax_unit_spouse` or `tax_unit_role_input` for these households:

- When a tax unit holds a married couple and an older adult outside their
Expand All @@ -183,11 +185,6 @@ This rule has four known limitations. Supply `is_tax_unit_head`,
becomes the head's dependent, whatever their income. Examples are a spouse
living apart in a care facility, or a self-supporting sibling, whose income
then leaves the return.
- When a separated adult outside a married couple's marital unit shares their
tax unit, the couple heads the unit but no spouse is inferred, since a unit
with a separated member infers none. The other member of the couple becomes
a dependent. Under the law a spouse can't be the other spouse's dependent,
and the couple could file jointly.

The Microcosm build carries its constructor's filing status in a
`filing_status_input` column. No variable has that name, so the column is
Expand Down
20 changes: 11 additions & 9 deletions policyengine_us/tests/core/test_tax_unit_role_input_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
roles equal an independent implementation of age ordering. The oldest adult
heads the unit, earliest member on ties, skipping adults input as tax unit
dependents unless every adult is one; the next such adult is the spouse
unless any member of the unit is separated. The generated situations pass
unless the head or that adult is separated. The generated situations pass
no marital units, so Core puts everyone in one marital unit and the
couple-first head and partner-first spouse rules reduce to age order.
4. Filing status is computed: no supplied status is read, so a unit files
Expand Down Expand Up @@ -120,20 +120,22 @@ def _situation(units, pin_explicitly=False):
}


def _age_rule(ages, separated, dependent_input=None):
# Mirrors the fallback as it stands, with marital units omitted (see
# invariant 3). Adults input as tax unit dependents are skipped unless
# every adult is one (#9630). Any separated member blocks every spouse;
# that unit-wide gate is a known defect (#9620), so update this reference
# when it is fixed.
def _age_rule(ages, separated, dependent_input=None, head_input=None):
"""Independent age order for situations without marital-unit inputs."""
adults = [i for i, age in enumerate(ages) if age >= 18]
if dependent_input is not None:
non_dependents = [i for i in adults if not dependent_input[i]]
if non_dependents:
adults = non_dependents
head = min(adults, key=lambda i: (-ages[i], i)) if adults else None
candidates = [] if any(separated) else [i for i in adults if i != head]
head = head_input
if head is None:
head = min(adults, key=lambda i: (-ages[i], i)) if adults else None
candidates = [i for i in adults if i != head]
spouse = min(candidates, key=lambda i: (-ages[i], i)) if candidates else None
if spouse is not None and (
(head is not None and separated[head]) or separated[spouse]
):
spouse = None
return head, spouse


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
"""Invariants for the inferred tax unit spouse when members are separated.

Separation under 26 U.S.C. 7703 concerns the filer's own marriage, so only
the head's or the would-be spouse's `is_separated` can leave a tax unit
without a spouse. The would-be spouse is the oldest head-or-spouse candidate
other than the head; candidates are adults not input as dependents (every
adult when all are). These situations omit marital units, so head and
spouse selection follows age order. Core breaks equal-age ties by member order.

Hypothesis draws batches of tax units of one to five people, with tied ages,
children, separated members and, in turn, no role inputs, input dependent
flags, or an input head. A seeded population of 300 units adds breadth. Each
batch runs as one vectorized simulation that also holds a twin of every unit
in which every member other than the head and the would-be spouse has
`is_separated` flipped. For every tax unit:

1. Structure: at most one spouse, who is neither the head nor a child.
2. Differential: head and spouse equal a pure-Python statement of the rule.
3. Separation of anyone but the head and the would-be spouse never changes
the spouse: each unit and its twin get the same spouse.
"""

import numpy as np
import pytest
from hypothesis import HealthCheck, given, settings
from hypothesis import strategies as st

from policyengine_us import Simulation
from policyengine_us.tests.core.test_tax_unit_role_input_properties import _age_rule

YEAR = 2024
ADULT_AGE = 18
AGES = [6, 17, 18, 25, 30, 38, 40, 75]
MODES = ["no_roles", "dependents", "head"]


@st.composite
def tax_units(draw):
size = draw(st.integers(1, 5))
members = [
{
"age": draw(st.sampled_from(AGES)),
"separated": draw(st.booleans()),
"dependent": draw(st.booleans()),
}
for _ in range(size)
]
return {"members": members, "head": draw(st.integers(0, size - 1))}


def _seeded_units(n=300, seed=20261006):
rng = np.random.default_rng(seed)
units = []
for _ in range(n):
size = int(rng.integers(1, 6))
members = [
{
"age": int(rng.choice(AGES)),
"separated": bool(rng.random() < 0.3),
"dependent": bool(rng.random() < 0.4),
}
for _ in range(size)
]
units.append({"members": members, "head": int(rng.integers(0, size))})
return units


def _reference(unit, mode):
"""Adapt the shared age-order reference to this test's input modes."""
members = unit["members"]
return _age_rule(
[m["age"] for m in members],
[m["separated"] for m in members],
[m["dependent"] for m in members] if mode != "no_roles" else None,
unit["head"] if mode == "head" else None,
)


def _twin(unit, mode):
"""The unit with every member but the head and would-be spouse's
separation flipped."""
members = unit["members"]
head, _ = _reference(unit, mode)
keep = {head}
# The would-be spouse is the spouse the rule finds when nobody is separated.
calm = {**unit, "members": [{**m, "separated": False} for m in members]}
keep.add(_reference(calm, mode)[1])
flipped = [
m if i in keep else {**m, "separated": not m["separated"]}
for i, m in enumerate(members)
]
return {**unit, "members": flipped}


def _situation(units, mode):
people, tax_unit_groups, households = {}, {}, {}
for u, unit in enumerate(units):
names = []
for i, member in enumerate(unit["members"]):
name = f"p{u}_{i}"
person = {
"age": {YEAR: member["age"]},
"is_separated": {YEAR: member["separated"]},
}
if mode in ("dependents", "head"):
person["is_tax_unit_dependent"] = {YEAR: member["dependent"]}
if mode == "head":
person["is_tax_unit_head"] = {YEAR: i == unit["head"]}
people[name] = person
names.append(name)
tax_unit_groups[f"t{u}"] = {"members": names}
households[f"h{u}"] = {"members": names, "state_code": {YEAR: "TX"}}
return {
"people": people,
"tax_units": tax_unit_groups,
"households": households,
}


def _check(units, mode):
batch = units + [_twin(unit, mode) for unit in units]
sim = Simulation(situation=_situation(batch, mode))
head = np.asarray(sim.calculate("is_tax_unit_head", YEAR), dtype=bool)
spouse = np.asarray(sim.calculate("is_tax_unit_spouse", YEAR), dtype=bool)
start = 0
spouses = []
for u, unit in enumerate(batch):
size = len(unit["members"])
unit_head = head[start : start + size]
unit_spouse = spouse[start : start + size]
ages = [m["age"] for m in unit["members"]]
start += size

# 1. Structure.
assert unit_spouse.sum() <= 1, (mode, unit)
assert not (unit_spouse & unit_head).any(), (mode, unit)
assert all(age >= ADULT_AGE for age, s in zip(ages, unit_spouse) if s)

# 2. Differential against the reference rule.
expected_head, expected_spouse = _reference(unit, mode)
assert list(np.flatnonzero(unit_head)) == (
[] if expected_head is None else [expected_head]
), (mode, unit, unit_head)
assert list(np.flatnonzero(unit_spouse)) == (
[] if expected_spouse is None else [expected_spouse]
), (mode, unit, unit_spouse)
spouses.append(list(np.flatnonzero(unit_spouse)))

# 3. Each unit and its twin share a spouse.
assert spouses[: len(units)] == spouses[len(units) :]


# A batch's cost is mostly per-variable overhead, so each example is a large
# batch and there are few examples.
SETTINGS = dict(
max_examples=10,
deadline=None,
derandomize=True,
suppress_health_check=[HealthCheck.too_slow, HealthCheck.data_too_large],
)


@settings(**SETTINGS)
@given(st.sampled_from(MODES), st.lists(tax_units(), min_size=5, max_size=40))
def test_tax_unit_spouse_separation_invariants(mode, units):
_check(units, mode)


@pytest.mark.parametrize("mode", MODES)
def test_seeded_population(mode):
_check(_seeded_units(), mode)
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,43 @@
output:
# Each parent has 5,100 of taxable benefits, not below 4,400.
filing_status: SINGLE

- name: A separated adult dependent does not stop a married couple filing jointly.
period: 2024
input:
people:
head:
age: 40
is_tax_unit_dependent: false
employment_income: 50_000
spouse:
age: 38
is_tax_unit_dependent: false
employment_income: 30_000
dependent:
age: 18
is_tax_unit_dependent: true
is_separated: true
marital_units:
couple_marital_unit:
members: [head, spouse]
dependent_marital_unit:
members: [dependent]
tax_units:
tax_unit:
members: [head, spouse, dependent]
households:
household:
members: [head, spouse, dependent]
state_code: TX
output:
filing_status: JOINT
tax_unit_married: true
# Rev. Proc. 2023-34: 2024 joint standard deduction is 29,200.
standard_deduction: 29_200
# Both spouses' wages, 50,000 + 30,000, less 29,200.
taxable_income: 50_800
# Continuous 2024 joint brackets: 10% of 23,200 plus 12% of (50,800 - 23,200).
income_tax_before_credits: 5_632
# The 18-year-old dependent qualifies for the 500 credit for other dependents.
income_tax: 5_132
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@
members: [parent, separated_adult, partner]
state_code: TX
output:
# A unit with a separated member infers no spouse, so the two-person
# marital unit is not a couple that can file jointly. The oldest adult
# heads the unit.
# The separated adult doesn't form a couple for head selection. The
# oldest adult heads the unit, and the selected spouse candidate's
# separation leaves the unit without a spouse.
is_tax_unit_head: [true, false, false]
is_tax_unit_spouse: [false, false, false]
filing_status: SINGLE
Expand Down Expand Up @@ -249,7 +249,7 @@
output:
# With marital units omitted, Core puts all three adults in one marital
# unit. It isn't a two-person unit, so the two non-separated adults aren't
# a couple, and the oldest adult heads the unit. A unit with a separated
# member infers no spouse.
# a couple, and the oldest adult heads the unit. That head's separation
# leaves the unit without a spouse.
is_tax_unit_head: [true, false, false]
is_tax_unit_spouse: [false, false, false]
Loading
Loading