Skip to content

Shared firm mechanics: band draws, thinning, registration, rules seam, engine inputs, receipts - #1756

Draft
vahid-ahmadi wants to merge 5 commits into
mainfrom
firm-shared-mechanics
Draft

vahid-ahmadi wants to merge 5 commits into
mainfrom
firm-shared-mechanics

Conversation

@vahid-ahmadi

Copy link
Copy Markdown
Contributor

Refs #1705, #1706.

Draft. This is the first PR of the #1706 split agreed in this comment. It adds the firm mechanics that don't depend on a country to a new shared package, microcosm.build.firms. #1706 will then be rebuilt as the UK port on top of it.

The package holds no UK data. Band edges, band roles, registered counts, the engine package and the receipts file all come in as arguments.

What's in it

Module What it does
firms/bands.py draw_in_band maps uniforms the caller draws into [max(lower, floor), upper), log-uniform for an open band. It returns the input's type and dtype (numpy or torch), so the caller keeps its own random stream.
firms/thinning.py stratified_thin keeps rows at one rate inside an analysis window and another outside it, per stratum (the caller's keys × window side). It tops each stratum up to a floor and returns exact base weights.
firms/registration.py Two universes, a business register and a tax-registration count, behind a RegistrationSpec. Each band is excluded, registrable (below a threshold: all in scope, some registered) or selected (above it: the selected firms are in scope and registered). registration_propensity gives each band's registered count ÷ frame mass. assign_scope_and_registration draws the flags with a seeded weighted selection per band.
firms/rules.py The seam to a rule engine. FirmRules.evaluate(firms, *, period) returns FirmRuleOutputs: named per-firm outputs keyed by firm_id. align_rule_outputs refuses a missing, repeated or unrequested id, and weighted_totals sums outputs by weight.
firms/engine_inputs.py read_entity_variables reads a policyengine-core package's variables on an entity from source with ast, without importing the package. input_status_problems checks a build's set / derived / assumed / not_set record against the engine's inputs. export_schema_problems checks an export's columns, dtypes and enum values.
firms/receipts.py Model totals beside sourced official receipts, as a diagnostic table. An unset input prints "input not populated" rather than a −100% gap, and every official figure must carry its publisher, title, edition, URL and access date.

What is not in it, and why

  • 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 Add firm entity and firm-side tax rules policyengine-uk#2940.
  • A build-receipt helper. main already has trace.sha256_file and artifact_files, and the UK firm build will use those.
  • frame_sampling.sample_frame_households is not reused for thinning. It samples households in a Frame at an exact count per stratum, with one fraction and no base weights. Firm thinning runs before a Frame exists, uses two keep rates, floors each cell and returns the base weights the solver starts from.
  • The country-specific parts stay in uk_runtime: the ONS and HMRC sources, the VAT and legal-status mappings, the policyengine-uk column names, the rule-period map, money formatting and caveat text.

Equivalence with the UK port

Each function was checked against its original on #1706's branch (f644ab023) with UK-shaped arguments, using uncommitted differential scripts.

  • Thinning, propensity, and scope and registration: 76 cases over random draws, with identical arrays.
  • Band draw: identical torch float32 values from the same global stream, compared against the UK draw formula.
  • Engine-input reader: on a policyengine-uk 0d1c25010 export it gives the same records as the UK snapshot tool (148 firm variables, 71 inputs), equal to the committed snapshot.
  • Receipts: the report is byte-identical to the UK tool's on the UK receipts file.

The rules seam is the exception. Its return type is now a mapping of named outputs, so the UK wrapper in #1706 will adapt to it. #1706 will pin that its outputs are unchanged once it imports from here.

Tests

Four new engine-free shared test files (56 tests):

  • test_firm_universe_mechanics.py;
  • test_firm_rules_seam.py;
  • test_firm_engine_inputs.py (a toy package written to tmp_path);
  • test_firm_receipts_comparison.py (a toy receipts CSV).

The registration tests use a UK-shaped eight-band spec and a four-band spec with no excluded or registrable band, so the general case is tested, not just the UK shape.

Locally, those four files pass, along with the shared guard tests that scan packages and seeds. ruff and tools/ci_test_plan.py verify pass. The full engine-free suite was not run locally.

🤖 Generated with Claude Code

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>

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.

1 participant