Skip to content

Sample a base H5 by whole source households for the 5% national run - #1860

Draft
anth-volk wants to merge 5 commits into
mainfrom
claude/us-base-household-sampler
Draft

anth-volk wants to merge 5 commits into
mainfrom
claude/us-base-household-sampler

Conversation

@anth-volk

@anth-volk anth-volk commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1859

What this changes

tools/sample_us_base_households.py writes a seeded sample of a US base H5 (tools/build_us_puf_support_base.py's output) and its sample_receipt.json, for the 5% national run: the national release run with --base-h5 on the sample. It defaults to --fraction 0.05 and --seed 0. It is tracker task 1.9 · 1 of "15. US build memory, CPU and run time"; the release's --sample-receipt mode (1.9 · 2) reads the receipt, and route_a.sh's sample mode (1.9 · 3) runs the command.

The tool loads tools/sample_us_export_households.py and reuses its chunked reader, its two rules for households kept whole, its draw, its writer and its post-write check. The export sampler, route_a.sh --probe and their receipts are unchanged. What differs for a base H5:

  • The draw's unit is the source household. A base H5 holds each source household in two or three copies that share household_source_id and differ in household_support_clone_index: 0 the ASEC record, 1 its PUF tax-detail copy, 2 the capital-gains copy (on some source households only; its weight is split off copy 1). A selected source household enters with every copy, and each copy with its persons and every group unit they reference.
  • Groups. Source households are grouped by source_year and by whether they have a capital-gains copy, so the capital-gains copies, which carry little weight and much of the weighted capital gains, are drawn in their own groups. In each group min(N, max(2, floor(0.05 N))) of the N source households not kept are drawn. Every copy of a drawn source household is multiplied by its group's one factor, so each group's weight total is kept exactly and the weight ratios between a source household's copies are unchanged.
  • Source households kept whole at their source weights, by the export sampler's two rules applied to source households:
    • rare inputs: when 5% of a reform-coverage probe's carriers is fewer than 30 (--certainty-threshold), every carrier is kept; a source household carries an input when any copy holds a nonzero value;
    • dominant households: every carrier holding at least twice the weighted amount one sampled carrier stands for is kept (--size-certainty-multiplier), repeated until none qualifies. A source household's amount is the sum over its copies' rows of the absolute value times that copy's own household weight, because a capital-gains copy holds large amounts at a small part of its source household's weight. This rule also checks long_term_capital_gains_before_response and short_term_capital_gains together, which no probe checks.
  • Root attributes. The base's own root attributes are copied onto the sample. The release checks the congressional-district crosswalk attributes on every run (_assert_cd_vintage_support_matches; the crosswalk defaults to the packaged one) and refuses a base without them, and the writer sets no root attributes.
  • Refusals. A base without household_source_id or household_support_clone_index, with two households of one copy index in one source household, or whose copies of a source household disagree on source_year.
  • Post-write check. The export sampler's check (household IDs, weights, group totals, row counts, stored columns, dtype kinds), plus: the written (source household, copy) pairs are every copy of the selected source households, and the root attributes equal the base's.

The receipt

sample_receipt.json, kind us_base_source_household_sample, schema_version 1. It keeps the export sampler's layout where the meaning is the same: source (path, SHA-256, size, rows, copies, root attributes), design (fraction, seed, rule texts), strata (the groups), certainty (the kept source households and each one's reasons), probes, capital_gains, selection, output (path, SHA-256, size, root attributes copied), verification, timing, tool_source (this tool's commit and SHA-256, and the export sampler's SHA-256). A count named source_households counts source households, and one named households counts base households. Each group's sampled_weight_total is the total of the weights written.

Other files

  • docs/us-release-build-rule.md: a new section, "Sampling a base H5 for the 5% national run".
  • docs/agent-guide.md: points to it beside the export sampler.
  • changelog.d/us-base-household-sampler.added.md.
  • packages/microcosm-build/src/microcosm/build/frame_serializer_registry.py: lists the tool's write_root_attributes as an HDF write that copies root attributes only.
  • Tests and fixtures: packages/microcosm-build/tests/engine_free/us/test_us_base_subsample.py, test_support/microcosm_build/us_base_subsample.py.

No build's output changes: this adds a tool. No recorded hash or pin moves. No code shared with the UK build changes.

Tests

