Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
af63395
Preserve marital inputs and infer missing units from explicit pairs
hua7450 Oct 7, 2026
b7f619d
Merge upstream/main into preserve-marital-unit-inputs
hua7450 Oct 8, 2026
f6da080
Merge branch 'main' of https://github.com/PolicyEngine/policyengine-u…
hua7450 Oct 31, 2026
287ca0b
Pin policyengine-core to the updated situation-hook PR head
hua7450 Oct 31, 2026
7feb27f
Pair married families and keep inferred units through snapshots
hua7450 Oct 31, 2026
ddcfe2d
Pair the tax-role couple when a situation omits marital units
hua7450 Nov 12, 2026
9ab2ec7
Merge remote-tracking branch 'upstream/main' into preserve-marital-un…
hua7450 Nov 12, 2026
389f95c
Describe omitted marital units as built by the role rules
hua7450 Nov 12, 2026
fefae96
Address review of the marital-unit hook
hua7450 Nov 12, 2026
d16dda6
Merge branch 'main' of https://github.com/PolicyEngine/policyengine-u…
hua7450 Nov 12, 2026
e7d1a89
Let the second California couple qualify on their own income
hua7450 Nov 12, 2026
648ef25
Read the defaults Core fills in when anyone supplies an input
hua7450 Nov 13, 2026
4500aee
Merge remote-tracking branch 'upstream/main' into preserve-marital-un…
hua7450 Nov 13, 2026
a805ef8
Merge remote-tracking branch 'upstream/main' into preserve-marital-un…
hua7450 Nov 13, 2026
c967358
Require the Core release with the situation hook
hua7450 Nov 13, 2026
dd5b94b
Pair couples from filing status and fix tests that relied on one mari…
hua7450 Nov 13, 2026
dcb5968
Read a changing filing status as no status, and test each separate-fi…
hua7450 Nov 13, 2026
f177b22
Merge branch 'main' of https://github.com/PolicyEngine/policyengine-u…
hua7450 Nov 13, 2026
532194f
Pin how a changing filing status is read, and document two edge cases
hua7450 Nov 13, 2026
e8d33ae
Merge branch 'main' of https://github.com/PolicyEngine/policyengine-u…
hua7450 Nov 13, 2026
30d0bf1
Merge main and follow its narrower separation rule in step 4
hua7450 Nov 14, 2026
6e27727
Merge branch 'main' of https://github.com/PolicyEngine/policyengine-u…
hua7450 Nov 14, 2026
47d0b59
Apply the age test in the shared age-order reference
hua7450 Nov 14, 2026
ec20e1e
Merge branch 'main' of https://github.com/PolicyEngine/policyengine-u…
hua7450 Nov 14, 2026
5baf621
Merge branch 'main' of https://github.com/PolicyEngine/policyengine-u…
hua7450 Nov 16, 2026
36cf139
Test a separated head, and document a lone spouse input beside a sepa…
hua7450 Nov 16, 2026
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/preserve-marital-unit-inputs.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Build omitted household marital units from explicit couples, supplied tax roles, a married family or joint and separate filing statuses, and otherwise pair the head and spouse the tax-role fallback infers, instead of putting everyone in one marital unit.
108 changes: 106 additions & 2 deletions docs/usage/microsimulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,109 @@

The `Microsimulation` class is the primary tool for population-level policy analysis in PolicyEngine US. It combines representative survey microdata with PolicyEngine's tax-benefit model to estimate how policies affect the entire US population.

## Marital membership and tax roles

Dataset `person_marital_unit_id` and `marital_unit_id` columns define marital
membership. Loading the dataset and extending its years preserve those IDs.
Microcosm builds these units from the survey's spouse pointers, and
policyengine-us never rebuilds a dataset's units.

A household situation's `marital_units` mapping also stands as supplied. An
empty mapping gives each person their own unit. A married couple may share a
marital unit while belonging to separate tax units.

When an entity-form situation omits `marital_units`, the model pairs at most
one couple in each tax unit and household, from the first of these that
applies, and gives everyone else their own unit:

