Skip to content

UK firm data: paper-parity port on the shared firm layer - #1706

Draft
vahid-ahmadi wants to merge 14 commits into
mainfrom
uk-firm-paper-parity
Draft

vahid-ahmadi wants to merge 14 commits into
mainfrom
uk-firm-paper-parity

Conversation

@vahid-ahmadi

@vahid-ahmadi vahid-ahmadi commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #258, fixes #636. Refs #1705, #1756 and PolicyEngine/policyengine-uk#2940.

Draft. Narrowed on 26 Nov 2026 as step 2 of the split agreed in this comment. This PR is now the UK paper-parity port, rebuilt on the country-neutral microcosm.build.firms layer from #1756.

Stacked on #1756. The base stays main so that CI runs, which means that until #1756 merges, this diff also shows #1756's five commits (microcosm/build/firms/, its tests and its changelog fragment). Review only the commits after eb799a296. The 36-commit version this PR held before is kept on the branch uk-firm-paper-parity-full (f644ab023), and the later PRs are carved from it.

What this PR contains

  • Paper-parity port (uk_runtime/firm_generation.py): sector-conditional employment, the two universes (ONS frame and HMRC registered traders, with a per-band registration propensity), zero-turnover traders as an out-of-frame stratum, optional stratified thinning, and the in_frame / vat_scope / vat_registered flags.
  • Thin UK wrappers on microcosm.build.firms:
    • firm_universe runs thinning and the two-universe functions through UK_VAT_REGISTRATION_SPEC: Negative_or_Zero is excluded, £1_to_Threshold is registrable, and the six bands above the threshold are selected.
    • firm_rules.UKFirmRuleOutputs is a FirmRuleOutputs carrying vat_liability_if_registered.
    • Band turnover is drawn with draw_in_band.
  • Rules seam and policyengine-uk adapter.
    • PolicyEngineUKFirmRules reads firm_vat_liability_if_registered.
    • evaluate_uk_firm_taxes and weighted_uk_firm_tax_totals read every pe-uk firm tax.
    • CI tests these with a fake simulation, because no released pe-uk has the firm entity.
    • Rules for 2024-25 run in period 2024.
  • Local build (tools/build_uk_firms.py, uk_runtime/firm_build.py): writes uk_firms.parquet and a deterministic receipt. It has no publish or staging path.
  • Receipts comparison (tools/compare_uk_firm_taxes_to_receipts.py): compares totals with HMRC and MHCLG 2024-25 figures, on firms.receipts.
  • pe-uk schema check: a snapshot of pe-uk's firm variables at 0d1c25010 (71 inputs), read through read_entity_variables. The input-status map sets only the 10 base columns, and every other input is not_set.

Review fixes from the 24 Nov review

  • Rule period vs data year. The build receipt now records rule_period (2024 for 2024-25) apart from data_years:
    • for 2024-25, the ONS frame's turnover is mostly from VAT returns for 2023-24 (ONS UK Business 2025, note 4), while HMRC's VAT statistics are for 2024-25;
    • rule_period_note says a change dated later in the tax year, such as 1 January, is not apportioned unless the parameter is day-weighted.
  • Parity, not "reproduces". The port matches the paper within set tolerances; it does not reproduce it row for row. The full build had the paper's 2,946,015 rows, frame population +0.15%, and net-VAT band accuracy 0.999 against the paper's 0.981.
  • Done in later PRs, as agreed:
    • the legal-status changes (ONS status 4 → COMPANY plus the public-body flag, status 7 → firm_is_charity as an assumption) and the LLP check go to PR 3;
    • the input-VAT split from the Supply and Use Tables comes after the split.

Not in this PR (later PRs of the split)

  1. Region and legal status:

    • region and legal status, Tables 8, 12, 13 and 15, the legal status × employment coupling, and the export-group registry;
    • the value-free target candidates;
    • the mapping changes above.

    Until PR 3 lands, this PR's build writes no region or legal status, so pe-uk applies its defaults.

  2. Opt-in input groups, each with its source receipts:

    • 4a labour: paybill, pay split, Employment Allowance flags and take-up, BPE headcount, public sector, labour link, and the 250+ headcount tail;
    • 4b CT profit and R&D;
    • 4c rateable value and empty property;
    • 4d consumption, BPE turnover and the household-sales decomposition.
  3. The household-link design note, off main.

None of the opt-in diagnostics should feed a published costing yet.