test_us_base_subsample.py (engine-free, 14 tests), on a synthetic base written as the base stage writes it (tables in USSingleYearDataset's layout, root attributes through h5py):

  • test_whole_source_households_enter_with_every_copy
  • test_each_group_draws_its_share_and_keeps_its_weight_total (also: one factor for every copy of a group's drawn source households; the receipt's group total equals the written total)
  • test_rare_input_source_households_are_kept_whole_at_source_weight (also: a group holding both kept and drawn source households)
  • test_the_default_path_checks_the_base_against_the_deny_list
  • test_the_dominant_household_rule_weights_each_copy_by_its_own_weight
  • test_root_attributes_are_copied_onto_the_sample
  • test_the_cli_defaults_to_five_percent_and_seed_zero
  • test_the_receipt_names_the_base_and_both_tools
  • test_the_draw_depends_only_on_the_seed
  • test_a_base_without_copy_columns_is_refused
  • test_a_source_household_with_two_rows_of_one_copy_is_refused
  • test_copies_that_disagree_on_source_year_are_refused
  • test_the_copy_columns_match_the_provenance_module (reads support_provenance from its file, without running microcosm.build.us_runtime's init)
  • test_the_fixture_path_imports_no_engine (a fresh interpreter that refuses policyengine-us and microcosm.build.us_runtime imports)

Local results, each test selected by its node ID (US environment, uv sync --all-packages --locked --extra us):

  • 279653358, the tests with a placeholder tool that draws base households one by one through the export sampler: 13 failed, each on an assertion.
  • 07dc24f05, the tool: the same 13 passed.
  • cc134d1f5, after review round 1: the copy-column test now checks that it imports no microcosm.build.us_runtime module, and failed on that assertion. The group-total check added in the same commit passed before the change (the fixture's totals already agreed).
  • fe8dd20a8, the round 1 fixes: all 14 passed.
  • 61e392eff: the engine-free (3.14) CI job failed one test already in the repository, test_registry_classifies_every_writable_production_hdf_site, which requires every production function that opens an HDF file for writing to be listed in the HDF write registry. It failed on that assertion locally at fe8dd20a8; after the registry entry it passes.

Ruff check and format pass on the changed Python files; tools/ci_test_plan.py verify passes. CI has not run: this PR starts it.

One run on the real base H5

The base of decision 1.1 · 5 (base_populace_us_2024_puf_support.h5, 2,370,507,790 bytes, SHA-256 b5db384f…; 352,932 households, 168,852 source households), at 07dc24f05, with the defaults, on a 10-core, 32 GiB Apple M5 Mac with other work running:

  • 155 s wall, 93 s user and 22 s system CPU. Importing the release modules, which builds the engine's tax-benefit system, took 90 s of that, and writing took 31 s.
  • Peak physical footprint 2.44 GiB (/usr/bin/time -l).
  • 9,736 source households (5.77%): 8,371 drawn and 1,365 kept whole. That is 20,613 households (5.84%), 1,141 of them capital-gains copies, and 54,182 persons; the H5 is 143 MB.
  • Kept, by reason: the dominant-household rule on the adult-care expense probe (492), a qualified-business-income probe (206) and the capital-gains amounts (159, 28% of the weighted capital gains), among others; the rare-input rule on Form 4952 elections (296), casualty losses (88) and Keogh distributions (13).
  • Six groups, each group's weight total kept to within 4e-16 relative. The post-write check passed. Read separately: the root attributes are on the sample, and the capital-gains copies' donor IDs are one-to-one.

The sample was deleted after these checks. The fixes after review round 1 changed only the receipt's group totals (now taken from the written weights), rule texts and tests, not the draw.

Review rounds

  • Round 1 (head 07dc24f05): one bug, low: the copy-column test imported microcosm.build.us_runtime, whose init builds the engine wherever policyengine-us is installed. Fixed with a test that failed first (cc134d1f5, fe8dd20a8). The same commits took up its code-quality points: receipt group totals from the written weights, rule texts for groups and source households, the receipt keys documented, root attributes written through h5py in the fixtures, a test of the default deny-list path, and a test of a group holding both kept and drawn source households.
  • Round 2 (head fe8dd20a8): no bugs found, so the reviews stopped there.

Points from round 2 left for later (code quality, not bugs):

  • Root attributes are copied and compared through PyTables. The base stage writes only text attributes, which copy exactly; a non-text attribute would need copying through h5py.
  • Refusals raised inside the export sampler's draw still say "household(s)" and "export".
  • A base without source_year is grouped by capital-gains copy only; the receipt records the column as absent.
  • The receipt's file name is fixed (sample_receipt.json), so a second sample written to the same --out replaces it. For 1.9 · 2's --sample-receipt reader: compare the SHA-256 of the H5 the release is given with the receipt's output.sha256, not the path.
  • No test covers zero-weight copies, source_year on the household table, fraction 1, the five household-location attributes, or a writer that drops one copy.

What the merge waits for

No earlier task: 1.9 · 1 is the first task of substage 1.9, has no dependencies, and the substage depends on none. This PR builds on main. The merge waits only for this PR's own checks.

🤖 Generated with Claude Code

anth-volk and others added 4 commits December 3, 2026 01:22
Adds tests for tools/sample_us_base_households.py, which samples a US base
H5 for the 5% national run, and a placeholder tool that draws base
households one by one through the export sampler. Every test fails on its
assertions against the placeholder:

- a drawn source household enters with every copy;
- source households with a capital-gains copy form their own groups, each
  group keeps its weight total, and one factor scales every copy;
- rare-input source households are kept with every copy at source weight;
- the dominant-household rule also checks the two main capital-gains
  amounts, weighting each copy's amount by that copy's own weight;
- the base's root attributes are copied onto the sample;
- the command defaults to 5% and seed 0;
- the receipt names the base, both tools and the source households drawn;
- bases without copy columns, with a repeated copy index or with copies
  that disagree on source_year are refused;
- the fixture path imports no engine module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…1.9 · 1)

tools/sample_us_base_households.py writes a seeded sample of a US base H5
and its sample_receipt.json (kind us_base_source_household_sample),
defaulting to 5% and seed 0. It loads the export sampler and reuses its
chunked reader, rare-input and dominant-household rules, draw and
post-write check, with these differences:

- The draw's unit is the source household (household_source_id): it
  enters with every copy (household_support_clone_index 0, 1 and, when
  present, 2) and each copy with its persons and group units.
- Source households are grouped by source_year and by whether they have
  a capital-gains copy; every copy of a drawn source household is scaled
  by its group's one factor, so group weight totals and the weight ratios
  between copies are kept.
- The two rules keep source households whole at their source weights. A
  copy's input amount counts at that copy's own weight, and the
  dominant-household rule also checks long_term_capital_gains_before_response
  and short_term_capital_gains together.
- The base's root attributes, among them the congressional-district
  crosswalk attributes the release checks on every run, are copied onto
  the sample; the writer sets none.
- A base without the copy columns, with two households of one copy index
  in a source household, or with copies that disagree on source_year is
  refused.

The size-certainty record in the capital-gains test now reads
source_households, the receipt's name for counts of source households.
Docs: the release build rule gains a section on sampling a base H5, and
the agent guide points to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test_the_copy_columns_match_the_provenance_module now checks that reading
the provenance module's names imports no microcosm.build.us_runtime
module: that package's init builds the policyengine-us tax-benefit system
wherever the engine is installed, and the agent guide keeps both
samplers' tests off it. It fails while the test imports the module
through its package.

The group test also checks that each group's receipt total equals the
written weights' total, which the next commit computes from the written
weights; on this fixture the two already agree, so that check passes
before the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the written weights (1.9 · 1)

Review round 1 fixes:

- test_the_copy_columns_match_the_provenance_module loads the provenance
  module from its file under a private name, so the test no longer runs
  microcosm.build.us_runtime's init, which builds the engine wherever
  policyengine-us is installed.
- Each group's sampled_weight_total and conservation errors in the receipt
  are the written weights' total (the post-write check's
  stratum_weight_totals), not the draw's per-source-household vector.
- The receipt's count and draw rules are written for groups and source
  households; the tool's docstring and the release build rule say which
  receipt keys hold the groups (strata) and the kept source households
  (certainty), and that the release checks the crosswalk attributes on
  every run against the packaged crosswalk unless another is given.
- Tests: the fixtures write the base's root attributes with h5py, as the
  base stage does; a test covers the default deny-list path through a stub
  h5_io; the rare-input test checks a group holding both kept and drawn
  source households.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ry (1.9 · 1)

The registry test that scans production code for HDF files opened for
writing (test_registry_classifies_every_writable_production_hdf_site,
already in the repository) failed at fe8dd20 on an assertion: the new
tool's write_root_attributes opens the sample H5 in append mode and was not
listed. It copies the base H5's root attributes and writes no Frame column,
so it is listed as an exclusion. The test passes after this change.

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

This branch has not been deployed

No deployments
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.

Sample a base H5 by whole source households for the 5% national run

1 participant