Skip to content

List a US dataset's columns in input_variables, so marginal tax rates, cliff gaps and labor-supply responses match policyengine-us - #783

Merged
anth-volk merged 4 commits into
mainfrom
v2plan/d5-11-record-dataset-inputs
Dec 2, 2026
Merged

anth-volk merged 4 commits into
mainfrom
v2plan/d5-11-record-dataset-inputs

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Fixes #781

What changes

  • _build_simulation_from_dataset (src/policyengine/tax_benefit_models/us/model.py) collects the live ID names and dataset columns it stores with set_input. It then adds them to the country simulation's input_variables with legacy_inputs._record_input_variables, the helper the legacy input renames and the SPM declarations already use.
  • run calls _build_simulation_from_dataset for the reform's baseline and then for the reform simulation, so both lists are updated. policyengine-core gives each its own list.
  • Why it's needed:
    • Since 6.0.0 (62d8eb98, Add canonical SPM selection, receipts and native loading to the wrapper #515), PolicyEngineUSLatest.run constructs the policyengine-us Microsimulation empty (situation={}) and stores the dataset afterwards.
    • policyengine-core fills input_variables only in Simulation.__init__, so the baseline's list was empty. The reform's list 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. Its copies therefore lost age, state, county, Social Security and every other dataset input.
  • Docstrings and changelog.d/781.fixed.md. No other path changes:
    • create_datasets constructs its Microsimulation with the dataset passed in.
    • The US household path passes a full situation at construction.
    • The UK model passes its dataset at construction.

Effect on results

For a one-adult household in Wake County, North Carolina, in 2026 (policyengine-us 2.2.2, policyengine-core 3.34.10):

Household Output Before After policyengine-us Simulation(situation=...)
67, $15,000 earnings, $24,000 Social Security marginal_tax_rate 23.6449 0.1174 0.1174
cliff_gap 22,644.95 0 0
40, $40,000 earnings, $10,000 interest; 12 % rate raised to 17 %, substitution elasticity 0.25 reform's marginal_tax_rate 8.1558 0.2871 0.2871
employment_income_behavioral_response -591.09 -655.38 -655.38

The federal, state and FICA marginal rates, marginal_tax_rate_including_health_benefits and the Social Security trust-fund revenue variables delete unlisted values the same way, so they change too. They have no tests of their own here: the first test below checks the list they all read.

Tests

New module tests/test_us_dataset_input_variables.py. Each test runs one adult through Simulation(dataset=...) from in-memory tables, with no downloads.

  • test_reform_and_baseline_list_every_stored_dataset_column_as_an_input: after a run with a reform that raises the 12 % rate, the reform simulation and its baseline both list every stored ID and dataset column in input_variables, and the reform keeps the five inputs policyengine-us adds.
  • test_retiree_cliff_gap_and_marginal_tax_rate_match_policyengine_us: the retiree's cliff_gap is 0, and marginal_tax_rate equals policyengine-us's for the same inputs, computed in the test.
  • test_labor_supply_response_matches_policyengine_us: the worker's labor-supply response under the reform equals policyengine-us's.

Local results, each test run by node ID:

  • On the test commit 1fd0f4f, before the change, all three fail on their assertions:
    • The baseline is missing 22 stored columns.
    • cliff_gap is 22,644.95.
    • The response is -591.09 against -655.38.
  • On 459bba3 all three pass.
  • 84ddfe6 switched the first test to a reform that only changes the rate (18 s instead of 85 s) and added the check on the five inputs. With that file, the first test fails on its assertion on 1fd0f4f (22 columns missing) and passes on 84ddfe6.

ruff format and ruff check are clean on the changed files. mypy reports eight errors in us/model.py, all on lines this PR doesn't change; mypy is informational in this repository's CI. towncrier check passes.

Out of scope

Review

Two rounds of adversarial review before this PR was opened. The second found no bugs, so there was no third.

  • Round 1 (459bba3): no high- or medium-priority bugs; two low-priority ones.
    • Low, fixed in e813c88: the changelog overstated two things.
      • The capital-gains response deletes only unlisted variables that have a formula, so its copies never lost the dataset's inputs.
      • The reform's list always held the five inputs policyengine-us adds.
    • The same commit corrected three docstrings: _record_input_variables had claimed that policyengine-core's derivative reads the list, and the new docstring in _build_simulation_from_dataset and the test module's docstring had claimed more than the code and tests do.
    • Low, left: outputs saved by .py 6.0.0 to 6.20.4 under a fixed Simulation.id are still reused by Simulation.ensure(), because the US output loader keeps no record that would mark them as calculated before this fix. Fixing it needs a new record in saved US output files and a check on load.
      • Runs with the default random ID are not affected.
      • The simulation service's precomputed baselines include the .py version in their ID, so they aren't either.
  • Round 2 (e813c88): no bugs.
    • It reran the three tests by node ID: they fail before the change and pass after it.
    • Its notes led to 84ddfe6: the changelog now names the household each measured value belongs to, and the first test is faster and also checks the five added inputs.

Bugs left: the low-priority saved-output one above.

Merge order

This is tracker task D5 · 11 of the API v2 migration. It builds on main, not on another PR's branch. Every earlier task is complete. PolicyEngine/policyengine-api#4377 (D5 · 9: API v1 on .py 6.20.4) has merged, and being merged is enough: it changes only API v1's .py pin, and this PR needs nothing it deploys. Once released, the simulation service and API v1 take the new version in their own PRs.

🤖 Generated with Claude Code

anth-volk and others added 4 commits December 2, 2026 01:06
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
…gelog 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 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review December 2, 2026 16:16
@anth-volk
anth-volk merged commit 4e5542f into main Dec 2, 2026
13 checks passed
anth-volk added a commit to PolicyEngine/policyengine-sim-api that referenced this pull request Dec 2, 2026
Both policyengine[models] pins in the executor (its dependencies and the
modal-simulation-image group) and uv.lock move from 6.20.4 to 6.20.5,
which lists a US dataset's stored columns in input_variables
(PolicyEngine/policyengine.py#783). Its bundle changes only version
labels: policyengine-core, policyengine-us, policyengine-uk and the data
releases are unchanged. The Stage 12 cliff test now gives policyengine-us's
cliff gap and share for both households.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant