Skip to content

Hold every rare SPI output's carriers in stage 1's donor frame, and calibrate its gates by level (#1276) - #1584

Merged
juaristi22 merged 13 commits into
mainfrom
uk-spi-stage1-donor-frame-1276
Nov 12, 2026
Merged

juaristi22 merged 13 commits into
mainfrom
uk-spi-stage1-donor-frame-1276

Conversation

@juaristi22

@juaristi22 juaristi22 commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Part A of #1276. #1583 is merged and this PR now targets main. It also carries the strict LCWRA proxy check from #1583's review (Nit 7).

What changes

QRF sign gates (microcosm-fit)

  • gate_calibration: cross_fitted_balanced: each fold gate is fitted on class-balanced weights, so a class with a tiny share of the weight still shapes how the gate ranks rows. The out-of-fold calibration stays on the rows' own weights, so the calibrated rates do not change. The fold gates say which rows; the calibration says how many.
  • gate_level_columns: the cross-fitted calibration also gives each supported level of the named predictor columns an offset of its own.
    • An entry is a column, or a one-hot categorical's columns as a group. In each, the level with the most weight is the reference, so the offsets are identified. A level with fewer than 30 rows of some sign class shares the reference's offsets.
    • The calibrated gate then holds each class's weighted rate within every offset level, not only over the whole frame. Where that balance can't be met, the calibration falls back to the one without levels.
    • FittedRegimeGatedQRF.gate_levels() names each target's offset levels. gate_level_reports() records what each level fit came to, with each column's reference and the thin levels. The checkpointed chain refuses level columns for now.

SPI stage 1 (hmrc_spi_income_spine)

  • The donor frame declares sample_design: rare_carriers_certain. An output is rare when its tape carriers number fewer than a quarter of the frame's rows, and every record that carries a rare output is in the frame. The other rows are drawn in proportion to FACT without replacement, each weighted at FACT over its inclusion probability.
  • The gates declare cross_fitted_balanced and gate_level_columns: [spi_age_band, main_source, has_pay, has_trade]. Each categorical predictor's one-hot columns go to the gate as one group, and the stage refuses a level column that is not a stage-1 predictor.
  • The FRS reference forests that place each copy's draw (UK spine: composition-faithful SPI income draw — bridge predictors and rank-preserving replacement (with-children UC residual) #840) take the same gate setting. A copy's sign uniform is its twin's position in the reference gate, so the copies keep the tape gate's rate within a level only where the reference gate holds the FRS rows' rate there.
  • New stage evidence:
    • donor_frame: its counts of tape records, rounded as every stage receipt's are, and each output's share of the tape's carriers the frame keeps;
    • gate_levels: the levels each fit's gates offset, what each level fit came to and the settings behind them, counted over the folds for the reference forests.

CGT asset types (hmrc_cgt_asset_type_spine)

  • The logistic slope solve, for the residential flag and for each Business Asset Disposal Relief band, now widens its bracket, doubling up to ±48, only where [-12, 12] cannot reach the published gains. Every solve that fits inside the default bracket is unchanged.
  • This stage reads stage-1 incomes, so new stage-1 draws can push a band past the default bracket. A build of this branch from before the gate level offsets needed a slope past 12 for the £500,000 to £999,999 BADR band, and its spine stopped at this stage. At 3457881 that band solves inside [-12, 12], so there the wider bracket is a guard.

Spec: gate_level_columns on the stage-1 operation in sources.yaml, source_stages.json and the schema. The stage notes describe the frame and the gates.

Why

  • On main the stage-1 frame is a FACT-weighted bootstrap of the tape, so the rarest outputs have few carriers in it. Their gates can fall back to flat class rates (see gate_outcomes), and their amounts come from few donors.
  • A gate calibrated over the whole frame can sit above the rate in some levels and below it in others while matching it overall. The level offsets hold each level's rate. That includes the composite records' age band, which no FRS copy belongs to.

How it was checked

  • Engine-free tests cover the balanced gate, the level offsets (including household-grouped folds, as the reference forests are fitted), the donor frame and its encoding, the stage's parameter checks, and the spine wiring. On this stack the focused set passes: 603 tests, 1 skipped.
  • For the CGT bracket: a target past the default bracket solves and meets both its count and its gains, and a target inside the bracket never leaves it.
  • Re-pinned at 3457881: fit.qrf graph parity, the UK release-input coverage manifest, the UK spine fixture (H2) and the US F0 spec pins.
  • Held-out trials on the tape and local builds were run on licensed data. Their results stay local.

Head build

  • This branch's spine and national calibration, built at 3457881 on licensed data, pass all 58 spine gates and all 7 terminal gates. Its other results stay local, like the trials'.

Not in this PR

🤖 Generated with Claude Code

juaristi22 and others added 8 commits November 12, 2026 10:27
GateCalibration.CROSS_FITTED_BALANCED fits each fold gate on class-balanced
weights and keeps the calibration on the rows' own weights, so a class that
holds a hundredth of a percent of the weight is ranked and still drawn at its
weighted rate. The in-sample and cross-fitted gates do not move.

Part of #1276. Local measurement arm: no pin, fixture or changelog yet.

Co-Authored-By: Claude <noreply@anthropic.com>
The stage-1 forest's 100,000 donor rows were drawn from the tape in proportion
to FACT with replacement and fitted at uniform weight, which keeps a few
percent of the records that carry the rare outputs. The frame now takes every
record carrying an output that fewer than a quarter of the frame's rows carry,
fills the rest in proportion to FACT without replacement, and fits each row at
FACT over its inclusion probability. The manifest declares the design
(sample_design: rare_carriers_certain), and the source contract holds the
replacement flag and the fit weight to it.

Part of #1276. Local measurement arm: notes, pins, fixtures and changelog are
not updated.

Co-Authored-By: Claude <noreply@anthropic.com>
Stage 1 and the FRS reference forests that take its declaration move to
gate_calibration: cross_fitted_balanced. Stage 2 keeps cross_fitted.

Part of #1276. Local measurement arm.

Co-Authored-By: Claude <noreply@anthropic.com>
The residential flag and each BADR band solve a logistic slope by bisection
inside [-12, 12]. With #1276's stage-1 draws, the BADR GBP 500,000 to 999,999
band needs a slope just past 12, so the spine stopped at
hmrc_cgt_asset_type_spine. The bracket now doubles, up to 48, only when the
default one cannot reach the target, so every solve that fits inside it is
unchanged. Tests: a target past the default bracket solves and meets both
targets; a target inside it never leaves it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One slope and one offset per sign class hold each class's weighted rate over
the whole frame and over no part of it. On the SPI tape the composite records
(age band -1) hold most of some rare outputs' carriers but little weight; a
gate that cannot separate them fully has its shortfall there spread onto every
other row, so the FRS copies, which are never composites, over-draw those
outputs (microcosm#1276).

RegimeGatedQRF(gate_level_columns=...) gives each supported level of the named
predictor columns offsets of its own in the cross-fitted calibration, fitted
on the same out-of-fold scores and the rows' own weights with a small ridge.
The calibrated probabilities then average each class's rate within every
offset level. A level needs 30 rows of every sign class; thinner and unseen
levels take their column's reference offsets. Without level columns, or with
none supported, or if the level fit's balance fails, the calibration is the
one without levels, unchanged. The checkpointed chain refuses level columns
for now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nd trade

Stage 1 declares gate_level_columns (spi_age_band, main_source, has_pay,
has_trade). The stage maps a categorical predictor to all of its one-hot
columns, so each supported level, the composite records' age band among them,
gets calibration offsets of its own. Both the tape forest and the FRS
reference forests take them. A copy's sign uniform is located in its twin's
reference gate, so the copies keep the tape gate's rate in a level only if
the reference gate holds the FRS rows' rate there. Stage 2 is unchanged.

FittedRegimeGatedQRF.gate_levels() names the levels each target's gate
offsets. The stage evidence records them under gate_levels: the declared
columns, each tape target's levels, and for the reference folds how many
folds gave each level an offset. The schema accepts the key. The stage
refuses level columns that are not stage-1 predictors. The stage notes now
describe stage 1's donor frame, its balanced gate and its level offsets.

The UK gate test expected stage 1 to declare cross_fitted, a leftover from
the balanced-gate commit; it now expects cross_fitted_balanced and checks
that only stage 1 declares level columns. The spine test's QRF double takes
the new keyword.

Part of #1276. Local arm: graph-parity, coverage-manifest and seed pins and
the changelog are not updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The FRS reference forests fit their gates with each household's members in
one fold, so the level calibration now has a test on grouped folds: the
outcome stays cross-fitted, the receipt names the level, and both levels keep
their rate on fresh rows.

Part of #1276. Local arm.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vahid-ahmadi

Copy link
Copy Markdown
Contributor

Review of 350ae4a. I checked the diff against uk-event-data-pr3 (#1583's changes left out), focusing on the statistics of the balanced gate, the level offsets and the donor frame. I ran the PR's engine-free tests (the two new microcosm-fit files, test_uk_spi_donor_frame, test_uk_qrf_gates, test_uk_cgt_asset_type, test_uk_spi_spine and the income source manifest: 114 passed) plus my own synthetic checks on the fit functions, with no SPI or FRS data.

Must

  1. The donor_frame receipt carries exact counts of tape records (uk_runtime/spi_income.py:2086-2097, written to stage evidence at spi_spine.py:500).
    • rows_carrying_a_rare_output, rows_sampled_in_proportion_to_fact and sampled_rows_taken_with_certainty are raw int(...) counts. Your 15 October comment on UK SPI stage 1 keeps 4–17% of the tape's donors; replace the removed QRF tail gate with a donor-fidelity check #1276 asks to "keep raw carrier and independent-draw counts out of committed gate notes and out of any report that ships with a release", and rows_carrying_a_rare_output is a carrier count. receipt_counts.py also says stage receipts can reach a release manifest, and its rule (UK: move the LCFS stage to the Living Costs and Food Survey 2024-25 #1164, floor of 3) puts every count of licensed records through rounded_count or distinct_count.
    • Failure case: sampled_rows_taken_with_certainty is the number of non-carrier records whose FACT share caps their inclusion at one, so it can be 1 or 2. The first two also add up to sample_size exactly, so rounding one alone gives the other back by difference.
    • Fix: pass each count through rounded_count (or report them as floored_shares of the frame), and round the two that sum to the frame size together. test_the_frame_holds_every_rare_carrier_once_at_the_tape_incidence asserts the exact count, so it changes too.

Should

  1. For a categorical level column, no reference level is dropped, and the ridge does all the identifying (qrf.py:598, spi_income.py:3487 and :3516).

    • The stage one-hots with drop_first=False, and _encoded_gate_level_columns passes every dummy. _select_gate_levels then treats each dummy as its own 0/1 column, so the "reference" is the heavier of 0 and 1 within that dummy, not the heaviest category. When every category clears 30 rows per class, the indicators plus the class offset are rank-deficient. In a synthetic 4-level partition, [1 | indicators] had rank 4 of 5. Only _GATE_LEVEL_RIDGE pins the split between the class offset and the level offsets.
    • The balance still holds in that case (largest within-level gap 8e-9), so draws are fine. But the PR body's "the level with the most weight is the reference" describes binary columns only, and the receipt names flip with the category mix: the same partition records cat_0=0 when category 0 holds 60% of the weight and cat_0=1 when it holds 40%.
    • Fix: drop one dummy per categorical, chosen by weight, so the model is identified without the ridge and the receipt names a true reference. _encoded_gate_level_columns has no weights, and dropping the first dummy in sorted order would drop spi_age_band_-1.0, the composite band the PR wants offset. So the choice has to happen where weights are known: pass the dummies of each categorical to _select_gate_levels as one group and drop the heaviest there. Otherwise, say in the docstring and the stage notes that categorical levels are identified by the ridge.
  2. When the level fit misses its balance, all levels drop silently (qrf.py:854, ridge at :187, tolerance at :192).

    • The ridge leaves a gap of about ridge × |offset| / level weight (in mean-one weights), so a level with little weight can fail the 1e-5 tolerance. Synthetic case: 80 rows in a level, half of them carriers, 20,000 rows overall. At a weight ratio of 30 the gap is 1.3e-6 and the level is kept. At 1,000 the gap is 4.4e-5, ok is false, and the whole gate falls back to the calibration without levels.
    • The cost is large. On 200,000 fresh rows from the same process, the gate kept at ratio 30 drew the level at 0.42 against a true 0.52. The fallback at ratio 1,000 drew it at 0.023. So a 4e-5 miss throws away a fit that was close, for one that is off by about 0.5.
    • The outcome stays cross_fitted, and gate_levels() is empty, which is what a gate with no supported level also shows. Every level is dropped, not only the light one, and nothing in the receipt says the level fit was tried and failed. A level below 30 rows is also recorded only by its absence: gate_levels lists the offset levels, never the declared ones left out.
    • I can't say whether any real SPI level sits near that ratio, but the receipt should be able to show it.
    • Fix: record the failed level fit (a GateOutcome, or a level_fit_failed entry in gate_levels evidence), and list the declared levels too thin for an offset. Consider a tolerance relative to the level's rate, or a ridge scaled by the level's weight. Put _GATE_LEVEL_MIN_ROWS and _GATE_LEVEL_RIDGE in that evidence too, since both are module constants with no manifest parameter. Fix 2 also shrinks the problem, since an identified fit needs no meaningful ridge.
  3. The tests pin the claimed properties loosely or not at all (test_qrf_balanced_gate.py:88, test_qrf_gate_levels.py).

    • Balanced gate: the only rate check is 0.5 < drawn / rate < 2.0 on fresh rows. Checked on the out-of-fold scores, the property is exact: in my run the weighted mean over the rate was 1.000000 at seeds 11, 13 and 17.
    • Levels: every level test uses one binary column. Nothing fits the production case, a full one-hot partition with a supported level for every category, and nothing covers the fallback in 3.
    • Fix: capture the out-of-fold scores (wrap _fit_gate_calibration, which the tests already import) and assert the balanced gate's weighted rate to 1e-6. Add a 4-category one-hot test asserting the within-level balance and the receipt names, and a test that forces ok=False and checks what the receipt shows.
  4. The receipt doesn't give the per-output donor-pool shares that part A of UK SPI stage 1 keeps 4–17% of the tape's donors; replace the removed QRF tail gate with a donor-fidelity check #1276 asks for (spi_income.py:2086-2097). The body says every rare carrier is now in the frame, and the receipt lists rare_outputs, but it doesn't say how the 4–17% table moves for the outputs that stay sampled.

    • On a synthetic tape (60 draws, frame 15,000 of 60,000), the frame keeps every rare carrier against 2% and 22% under the bootstrap. A sampled common output also gains, because no record repeats: pay goes from 0.215 to 0.250 and dividends from 0.020 to 0.045.
    • Fix: add each output's share of tape carriers kept (floored_share) to the receipt. It then answers part A directly and lets the head-build comment quote it.

Nit

  1. Under cross_fitted_balanced, a gate scored on its own donor rows draws the rare class well under its rate. Fresh rows don't show this. In the weight-1-against-400 case of test_qrf_balanced_gate.py, the weighted mean on the training frame was 0.57 to 0.63 of the rate, against 0.87 to 0.99 for plain cross_fitted. Release builds score the reference gates out of fold (cross_fit_transport), so this reaches only sampled rungs and synthetic fixtures, where cross_fit_transport may locate a fold in-sample (spi_rank_transport.py:340 and :368). One line in the CROSS_FITTED_BALANCED docstring would warn the next caller.

Checked and fine

  • The balanced weights are built from each fold's training labels only, so nothing leaks across folds. _can_cross_fit (at least 5 rows or groups per class) and _folds_carry_every_class (positive training weight) run first, so no class has zero rows or zero weight when the balancing divides.
  • The calibration on the rows' own weights restores the weighted class rate: out of fold exactly, and on fresh rows the deployed gate gave 0.99 to 1.13 of the rate, against 0.995 to 1.09 for plain cross_fitted at the same seeds.
  • The level offsets do what the body says. Without them, fresh-row rates by category were 0.37 to 2.03 of the truth. With them they were 0.96 to 1.29, and the overall rate didn't move. When only one category is thin, it is balanced as the remainder.
  • The donor frame is Horvitz-Thompson unbiased. Over 60 draws, the weighted total over the tape averaged 1.00 for every output, and the rare outputs' totals were exact. The forests' own bootstrap is in proportion to weight, so the certainty rows at FACT don't overweight the common outputs' forests. The earlier design's equal-weight frame isn't needed for that.
  • The CGT bracket only widens when [-12, 12] can't reach the target, so every solve inside it is unchanged.

CI at head: no checks ran. test.yml triggers only on pull requests to main, and this PR targets uk-event-data-pr3, so CI will first run when it is retargeted.

Verdict: the statistics hold up. Item 1 needs fixing before merge, and 2 to 5 would make the claims checkable from the receipt and the tests.

🤖 Generated with Claude Code

…evel fits

Addresses the review of 350ae4a on #1584.

- The donor_frame receipt's counts of tape records go through rounded_count,
  as every stage receipt's do (microcosm#1164). The sampled count is the
  frame's rows less the rounded carrier count, so the pair gives nothing back
  by difference; the whole-tape branch rounds its record count, and the
  effective rows are rounded too.
- gate_level_columns takes a one-hot categorical's columns as one group, and
  its heaviest category is the reference, so the level offsets are identified
  without the ridge, which drops to 1e-10 and only guards levels of two
  columns that mark the same rows. SPI stage 1 passes each categorical's
  one-hot columns as a group.
- A gate records what its level fit came to (GateLevelFit: offsets,
  no_supported_level or balance_not_met), the levels it offset or tried, each
  column's reference and the levels too thin for offsets of their own
  (FittedRegimeGatedQRF.gate_level_reports, gate_level_settings). The UK
  evidence records them per fit and pooled over the reference folds.
- The receipt gives each output's share of the tape's carriers the frame
  keeps, floored to the hundredth.
- Tests pin the balanced gate's out-of-fold rate to 1e-6 at three seeds; a
  four-category one-hot fit's within-level balance, identification and
  receipt names under two category mixes; a thin category; a forced balance
  failure; a fit with no supported level; and group validation.
- CROSS_FITTED_BALANCED's docstring says that scored on its own training
  rows a balanced gate draws a rare class under its rate.

The re-pins (graph parity, coverage manifest, H2, US F0) follow once #1583
settles.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@juaristi22

Copy link
Copy Markdown
Collaborator Author

Thanks, Vahid. All six are addressed in 9251dcf.

  1. Donor-frame counts (Must). Every count of tape records in the donor_frame receipt now goes through rounded_count. rows_sampled_in_proportion_to_fact is the frame's rows less the rounded carrier count, so the two sum to the frame size and neither gives the other back by difference. The whole-tape branch's record count and kish_effective_rows are rounded too. The test now checks the rounded values and the sum.

  2. One-hot levels. gate_level_columns now takes a one-hot categorical's columns as one group, and SPI stage 1 passes age band and main source that way.

    • In each group the heaviest category is the reference. _select_gate_levels picks it where the weights are known, so the composite band can still be offset and the model is identified without the ridge.
    • The ridge drops to 1e-10 and only guards against levels of two columns that mark the same rows.
    • Receipt names no longer flip with the category mix: the other categories are always <column>=1.
    • One behaviour change to flag: a thin category now shares the reference's offsets, as documented. Before, it was balanced as the remainder when it was the only thin one.
  3. Silent fallback. Each gate now records what its level fit came to (GateLevelFit: offsets, no_supported_level or balance_not_met), with the levels it offset or tried, each column's reference and the thin levels.

    • FittedRegimeGatedQRF.gate_level_reports() returns them, and gate_level_settings() gives the minimum rows, the ridge and the tolerance.
    • The UK evidence gate_levels now carries level_fits (per fit, pooled over the reference folds) and settings.
    • With the design identified, the ridge-driven gap you measured shrinks by four orders of magnitude, so I kept the absolute tolerance.
  4. Tests. New tests cover:

    • the balanced gate's out-of-fold weighted rate, to 1e-6, at seeds 11, 13 and 17;
    • a four-category one-hot fit: full rank, within-level balance to 1e-5, and the reference named;
    • the receipt names under two category mixes;
    • a thin category;
    • a forced ok=False, where the receipt says balance_not_met;
    • a fit with no supported level;
    • group validation.
  5. Donor-pool shares. The receipt now has tape_carriers_kept_share: each output's share of the tape's carriers the frame keeps, as a floored_share.

  6. In-sample warning. Added to the CROSS_FITTED_BALANCED docstring.

Locally, the focused engine-free set passes (603 tests, 1 skipped), as does microcosm-fit's engine-free suite (223). As you note, CI waits for the retarget to main. The re-pins (graph parity, coverage manifest, H2, US F0) follow once #1583 settles, since its next head moves the H2 fixture and the coverage manifest again.

@vahid-ahmadi

Copy link
Copy Markdown
Contributor

Round 2 at 9251dcf. I read the diff from 350ae4a and re-ran round 1's checks on a fresh clone: the PR's engine-free tests (the two microcosm-fit gate files, test_uk_spi_donor_frame, test_uk_qrf_gates, test_uk_cgt_asset_type, test_uk_spi_spine and the income source manifest: 127 passed), plus synthetic checks on the frame and the gate fits. I used no SPI or FRS data.

  1. Must, donor-frame counts: fixed. Every count of tape records in the receipt now goes through rounded_count, including the whole-tape rows and kish_effective_rows. The exception is the sampled frame's rows, which is the configured frame size (100,000, checked at spi_spine.py:1309). So the sampled count, rows less the rounded carrier count, gives back only the rounded figure. On synthetic tapes with 1, 2, 7 and 1,234 rare carriers at a frame of 15,000, rows less the sampled count came to 0, 0, 0 and 1,200. tape_carriers_kept_share is 1.0 for a rare output, so any exact per-output carrier count in the frame would now equal the tape's. I found none: the other SPI receipts count FRS rows, age cells or band pools, not an output's carriers in the frame or the union.
  2. Should, one-hot reference: fixed. I re-ran round 1's rank check on a 4-category partition passed as one group. [1 | indicators] now has full rank (4 of 4, where round 1 found 4 of 5). The largest within-level gap is under 2e-13. The reference is chosen by weight, not row count: with category 0 holding the most rows but category 2 half the weight, the receipt names cat_2=1 as the reference. Names no longer flip: cat_0=1 is the reference at both 60% and 40% of the weight, where round 1 got cat_0=0 and cat_0=1.
  3. Should, silent fallback: fixed. On round 1's case (80 rows in a level, half carriers, 20,000 rows), the within-level gap at a weight ratio of 1,000 is now 2.7e-9, against 4.4e-5 before, so the level is kept. It first misses the 1e-5 tolerance somewhere between a ratio of 1e5 (gap 2.7e-7) and 1e7 (gap 2.7e-5). There, gate_level_reports() gives balance_not_met with the level it tried and the reference. A level under 30 carriers is listed under thin_levels, and gate_level_settings() records the 30 rows, the 1e-10 ridge and the 1e-5 tolerance. gate_outcomes still says cross_fitted in that case, so a reader has to look in level_fits to see it. That's fine now that the receipt carries it.
  4. Should, tests: fixed. The balanced gate's out-of-fold rate is pinned to 1e-6 at seeds 11, 13 and 17. The 4-category test asserts full rank and within-level balance to 1e-5. The forced ok=False and no-supported-level cases are covered, and so is a thin category. Two small gaps remain (item 8).
  5. Should, per-output shares: fixed. tape_carriers_kept_share gives each output's share of its tape carriers that the frame keeps, as a floored_share, with None for an output no record carries.
  6. Nit, in-sample warning: fixed in the CROSS_FITTED_BALANCED docstring and the changelog.

New, both Nit

  1. In the banded branch, the sampled count can exceed the frame (uk_runtime/spi_income.py:2113-2117). When the carrier count prints as "<3" or "3-9", rows_sampled_in_proportion_to_fact is rounded_count(budget). At a frame size that isn't a multiple of 100, it can be larger than rows. At 15,055 rows with 2 carriers, I got 15,100 sampled. The rounding boundary can also split the "3-9" band: at that size, 5 or fewer carriers give 15,100 and 6 or more give 15,000. The committed 100,000 never hits this, so it only reaches fixtures and off-manifest sizes. Fix: in that branch record the sampled count as the frame's rows (it's within 9 of them), or leave it out.
  2. Two tests could pin more (test_qrf_gate_levels.py:377, test_uk_spi_donor_frame.py).
    • Both category mixes in the receipt-name test keep cat_0 heaviest by rows and by weight. So a reference chosen by row count, or by position, would also pass. Add a mix where a later category holds the most weight but not the most rows.
    • The donor-frame test accepts any rounded value for sampled_rows_taken_with_certainty, and no fixture has fewer than 10 rare carriers, so the banded branch in 7 is untested.

Checked and fine

  • Two level columns that mark the same rows (a one-hot category plus a copy of it as its own column) still fit with finite offsets and meet the balance under the 1e-10 ridge.
  • The thin-category behaviour change you flagged: a thin category now pools with the reference, and thin_levels names it, so the receipt shows where that happens. Every SPI stage-1 predictor is categorical or binary, so thin_levels never falls back to a bare count there.
  • pooled_gate_level_reports counts folds, not records.

CI at head: no checks ran. test.yml triggers only on pull requests to main, and this PR targets uk-event-data-pr3, so CI will first run after the retarget.

Verdict: all six round 1 items are addressed. The two new nits are small, and neither blocks merge.

🤖 Generated with Claude Code

juaristi22 and others added 3 commits November 12, 2026 12:24
Addresses round 2 of the review on #1584.

- Where the donor frame's carrier count prints as "<3" or "3-9", the
  sampled count sits within nine of the frame's rows, and a rounding of it
  beside them could exceed the frame or narrow the band, so the receipt
  leaves it out (None).
- A test fits a categorical whose commonest category is not its heaviest and
  checks that the reference goes by weight.
- The donor-frame test pins the certainty count to its rounded exact value,
  and a new test covers the banded branch at a frame size that is not a
  multiple of a hundred.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Folds in round 2's Nit 7 on #1583 into this PR, on María's word, since
#1583 merges first. frs_lcwra_proxy and frs_dwp_carer_care_hours take
strict, and the spine's call passes it: every reported benefit column and
esa_includes_support_component are there once _add_benefits has run, so a
renamed column is refused rather than read as no benefit. The unit tests'
partial frames keep the default. Test: strict accepts the full frame and
refuses a dropped reported column or support flag; the default reads them as
before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@juaristi22

Copy link
Copy Markdown
Collaborator Author

Thanks, Vahid. Round 2's two nits are fixed in 58f6b8a. #1583's Nit 7 is folded in here as 8aed8bf, since #1583 merges first.

  1. Banded sampled count. Where the carrier count prints as "<3" or "3-9", the receipt now leaves rows_sampled_in_proportion_to_fact out (None), so it can't exceed the frame or split the band.
  2. Tests.
    • A new test fits a categorical whose commonest category isn't its heaviest. Category 0 holds the most rows and category 2 the most weight, and the reference is cat_2=1.
    • The donor-frame test now pins sampled_rows_taken_with_certainty to the rounded count of records the capped inclusion takes for certain.
    • A new test covers the banded branch with 2 and 7 carriers, at a frame of 4,055 rows with five records taken for certain.

#1583's Nit 7. frs_lcwra_proxy and frs_dwp_carer_care_hours take strict, and the spine's call passes it. There, a missing reported-benefit column or a missing esa_includes_support_component is refused. The unit tests' partial frames keep the default, and a test covers both. To carry this, the branch now merges #1583's head e8689bd (77eba52).

Locally, the focused set plus test_uk_frs_spine, test_uk_frs_disability, test_uk_spi_income and test_uk_carers_allowance_take_up pass: 877 tests, 1 skipped.

On this branch's head, which merges #1583 at e8689bd:
- fit.qrf@1's H1 parity pin (tools/graph_parity_repin.py fit.qrf): the
  kernel's implementation hash moves with qrf.py; its bytes do not.
- The UK release-input coverage manifest: source_stages.json's hash.
- The hermetic H2 UK spine fixture (tools/graph_uk_spine_fixture.py): the
  SPI stage-1 changes, and #1583's carer link, move the synthetic spine.
- The US F0 spec pins, seed map and seed protocol included
  (repin_us_spec_engine_contracts.py, generate_us_bundle_from_constants.py,
  spec_engine_coverage.py). The re-pin pass ran again after the coverage
  regeneration, so the inventory count reads 41 of 41.

Tests: graph parity, the UK coverage manifest and the spec loader 84/84;
H2 parity 1/1; the US coverage tool 7/7; the US multispine pool tool 31/31.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@juaristi22
juaristi22 changed the base branch from uk-event-data-pr3 to main November 12, 2026 13:51
@juaristi22 juaristi22 closed this Nov 12, 2026
@juaristi22 juaristi22 reopened this Nov 12, 2026
@juaristi22
juaristi22 merged commit 57a32f8 into main Nov 12, 2026
12 checks passed
MaxGhenis added a commit that referenced this pull request Nov 12, 2026
Main 57a32f8 (#1579, #1553, #1311, #1583, #1584) changes two attested
inputs: microcosm.fit.qrf, on the seed protocol's QRF kernel list, and
sources.schema.json. #779 changes the immigration kernel, the US source
stages and the same schema, so every US F0 digest the two sides pinned
moved on both, and four files conflicted. Each conflicted pin was first
replaced by a sentinel, and the repository's tools wrote every value
from the merged tree:

- packages/microcosm-build/src/microcosm/build/spec_engine/inventory_coverage.py:
  EXPECTED_HASHES seed_map, seed_protocol and source_manifest, from
  tools/repin_us_spec_engine_contracts.py (source_manifest comes out at
  the branch's value; main left it alone). The branch's seed groups and
  counts merged cleanly.
- packages/microcosm-build/tests/engine_free/shared/test_spec_engine_loader.py:
  the minimal-loader golden, from the same repinner (it loads
  _rich_minimal through load_bundle exactly as the test does).
- packages/microcosm-build/tests/engine_workflow/us/test_us_multispine_pool_tool.py:
  the US spec_sha256, from the same repinner (load_bundle("us")).
- docs/evidence/spec-engine/us-f0-coverage.json: regenerated whole by
  tools/spec_engine_coverage.py (42781/42781 fields, 41/41 checks).

The repinner ran twice around the coverage regeneration. Its first pass
read the sentinels and wrote covered_item_count 39 into
tests/engine_contract/us/test_spec_engine_coverage_tool.py; the second
pass put it back to 41, so that file is unchanged from the branch.
ruff format changed only quote style on the three Python files.

No pin moved without a conflict:
- repin_us_spec_engine_contracts.py --legacy-only wrote nothing (main
  does not touch us/source_stages.json).
- tools/build_us_release_input_coverage_manifest.py rewrote
  us/release_input_coverage_manifest.json byte for byte.
- Both repinner --check and spec_engine_coverage.py --check exit 0.

The other four files both sides changed merged without conflict and
are git's three-way result: sources.schema.json,
tools/build_us_acs_local_release.py,
tools/build_us_fiscal_refresh_release.py and
tests/engine_free/us/test_us_acs_local_release_tool.py.

tools/generate_us_bundle_from_constants.py --check was not run: it needs
policyengine-us. Main changes no us/spec file and no
tools/us_bundle_generation file.

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

Development

Successfully merging this pull request may close these issues.

2 participants