From 1fd0f4fde3facc48be42455fbb72278f4a0d0786 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Wed, 2 Dec 2026 01:06:17 +0400 Subject: [PATCH 1/4] Test that US dataset runs list the dataset's columns as inputs Three tests that fail before the change (#781): after a run with a reform, the reform simulation and its baseline list every stored dataset column in input_variables; a retiree's cliff_gap is 0 and marginal_tax_rate equals policyengine-us's; and the labor-supply response equals policyengine-us's. Co-Authored-By: Claude Opus 5.5 --- tests/test_us_dataset_input_variables.py | 227 +++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 tests/test_us_dataset_input_variables.py diff --git a/tests/test_us_dataset_input_variables.py b/tests/test_us_dataset_input_variables.py new file mode 100644 index 000000000..d9a7ef55b --- /dev/null +++ b/tests/test_us_dataset_input_variables.py @@ -0,0 +1,227 @@ +"""US simulations built from a dataset list the dataset's columns as inputs. + +policyengine-core fills ``Simulation.input_variables`` once, at construction, +from the variables that hold values then. ``PolicyEngineUSLatest.run`` +constructs the country simulation empty and stores the dataset's columns +afterwards, so it has to list them itself. Several policyengine-us formulas +(``marginal_tax_rate``, and through it ``cliff_gap`` and the labor-supply +response) calculate in a copy of the simulation, after deleting from the copy +every stored value whose variable is not listed +(PolicyEngine/policyengine.py#781). + +Each test builds one single adult living alone in Wake County, North +Carolina, in 2026, runs it through ``Simulation(dataset=...)``, and compares +the result with policyengine-us's own ``Simulation(situation=...)`` for the +same inputs, which lists every input at construction. +""" + +from __future__ import annotations + +import pandas as pd +import pytest +from microdf import MicroDataFrame + +pytest.importorskip("policyengine_us") +pytest.importorskip("spm_calculator.policyengine_adapter") + +import policyengine as pe # noqa: E402 +from policyengine.core import Simulation # noqa: E402 +from policyengine.tax_benefit_models.us.datasets import ( # noqa: E402 + PolicyEngineUSDataset, + USYearData, +) +from policyengine.tax_benefit_models.us.model import ( # noqa: E402 + PolicyEngineUSLatest, +) + +YEAR = 2026 +NORTH_CAROLINA_STATE_FIPS = 37 +WAKE_COUNTY_FIPS = "37183" +GROUP_ENTITIES = ("household", "tax_unit", "spm_unit", "family", "marital_unit") +ENTITIES = ("person", *GROUP_ENTITIES) + +#: A retiree who still works: Social Security is most of the household's +#: income, so a copy of the simulation that loses it shows a large cliff. +RETIREE = { + "age": 67, + "employment_income_before_lsr": 15_000.0, + "social_security_retirement": 24_000.0, +} +#: A worker whose top dollar of earnings pays the 12 % federal rate. +WORKER = { + "age": 40, + "employment_income_before_lsr": 40_000.0, + "taxable_interest_income": 10_000.0, +} + +REFORM_PERIOD = f"{YEAR}-01-01.2100-12-31" +#: Raises the 12 % federal income tax rate to 17 % and turns on labor-supply +#: responses with a substitution elasticity of 0.25 for everyone. +HIGHER_RATE_WITH_LABOR_SUPPLY_RESPONSES = { + "gov.irs.income.bracket.rates.2": {REFORM_PERIOD: 0.17}, + "gov.simulation.labor_supply_responses.elasticities.substitution.all": { + REFORM_PERIOD: 0.25 + }, +} + + +def _one_adult_dataset(person_inputs: dict[str, float]) -> PolicyEngineUSDataset: + """Native entity tables for one adult who is every group's only member.""" + person = pd.DataFrame( + { + "person_id": [1], + **{f"person_{entity}_id": [1] for entity in GROUP_ENTITIES}, + "person_weight": [1.0], + **{name: [value] for name, value in person_inputs.items()}, + } + ) + household = pd.DataFrame( + { + "household_id": [1], + "household_weight": [1.0], + "state_fips": [NORTH_CAROLINA_STATE_FIPS], + "county_fips": [WAKE_COUNTY_FIPS], + } + ) + tables = { + "person": MicroDataFrame(person, weights="person_weight"), + "household": MicroDataFrame(household, weights="household_weight"), + } + for entity in GROUP_ENTITIES[1:]: + tables[entity] = MicroDataFrame( + pd.DataFrame({f"{entity}_id": [1], f"{entity}_weight": [1.0]}), + weights=f"{entity}_weight", + ) + return PolicyEngineUSDataset( + id="one-adult-wake-county", + name="one-adult-wake-county", + description="One adult living alone in Wake County, North Carolina", + filepath=None, + year=YEAR, + data=USYearData(**tables), + ) + + +def _dataset_person_values( + person_inputs: dict[str, float], + person_outputs: tuple[str, ...], + policy: dict | None = None, +) -> dict[str, float]: + """The adult's outputs through .py's ``Simulation(dataset=...)``.""" + simulation = Simulation( + dataset=_one_adult_dataset(person_inputs), + tax_benefit_model_version=pe.us.model, + policy=policy, + extra_variables={"person": list(person_outputs)}, + ) + simulation.run() + person = pd.DataFrame(simulation.output_dataset.data.person) + return {name: float(person[name].iloc[0]) for name in person_outputs} + + +def _situation_person_values( + person_inputs: dict[str, float], + person_outputs: tuple[str, ...], + reform: dict | None = None, +) -> dict[str, float]: + """The adult's outputs through policyengine-us's ``Simulation(situation=...)``.""" + from policyengine_us import Simulation as CountrySimulation + + situation = { + "people": { + "adult": {name: {YEAR: value} for name, value in person_inputs.items()} + }, + "tax_units": {"tax_unit": {"members": ["adult"]}}, + "spm_units": {"spm_unit": {"members": ["adult"]}}, + "families": {"family": {"members": ["adult"]}}, + "marital_units": {"marital_unit": {"members": ["adult"]}}, + "households": { + "household": { + "members": ["adult"], + "state_fips": {YEAR: NORTH_CAROLINA_STATE_FIPS}, + "county_fips": {YEAR: WAKE_COUNTY_FIPS}, + } + }, + } + country_simulation = CountrySimulation(situation=situation, reform=reform) + return { + name: float(country_simulation.calculate(name, YEAR)[0]) + for name in person_outputs + } + + +def test_reform_and_baseline_list_every_stored_dataset_column_as_an_input( + monkeypatch, +): + built_simulations = [] + build_simulation_from_dataset = PolicyEngineUSLatest._build_simulation_from_dataset + + def build_and_keep(self, microsim, dataset, system): + renames = build_simulation_from_dataset(self, microsim, dataset, system) + built_simulations.append(microsim) + return renames + + monkeypatch.setattr( + PolicyEngineUSLatest, "_build_simulation_from_dataset", build_and_keep + ) + simulation = Simulation( + dataset=_one_adult_dataset(RETIREE), + tax_benefit_model_version=pe.us.model, + policy=HIGHER_RATE_WITH_LABOR_SUPPLY_RESPONSES, + ) + simulation.run() + + # ``run`` builds the reform's baseline first, then the reform. + baseline, reform = built_simulations + assert baseline is reform.baseline + for country_simulation in (baseline, reform): + variables = country_simulation.tax_benefit_system.variables + stored_columns = { + column + for entity in ENTITIES + for column in pd.DataFrame(getattr(simulation.dataset.data, entity)) + if column in variables + } + assert { + "person_id", + "person_household_id", + "household_weight", + "age", + "employment_income_before_lsr", + "social_security_retirement", + "state_fips", + "county_fips", + } <= stored_columns + missing = stored_columns - set(country_simulation.input_variables) + assert not missing, ( + f"{country_simulation.branch_name}: stored dataset columns missing " + f"from input_variables: {sorted(missing)}" + ) + + +def test_retiree_cliff_gap_and_marginal_tax_rate_match_policyengine_us(): + outputs = ("marginal_tax_rate", "cliff_gap") + from_situation = _situation_person_values(RETIREE, outputs) + from_dataset = _dataset_person_values(RETIREE, outputs) + + assert from_situation["cliff_gap"] == 0 + assert from_dataset["cliff_gap"] == pytest.approx(0, abs=0.01) + assert from_dataset["marginal_tax_rate"] == pytest.approx( + from_situation["marginal_tax_rate"], abs=1e-4 + ) + + +def test_labor_supply_response_matches_policyengine_us(): + outputs = ("employment_income_behavioral_response",) + from_situation = _situation_person_values( + WORKER, outputs, reform=HIGHER_RATE_WITH_LABOR_SUPPLY_RESPONSES + ) + from_dataset = _dataset_person_values( + WORKER, outputs, policy=HIGHER_RATE_WITH_LABOR_SUPPLY_RESPONSES + ) + + response = "employment_income_behavioral_response" + # A higher marginal rate with a positive substitution elasticity lowers + # earnings. + assert from_situation[response] < 0 + assert from_dataset[response] == pytest.approx(from_situation[response], abs=0.01) From 459bba3dce5c237d5d6092f5e0ef110ddfd9019c Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Wed, 2 Dec 2026 01:12:01 +0400 Subject: [PATCH 2/4] List a US dataset's stored columns in input_variables PolicyEngineUSLatest.run constructs the country simulation empty and stores the dataset's columns afterwards, but policyengine-core fills input_variables only at construction. policyengine-us formulas that calculate in a copy of the simulation (marginal_tax_rate, and through it cliff_gap and the behavioral responses) delete every value whose variable is not listed, so the copies lost every dataset input. _build_simulation_from_dataset now records the live ID names and dataset columns it stores with _record_input_variables, for the reform simulation and its baseline (#781). Co-Authored-By: Claude Opus 5.5 --- changelog.d/781.fixed.md | 1 + .../tax_benefit_models/us/legacy_inputs.py | 11 +++++++---- src/policyengine/tax_benefit_models/us/model.py | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 changelog.d/781.fixed.md diff --git a/changelog.d/781.fixed.md b/changelog.d/781.fixed.md new file mode 100644 index 000000000..1a1cc2322 --- /dev/null +++ b/changelog.d/781.fixed.md @@ -0,0 +1 @@ +US simulations built from a dataset (`Simulation(dataset=...)`) list every stored dataset column in the country simulation's `input_variables`, for the reform simulation and its baseline. Since 6.0.0 the list held none of them: the US model constructs the country simulation empty and stores the dataset afterwards, and policyengine-core fills the list only at construction. policyengine-us formulas that calculate in a copy of the simulation delete every value whose variable is not listed, so the copies lost age, location, Social Security and every other dataset input. `marginal_tax_rate` and `cliff_gap` therefore differed from policyengine-us's (a 67-year-old in Wake County, North Carolina, with $15,000 of earnings and $24,000 of Social Security in 2026 had a marginal rate of 23.64 and a cliff gap of $22,645, against 0.1174 and $0), and so did labor-supply responses. Those three now equal policyengine-us's for the same household. The capital-gains response, the federal, state and FICA marginal rates and the Social Security trust-fund revenue variables calculate in the same kind of copy, so they change too. diff --git a/src/policyengine/tax_benefit_models/us/legacy_inputs.py b/src/policyengine/tax_benefit_models/us/legacy_inputs.py index f108ccd54..91c277a84 100644 --- a/src/policyengine/tax_benefit_models/us/legacy_inputs.py +++ b/src/policyengine/tax_benefit_models/us/legacy_inputs.py @@ -633,12 +633,15 @@ def _periods_of_year(year: int, definition_period: Any, context: str) -> list[st def _record_input_variables(simulation, live_names: Iterable[str]) -> None: - """List mapped inputs as inputs, as if the dataset had stored them. + """List inputs stored after construction as inputs, as construction would. A country Microsimulation lists ``input_variables`` once, at construction. - Dataset extraction (``create_datasets``) exports that list, and - policyengine-core's ``derivative`` keeps only those inputs in its clone, so - a mapped input must join it. + Dataset extraction (``create_datasets``) exports that list, + policyengine-core's ``derivative`` keeps only those inputs in its clone, + and policyengine-us formulas that calculate in a copy of the simulation + (``marginal_tax_rate``) delete every other value from the copy. So an + input stored later (a mapped input, an SPM declaration, or a dataset + column stored by ``PolicyEngineUSLatest.run``) must join it. """ input_variables = getattr(simulation, "input_variables", None) if not isinstance(input_variables, list): diff --git a/src/policyengine/tax_benefit_models/us/model.py b/src/policyengine/tax_benefit_models/us/model.py index bdda192dd..04c507246 100644 --- a/src/policyengine/tax_benefit_models/us/model.py +++ b/src/policyengine/tax_benefit_models/us/model.py @@ -21,6 +21,7 @@ from .datasets import PolicyEngineUSDataset, USYearData, _validate_entity_ids from .legacy_inputs import ( RENAMES_RECORD_KEY, + _record_input_variables, apply_legacy_input_renames, apply_legacy_input_renames_to_microsimulation, ) @@ -393,6 +394,9 @@ def _build_simulation_from_dataset(self, microsim, dataset, system): Stored columns the engine has since renamed are mapped onto their live inputs (see ``legacy_inputs.LEGACY_INPUT_RENAMES``). + Every input it stores is listed in ``microsim.input_variables``, as it + would be had the dataset been passed at construction. + Returns: The legacy input renames applied, ``{legacy: live}``. """ @@ -517,6 +521,7 @@ def _build_simulation_from_dataset(self, microsim, dataset, system): } stored_tables = {} + stored_inputs: list[str] = [] for entity_name, entity_df in [ ("person", dataset.data.person), ("household", dataset.data.household), @@ -543,9 +548,18 @@ def _build_simulation_from_dataset(self, microsim, dataset, system): for live_name, column in live_ids.items(): if live_name in system.variables: microsim.set_input(live_name, dataset.year, df[column].values) + stored_inputs.append(live_name) 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) + stored_inputs.append(column) + + # policyengine-core lists ``input_variables`` only at construction, and + # this simulation was constructed empty. policyengine-us formulas that + # calculate in a copy of the simulation (``marginal_tax_rate``, and + # through it ``cliff_gap`` and the behavioral responses) delete every + # value whose variable is not listed, so list what was stored (#781). + _record_input_variables(microsim, stored_inputs) # The loop above skips columns the engine does not define, including # stored inputs it has since renamed. The tables are already in the From e813c8810b3d1d1654a4485d07579c32cdb584c4 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:32:11 +0400 Subject: [PATCH 3/4] Correct the input_variables comments and changelog after review Review round 1 found that the capital-gains response deletes only unlisted variables that have a formula, so its copies never lost the dataset's inputs; that the reform simulation's list always held the five behavioral-response inputs policyengine-us adds; that policyengine-core's derivative does not read input_variables; and that the test module's docstring described the first test wrongly. Wording only. Co-Authored-By: Claude Opus 5.5 --- changelog.d/781.fixed.md | 2 +- .../tax_benefit_models/us/legacy_inputs.py | 13 ++++++------- src/policyengine/tax_benefit_models/us/model.py | 13 +++++++------ tests/test_us_dataset_input_variables.py | 7 ++++--- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/changelog.d/781.fixed.md b/changelog.d/781.fixed.md index 1a1cc2322..6f236ce61 100644 --- a/changelog.d/781.fixed.md +++ b/changelog.d/781.fixed.md @@ -1 +1 @@ -US simulations built from a dataset (`Simulation(dataset=...)`) list every stored dataset column in the country simulation's `input_variables`, for the reform simulation and its baseline. Since 6.0.0 the list held none of them: the US model constructs the country simulation empty and stores the dataset afterwards, and policyengine-core fills the list only at construction. policyengine-us formulas that calculate in a copy of the simulation delete every value whose variable is not listed, so the copies lost age, location, Social Security and every other dataset input. `marginal_tax_rate` and `cliff_gap` therefore differed from policyengine-us's (a 67-year-old in Wake County, North Carolina, with $15,000 of earnings and $24,000 of Social Security in 2026 had a marginal rate of 23.64 and a cliff gap of $22,645, against 0.1174 and $0), and so did labor-supply responses. Those three now equal policyengine-us's for the same household. The capital-gains response, the federal, state and FICA marginal rates and the Social Security trust-fund revenue variables calculate in the same kind of copy, so they change too. +US simulations built from a dataset (`Simulation(dataset=...)`) list every stored ID and dataset column in the country simulation's `input_variables`, for the reform simulation and its baseline. Since 6.0.0 the US model constructs the country simulation empty and stores the dataset afterwards, and policyengine-core fills the list only at construction, so it held none of the dataset's columns: the baseline's list was empty, and the reform's held only the five behavioral-response inputs policyengine-us always adds (such as `employment_income_before_lsr`). `marginal_tax_rate` calculates in a copy of the simulation after deleting every value whose variable is not listed, so its copies lost age, location, Social Security and the other dataset inputs. `marginal_tax_rate` and `cliff_gap` therefore differed from policyengine-us's (a 67-year-old in Wake County, North Carolina, with $15,000 of earnings and $24,000 of Social Security in 2026 had a marginal rate of 23.64 and a cliff gap of $22,645, against 0.1174 and $0), and so did the labor-supply response, which reads marginal tax rates. All three now equal policyengine-us's for the same household. The federal, state and FICA marginal rates, `marginal_tax_rate_including_health_benefits` and the Social Security trust-fund revenue variables delete unlisted values in the same way, so they change too. diff --git a/src/policyengine/tax_benefit_models/us/legacy_inputs.py b/src/policyengine/tax_benefit_models/us/legacy_inputs.py index 91c277a84..971bce942 100644 --- a/src/policyengine/tax_benefit_models/us/legacy_inputs.py +++ b/src/policyengine/tax_benefit_models/us/legacy_inputs.py @@ -633,15 +633,14 @@ def _periods_of_year(year: int, definition_period: Any, context: str) -> list[st def _record_input_variables(simulation, live_names: Iterable[str]) -> None: - """List inputs stored after construction as inputs, as construction would. + """List inputs stored after construction in ``input_variables``. A country Microsimulation lists ``input_variables`` once, at construction. - Dataset extraction (``create_datasets``) exports that list, - policyengine-core's ``derivative`` keeps only those inputs in its clone, - and policyengine-us formulas that calculate in a copy of the simulation - (``marginal_tax_rate``) delete every other value from the copy. So an - input stored later (a mapped input, an SPM declaration, or a dataset - column stored by ``PolicyEngineUSLatest.run``) must join it. + Dataset extraction (``create_datasets``) exports that list, and + policyengine-us formulas that calculate in a copy of the simulation (such + as ``marginal_tax_rate``) delete values whose variables are not in it from + the copy. So an input stored later (a mapped input, an SPM declaration, or + a dataset column stored by ``PolicyEngineUSLatest.run``) must join it. """ input_variables = getattr(simulation, "input_variables", None) if not isinstance(input_variables, list): diff --git a/src/policyengine/tax_benefit_models/us/model.py b/src/policyengine/tax_benefit_models/us/model.py index 04c507246..f28b26b7c 100644 --- a/src/policyengine/tax_benefit_models/us/model.py +++ b/src/policyengine/tax_benefit_models/us/model.py @@ -394,8 +394,9 @@ def _build_simulation_from_dataset(self, microsim, dataset, system): Stored columns the engine has since renamed are mapped onto their live inputs (see ``legacy_inputs.LEGACY_INPUT_RENAMES``). - Every input it stores is listed in ``microsim.input_variables``, as it - would be had the dataset been passed at construction. + Every ID and dataset column it stores is added to + ``microsim.input_variables``, which policyengine-core fills only at + construction, before any of them is stored. Returns: The legacy input renames applied, ``{legacy: live}``. @@ -555,10 +556,10 @@ def _build_simulation_from_dataset(self, microsim, dataset, system): stored_inputs.append(column) # policyengine-core lists ``input_variables`` only at construction, and - # this simulation was constructed empty. policyengine-us formulas that - # calculate in a copy of the simulation (``marginal_tax_rate``, and - # through it ``cliff_gap`` and the behavioral responses) delete every - # value whose variable is not listed, so list what was stored (#781). + # this simulation was constructed empty. ``marginal_tax_rate`` (and + # through it ``cliff_gap`` and the labor-supply response) calculates + # in a copy of the simulation after deleting every value whose + # variable is not listed, so list what was stored (#781). _record_input_variables(microsim, stored_inputs) # The loop above skips columns the engine does not define, including diff --git a/tests/test_us_dataset_input_variables.py b/tests/test_us_dataset_input_variables.py index d9a7ef55b..07dbc9e47 100644 --- a/tests/test_us_dataset_input_variables.py +++ b/tests/test_us_dataset_input_variables.py @@ -10,9 +10,10 @@ (PolicyEngine/policyengine.py#781). Each test builds one single adult living alone in Wake County, North -Carolina, in 2026, runs it through ``Simulation(dataset=...)``, and compares -the result with policyengine-us's own ``Simulation(situation=...)`` for the -same inputs, which lists every input at construction. +Carolina, in 2026, and runs it through ``Simulation(dataset=...)``. The first +checks the country simulations' ``input_variables`` directly. The others +compare the results with policyengine-us's own ``Simulation(situation=...)`` +for the same inputs, which lists every input at construction. """ from __future__ import annotations From 84ddfe65742f4150d267c47c1627c3b4ef4df4ec Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:54:29 +0400 Subject: [PATCH 4/4] Use a rate-only reform in the input_variables test, and make the changelog exact The input_variables test checked no labor-supply output, so it runs with a reform that only raises the 12 % rate, which takes 18 s instead of 85 s. It also checks that the reform simulation keeps the five inputs policyengine-us adds to the list. The changelog now names the household each measured value belongs to, says which inputs were already listed, and describes the trust-fund variables as covering Social Security and Medicare. Co-Authored-By: Claude Opus 5.5 --- changelog.d/781.fixed.md | 2 +- tests/test_us_dataset_input_variables.py | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/changelog.d/781.fixed.md b/changelog.d/781.fixed.md index 6f236ce61..ff967731d 100644 --- a/changelog.d/781.fixed.md +++ b/changelog.d/781.fixed.md @@ -1 +1 @@ -US simulations built from a dataset (`Simulation(dataset=...)`) list every stored ID and dataset column in the country simulation's `input_variables`, for the reform simulation and its baseline. Since 6.0.0 the US model constructs the country simulation empty and stores the dataset afterwards, and policyengine-core fills the list only at construction, so it held none of the dataset's columns: the baseline's list was empty, and the reform's held only the five behavioral-response inputs policyengine-us always adds (such as `employment_income_before_lsr`). `marginal_tax_rate` calculates in a copy of the simulation after deleting every value whose variable is not listed, so its copies lost age, location, Social Security and the other dataset inputs. `marginal_tax_rate` and `cliff_gap` therefore differed from policyengine-us's (a 67-year-old in Wake County, North Carolina, with $15,000 of earnings and $24,000 of Social Security in 2026 had a marginal rate of 23.64 and a cliff gap of $22,645, against 0.1174 and $0), and so did the labor-supply response, which reads marginal tax rates. All three now equal policyengine-us's for the same household. The federal, state and FICA marginal rates, `marginal_tax_rate_including_health_benefits` and the Social Security trust-fund revenue variables delete unlisted values in the same way, so they change too. +US simulations built from a dataset (`Simulation(dataset=...)`) list every stored ID and dataset column in the country simulation's `input_variables`, for the reform simulation and its baseline. Since 6.0.0 the US model constructs the country simulation empty and stores the dataset afterwards, and policyengine-core fills the list only at construction, so it held none of the dataset's ordinary columns: only mapped legacy inputs and legacy SPM declarations were listed, plus, in the reform simulation, the five behavioral-response inputs policyengine-us always adds (such as `employment_income_before_lsr`). `marginal_tax_rate` calculates in a copy of the simulation after deleting every value whose variable is not listed, so its copies lost age, location, Social Security and the other dataset inputs. A 67-year-old in Wake County, North Carolina, with $15,000 of earnings and $24,000 of Social Security in 2026 had a `marginal_tax_rate` of 23.64 and a `cliff_gap` of $22,645, against policyengine-us's 0.1174 and $0. A 40-year-old there with $40,000 of earnings, under a reform that raises the 12 % rate to 17 % with a labor-supply substitution elasticity of 0.25, had a labor-supply response of -$591.09, against policyengine-us's -$655.38. Both households now get policyengine-us's values. The federal, state and FICA marginal rates, `marginal_tax_rate_including_health_benefits`, and the variables that attribute income tax revenue to the Social Security and Medicare trust funds (`tob_revenue_total` and the others) delete unlisted values in the same way, so they change too. diff --git a/tests/test_us_dataset_input_variables.py b/tests/test_us_dataset_input_variables.py index 07dbc9e47..c6d3fcba6 100644 --- a/tests/test_us_dataset_input_variables.py +++ b/tests/test_us_dataset_input_variables.py @@ -56,14 +56,25 @@ } REFORM_PERIOD = f"{YEAR}-01-01.2100-12-31" -#: Raises the 12 % federal income tax rate to 17 % and turns on labor-supply -#: responses with a substitution elasticity of 0.25 for everyone. +#: Raises the 12 % federal income tax rate to 17 %. +HIGHER_RATE = {"gov.irs.income.bracket.rates.2": {REFORM_PERIOD: 0.17}} +#: The same, with labor-supply responses turned on: a substitution +#: elasticity of 0.25 for everyone. HIGHER_RATE_WITH_LABOR_SUPPLY_RESPONSES = { - "gov.irs.income.bracket.rates.2": {REFORM_PERIOD: 0.17}, + **HIGHER_RATE, "gov.simulation.labor_supply_responses.elasticities.substitution.all": { REFORM_PERIOD: 0.25 }, } +#: Inputs policyengine-us's ``Microsimulation.__init__`` adds to a +#: simulation's ``input_variables`` whether or not anything stores them. +POLICYENGINE_US_ADDED_INPUTS = { + "employment_income_before_lsr", + "self_employment_income_before_lsr", + "sstb_self_employment_income_before_lsr", + "weekly_hours_worked_before_lsr", + "long_term_capital_gains_before_response", +} def _one_adult_dataset(person_inputs: dict[str, float]) -> PolicyEngineUSDataset: @@ -168,13 +179,15 @@ def build_and_keep(self, microsim, dataset, system): simulation = Simulation( dataset=_one_adult_dataset(RETIREE), tax_benefit_model_version=pe.us.model, - policy=HIGHER_RATE_WITH_LABOR_SUPPLY_RESPONSES, + policy=HIGHER_RATE, ) simulation.run() # ``run`` builds the reform's baseline first, then the reform. baseline, reform = built_simulations assert baseline is reform.baseline + # Recording the dataset's columns keeps what the list already held. + assert POLICYENGINE_US_ADDED_INPUTS <= set(reform.input_variables) for country_simulation in (baseline, reform): variables = country_simulation.tax_benefit_system.variables stored_columns = {