Checks

  • Unchanged outputs. The same-seed thinned 2024-25 population (295,271 rows, 1,000 epochs) is exactly identical before and after the move onto microcosm.build.firms, both on current main, checked with assert_frame_equal(check_exact=True). On current main, the code before the move still passed every check in test_uk_firm_paper_parity.py, including sector accuracy against the 0.90 bar, so main's solver change does not break parity.

  • Tool equivalence. The rewired snapshot tool reproduces the committed snapshot. The receipts tool's report matched the previous tool's byte for byte, before the paybill caveat was reworded for this scope.

  • Local tests. The UK firm test files pass, along with ruff and tools/ci_test_plan.py verify:

    • test_uk_firm_paper_parity, test_uk_firm_generation, test_uk_firm_universe and test_uk_firm_rules;
    • test_uk_firm_build and test_uk_firms_targets;
    • test_uk_firm_policyengine_uk_rules and test_uk_firm_policyengine_uk_schema;
    • test_uk_firm_tax_receipts, test_uk_firm_table and test_uk_runtime_exports.

    The full engine-free suite was not run locally.

🤖 Generated with Claude Code

@juaristi22 juaristi22 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at f644ab023. The port and the rules seam look sound, #636 is fixed, and CI is green. Your three questions first, then some notes.

Rule period: 2024-25 evaluated as period 2024

This is right. In pe-uk, period Y is the fiscal year that starts in Y. Each parameter takes its value on 30 April Y, unless it opts into day-weighting (convert_to_fiscal_year_parameters). That gives the £90k threshold, the FY2024 CT rates and the 2024-25 NICs.

Two caveats are worth recording in the receipt or the docs:

  • 1 January changes aren't apportioned. A change dated 1 January only counts for part of the year if the parameter is day-weighted.
  • The data lag the rules by a year. The ONS 2025 workbook's turnover comes mostly from VAT returns for FY2023/24 (its note 4, which is recorded in the fixture's provenance). So the "2024-25" frame's turnover predates the £90k threshold. The rules are 2024-25, but the turnover distribution near the threshold is from the £85k regime. The receipt should record the rule period and the data year separately.

ONS legal status to pe-uk legal forms

Counts from the fixture (ONS 2025, Table 12):

  • companies: 2,096,600
  • sole proprietors: 380,515
  • partnerships: 160,865
  • public corporations: 165
  • central government: 3,350
  • local authorities: 9,120
  • non-profit bodies: 84,000

By status:

  • Statuses 1–3 map one to one. Fine.
  • Statuses 5–6 go to OTHER with the public-body flag. This is right:
    • local authorities are outside "company" for CT (CTA 2010 s.1121);
    • the Crown isn't charged;
    • both are excluded from the Employment Allowance.
  • Status 4 (public corporations) goes to OTHER, which takes them out of CT. But most public corporations are bodies corporate, and so within CT. As public-sector bodies, they're probably also excluded from the Employment Allowance. COMPANY plus the public-body flag would be closer. The count is tiny, but some of these bodies are large.
  • Status 7 (non-profit bodies) goes to OTHER. That's reasonable for CT, since charitable profits and mutual trading are exempt.
    • The catch is that pe-uk ties two things to firm_is_charity: mandatory charitable rate relief (80%) and the Employment Allowance's charity exception.
    • The export leaves firm_is_charity as not_set, so status-7 bodies pay full business rates.
    • Setting firm_is_charity as an assumption for status 7, or for a share of those bodies, would fix most of that.
  • LLPs are worth checking. The workbook only says "3 - Partnership". If ONS puts LLPs under "1 - Company", they're charged CT, which they don't pay.

Input VAT at 20% of input costs

This is fine as a labelled placeholder, but it's an upper bound. It also combines with the export's other VAT assumption: in-scope turnover is all standard-rated, out-of-scope turnover is all exempt, and nothing is zero-rated or reduced-rated.

  • No firm can have negative net VAT. Inputs cap at 0.95 of turnover, so repayment traders can't exist. The paper puts them out of scope and leaves the £1-to-threshold band uncalibrated; that band's net VAT was −£300m in 2024-25.

  • Gross output VAT and input VAT are both overstated. Calibration pins net VAT by band, so totals are right by construction. The 1.068× VAT check is in-sample, so it doesn't test this. But anything that depends on the output/input split is off:

    • the Flat Rate Scheme choice;
    • partial exemption;
    • voluntary registration;
    • reforms that treat sales and inputs differently.
  • The PR already has the data for a fix. It extracts the ONS Supply and Use Tables 2024 for consumption shares. The same tables give each sector's purchase mix, and from that, by SIC section:

    • an effective recoverable input-VAT rate;
    • the shares of sales that are zero-rated and reduced-rated.

    Until then, I'd keep the table out of analyses that depend on the split.