1. an unambiguous pair of true `is_tax_unit_head` and `is_tax_unit_spouse`
inputs for overlapping periods, neither input as a dependent or separated;
2. the head and spouse of a `tax_unit_role_input` supplied for every member;
3. the two adults of a family whose `is_married` input is true, when exactly
two of its adults in the tax unit and household are neither input as
dependents nor separated;
4. otherwise, the head and spouse that `is_tax_unit_head` and
`is_tax_unit_spouse` infer from age order: the oldest adult, and the
next-oldest adult who fails the qualifying child age test against them
(under 19, or under 24 if a full-time student, and younger than the head,
or permanently and totally disabled). No couple is formed when the head or
that spouse is separated (another member's separation doesn't matter), when
the two live in different households, when
a family they share has a false `is_married` input, or when either has a
false `is_tax_unit_head` or `is_tax_unit_spouse` input for the role this
step gives them. A tax unit whose `filing_status` input is `JOINT` skips
the age test, because only a married couple files jointly
([26 USC 6013(a)](https://www.law.cornell.edu/uscode/text/26/6013#a)).

Only married people file separately
([26 USC 1(d)](https://www.law.cornell.edu/uscode/text/26/1#d)), so when a
household holds exactly two tax units whose `filing_status` input is
`SEPARATE`, their two filers are then paired, unless either is separated, a
family they share has a false `is_married` input, or either is already in a
couple from the steps above. A unit's filer is its one true `is_tax_unit_head`
input, or else its only adult who isn't input as a dependent; a `SEPARATE` unit
with no such filer still counts toward the two, so it blocks the pairing. A
marital unit is a married, co-habiting couple, so separate filers in different
households stay apart. A `filing_status` input that changes between periods
counts as no status.

The fourth step reads the same inputs and parameters as the role formulas, so
the units it builds agree with the inferred tax roles in the years the inputs
are dated for: a couple sent without marriage information is a couple for SSI
and Medicaid as well as for tax, and
a parent's adult child who meets the age test is neither. When anyone in a
situation supplies a role, student or age input for a year, Core gives everyone
else that variable's default for the year instead of running its formula, and
the fourth step reads the same defaults. The fourth step checks the years the
inputs are dated for, so a payload dated for a year other
than the simulation's default input year pairs the same couple. Under a reform,
it uses the reform's dependent age limits.

Membership can't change between periods. Inputs that can't settle one static
couple give everyone in that tax unit and household their own unit: two head
or two spouse inputs, a dependent also marked head or spouse in a unit with
both a head and a spouse input, role or `is_married` inputs that change between
periods, or a fourth-step couple that differs between years. With only a spouse
input, on a person also input as a dependent, the fourth step pairs that person
with the oldest other adult, and the role formulas give the same couple.

Known limitations, which callers avoid by supplying `marital_units`:

- Like the role fallback, the fourth step pairs an adult who fails the age test
with the head, such as a parent and their adult child or two unrelated adults
in one tax unit.
- A tax unit with two couples pairs only the couple the role formulas pick;
the other two adults get their own units.
- Spouses in two tax units are paired only as the two separate filers of one
household. A family's true `is_married` input doesn't pair them, and neither
do separate returns when the household holds more than two.
- The separate-filer pairing reads no age or relationship, so a parent and an
adult child who each file separately from an absent spouse, beside a third
tax unit, become a couple.
- A `filing_status` input dated for more than one year counts only when it is
the same in every year, so a status that is `JOINT` in the calculation year
and different in another year reads as no status.
- `JOINT` and `SEPARATE` inputs say who is married, but `SINGLE`,
`HEAD_OF_HOUSEHOLD` and `SURVIVING_SPOUSE` inputs don't say who isn't: two
adults in one tax unit with any of them can still be paired by the fourth
step, as on main.
- A lone true `is_tax_unit_spouse` input on the oldest adult makes that person
both head and spouse in the role formulas, as on main; the fourth step pairs
them with the next adult.
- A lone true `is_tax_unit_spouse` input skips the fourth step's separation
check: the fourth step pairs that person with the oldest other adult even
when that adult is separated.
- Marital units follow the years the inputs are dated for, while the role
formulas run again in any year without a role input. So a calculation for
another year can give roles the units don't match. For example, a
grandchild's undated `is_tax_unit_spouse: false` lands in the default input
year (2024), so the grandparents get their own units, while a 2026
calculation still makes them head and spouse.

These rules apply to Python household simulations and entity-form YAML tests,
including tests with reforms or inline parameters. Variable-only tests keep
Core's own defaults.

## Getting started

```python
Expand Down Expand Up @@ -165,8 +268,9 @@ 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.
marital units, the model builds them by these same rules (see "Marital
membership and tax roles" above), so they give the same roles. A minor living
without a parent is left with no head.

This rule has three known limitations. Supply `is_tax_unit_head`,
`is_tax_unit_spouse` or `tax_unit_role_input` for these households:
Expand Down
4 changes: 4 additions & 0 deletions policyengine_us/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
uprate_parameters,
)
from .tools.default_uprating import add_default_uprating
from .tools.marital_units import infer_missing_marital_units
from .tools.tob import TobCalculationMixin
from .tools.period_branch import delete_input_values
from .tools.social_security_inputs import (
Expand Down Expand Up @@ -114,6 +115,9 @@ class CountryTaxBenefitSystem(TaxBenefitSystem):
]
modelled_policies = COUNTRY_DIR / "programs.yaml"

def preprocess_situation(self, situation, default_period=None):
return infer_missing_marital_units(situation, default_period, self)

def __init__(
self,
reform: tuple | None = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,8 @@ def test_ar_medical_expense_deduction_is_monotone(year):
@pytest.mark.parametrize("statuses", list(product(["SINGLE", "SEPARATE"], repeat=2)))
@pytest.mark.parametrize("year", [2015, 2017])
def test_ar_medical_expense_floor_in_two_person_simulations(year, statuses):
# With exactly two people and no marital units, the default marital unit
# looks like a couple. Only two separate filers are read as spouses of
# each other (intended: married people filing separately must both file
# With no marital units, only two separate filers in one household are
# paired as a couple (married people filing separately must both file
# separately); anyone else keeps the ages on their own return.
period = str(year)
ages = list(product(HEAD_AGES, HEAD_AGES))
Expand All @@ -191,8 +190,7 @@ def test_ar_medical_expense_floor_in_two_person_simulations(year, statuses):
for i, (name, status) in enumerate(zip(people, statuses))
},
"households": {
f"h{i}": {"members": [name], "state_code": {period: "AR"}}
for i, name in enumerate(people)
"h": {"members": list(people), "state_code": {period: "AR"}}
},
}
floor = Simulation(situation=situation).calculate(
Expand Down
26 changes: 20 additions & 6 deletions policyengine_us/tests/core/test_tax_unit_role_inference.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Invariants for inferring the tax unit head and spouse.

``is_tax_unit_head`` picks the oldest adult and ``is_tax_unit_spouse`` the
next oldest, skipping anyone input as a tax unit dependent unless every adult
in the unit is one. These tests enumerate every tax unit of up to three people
drawn from a small grid of ages and dependent inputs, and compare the model
against reference implementations of that rule and of the rule before
dependent inputs were honoured.
next oldest who fails the qualifying child age test against the head (26 USC
152(c)(3)), skipping anyone input as a tax unit dependent unless every adult
in the unit is one. The situations omit marital units, so
``tools/marital_units.py`` builds them by the same rules. These tests
enumerate every tax unit of up to three people drawn from a small grid of ages
and dependent inputs, and compare the model against reference implementations
of that rule and of the rule before dependent inputs were honoured.
"""

import itertools
Expand All @@ -19,6 +21,10 @@
# Covers children, the age-18 adult boundary, and ties between adults.
AGES = (10, 18, 40, 70)
MAX_UNIT_SIZE = 3
# A non-student under 19 and younger than the head meets the qualifying child
# age test (26 USC 152(c)(3)(A)(i)), so an 18-year-old is not the spouse of an
# older head. The grid has no students or disabled people.
QUALIFYING_CHILD_AGE_LIMIT = 19


def _all_units(flag_values):
Expand Down Expand Up @@ -67,7 +73,15 @@ def _reference_roles(members, honour_dependent_input: bool):
candidates = non_dependents
ranked = sorted(candidates, key=lambda i: (-members[i][0], i))
head = ranked[0] if ranked else None
spouse = ranked[1] if len(ranked) > 1 else None
spouses = [
i
for i in ranked[1:]
if not (
members[i][0] < QUALIFYING_CHILD_AGE_LIMIT
and members[i][0] < members[head][0]
)
]
spouse = spouses[0] if spouses else None
return head, spouse


Expand Down
27 changes: 22 additions & 5 deletions policyengine_us/tests/core/test_tax_unit_role_input_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
3. Fallback (marital units omitted): in a unit without supplied roles, the
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 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.
dependents unless every adult is one; the next such adult who fails the
qualifying child age test against the head (26 USC 152(c)(3)) is the
spouse unless the head or that adult is separated. The generated
situations pass no marital units, so the model builds them by these same
rules 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
jointly exactly when it has a spouse, supplied or inferred.
5. Workaround equivalence (differential): supplying roles through
Expand Down Expand Up @@ -120,6 +122,12 @@ def _situation(units, pin_explicitly=False):
}


# A non-student under 19 and younger than the head meets the qualifying child
# age test (26 USC 152(c)(3)(A)(i)), so they are not the head's spouse. The
# generated units have no students or disabled people.
QUALIFYING_CHILD_AGE_LIMIT = 19


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]
Expand All @@ -130,7 +138,16 @@ def _age_rule(ages, separated, dependent_input=None, head_input=None):
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]
candidates = [
i
for i in adults
if i != head
and not (
head is not None
and ages[i] < QUALIFYING_CHILD_AGE_LIMIT
and ages[i] < ages[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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
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.
other than the head who fails the qualifying child age test against them;
candidates are adults not input as dependents (every adult when all are).
These situations omit marital units, so the model builds them by the same
rules and 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
Expand Down
Loading
Loading