Repository navigation
Shared firm mechanics: band draws, thinning, registration, rules seam, engine inputs, receipts - #1756
Draft
vahid-ahmadi wants to merge 5 commits into
Draft
vahid-ahmadi wants to merge 5 commits into
vahid-ahmadi wants to merge 5 commits into
Conversation
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 was referenced Nov 26, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
firms/bands.pydraw_in_bandmaps 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.pystratified_thinkeeps 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.pyRegistrationSpec. Each band isexcluded,registrable(below a threshold: all in scope, some registered) orselected(above it: the selected firms are in scope and registered).registration_propensitygives each band's registered count ÷ frame mass.assign_scope_and_registrationdraws the flags with a seeded weighted selection per band.firms/rules.pyFirmRules.evaluate(firms, *, period)returnsFirmRuleOutputs: named per-firm outputs keyed byfirm_id.align_rule_outputsrefuses a missing, repeated or unrequested id, andweighted_totalssums outputs by weight.firms/engine_inputs.pyread_entity_variablesreads a policyengine-core package's variables on an entity from source withast, without importing the package.input_status_problemschecks a build's set / derived / assumed / not_set record against the engine's inputs.export_schema_problemschecks an export's columns, dtypes and enum values.firms/receipts.pyWhat is not in it, and why
Frame. It needsEntitySchemato 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.mainalready hastrace.sha256_fileandartifact_files, and the UK firm build will use those.frame_sampling.sample_frame_householdsis not reused for thinning. It samples households in aFrameat an exact count per stratum, with one fraction and no base weights. Firm thinning runs before aFrameexists, uses two keep rates, floors each cell and returns the base weights the solver starts from.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.0d1c25010export it gives the same records as the UK snapshot tool (148 firm variables, 71 inputs), equal to the committed snapshot.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 totmp_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 verifypass. The full engine-free suite was not run locally.🤖 Generated with Claude Code