Notes

  • #636 and #258. #636 is fixed: firm_generation.py now imports from microcosm.calibrate.solve only names that are in its __all__. For #258, the 0.20 rate now sits in the rules engine.

  • Size and description. The PR is now 36 commits, 146 files and +44,589 lines, and the body's plan predates most of it. These are all in as opt-in groups:

    • paybill, CT profit, rateable value and R&D;
    • Employment Allowance take-up and BPE headcount;
    • the Supply and Use consumption shares and the pay split;
    • the household-link design.

    I'd update the body and split the PR for review:

    1. the paper-parity port, the rules seam and the #258/#636 fixes;
    2. region and legal status;
    3. the opt-in input groups, each with its source receipts;
    4. the household-link design note.
  • The opt-in inputs aren't ready yet. The receipts comparison shows:

    • the Employment Allowance at £10.98bn and 2.21m claimants, against HMRC's £3.30bn and 1.22m;
    • business rates in England at 1.226× official;
    • the apprenticeship levy at 0.616× official.

    That's fine as diagnostics in a draft, but none of it should feed a published costing yet.

  • Parity. The full build matches the paper within tolerances: the row count is exact and the frame population is +0.15%. It also fits net VAT more closely than the paper does (0.999 against 0.981). So "parity" describes it better than "reproduces".

Checked

  • The generator, firm table, attribute and rules-adapter code.
  • The solver imports.
  • The legal-status fixture and its provenance.
  • pe-uk's convert_to_fiscal_year_parameters.
  • CI at the head, which is all green.

@juaristi22

Copy link
Copy Markdown
Collaborator

One more question, apart from the review above: how much of this could be made country-agnostic?

Most of the details are UK-specific: the ONS and HMRC sources, the VAT and legal-status mappings, and the policyengine-uk columns. But some of the algorithms, mechanics and tools underneath may not be. If another country wanted similar synthetic firm data, it could reuse those rather than start again.

From the PR description, possible candidates are:

  • the two-universe calibration, which gives each frame firm a registration propensity: registered-trader counts divided by frame mass;
  • the band draws (uniform inside a band, log-uniform in the open band) and the stratified thinning with base weights;
  • the rules seam: evaluate(firms, period) returning outputs keyed by firm_id, checked by align_rule_outputs;
  • the input-status registry and the engine-input snapshot check;
  • the build receipt and the receipts comparison tool;
  • the firm-only Frame. The body lists this as not yet done, because EntitySchema needs person and group entities.

Where do you think the line between uk_runtime and a shared layer could sit?

@vahid-ahmadi

Copy link
Copy Markdown
Contributor Author

Good question. My rough line: the data sources, the mappings and the policyengine-uk column names stay in uk_runtime. The mechanics move into the shared build layer, next to frame_sampling.py and stochastic_assignment.py.

Could be shared, with little change:

  • Band draws and stratified thinning. Uniform inside a band, log-uniform in the open band, with base weights. Nothing in them is UK-specific beyond the band edges, which come in as data.
  • The rules seam. evaluate(firms, period) returns outputs keyed by firm_id, checked by align_rule_outputs. The protocol is country-neutral; only firm_policyengine_uk_rules.py is UK.
  • The input-status registry and the engine-input snapshot check. These are generic once the variable list is passed in.
  • The build receipt and the receipts comparison tool. The comparison logic is generic. The target receipts (HMRC outturns) stay per country.

Shared in shape, but needs a country spec:

  • Two-universe calibration. The registration propensity (registered-trader counts divided by frame mass) generalises to any country with a business register and a tax-registration count. But which register and which threshold differ by country, so it would take a small spec rather than hard-coded VAT logic.

Blocked:

  • The firm-only Frame. It needs EntitySchema to allow a root population with no person or group entities. That is the same gap as the core issue on pe-uk#2940, so I'd do both together.

I'd keep this PR UK-only so it stays reviewable, then do the extraction as a follow-up once a second country actually needs it. That way the shared interface is shaped by two real cases rather than guessed from one. Does that line match what you had in mind?

Reply drafted with AI assistance.

@juaristi22

Copy link
Copy Markdown
Collaborator

