Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
eead6e8
Add shared target-loss rules: weights and caps from what each row is
juaristi22 Nov 25, 2026
6e4c56a
Run the US loss-weight formula through the shared rule; declare the U…
juaristi22 Nov 25, 2026
b4372d2
Declare the UK local weight rules as shared rules, held equal by test
juaristi22 Nov 25, 2026
ad901fd
Let the county profile's loss cap apply to the county rows only
juaristi22 Nov 25, 2026
b5254f7
Add a shared area support module; the US district gate reads its gate
juaristi22 Nov 25, 2026
050457e
Add a spine and target diagnostic that reads saved calibration inputs
juaristi22 Nov 25, 2026
88ee1a6
Expose the solver's L2 anchor in the ACS local tool
juaristi22 Nov 25, 2026
974cf2a
State the weight bound's contract; add declared references and a report
juaristi22 Nov 25, 2026
823b380
Merge main (fccd9a68b) into the shared calibration machinery branch
juaristi22 Nov 25, 2026
9dac441
Let the solver bound weights against a declared reference; re-pin
juaristi22 Nov 25, 2026
aa27505
Let the ACS local tool bound weights against a shared reference
juaristi22 Nov 25, 2026
657ae0c
Compute the UK loss weights with the shared rules
juaristi22 Nov 25, 2026
d4f5cd9
Give the shared area support gate optional floors and a relative floor
juaristi22 Nov 25, 2026
fbcd52f
Re-pin the US seed protocol digests for the solve.py edit
juaristi22 Nov 25, 2026
a4734af
Name the UK problem's loss-weight modules instead of importing them
juaristi22 Nov 25, 2026
3f4edd4
Record row caps and the census against them in the diagnostics
juaristi22 Nov 25, 2026
24aaded
Test the softmax mass path under a declared weight-bound reference
juaristi22 Nov 25, 2026
1ffa4cf
Merge main (63e22f9d7) and re-pin the US seed protocol digests
juaristi22 Nov 25, 2026
3a564cd
Merge main (268156c8f) and re-pin the US seed protocol digests
juaristi22 Nov 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1729-county-rows-loss-cap-scope.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ACS local release takes `--county-target-loss-cap-scope`. `run` (default) applies the county profile's loss cap to every row of a county build, as before. `county_rows` applies it to the county rows only, so the state and district rows keep `--target-loss-cap` when county rows join the solve. The caps are declared by level through the shared `target_loss_caps`, and the solver gets the equivalent one-cap loss, so its cap stays one number. Under `county_rows` the summary and `calibration_diagnostics.json` carry a `target_loss_caps` block: the declared caps and the past-cap census against each row's own cap (`row_cap_census`), because the solver's own census reads every row against the equivalent cap (microcosm#1729).
1 change: 1 addition & 0 deletions changelog.d/1729-shared-area-support.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`microcosm.build.gates.area_support_gate` takes each floor as optional and gains `minimum_effective_sample_size_ratio`, a floor relative to the area's starting ESS (the rule microcosm#1078 adopted). With the three absolute floors the UK local build declares, its result is unchanged. `microcosm.build.area_support` adds the Kish ESS of each area, the long-form area support table from a weight vector, and the gate's verdict area by area. The ACS local release's district ESS gate declares its floors to the shared gate and lays out the same entry as before; `ess_by_group` and the inline Kish arithmetic are gone from the US tools. The district gate stays report-only by default. `gates.py` is in the UK full-gates kernel's hash, so that node re-keys (microcosm#1729).
1 change: 1 addition & 0 deletions changelog.d/1729-shared-target-loss-rules.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add shared target-loss rules (`microcosm.calibrate.target_loss_rules`): a loss weight and a loss cap for each target from its level, family, value basis and value, with the rule declared by name and parameters. Both countries now compute their loss weights with them, with the same vectors as before, bit for bit: the US formula is the shared rule `sqrt_value_concept_budget`; the UK local rules (`uk_runtime.target_weights`) are `uniform`, `level_equal` and `level_family_equal`, and the UK national `family_equal` (`uk_runtime.national_doctrine`) is `level_equal`. Each country declares its rule names as shared rules. The ACS local release takes `--target-weight-rule` (default: the formula every build has used). The UK problem, national and rotated-holdout kernels hash the shared module, so those UK nodes re-key (microcosm#1729).
1 change: 1 addition & 0 deletions changelog.d/1729-spine-target-shares.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a spine and target diagnostic for any pooled build (`microcosm.calibrate.spine_target_shares`, `tools/spine_target_shares.py`): for each target, the share of its weighted total that each set of rows (spine) carries, against that spine's share of the weight among the records the target is about. It is report-only, reads a saved constraint matrix, weights and spine labels (or an ACS local checkpoint directory), runs no engine and no solve, and can withhold targets with few distinct carriers (microcosm#1729).
1 change: 1 addition & 0 deletions changelog.d/1729-us-acs-local-l2-anchor.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ACS local release takes `--l2-anchor` and `--l2-anchor-weights`, which it passes to the shared solver's `l2_anchor` and `l2_anchor_weights`. `initial` (default) is each record's own starting weight, as every solve has used; `uniform` is the mean starting weight; `--l2-anchor-weights FILE` makes the `--l2-lambda` penalty pull toward a declared design without a change to the starting weights. The default adds no keyword to the solve and no key to the solver stamp (microcosm#1729).
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ACS local release takes `--weight-bound-reference`. `own` (default) bounds each household at `--max-weight-ratio` times its own starting weight, as before. `mean`, `state_mean` and `district_mean` bound it at `--max-weight-ratio` times one shared weight (the mean starting weight overall, or of its state or district), through the solver's `max_weight_reference`, so a donor row gets no larger a bound than the ACS rows beside it. The default adds no keyword to the solve and no key to the solver stamp (microcosm#1729).
1 change: 1 addition & 0 deletions changelog.d/1729-weight-bound-contract.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
State one contract for the solver's weight bound (`microcosm.calibrate.weight_bound`, docs/calibration-local-area-machinery.md): `max_weight_ratio` bounds each record against its weight on the frame the solve was given, never against a warm start, and a new frame is a new reference. Tests hold the solver to it. `calibrate` and `solve_constraint_system` take an opt-in `max_weight_reference`: the bound is then `max_weight_ratio` times a declared weight for each record (for example the mean weight, from `weight_bound_reference`), so a record that starts heavier gets no larger a bound. Without it the solve is unchanged byte for byte. `weight_bound_report` measures any weight vector against a reference, report-only. The calibrate parity pin is re-recorded for the edit to `solve.py` (microcosm#1729, microcosm#493).
220 changes: 220 additions & 0 deletions docs/calibration-local-area-machinery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
# Shared calibration machinery for local areas

The UK local build and the US local build each calibrate a pooled file to
targets at several geography levels. This page lists the pieces of that work
that are shared code, what each country declares, and how to turn each option
on. It is the reference for microcosm#1729.

Two rules hold for every piece:

- **A default changes nothing.** With the default declarations each build
calls the solver with the arguments it used before and gets the same
weights. An option changes an output only when a build asks for it.
- **Nothing here blocks a release.** The gate and the diagnostics report. A
caller decides what a failure means.

## What is shared, and what each country declares

| Piece | Shared code | UK local | US local (ACS) |
|---|---|---|---|
| L2 penalty anchor | `calibrate(l2_anchor=, l2_anchor_weights=)` | `uniform` on the refit stage (`UK_LOCAL_SIZE_REFIT_L2_ANCHOR`) | `--l2-anchor` (default `initial`), `--l2-anchor-weights` |
| Target loss weights | `microcosm.calibrate.target_loss_rules` | `UK_LOCAL_TARGET_WEIGHT_RULE_DECLARATIONS` (`uk_runtime.target_weights`), `UK_NATIONAL_TARGET_WEIGHT_RULE_DECLARATIONS` (`uk_runtime.national_doctrine`) | `US_ACS_LOCAL_TARGET_WEIGHT_RULES`, `--target-weight-rule` |
| Loss cap for a level or a family | `target_loss_caps`, `single_cap_equivalent` | one number (`UK_LOCAL_TARGET_LOSS_CAP`) | one number; `--county-target-loss-cap-scope county_rows` |
| Area support | `gates.area_support_gate`; measurement in `microcosm.build.area_support` | three absolute floors in `uk/gates.json` | `district_ess_collapse_gate` declares a relative and an absolute ESS floor |
| Weight bound | `max_weight_ratio`, `max_weight_reference`; contract in `microcosm.calibrate.weight_bound` | `UK_LOCAL_MAX_WEIGHT_RATIO`, against each record's own weight | `--max-weight-ratio`, `--weight-bound-reference` (default `own`) |
| Which rows carry each target | `microcosm.calibrate.spine_target_shares`, `tools/spine_target_shares.py` | not run | run on a checkpoint |

## Target loss weights

A rule gives each target a loss weight from what the target is: its **level**
(nation, state, district, local authority), its **family**, its value
**basis** (`count` or `amount`) and its **value**. A country says what each row
is (`TargetLossRows`) and declares the rule by name with its parameters
(`TargetWeightRule`).

| Rule | What it does |
|---|---|
| `uniform` | Every target weighs one. |
| `level_equal` | Each level takes one equal share; rows in a level share it equally. |
| `level_family_equal` | Each level takes one equal share, each family in a level one equal share of it. With `count_power` (for example `0.5`), the count rows of a family share its count budget in proportion to `max(abs(value), 1) ** count_power`. |
| `sqrt_value_concept_budget` | The US formula: value weight over its basis mean, a budget for each concept group, an equal split between counts and amounts. It reads no level. |

Any rule may carry `family_multipliers`.

- **US.** The default, `concept_budget`, is the formula every build has used.
It is the shared rule `sqrt_value_concept_budget`, and gives the same
weights bit for bit. `--target-weight-rule level_family_equal_sqrt_count`
gives each geography level one equal share of the loss. On the September
surface the state SOI rows held about 90% of the loss weight under the
default.
- **UK.** The UK computes every loss weight with the shared rules.
`uk_runtime.target_weights` says what a UK row is and declares each local
rule name as a shared rule: the doctrine's
`nation_grain_family_equal_sqrt_count` is `level_family_equal` with
`count_power` 0.5, over the UK's own mapping of rows to levels. The national
build's `family_equal` (`uk_runtime.national_doctrine`) is `level_equal`
with each family as one level. The vectors are the same as before, bit for
bit: the tests hold them to verbatim copies of the UK arithmetic as it
stood before the move.

The shared module is in the hash of the UK nodes that bind loss weights
(`uk.full.problem@1`, the two national kernels and the rotated holdout), so
an edit to a shared rule re-keys them.

## A loss cap for a level or a family

The solver's loss is `sum(w * min(abs(miss) / s, cap)) / sum(w)` with one cap.
`target_loss_caps(default, by_level=..., by_family=...)` declares a cap for
each row: one number when nothing is declared, a vector otherwise. A family
cap wins over a level cap, and an override that matches no row is an error.

The solver needs no change to honor a vector. For row caps `c`, weights `w`
and scales `s`, `single_cap_equivalent` returns

- `cap0 = sum(w * c) / sum(w)`,
- `w' = w * c / cap0` and `s' = s * c / cap0`.

Then `w' * min(abs(miss) / s', cap0) == w * min(abs(miss) / s, c)` for every
row and `sum(w') == sum(w)`. The loss value, each row's contribution and the
gradient are those of the loss with row caps. When every row has the same cap
the weights and scales come back unchanged.

How to read a run with row caps:

- The solver records the transformed weights and scales, and `cap0` as its
cap. So in `calibration_diagnostics.json` the top-level `past_cap_census`
and each target's `target_loss_scale` and `target_loss_weight` read every
row against `cap0`, not against the row's own cap.
- The declared caps and the census against them are in the `target_loss_caps`
block, in the tool's summary and in the `build` block of
`calibration_diagnostics.json`: the default cap, the cap of each level, the
rows at each cap, each row that is not at the default, and
`past_cap_census` against each row's own cap (`row_cap_census`). A run with
one cap has no such block.

**US county builds.** By default the county profile's cap (10) applies to
every row of a county build, as before. With
`--county-target-loss-cap-scope county_rows` it applies to the county rows
only, and the state and district rows keep `--target-loss-cap`. The weight
ratio is a bound on records, not on targets, so it stays one number for the
run.

## Area support

There is one gate, `microcosm.build.gates.area_support_gate`. It checks every
area of a declared roster against the floors a country declares. Each floor
is optional: `minimum_rows`, `minimum_distinct_sources`,
`minimum_effective_sample_size`, and `minimum_effective_sample_size_ratio`, a
floor relative to the area's starting ESS (the rule microcosm#1078 adopted).

The UK local build declares the three absolute floors (50 rows, ESS 50 and 50
distinct sources). With those three and no relative floor the gate gives the
result it always gave: a test holds it to a verbatim copy of the gate as it
stood before the floors became optional, reviewed exclusions included.

`microcosm.build.area_support` measures what the gate reads:

- `effective_sample_size_by_group` is the Kish ESS of the records in each
area.
- `area_support_table` builds the long-form table from a weight vector and
each record's area: positive-weight rows, distinct positive-weight source
households, ESS, and the ESS under the starting weights.
- `area_support_floor_table` gives the gate's verdict area by area, with one
flag for each declared floor.

The US district gate (`us_local_district_ess_collapse` in `us/gates.json`)
declares a relative floor of 0.25 and an absolute floor of 15 to the shared
gate. Its entry in `gate_summary.json` is the same as before. It is
report-only unless a build passes the existing `--district-ess-gate-blocking`.

## The weight bound

`max_weight_ratio` is a hard bound on each record: no returned weight exceeds
`max_weight_ratio` times the record's **reference**, which is its weight on
the frame the solve was given.

1. The reference is the frame, never the warm start. An epoch-batched solve
keeps the bound against the frame's weights on every batch.
2. A new frame is a new reference. A stage whose frame carries an earlier
stage's calibrated weights bounds against those, so two stages at ratio `R`
admit `R ** 2` against the first stage's starting weights.
3. The bound is relative, so a heavy record gets a larger absolute bound. In
the US local file a donor row starts about 27 times heavier than an ACS
row, so its absolute bound is about 27 times higher under the same ratio.

**A declared reference.** Point 3 is a choice. `calibrate` and
`solve_constraint_system` take `max_weight_reference`, one positive weight for
each record, and then bound each record at `max_weight_ratio` times that
reference. `weight_bound_reference` builds one: `mean`, or `group_mean` for
the mean starting weight of the record's area. The frame's weights still set
the starting point, the mass and the L2 anchor. A record that starts above its
bound starts from the bound, with the mass it gives up spread below the
bounds. Under mass conservation the bounds must admit the total, or the solve
refuses. Without `max_weight_reference` the solve is the historical one, byte
for byte.

In the US local tool, `--weight-bound-reference mean`, `state_mean` or
`district_mean` turns it on. The default, `own`, is each household's own
starting weight.

`weight_bound_report` measures any weight vector against a reference: the
largest ratio, the records above the bound, the records that start above it,
and whether the bounds admit the starting mass. Read it on a finished solve
before you turn a declared reference on: in the pooled US file the donor rows
start far above a shared bound at the default ratio, so the bound moves
starting mass off them.

## Which rows carry each target

`spine_target_shares` reports, for each target, the share of its weighted
total that each spine's rows carry, against that spine's share of the weight
among the records the target is about. A target that the donor rows carry
almost alone pulls weight onto them. `excess` (share minus mass share) and
`lift` (their ratio) rank the targets.

The tool reads saved inputs and needs no build:

```bash
uv run python tools/spine_target_shares.py \
--us-acs-local-checkpoint <run>/checkpoints --out shares.json --spine <donor spine>
```

It reports at the design weights, and at the calibrated weights when
`weights_latest.npz` is in the checkpoint. For any other pooled build pass
`--matrix`, `--weights` and `--spines`. `--minimum-carriers N` withholds a
target that a spine carries with fewer than `N` distinct source households;
pass the disclosure floor for a build on licensed microdata.

## What only a full build can show

The tests prove that the default declarations send the solver the same
arguments and return the same arrays on small fixtures, and that the UK
vectors and the UK gate result are those of the code before the move. They do
not run a full ACS local build or a UK build. A full build is the only proof
for the full surface, the full pool and the stages around the solve.

## What re-keys in the UK graph

The UK graph hashes the bytes of the modules its kernels name. This work
edits some of them, with the same outputs, so these nodes compute again on
the next run instead of reading a cached result:

- every UK solve node, through `calibrate.adam@1` (`calibrate/solve.py`);
- the kernels of `uk_runtime/graph_targets.py`, which hash their own module:
`uk.full.target_compilation@1`, `target_selection@1`, `measures@1` and
`problem@1`. The problem also hashes the modules that decide its loss
weights, which now include the shared rules. Those modules are named in
`uk_runtime/target_compile_modules.py`
(`UK_FULL_PROBLEM_LOSS_WEIGHT_MODULE_NAMES`) and no longer imported by
`graph_targets`, so the other three kernels have no import edge to them;
- the kernels of `uk_runtime/graph_national.py`: `uk.full.national_targets@1`,
`national_problem@1`, `national-gates@1` and `national-readback@1`;
- the kernels of `uk_runtime/graph_terminal.py`: the rotated holdout,
`uk.full-gates@1` (which also hashes `build/gates.py`), its preflight, and
the three export kernels;
- outside the UK, the transport gate kernel, which hashes `build/gates.py`.

The UK spine kernels are not among them.

No `uk/gates.json` digest, gate replay or receipt moves. The calibrate parity
pin is re-recorded.
16 changes: 8 additions & 8 deletions docs/evidence/spec-engine/us-f0-coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -1642,13 +1642,13 @@
"compiler_ir.node_slices"
],
"expected": {
"map_sha256": "65677b9b7f7a7504e611451d2d494525efd774aba7dd3ca0b6332dcdb03d8976",
"protocol_sha256": "4fc066840c23d994a6b52a6b510eedd99b6ca5bed522c6837f7ff258b91596eb"
"map_sha256": "20648ca766969baf424f491150ab3d12b3edeb89a0544433e64e88c8efb42bb2",
"protocol_sha256": "c321797d65496f01b3186c83dee038c10dec9ed504d1d08a84b452d5f6538c15"
},
"failures": [],
"observed": {
"map_sha256": "65677b9b7f7a7504e611451d2d494525efd774aba7dd3ca0b6332dcdb03d8976",
"protocol_sha256": "4fc066840c23d994a6b52a6b510eedd99b6ca5bed522c6837f7ff258b91596eb"
"map_sha256": "20648ca766969baf424f491150ab3d12b3edeb89a0544433e64e88c8efb42bb2",
"protocol_sha256": "c321797d65496f01b3186c83dee038c10dec9ed504d1d08a84b452d5f6538c15"
},
"status": "covered"
},
Expand All @@ -1663,7 +1663,7 @@
"compiler_ir.seed_stream_map"
],
"expected": {
"implementation_sha256": "4fc066840c23d994a6b52a6b510eedd99b6ca5bed522c6837f7ff258b91596eb",
"implementation_sha256": "c321797d65496f01b3186c83dee038c10dec9ed504d1d08a84b452d5f6538c15",
"protocol": "legacy-v1",
"streams": [
"build_model",
Expand All @@ -1684,7 +1684,7 @@
},
"failures": [],
"observed": {
"implementation_sha256": "4fc066840c23d994a6b52a6b510eedd99b6ca5bed522c6837f7ff258b91596eb",
"implementation_sha256": "c321797d65496f01b3186c83dee038c10dec9ed504d1d08a84b452d5f6538c15",
"protocol": "legacy-v1",
"streams": [
"build_model",
Expand Down Expand Up @@ -2585,7 +2585,7 @@
"country": "us",
"schema_id": "country_spec",
"schema_version": 1,
"spec_sha256": "9f56635edf7da4efa90d01e6ab263eb5f17267ab4bf5b2ad843d76b27f00cfb2"
"spec_sha256": "b94bfc6a5742de010443938a676d4d34e70df80dd405f89e2ff7a70f747c8107"
}
},
"report_schema_version": 3,
Expand All @@ -2595,7 +2595,7 @@
"country": "us",
"schema_id": "country_spec",
"schema_version": 1,
"spec_sha256": "9f56635edf7da4efa90d01e6ab263eb5f17267ab4bf5b2ad843d76b27f00cfb2"
"spec_sha256": "b94bfc6a5742de010443938a676d4d34e70df80dd405f89e2ff7a70f747c8107"
},
"status": "pass"
}
Loading
Loading