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/753.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preserve population-mode Medicaid, county and SPM behavior and live identifier inputs when US simulations are built from entity tables, including their baseline arms.
20 changes: 20 additions & 0 deletions src/policyengine/tax_benefit_models/us/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ def _build_simulation_from_dataset(self, microsim, dataset, system):
validate_policyengine_inputs(dataset.data.entity_data)
_validate_entity_ids(dataset.data.entity_data)

# These validated entity tables are a population even though the
# country constructor received situation={} rather than a Core Dataset.
# Set context before inputs so dataset-only SPM protections also apply.
microsim.is_over_dataset = True

builder = SimulationBuilder()
builder.populations = system.instantiate_entities()

Expand Down Expand Up @@ -522,6 +527,21 @@ def _build_simulation_from_dataset(self, microsim, dataset, system):
df = pd.DataFrame(entity_df).set_index(f"{entity_name}_id", drop=False)
df = df.loc[microsim.populations[entity_name].ids]
stored_tables[entity_name] = df
# Declaring population IDs does not populate the live ID variables:
# person_id would otherwise calculate a new regional row number.
# Keep the native inputs for ID-keyed formulas as well as structure.
live_ids = {f"{entity_name}_id": f"{entity_name}_id"}
if entity_name == "person":
live_ids.update(
person_household_id=household_id_col,
person_marital_unit_id=marital_unit_id_col,
person_family_id=family_id_col,
person_spm_unit_id=spm_unit_id_col,
person_tax_unit_id=tax_unit_id_col,
)
for live_name, column in live_ids.items():
if live_name in system.variables:
microsim.set_input(live_name, dataset.year, df[column].values)
for column in df.columns:
if column not in id_columns and column in system.variables:
microsim.set_input(column, dataset.year, df[column].values)
Expand Down
62 changes: 60 additions & 2 deletions tests/test_us_native_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,36 @@
from policyengine.tax_benefit_models.us.spm import resolve_spm_selection


def _assert_unemployment_allocation_uses_source_keys(country, person_ids):
"""Exercise the ID-keyed monthly formula on country versions providing it."""
module_name = (
"policyengine_us.variables.gov.usda.snap.eligibility.work_requirements."
"is_receiving_unemployment_compensation"
)
try:
module = importlib.import_module(module_name)
except ModuleNotFoundError as exc:
if module_name == exc.name or module_name.startswith(exc.name + "."):
return # The bundled US2.2.1 predates this monthly allocation.
raise
start = module.unemployment_compensation_start_month_index(person_ids)
positional = module.unemployment_compensation_start_month_index(
np.arange(len(person_ids))
)
assert (start != positional).any(), "Fixture must distinguish source and row IDs"
country.set_input(
"unemployment_compensation_months", 2026, np.ones(len(person_ids))
)
for month in range(1, 13):
expected = (month - 1 - start) % 12 == 0
np.testing.assert_array_equal(
country.calculate(
"is_receiving_unemployment_compensation", f"2026-{month:02d}"
).values,
expected,
)


@pytest.fixture
def independent_entity_frames():
# Person order, group IDs, table orders, and indexes differ independently.
Expand Down Expand Up @@ -72,8 +102,11 @@ def independent_entity_frames():

@pytest.mark.parametrize("format", ["fixed", "table"])
@pytest.mark.parametrize("link_style", ["native", "plain"])
@pytest.mark.parametrize(
"policy", [None, {"gov.irs.credits.ctc.amount.base[0].amount": 3_000}]
)
def test_native_inputs_and_calculated_outputs_align_by_id(
tmp_path, independent_entity_frames, format, link_style, monkeypatch
tmp_path, independent_entity_frames, format, link_style, policy, monkeypatch
):
frames = independent_entity_frames
if link_style == "plain":
Expand Down Expand Up @@ -124,10 +157,35 @@ def capture(self, country_simulation, dataset, system):
return applied

monkeypatch.setattr(PolicyEngineUSLatest, "_build_simulation_from_dataset", capture)
simulation = pe.Simulation(dataset=dataset, tax_benefit_model_version=pe.us.model)
simulation = pe.Simulation(
dataset=dataset, tax_benefit_model_version=pe.us.model, policy=policy
)
simulation.run()
output = simulation.output_dataset.data.entity_data
country = country_simulations[0]
assert len(country_simulations) == (2 if policy else 1)

# Query the live country variables, not only IDs copied into the output.
# Population structure alone leaves these accessors at row IDs or zero.
for native in country_simulations:
for entity, population in native.populations.items():
np.testing.assert_array_equal(
native.calculate(f"{entity}_id", 2026).values, population.ids
)
for entity in frames:
if entity == "person":
continue
native_link = f"person_{entity}_id"
source_link = (
native_link if native_link in inputs["person"] else f"{entity}_id"
)
np.testing.assert_array_equal(
native.calculate(native_link, 2026).values,
inputs["person"][source_link].to_numpy(),
)
_assert_unemployment_allocation_uses_source_keys(
native, inputs["person"]["person_id"].to_numpy()
)