Thanks, that breakdown helps. On timing, though, I'd recommend making as much of this country-agnostic as we can now, rather than planning a migration later. Otherwise the next country tends to rewrite the same code without the abstraction, instead of extracting it.

Would that be too big a lift here? If not, I'd recommend this approach:

  • move your first two groups into the shared build layer now, with the small country spec for the two-universe calibration;
  • keep the data sources, the mappings and the policyengine-uk column names in uk_runtime, as you propose;
  • leave the firm-only Frame until EntitySchema allows root populations, together with the core issue on Add firm entity and firm-side tax rules policyengine-uk#2940.

If reviewability is the concern, the shared mechanics could be one of the split PRs suggested in the review.

@vahid-ahmadi

Copy link
Copy Markdown
Contributor Author

Agreed, let's do it now rather than later. Plan:

  1. A new PR with the shared firm mechanics in microcosm/build/firms/, with no UK data:
    • band draws and thinning;
    • two-universe calibration behind a small registration spec;
    • the rules seam;
    • the input-status registry and snapshot check;
    • the receipts comparison.
  2. This PR, narrowed to the UK paper-parity port on top of it, with your rule-period/data-year, 1 January and wording fixes.
  3. Region and legal status, taking your status 4 → COMPANY + public body and status 7 → charity suggestions, plus the LLP check.
  4. The opt-in input groups with their receipts, in four PRs by family:
    • labour;
    • CT profit and R&D;
    • rateable value;
    • consumption and BPE turnover.
  5. The household-link design note on its own.

The firm-only Frame waits for EntitySchema root populations, alongside the core issue on PolicyEngine/policyengine-uk#2940. The input-VAT split from the Supply and Use Tables comes after, as its own PR.

Reply drafted with AI assistance.

vahid-ahmadi and others added 5 commits November 26, 2026 12:39
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… against them

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 Author

The shared layer (step 1 of the split) is open as a draft: #1756. This PR will be rebuilt on it as the UK port. It will keep main as its base so CI runs, and carry #1756's commits until that merges.

Reply drafted with AI assistance.

vahid-ahmadi and others added 9 commits November 26, 2026 13:04
Port sector-conditional employment, two-universe calibration with per-band
registration propensity, stratified thinning with base weights and
in_frame/vat_scope flags from firm-microsim-paper. Solve with the public
solve_constraint_system (fixes #636) and use the paper's input-ratio
imputation (fixes #258). Add the UKFirmRules seam keyed by firm_id, the
firm universe helpers, and a policyengine-uk-named firm table export.
Name policyengine-uk as the firm rules home in docs/firm-support-plan.md.

Refs #1705, PolicyEngine/policyengine-uk#2939.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Commit the paper's processed 2024-25 tables with provenance and golden
numbers from paper runs at aa1ce07 (full, fast and CI modes, seed 42).
The engine-free test runs a thinned build and checks exact targets and
propensities plus distributional tolerances; tools/firm_paper_parity.py
prints the full comparison table.

Refs #1705.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Share the HMRC band names between firm_universe and firm_generation,
remove the unused assign_vat_flags and calculate_vat_liability_values,
add UKFirmGenerationConfig.with_vintage, prove thinned solves start
from base weights, feed a generated population through the
policyengine-uk firm table, rename the parity tool to
tools/uk_firm_paper_parity.py, and correct the changelog and docs.

Refs #1705.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Hash the reference JSON in the fixture provenance with the commands
that produced it, assert the golden full run reproduces the paper's
published 2024-25 headline, reuse the shared HMRC band names, and note
the parity test in the firm support plan.

Refs #1705.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
policyengine-uk gives period Y the parameter values in force on 30 April
Y, so the 2024-25 vintage is period 2024 (the £90k threshold) and
2023-24 is period 2023. Read test fixtures as UTF-8 and clarify the
#258 changelog.

Refs #1705.

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

Carved from #1706's state at bd59841 without the region, legal-status and
paybill steps, which move to later PRs of the split. The input-status map
covers the 71 policyengine-uk inputs at the committed 0d1c25010 snapshot,
with only the base columns set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…apart from the rule period

The same-seed thinned 2024-25 population (295,271 rows) is identical,
checked exactly against the code before this commit on the same main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…what this PR sets

Fable review fixes: the support plan and README no longer say the
policyengine-uk adapter is unimplemented; the receipts paybill caveat no
longer describes an imputation this PR does not contain; note 4 cites the
ONS publication; a FirmRuleOutputs without the VAT output raises ValueError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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

2 participants