Fix error bars in code-capacity estimation - #580
Merged
Conversation
Replace the plug-in per-weight binomial variance f(1-f)/n, which is zero whenever a weight sees no observed failures, with the Beta(x+1/2, n-x+1/2) posterior variance phi(1-phi)/(n+2). It stays positive at zero failures and reverts to the prior variance 1/8 for a weight with no data, so the reported uncertainty no longer collapses in the rare-event regime. The central rate estimate and the (rate, sigma) contract are unchanged; the weight-0 no-error bin is held at exactly zero. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add a __post_init__ guard rejecting mismatched-shape, empty, negative, or over-counted (failures plus discards exceeding samples) inputs, so a malformed construction fails loudly instead of producing a nan error bar or indexing past the weight-0 bin. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Enforce the weight-0 no-error invariant (num_failures[0] == num_discards[0] == 0) at construction, so both variances[0] = 0 overrides rest on a validated invariant rather than an assumption. Stop fabricating a weight-0 sample in _get_sample_allocation: leave its allocation at zero (weight 0 is not sampled) and guard the empty-allocation case in the trailing-zero truncation directly. Document that the reported uncertainty is a regularized posterior dispersion that need not vanish or be centered on the point estimate. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Fix _get_error_probs_by_weight to treat max_weight=0 as a request for the weight-0 bin only, rather than falsy "unset" that expands to block_length; this let a single-weight ErrorRateFunc crash in __call__ on a size mismatch. Guard _get_sample_allocation against a distribution with no weight >= 1 mass (max_error_rate 0 or an empty code), which previously divided by zero and produced a nan allocation; it now returns a lone weight-0 bin. Trim the high-level ErrorRateFunc docstring, leaving the variance rationale in infidelity_variances and _jeffreys_variance. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Describe the returned uncertainty as a posterior standard deviation in the public get_logical_error_rate_func docstrings, and give discard_rate_variances the same weight-0 explanation as infidelity_variances. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
AI-assisted summary
ErrorRateFuncincodes/_monte_carlo.pyestimates a logical error (or discard) rate as a weighted sum of independent per-weight binomial rates, and reports an uncertainty by propagating per-weight variances throughsqrt(weight_probs**2 @ variances). The per-weight variance was set tof(1 - f)/n, which is exactly zero whenever a contributing weight sees zero observed failures.This PR replaces the per-weight variance with the variance of the
Beta(x + 1/2, n - x + 1/2)posterior (a Jeffreys prior updated byxevents inntrials),phi*(1 - phi)/(n + 2)withphi = (x + 1/2)/(n + 1), in a new_jeffreys_variancehelper used by both the infidelity and discard-rate variances. The posterior variance stays positive at zero observed events and stays finite when a weight has no data at all, reverting there to the prior variance1/8rather than a confident zero.Construction of
ErrorRateFuncnow validates its count arrays — equal shapes, non-negative counts, failures plus discards within the sample count, and no failures or discards at the weight-0 no-error bin — so malformed data fails at build time rather than producing ananerror bar. The sampling helpers also handle degenerate weight distributions (a zero maximum error rate or an empty code) without dividing by zero, returning a lone weight-0 bin.The stored outputs of
examples/logical_error_rates/1_code_capacity.ipynbare regenerated so its plotted error bands reflect the new variance.