Skip to content

Fix error bars in code-capacity estimation - #580

Merged
perlinm merged 10 commits into
mainfrom
fix/jeffreys-error-bars
Aug 30, 2026
Merged

Fix error bars in code-capacity estimation#580
perlinm merged 10 commits into
mainfrom
fix/jeffreys-error-bars

Conversation

@perlinm

@perlinm perlinm commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

AI-assisted summary

ErrorRateFunc in codes/_monte_carlo.py estimates 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 through sqrt(weight_probs**2 @ variances). The per-weight variance was set to f(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 by x events in n trials), phi*(1 - phi)/(n + 2) with phi = (x + 1/2)/(n + 1), in a new _jeffreys_variance helper 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 variance 1/8 rather than a confident zero.

Construction of ErrorRateFunc now 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 a nan error 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.ipynb are regenerated so its plotted error bands reflect the new variance.

perlinm and others added 10 commits August 28, 2026 12:29
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>
@perlinm
perlinm merged commit 5390911 into main Aug 30, 2026
3 checks passed
@perlinm
perlinm deleted the fix/jeffreys-error-bars branch August 30, 2026 18:51
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