From 344253b3e407424cc1bebea6a9c0934123017384 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Thu, 12 Nov 2026 17:01:10 -0500 Subject: [PATCH 1/3] Add history-preserving household scope to US managed runs --- changelog.d/757.added.md | 1 + .../tax_benefit_models/us/managed_scope.py | 116 ++++++++++++++++++ .../tax_benefit_models/us/model.py | 45 ++++++- tests/test_us_managed_scope.py | 108 ++++++++++++++++ 4 files changed, 268 insertions(+), 2 deletions(-) create mode 100644 changelog.d/757.added.md create mode 100644 src/policyengine/tax_benefit_models/us/managed_scope.py create mode 100644 tests/test_us_managed_scope.py diff --git a/changelog.d/757.added.md b/changelog.d/757.added.md new file mode 100644 index 000000000..5f1343d5d --- /dev/null +++ b/changelog.d/757.added.md @@ -0,0 +1 @@ +Add optional household row filtering to US managed microsimulations while retaining every native projected year and the source dataset provenance. diff --git a/src/policyengine/tax_benefit_models/us/managed_scope.py b/src/policyengine/tax_benefit_models/us/managed_scope.py new file mode 100644 index 000000000..eefb5a315 --- /dev/null +++ b/src/policyengine/tax_benefit_models/us/managed_scope.py @@ -0,0 +1,116 @@ +"""Scope a native projected US dataset without dropping supplied years.""" + +from __future__ import annotations + +import hashlib + +import pandas as pd + +from policyengine.core.scoping_strategy import RowFilterStrategy +from policyengine.utils.entity_utils import _resolve_id_column + +GROUPS = ("household", "tax_unit", "spm_unit", "family", "marital_unit") +ENTITIES = ("person", *GROUPS) + + +def _digest(values: pd.Series) -> str: + """Hash native values, dtype and order without exposing household IDs.""" + digest = hashlib.sha256(str(values.dtype).encode()) + digest.update(pd.util.hash_pandas_object(values, index=False).values.tobytes()) + return digest.hexdigest() + + +def scope_native_history(dataset, strategy: RowFilterStrategy): + """Return an official native dataset for one fixed, closed cohort. + + The caller obtains ``dataset`` from the ordinary native constructor, so + the country package owns projection and its parameter/reform timing. + This function only filters the stored tables. It never calculates a + variable, derives a weight or writes an input file. + """ + from policyengine_us.data import USMultiYearDataset, USSingleYearDataset + + if type(strategy) is not RowFilterStrategy: + raise ValueError("Managed scoping supports only RowFilterStrategy.") + if not isinstance(dataset, USMultiYearDataset) or not dataset.datasets: + raise ValueError("Managed scoping requires a native USMultiYearDataset.") + + scoped_years = [] + reference_ids = None + reference_memberships = None + receipt_years = {} + for year, single in sorted(dataset.datasets.items()): + tables = {entity: getattr(single, entity) for entity in ENTITIES} + for entity, table in tables.items(): + id_column = f"{entity}_id" + if ( + not isinstance(table, pd.DataFrame) + or not table.columns.is_unique + or id_column not in table + or table[id_column].isna().any() + or not table[id_column].is_unique + ): + raise ValueError(f"Invalid native {entity} IDs or columns for {year}.") + + person = tables["person"] + household_column = _resolve_id_column(person, "household") + for group in GROUPS: + column = _resolve_id_column(person, group) + if not person[column].isin(tables[group][f"{group}_id"]).all(): + raise ValueError(f"Dangling {group} membership for {year}.") + # A group spanning selected and unselected households cannot be + # calculated on either subset without changing its population. + membership = pd.DataFrame( + {"group": person[column], "household": person[household_column]} + ) + if ( + membership.groupby("group", dropna=False) + .household.nunique() + .gt(1) + .any() + ): + raise ValueError(f"Managed scoping requires household-closed {group}s.") + + scoped = strategy.apply(tables, list(GROUPS), int(year)) + # RowFilterStrategy's temporary MicroDataFrame metadata never supplies + # statistical weights here. Keep only the original table columns. + scoped = {entity: pd.DataFrame(scoped[entity]).copy() for entity in ENTITIES} + ids = { + entity: table[f"{entity}_id"].reset_index(drop=True) + for entity, table in scoped.items() + } + scoped_memberships = { + group: scoped["person"][ + _resolve_id_column(scoped["person"], group) + ].reset_index(drop=True) + for group in GROUPS + } + if reference_ids is not None and ( + any(not ids[entity].equals(reference_ids[entity]) for entity in ENTITIES) + or any( + not scoped_memberships[group].equals(reference_memberships[group]) + for group in GROUPS + ) + ): + raise ValueError( + "Managed scoping requires the same ordered cohort every year." + ) + reference_ids = ids + reference_memberships = scoped_memberships + scoped_years.append(USSingleYearDataset(**scoped, time_period=int(year))) + receipt_years[str(year)] = { + "source_rows": {entity: len(table) for entity, table in tables.items()}, + "scoped_rows": {entity: len(table) for entity, table in scoped.items()}, + "id_sha256": {entity: _digest(values) for entity, values in ids.items()}, + "household_weight_sha256": ( + _digest(scoped["household"]["household_weight"]) + if "household_weight" in scoped["household"] + else None + ), + } + return USMultiYearDataset(datasets=scoped_years), { + "strategy": strategy.model_dump(mode="json"), + "cache_key": strategy.cache_key, + "years": receipt_years, + "method": "native_projected_history_row_filter", + } diff --git a/src/policyengine/tax_benefit_models/us/model.py b/src/policyengine/tax_benefit_models/us/model.py index b008dc97f..c4306174c 100644 --- a/src/policyengine/tax_benefit_models/us/model.py +++ b/src/policyengine/tax_benefit_models/us/model.py @@ -537,6 +537,7 @@ def managed_microsimulation( dataset: Optional[str] = None, allow_unmanaged: bool = False, spm: Optional[SPMSelection] = None, + scoping_strategy=None, **kwargs, ): """Construct a country-package Microsimulation pinned to this bundle. @@ -548,6 +549,11 @@ def managed_microsimulation( Stored inputs the engine has since renamed are mapped onto their live inputs (see ``legacy_inputs.LEGACY_INPUT_RENAMES``), and the renames applied are recorded as ``policyengine_bundle["legacy_input_renames"]``. + + ``scoping_strategy=RowFilterStrategy(...)`` selects a fixed, closed + household cohort from every native projected year before financial + calculations. This retains earlier supplied income for lookbacks. The + unscoped constructor continues to own projection and reform timing. """ from policyengine_us import Microsimulation @@ -564,8 +570,6 @@ def managed_microsimulation( dataset, allow_unmanaged=allow_unmanaged, ) - microsim = Microsimulation(dataset=source.path, spm=selection, **kwargs) - legacy_input_renames = apply_legacy_input_renames_to_microsimulation(microsim) # BACKWARD COMPATIBILITY (#597, #598): a legacy certified source carries # no SPM declarations, so derive them from its stored evidence and set # them on the simulation and its branches (see ``legacy_spm``). @@ -573,6 +577,41 @@ def managed_microsimulation( legacy_source = legacy_spm_source( source.bundle_dataset.sha256 if source.bundle_dataset else None ) + if scoping_strategy is not None: + from policyengine.core.scoping_strategy import RowFilterStrategy + + if type(scoping_strategy) is not RowFilterStrategy: + raise ValueError("Managed scoping supports only RowFilterStrategy.") + if legacy_source is not None: + raise ValueError( + "Managed scoping does not support legacy SPM declarations." + ) + + microsim = Microsimulation(dataset=source.path, spm=selection, **kwargs) + scoping_record = None + if scoping_strategy is not None: + import gc + + from policyengine.tax_benefit_models.us.managed_scope import ( + scope_native_history, + ) + + scoped_dataset, scoping_record = scope_native_history( + microsim.dataset, scoping_strategy + ) + # Release the national input-only simulation before constructing the + # scoped simulation. All retained data belong to copied scoped tables. + del microsim + gc.collect() + scoped_kwargs = dict(kwargs) + # A native multi-year dataset already contains the exact projected + # horizon; country constructors accept this option only for single-year + # inputs. Do not project or extend the scoped data a second time. + scoped_kwargs.pop("dataset_end_year", None) + microsim = Microsimulation( + dataset=scoped_dataset, spm=selection, **scoped_kwargs + ) + legacy_input_renames = apply_legacy_input_renames_to_microsimulation(microsim) if legacy_source is not None: declarations, years = derive_from_stored_tables( legacy_source, microsim.dataset, data_dir=Path(source.path).parent @@ -596,6 +635,8 @@ def managed_microsimulation( ) if legacy_spm_record is not None: microsim.policyengine_bundle[LEGACY_SPM_RECORD_KEY] = legacy_spm_record + if scoping_record is not None: + microsim.policyengine_bundle["scoping"] = scoping_record return microsim diff --git a/tests/test_us_managed_scope.py b/tests/test_us_managed_scope.py new file mode 100644 index 000000000..56a94ee8f --- /dev/null +++ b/tests/test_us_managed_scope.py @@ -0,0 +1,108 @@ +"""Managed regional calculations retain the native supplied history.""" + +from __future__ import annotations + +import copy + +import numpy as np +import pandas as pd +import pytest + +pytest.importorskip("policyengine_us") +from policyengine_us.data import USMultiYearDataset, USSingleYearDataset # noqa: E402 + +from policyengine.core.scoping_strategy import RowFilterStrategy # noqa: E402 +from policyengine.tax_benefit_models.us.managed_scope import ( # noqa: E402 + ENTITIES, + GROUPS, + scope_native_history, +) + + +def _native(years=(2023, 2025)): + singles = [] + for year in years: + person = pd.DataFrame( + { + "person_id": [909, 37, 604], + "age": [66 + year - 2023, 40, 8], + "employment_income": [50_000 if year == 2023 else 300_000, 10_000, 0], + "would_claim_wic": [False, True, False], + **{f"person_{group}_id": [91, 23, 23] for group in GROUPS}, + } + ) + tables = {"person": person} + for group in GROUPS: + tables[group] = pd.DataFrame({f"{group}_id": [23, 91]}) + tables["household"]["state_fips"] = [6, 41] + tables["household"]["household_weight"] = [13.5, 7.25] + singles.append(USSingleYearDataset(**tables, time_period=year)) + return USMultiYearDataset(datasets=singles) + + +def test_scopes_all_years_and_retains_native_values_order_and_weights(): + native = _native() + original = copy.deepcopy(native) + scoped, receipt = scope_native_history( + native, RowFilterStrategy(variable_name="state_fips", variable_value=41) + ) + assert scoped.years == [2023, 2025] + assert scoped.time_period == native.time_period == "2023" + assert receipt["years"]["2023"]["scoped_rows"]["household"] == 1 + for year in native.years: + for entity in ENTITIES: + pd.testing.assert_frame_equal( + getattr(native.datasets[year], entity), + getattr(original.datasets[year], entity), + ) + expected = getattr(native.datasets[year], entity) + own_id = f"{entity}_id" + expected = expected[expected[own_id] == (909 if entity == "person" else 91)] + pd.testing.assert_frame_equal( + getattr(scoped.datasets[year], entity), expected.reset_index(drop=True) + ) + assert scoped.datasets[2023].person.employment_income.tolist() == [50_000] + assert scoped.datasets[2025].person.employment_income.tolist() == [300_000] + assert scoped.datasets[2025].household.household_weight.tolist() == [7.25] + + +def test_refuses_cohort_change_and_cross_household_groups(): + native = _native() + native.datasets[2025].household["state_fips"] = [41, 6] + strategy = RowFilterStrategy(variable_name="state_fips", variable_value=41) + with pytest.raises(ValueError, match="same ordered cohort"): + scope_native_history(native, strategy) + native = _native() + native.datasets[2023].person["person_tax_unit_id"] = [23, 23, 23] + with pytest.raises(ValueError, match="household-closed"): + scope_native_history(native, strategy) + + +@pytest.mark.parametrize("defect", ["duplicate_id", "dangling", "duplicate_column"]) +def test_refuses_invalid_native_relationships(defect): + native = _native() + if defect == "duplicate_id": + native.datasets[2023].person["person_id"] = [909, 909, 604] + elif defect == "dangling": + native.datasets[2023].person["person_family_id"] = [999, 23, 23] + else: + frame = native.datasets[2023].person + native.datasets[2023].person = pd.concat([frame, frame[["age"]]], axis=1) + with pytest.raises(ValueError): + scope_native_history( + native, RowFilterStrategy(variable_name="state_fips", variable_value=41) + ) + + +def test_scoped_tables_do_not_share_source_buffers(): + native = _native() + scoped, _ = scope_native_history( + native, RowFilterStrategy(variable_name="state_fips", variable_value=41) + ) + for year in scoped.years: + for entity in ENTITIES: + for name in getattr(scoped.datasets[year], entity): + assert not np.shares_memory( + getattr(scoped.datasets[year], entity)[name].to_numpy(), + getattr(native.datasets[year], entity)[name].to_numpy(), + ) From 628cbaf63e31dce8477be4b0215c07bcc3b49d47 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Thu, 12 Nov 2026 17:15:50 -0500 Subject: [PATCH 2/3] Verify scoped native history and reform-arm equivalence --- .../tax_benefit_models/us/managed_scope.py | 20 +- .../tax_benefit_models/us/model.py | 7 +- tests/test_us_managed_scope.py | 239 +++++++++++++++++- 3 files changed, 260 insertions(+), 6 deletions(-) diff --git a/src/policyengine/tax_benefit_models/us/managed_scope.py b/src/policyengine/tax_benefit_models/us/managed_scope.py index eefb5a315..d69c17b80 100644 --- a/src/policyengine/tax_benefit_models/us/managed_scope.py +++ b/src/policyengine/tax_benefit_models/us/managed_scope.py @@ -20,7 +20,7 @@ def _digest(values: pd.Series) -> str: return digest.hexdigest() -def scope_native_history(dataset, strategy: RowFilterStrategy): +def scope_native_history(dataset, strategy: RowFilterStrategy, variables=None): """Return an official native dataset for one fixed, closed cohort. The caller obtains ``dataset`` from the ordinary native constructor, so @@ -38,6 +38,7 @@ def scope_native_history(dataset, strategy: RowFilterStrategy): scoped_years = [] reference_ids = None reference_memberships = None + eternity_inputs = {} receipt_years = {} for year, single in sorted(dataset.datasets.items()): tables = {entity: getattr(single, entity) for entity in ENTITIES} @@ -58,6 +59,8 @@ def scope_native_history(dataset, strategy: RowFilterStrategy): column = _resolve_id_column(person, group) if not person[column].isin(tables[group][f"{group}_id"]).all(): raise ValueError(f"Dangling {group} membership for {year}.") + if set(person[column]) != set(tables[group][f"{group}_id"]): + raise ValueError(f"Managed scoping requires members in every {group}.") # A group spanning selected and unselected households cannot be # calculated on either subset without changing its population. membership = pd.DataFrame( @@ -75,6 +78,21 @@ def scope_native_history(dataset, strategy: RowFilterStrategy): # RowFilterStrategy's temporary MicroDataFrame metadata never supplies # statistical weights here. Keep only the original table columns. scoped = {entity: pd.DataFrame(scoped[entity]).copy() for entity in ENTITIES} + for table in scoped.values(): + for name in table: + variable = (variables or {}).get(name) + if ( + variable is not None + and str(variable.definition_period) == "eternity" + ): + values = table[name].reset_index(drop=True) + if name in eternity_inputs and not values.equals( + eternity_inputs[name] + ): + raise ValueError( + f"Conflicting ETERNITY input {name} across years." + ) + eternity_inputs[name] = values ids = { entity: table[f"{entity}_id"].reset_index(drop=True) for entity, table in scoped.items() diff --git a/src/policyengine/tax_benefit_models/us/model.py b/src/policyengine/tax_benefit_models/us/model.py index c4306174c..9ceca8656 100644 --- a/src/policyengine/tax_benefit_models/us/model.py +++ b/src/policyengine/tax_benefit_models/us/model.py @@ -6,6 +6,7 @@ from microdf import MicroDataFrame from policyengine.core import TaxBenefitModel +from policyengine.core.scoping_strategy import RowFilterStrategy from policyengine.provenance.dataset_materialization import ( materialize_dataset, ) @@ -537,7 +538,7 @@ def managed_microsimulation( dataset: Optional[str] = None, allow_unmanaged: bool = False, spm: Optional[SPMSelection] = None, - scoping_strategy=None, + scoping_strategy: Optional[RowFilterStrategy] = None, **kwargs, ): """Construct a country-package Microsimulation pinned to this bundle. @@ -578,8 +579,6 @@ def managed_microsimulation( source.bundle_dataset.sha256 if source.bundle_dataset else None ) if scoping_strategy is not None: - from policyengine.core.scoping_strategy import RowFilterStrategy - if type(scoping_strategy) is not RowFilterStrategy: raise ValueError("Managed scoping supports only RowFilterStrategy.") if legacy_source is not None: @@ -597,7 +596,7 @@ def managed_microsimulation( ) scoped_dataset, scoping_record = scope_native_history( - microsim.dataset, scoping_strategy + microsim.dataset, scoping_strategy, microsim.tax_benefit_system.variables ) # Release the national input-only simulation before constructing the # scoped simulation. All retained data belong to copied scoped tables. diff --git a/tests/test_us_managed_scope.py b/tests/test_us_managed_scope.py index 56a94ee8f..d2bbd9cb6 100644 --- a/tests/test_us_managed_scope.py +++ b/tests/test_us_managed_scope.py @@ -3,6 +3,10 @@ from __future__ import annotations import copy +import gc +import importlib +import weakref +from types import SimpleNamespace import numpy as np import pandas as pd @@ -18,6 +22,37 @@ scope_native_history, ) +us_model = importlib.import_module("policyengine.tax_benefit_models.us.model") + + +def _inject_native_source(monkeypatch, native): + """Inject synthetic supplied history at the materialization boundary. + + All native constructors, input writes, projections and formulas remain + real. The test replaces only source resolution and its file provenance. + """ + monkeypatch.setattr( + us_model, + "materialize_dataset", + lambda *args, **kwargs: SimpleNamespace( + path=native, source_uri="synthetic://supplied-history", bundle_dataset=None + ), + ) + monkeypatch.setattr( + us_model, + "build_runtime_dataset_provenance", + lambda *args: {"source_uri": "synthetic://supplied-history"}, + ) + + +def _managed(**kwargs): + return us_model.managed_microsimulation( + dataset="synthetic-history", + allow_unmanaged=True, + spm={"geography_kind": "national", "scenario": "zero_real"}, + **kwargs, + ) + def _native(years=(2023, 2025)): singles = [] @@ -26,6 +61,7 @@ def _native(years=(2023, 2025)): { "person_id": [909, 37, 604], "age": [66 + year - 2023, 40, 8], + "is_household_head": [True, True, False], "employment_income": [50_000 if year == 2023 else 300_000, 10_000, 0], "would_claim_wic": [False, True, False], **{f"person_{group}_id": [91, 23, 23] for group in GROUPS}, @@ -73,7 +109,7 @@ def test_refuses_cohort_change_and_cross_household_groups(): with pytest.raises(ValueError, match="same ordered cohort"): scope_native_history(native, strategy) native = _native() - native.datasets[2023].person["person_tax_unit_id"] = [23, 23, 23] + native.datasets[2023].person["person_tax_unit_id"] = [23, 91, 23] with pytest.raises(ValueError, match="household-closed"): scope_native_history(native, strategy) @@ -106,3 +142,204 @@ def test_scoped_tables_do_not_share_source_buffers(): getattr(scoped.datasets[year], entity)[name].to_numpy(), getattr(native.datasets[year], entity)[name].to_numpy(), ) + + +def test_refuses_conflicting_eternity_inputs_and_orphan_groups(): + native = _native() + native.datasets[2025].person.loc[0, "is_household_head"] = False + strategy = RowFilterStrategy(variable_name="state_fips", variable_value=41) + with pytest.raises(ValueError, match="Conflicting ETERNITY"): + scope_native_history( + native, + strategy, + {"is_household_head": SimpleNamespace(definition_period="eternity")}, + ) + native = _native() + native.datasets[2023].family.loc[2] = [999] + with pytest.raises(ValueError, match="members in every family"): + scope_native_history(native, strategy) + + +def test_unsupported_strategy_and_legacy_spm_fail_before_country_construction( + monkeypatch, +): + import policyengine_us + + from policyengine.core.scoping_strategy import RegionGroupStrategy + + _inject_native_source(monkeypatch, _native()) + + def forbidden_constructor(*args, **kwargs): + pytest.fail("unsupported scope reached native construction") + + monkeypatch.setattr(policyengine_us, "Microsimulation", forbidden_constructor) + strategy = RowFilterStrategy(variable_name="state_fips", variable_value=41) + with pytest.raises(ValueError, match="only RowFilterStrategy"): + _managed(scoping_strategy=RegionGroupStrategy(members=[strategy])) + monkeypatch.setattr(us_model, "legacy_spm_source", lambda *args: object()) + with pytest.raises(ValueError, match="legacy SPM"): + _managed(scoping_strategy=strategy) + + +@pytest.mark.parametrize("reform", [False, True]) +def test_real_managed_scope_keeps_all_supplied_inputs_and_releases_source( + monkeypatch, reform +): + """Country input holders, live IDs and native weights retain the history.""" + import policyengine_us + + native = _native() + _inject_native_source(monkeypatch, native) + constructor = policyengine_us.Microsimulation + if reform: + from policyengine_core.reforms import Reform + + policy = Reform.from_dict( + { + "gov.irs.deductions.standard.amount.SINGLE": { + "2025-01-01.2100-12-31": 100_000 + } + }, + country_id="us", + ) + else: + policy = None + unscoped = _managed(reform=policy) + constructed = [] + + def observed_constructor(*args, **kwargs): + if constructed: + gc.collect() + assert constructed[0]() is None, "national input simulation remained live" + assert kwargs["dataset"].datasets[2023].household.household_id.tolist() == [ + 91 + ] + result = constructor(*args, **kwargs) + constructed.append(weakref.ref(result)) + return result + + monkeypatch.setattr(policyengine_us, "Microsimulation", observed_constructor) + scoped = _managed( + reform=policy, + scoping_strategy=RowFilterStrategy( + variable_name="state_fips", variable_value=41 + ), + ) + assert len(constructed) == 2 + assert scoped.is_over_dataset + assert scoped.default_calculation_period == "2023" + assert scoped.dataset.years == [2023, 2025] + assert scoped.policyengine_bundle["source_uri"] == "synthetic://supplied-history" + assert scoped.policyengine_bundle["legacy_input_renames"] == { + "would_claim_wic": "takes_up_wic_if_eligible" + } + source_arms = [unscoped, unscoped.baseline] if reform else [unscoped] + scoped_arms = [scoped, scoped.baseline] if reform else [scoped] + for source_arm, scoped_arm in zip(source_arms, scoped_arms): + for name in source_arm.input_variables: + entity = source_arm.tax_benefit_system.variables[name].entity.key + source_ids = np.asarray(source_arm.populations[entity].ids) + target_ids = np.asarray(scoped_arm.populations[entity].ids) + positions = pd.Index(source_ids).get_indexer(target_ids) + assert (positions >= 0).all() + holder = source_arm.get_holder(name) + supplied = holder.get_input_periods(source_arm.branch_name) + assert {str(p) for p in supplied} == { + str(p) + for p in scoped_arm.get_holder(name).get_input_periods( + scoped_arm.branch_name + ) + }, name + for period in supplied: + np.testing.assert_array_equal( + scoped_arm.get_array(name, period), + source_arm.get_array(name, period)[positions], + err_msg=f"{name}/{period}", + ) + if reform: + assert ( + scoped_arm.tax_benefit_system.parameters( + "2025-01-01" + ).gov.irs.deductions.standard.amount.SINGLE + == source_arm.tax_benefit_system.parameters( + "2025-01-01" + ).gov.irs.deductions.standard.amount.SINGLE + ) + expected_tax = source_arm.calculate("income_tax", 2025, map_to="household") + actual_tax = scoped_arm.calculate("income_tax", 2025, map_to="household") + assert actual_tax.tolist() == [expected_tax.iloc[1]] + assert actual_tax.weights.tolist() == [7.25] + if reform: + assert ( + scoped.calculate("income_tax", 2025).tolist() + != scoped.baseline.calculate("income_tax", 2025).tolist() + ) + for year in (2023, 2025): + assert scoped.calculate("person_id", year).tolist() == [909] + for group in GROUPS: + assert scoped.calculate(f"{group}_id", year).tolist() == [91] + assert scoped.calculate(f"person_{group}_id", year).tolist() == [91] + for month in range(1, 13): + period = f"{year}-{month:02d}" + holder = scoped.get_holder("takes_up_wic_if_eligible") + assert period in { + str(p) for p in holder.get_input_periods(scoped.branch_name) + } + assert scoped.calculate("takes_up_wic_if_eligible", period).tolist() == [ + False + ] + income = scoped.calculate("employment_income", year, map_to="household") + assert income.weights.tolist() == [7.25] + assert income.tolist() == [50_000 if year == 2023 else 300_000] + + +def test_real_history_scope_preserves_irmaa_and_snap_medical_deduction(monkeypatch): + """Earlier income changes an actual Medicare premium and SNAP deduction.""" + native = _native() + _inject_native_source(monkeypatch, native) + unscoped = _managed() + if ( + "medicare_irmaa_magi_two_years_prior" + not in unscoped.tax_benefit_system.variables + ): + pytest.skip("This country pin predates the Medicare IRMAA lookback variable") + from policyengine_us.tools.general import earliest_input_year + + scoped = _managed( + scoping_strategy=RowFilterStrategy( + variable_name="state_fips", variable_value=41 + ) + ) + _inject_native_source(monkeypatch, _native(years=(2025,))) + target_only = _managed( + scoping_strategy=RowFilterStrategy( + variable_name="state_fips", variable_value=41 + ) + ) + assert earliest_input_year(scoped) == 2023 + assert earliest_input_year(target_only) == 2025 + assert scoped.calculate("medicare_irmaa_magi_two_years_prior", 2025)[ + 0 + ] == pytest.approx(50_000) + assert ( + target_only.calculate("medicare_irmaa_magi_two_years_prior", 2025)[0] > 200_000 + ) + for variable in ( + "medicare_irmaa_magi_two_years_prior", + "gross_medicare_part_b_premium", + "medicare_part_b_premium", + "snap_allowable_medical_expenses", + ): + expected = unscoped.calculate(variable, 2025, map_to="household") + actual = scoped.calculate(variable, 2025, map_to="household") + # The source household table deliberately puts the selected ID second. + assert actual.tolist() == [expected.iloc[1]] + assert actual.weights.tolist() == [7.25] + base = scoped.calculate("base_part_b_premium", 2025)[0] + assert scoped.calculate("gross_medicare_part_b_premium", 2025)[0] == base + assert target_only.calculate("gross_medicare_part_b_premium", 2025)[0] > base + assert ( + target_only.calculate("snap_allowable_medical_expenses", 2025)[0] + > scoped.calculate("snap_allowable_medical_expenses", 2025)[0] + > 0 + ) From 3df3579a902d128e54e2eea215b24e1388e31a10 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Thu, 12 Nov 2026 17:30:11 -0500 Subject: [PATCH 3/3] Guard the development-only history helper in country tests --- tests/test_us_managed_scope.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_us_managed_scope.py b/tests/test_us_managed_scope.py index d2bbd9cb6..0ed8646db 100644 --- a/tests/test_us_managed_scope.py +++ b/tests/test_us_managed_scope.py @@ -303,7 +303,10 @@ def test_real_history_scope_preserves_irmaa_and_snap_medical_deduction(monkeypat not in unscoped.tax_benefit_system.variables ): pytest.skip("This country pin predates the Medicare IRMAA lookback variable") - from policyengine_us.tools.general import earliest_input_year + general = importlib.import_module("policyengine_us.tools.general") + if not hasattr(general, "earliest_input_year"): + pytest.skip("This country pin predates the supplied-history fallback helper") + earliest_input_year = general.earliest_input_year scoped = _managed( scoping_strategy=RowFilterStrategy(