# Assert numeric geography at both boundaries, including the two CA people.
assert output["person"]["county_fips"].tolist() == [
Expand Down
132 changes: 132 additions & 0 deletions tests/test_us_population_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
"""Entity-table population runs retain native dataset-mode behavior."""

import numpy as np
import pandas as pd
import pytest
from microdf import MicroDataFrame

pytest.importorskip("policyengine_us")
pytest.importorskip("spm_calculator.policyengine_adapter")

import policyengine as pe
from policyengine.core.simulation import Simulation
from policyengine.tax_benefit_models.us.datasets import (
PolicyEngineUSDataset,
USYearData,
)
from policyengine.tax_benefit_models.us.model import PolicyEngineUSLatest


@pytest.fixture
def population_tables():
"""Two CA households with unequal native weights and observed counties."""
tables = {}
for entity in (
"person",
"household",
"tax_unit",
"spm_unit",
"family",
"marital_unit",
):
frame = pd.DataFrame({f"{entity}_id": [1, 2], f"{entity}_weight": [2.0, 3.0]})
if entity == "person":
for group in (
"household",
"tax_unit",
"spm_unit",
"family",
"marital_unit",
):
frame[f"{group}_id"] = [1, 2]
frame["age"] = [35, 45]
frame["medicaid_enrolled"] = [True, True]
frame["medicaid_slcsp_cost_index"] = [100.0, 200.0]
elif entity == "household":
frame["state_code"] = ["CA", "CA"]
frame["county_fips"] = ["06037", "06037"]
frame["county"] = ["ALAMEDA_COUNTY_CA", "ALAMEDA_COUNTY_CA"]
tables[entity] = MicroDataFrame(frame, weights=f"{entity}_weight")
return PolicyEngineUSDataset(
name="synthetic-population-context",
description="Uncertified two-household population-context fixture",
year=2026,
data=USYearData(**tables),
)


@pytest.mark.parametrize(
"reform", [None, {"gov.irs.credits.ctc.amount.base[0].amount": 3_000}]
)
def test_population_context_precedes_inputs_in_both_arms(
population_tables, monkeypatch, reform
):
from policyengine_us.spm import SPMSimulationMixin

building = set()
countries = []
writes = []
build = PolicyEngineUSLatest._build_simulation_from_dataset
set_input = SPMSimulationMixin.set_input

def capture_build(self, country, dataset, system):
building.add(id(country))
try:
return build(self, country, dataset, system)
finally:
building.remove(id(country))
countries.append(country)

def capture_input(self, *args, **kwargs):
if id(self) in building:
assert self.is_over_dataset, "Population mode must precede the first input"
writes.append(id(self))
return set_input(self, *args, **kwargs)

monkeypatch.setattr(
PolicyEngineUSLatest, "_build_simulation_from_dataset", capture_build
)
monkeypatch.setattr(SPMSimulationMixin, "set_input", capture_input)
monkeypatch.setattr(
PolicyEngineUSLatest,
"resolve_entity_variables",
lambda self, sim: {"person": ["medicaid_slcsp_state_denominator"]},
)
simulation = Simulation(
dataset=population_tables, tax_benefit_model_version=pe.us.model, policy=reform
)
simulation.run()
assert len(countries) == (2 if reform else 1)
assert set(writes) == {id(country) for country in countries}

# The actual population formula uses the native weighted enrollee sum,
# rather than the calibrated single-household denominator.
np.testing.assert_array_equal(
simulation.output_dataset.data.person["medicaid_slcsp_state_denominator"],
[800.0, 800.0],
)
for country in countries:
assert country.is_over_dataset
assert country.dataset is None
assert not country.check_macro_cache("employment_income", "2026")
for entity, population in country.populations.items():
np.testing.assert_array_equal(
country.calculate(f"{entity}_id", 2026).values, population.ids
)
for entity in ("household", "tax_unit", "spm_unit", "family", "marital_unit"):
np.testing.assert_array_equal(
country.calculate(f"person_{entity}_id", 2026).values, [1, 2]
)
np.testing.assert_array_equal(
country.calculate("medicaid_slcsp_state_denominator", 2026).values,
[800.0, 800.0],
)
# Dataset counties carry forward from stored periods instead of
# being recomputed from the deliberately different FIPS input.
assert country.calculate("county", 2027).tolist() == [
"ALAMEDA_COUNTY_CA",
"ALAMEDA_COUNTY_CA",
]
# The real SPM mixin must protect its formulas in both arms.
with pytest.raises(ValueError, match="formula-owned SPM output"):
country.set_input("spm_unit_spm_threshold", 2026, [1.0, 1.0])
Loading