Repository navigation
Conversation
…t simulation uc_deduction_random_draw, uc_deduction_type_random_draw and attends_private_school decided between microdata imputation and household defaults by testing whether total weight was below 1e6. policyengine.py builds constituency and local-authority simulations by filtering rows from the national data (RowFilterStrategy), so those fell below the threshold and got household defaults: no UC deductions and no private school attendance. Each now reads Simulation.built_from_dataset (added in #1899). filter_dataset carries each person's attends_private_school into an extract, since one household alone would rank at the 100th income percentile. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- attends_private_school no longer raises when no household has weight (the old 1e6 gate returned early; MicroSeries cannot rank zero weight). - The weight-scale property scales by powers of two, so invariance is exact; the national fixture carries national-scale weight, so the region test compares a national run with a constituency-sized one. - Replace the vacuous attends_private_school YAML cases with situation cases that fail under the old gate. - Document what filter_dataset carries, and that the changelog's filtered regions rank private school attendance locally. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…datasets - build_from_situation clears Simulation.built_from_dataset and build_from_ids (every data path) sets it, so a simulation rebuilt in place follows its new source. __init__ no longer sets it separately. - utils.data_source.built_from_data falls back to policyengine-core's is_over_dataset, so a core Simulation built over the UK system from data gets the imputations. The four gates (both UC draws, private school attendance, months_since_last_birthday) use it. - The region test's population now sits below the old threshold for people as well as benefit units, so its private school assertion fails under the old gate. New tests cover both rebuild directions and a core simulation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A calculated clone rebuilt from data kept core's simulation-level _fast_cache, so months_since_last_birthday (now enabled by the data-built flag) read a two-person person_weight against an 80-person population and raised. build_from_situation and build_from_ids now record the source and reset _fast_cache and _user_input_keys (a clone shares the latter with its original). The rebuild test calculates on a clone before rebuilding, in both directions. 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 #1902
Builds on #1899 (merged), which adds
Simulation.built_from_dataset. The branch has main merged in at 2fad4c0.Summary
Three variables chose between microdata imputation and household defaults by testing whether total weight was below 1e6:
uc_deduction_random_drawuc_deduction_type_random_drawattends_private_schoolperson.household(...)), so in effect peoplepolicyengine.py builds constituency and local-authority simulations by filtering rows from the national data.
src/policyengine/countries/uk/regions.pyusesRowFilterStrategyonconstituency_code_oa/la_code_oa.filter_dataset_by_household_idsinsrc/policyengine/utils/entity_utils.pykeeps rows without rescaling weights.run()insrc/policyengine/tax_benefit_models/uk/model.pywraps them inUKSingleYearDatasetand callsMicrosimulation. So every constituency and local authority fell below the threshold and got household defaults: no UC deductions, and no private school attendance even under a private school VAT reform.Each variable now gates on
built_from_data(simulation)(new,policyengine_uk/utils/data_source.py), which is true for any simulation built from data, however little weight it carries. It readsSimulation.built_from_datasetand falls back to policyengine-core'sis_over_dataset, so a coreSimulationbuilt over the UK system from data counts too. #1899'smonths_since_last_birthdaynow uses the same helper.The builders now set
built_from_dataset:build_from_situationclears it andbuild_from_ids, which every data path goes through, sets it. Previously only__init__set it, so a simulation rebuilt in place kept a stale value. The builders also drop core's simulation-level caches (_fast_cache,_user_input_keys), so a calculated clone rebuilt in place never reads arrays sized for its old population.attends_private_schoolalso loses a deadhasattr(person.simulation, "dataset")check (Simulation.datasetis a class attribute, so it always held).filter_datasetnow carries each person'sattends_private_schoolinto the household it extracts, as #1899 does formonths_since_last_birthday. A household alone ranks at the 100th income percentile (rate 0.47 × 0.85), so without this about 40% of children in an extract would be assigned to private school. UC draws need no carrying: they hashbenunit_id, which the extract keeps.attends_private_schoolalso no longer raises when no household has weight. The old gate returned before ranking, andMicroSeriescannot rank zero total weight. Households without weight stay at percentile 0, as before.The
attends_private_schoolYAML cases were vacuous under the new gate (situations always return False), so they are replaced with situation cases: a household with 1e9 of weight and the top income attends no private school unless set, and a set value is kept.clone()andget_branch()copy the instance__dict__, so the flag survives intobaselineand branch simulations.Invariants (stated and tested)
policyengine_uk/tests/test_data_built_imputations.py:uc_has_deduction,uc_deduction_combination,uc_deductionsandattends_private_schoolunchanged (Hypothesis). Powers of two scale exactly in floating point, so the property holds exactly and can't flake on percentile boundaries.splitmix64_uniform(benunit_id)draws, some deductions and some private school attendance.filter_datasetextract reproduces the full simulation's UC deductions and private school attendance, and the test asserts both sets are non-empty.Simulation(tax_benefit_system=system, dataset=<DataFrame>)gets hashed draws and private school attendance.All eight fail on the pre-PR code (325d585), and the first new YAML case fails there too. Targeted checks, each run with one fix removed:
filter_datasetcarry.__init__, and with the cache reset removed (it reproduces the broadcast error).is_over_datasetfallback.Intended exception: private school attendance is not row-filter invariant. It ranks incomes within the simulated population, so a constituency ranks against itself (see caveats).
Constituency and local-authority runs, before and after
Real runs, following policyengine.py's path: filter the national tables by
constituency_code_oa/la_code_oa/region, buildUKSingleYearDataset+Microsimulation, and calculate 2026.632 constituencies (sums over constituency runs)
363 local authorities
months_since_last_birthdayspreads birthdays within the simulated population. UC paid differs by more than 0.1% in 168 constituencies. I did not trace every case.Caveats and follow-ups
shareholdingandcorporate_land_value. In a filtered constituency this puts the whole national total on the constituency. For E14001063 (87 records),corporate_tax_incidenceis £34,863m in the constituency run vs £28m for the same households nationally, andbusiness_ratesis £31,733m vs £25.6m. Household net income is −£35,634m vs £2,328m. This affects every constituency run in policyengine.py (follow-up task). The net-income figures above are differences, in which it cancels.Tests run
test_data_built_imputations.py: 8 passed.test_state_pension_age.py+test_uc_deductions.pyat the latest head: 41 passed. Labour,is_SP_ageandstate_pension_ageYAML: 45 passed.test_uc_deductions.py+test_state_pension_age.py: 37 passed.contrib/labour/attends_private_school.yaml+private_school_vat.yaml: 5 passed.ruff format/ruff checkclean.axiom: n/a: microsimulation imputation
🤖 Generated with Claude Code