Repository navigation
Conversation
Fixes #732 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Group IDs numbered within households merge strangers, and rows of one group that disagree lose all but the first person's value. Neither raises at load. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The pinned loader sorts groups by ID but reads a one-row-per-group column in
row order, so one-person groups in any other order took each other's values.
Number memberships in file order, and keep the file's own IDs in {group}_id so
results merge back on them.
Check geography across every year the file holds, including county names and
tract, block and district columns, and drop stored copies of what the model
derives from the state. Record the year, state and a digest with the run, and
rewrite the recipe around what the trial found unclear.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #732
Adds
pe.us.flat_file_microsimulationandpe.us.prepare_flat_file, which run a researcher's own flat file through one state's rules or through the file's own states, plus a simulated instruments recipe atdocs/simulated-instruments.md.Why here
policyengine.pyis the interface researchers should build on. The model fixes in PolicyEngine/policyengine-core#646 and PolicyEngine/policyengine-us#10574 cover direct use, but this package pinspolicyengine-core==3.33.3andpolicyengine-us==2.2.1, and those fixes will not reach it soon. So the helper works at those pins by itself. No pin moves in this PR, and no bundled number changes.What it does
prepare_flat_filechecks a file and writes it in the one form the pinned loader reads correctly:variable__period, and every ID moves to each person's row. At the pinned core, a barehousehold_idcolumn crashes the load with anIndexError.person_{group}_idnumbers groups in file order, and{group}_idkeeps the file's own IDs. The pinned loader sorts groups by ID but reads a one-row-per-group column in row order, so a file of one-person groups in any other order gave each group another's state and weight. File order makes the two agree for every entity at once. Keeping the file's IDs in{group}_idmeans results merge back on them.person_idgets one.county_fipsbecomes five-digit text from 6037, 6037.0 or "6037".state=, it setsstate_fipsin every year the file holds, and clears stored state labels, geography finer than a state, and what the model derives from that geography.state_group, which otherwise hide the formula: a stalestate_groupkept Alaska on the contiguous poverty guideline.state=, it requiresstate_fips, and refuses a county, tract, block, congressional district or state label that contradicts it in any year the file holds, including a column carried from another year and the latest storedcountythat the model reads from any year.It refuses, with a message that names the fix, a file that has no rows, no
age, no group ID, a blank ID, a repeatedperson_id, group IDs numbered within households, or rows of one group that disagree on a group-level variable. The last two matter because they load without an error: the loader merges those people or takes the first person's value.flat_file_microsimulationthen builds aMicrosimulationwith a resolved SPM selection and stampspolicyengine_bundle, recordingflat_fileas the runtime dataset, plus the year, the state and a SHA-256 digest of the prepared file.Decisions
spm={"geography_kind": "national"}, and the recipe passes it. The helper does not change the bundle's default.state=clears every household's county, including households already in that state. Every state then runs the same population on the same terms, which is the point of the instrument. To keep real counties, leavestateout.Invariants
Tests
tests/test_us_flat_file.py: 80 passed at the pinned versions, including realMicrosimulationruns from a CSV.Real-data check
A 300-household Populace US sample with 822 people, read back from CSV, ran through the helper for CA, MS, MN, DC and NY, and through its own states.
state_codewas right in each, and national SPM thresholds matched across states. It logged five columns the pinned model does not know, as designed.Differential at the pinned versions: the helper with
state="MN"gives the same SNAP eligibility, Medicaid eligibility, income, state tax and net income as the same file loaded directly with geography dropped by hand. Means match, and values match once sorted, because group results follow file order.axiom: n/a: microsim input handling, no policy change
🤖 Generated with Claude Code