Proportion Confidence Interval (cross-cutting/numerics/proportion-confidence-interval)
Use when you must estimate a confidence interval for a binomial
proportion from attribute data (pass-fail, go-no-go, defect counts):
the Wilson score interval, its continuity-corrected variant, the exact
Clopper-Pearson interval computed by inverting the binomial tail through
an in-leaf regularized incomplete beta, and the normal-approximation
interval for the difference of two proportions. All quantiles and beta
values come from the module's own inversion machinery, pure Python
stdlib with no external statistics packages. It pairs with
cross-cutting/numerics/confidence-interval-estimation for intervals on
continuous measurements and with cross-cutting/numerics/hypothesis-testing
for the verdict layer on the same attribute data.
Domain quick reference
- Conventions: k successes in n trials, confidence level cl in (0, 1)
(default 0.95), z = normal_quantile((1 + cl) / 2) computed in-leaf
(1.959964 for 0.95). All single-proportion bounds live in [0, 1].
- Wilson score interval: center = (phat + z^2 / (2n)) / (1 + z^2 / n),
half-width = z * sqrt(phat(1 - phat) / n + z^2 / (4n^2)) /
(1 + z^2 / n), bounds = center +/- half-width with k = 0 forcing lower
0 and k = n forcing upper 1. Good default for most attribute claims.
- Continuity-corrected Wilson: the Newcombe form, which moves the
interval outward by the 1/(2n) correction term; it is always at least
as wide as the plain Wilson interval and keeps the same endpoints at
k = 0 and k = n.
- Clopper-Pearson exact interval: lower solves I_p(k, n - k + 1) =
alpha / 2 and upper solves I_p(k + 1, n - k) = 1 - alpha / 2, where
I_p is the regularized incomplete beta evaluated in-leaf with the
symmetry transform (below the crossover (a + 1) / (a + b + 2) use
betacf(a, b, x) / a, at or above it use 1 - bt * betacf(b, a, 1 - x) /
b) and inverted by bisection to 1e-10. k = 0 gives lower 0 and k = n
gives upper 1 by endpoint handling.
- Two-proportion difference (normal approximation): diff = p1 - p2 with
standard error sqrt(p1(1 - p1) / n1 + p2(1 - p2) / n2); the interval
is diff +/- z * se and the reported width is the margin z * se of the
worked example.
- The Clopper-Pearson interval is exact but conservative (guaranteed
coverage at or above the level); Wilson is approximate with good
coverage that is closer to nominal, so Wilson is the default and
Clopper-Pearson the audit-grade check.
- NACA TR-824 frames the aeronautics numerics context; the relations
above are standard engineering methodology, summary-only.
Workflow
- State the attribute data: k successes in n trials (or the defect
count for a fraction-defective claim) and the confidence level cl.
- Compute the Wilson score interval with wilson_score_interval(k, n,
cl) as the default estimate for the proportion.
- If the sample is small or the claim is audit-grade, cross-check with
the exact clopper_pearson_interval(k, n, cl) and report the exact
bound when it differs materially.
- When a continuity correction is wanted for conservatism near the
edges, run wilson_score_cc_interval(k, n, cl) alongside the plain
Wilson interval.
- For a difference between two groups (configuration A versus B,
batch versus batch), run two_proportion_diff_interval(k1, n1, k2,
n2, cl) and report diff, the bounds, and the width.
- Form the method recommendation: Wilson for the default gate,
Clopper-Pearson when the claim must hold at the stated confidence,
and the two-proportion interval when the comparison is the claim.
- Confirm every result with the deterministic checks and the contract
test scripts/test_proportion_confidence_interval.py.
Worked example
Attribute data with 12 successes in 400 trials at cl = 0.95
(phat = 0.03), module output in brackets:
- Wilson: [0.017243, 0.051699], width 0.034456; a 95% claim on the
pass rate of 3% runs from 1.72% to 5.17%.
- Clopper-Pearson: [0.015596, 0.051817], width 0.036222; the exact
interval is wider, as expected at finite n.
- Zero-success edge: k = 0, n = 30 gives Wilson [0.0, 0.113513] and
Clopper-Pearson [0.0, 0.115703]; the Clopper-Pearson upper obeys the
rule of three: 1 - 0.025^(1/30) = 0.1157.
- All-success edge: k = 30, n = 30 gives Wilson [0.886487, 1.0] and
Clopper-Pearson [0.884297, 1.0].
- Two-proportion: k1 = 5, n1 = 100 versus k2 = 1, n2 = 100 gives diff
0.04, width 0.046957 and interval [-0.006957, 0.086957]; the interval
contains zero, so the 5% versus 1% observed rates are not separated
at 95% confidence with these sample sizes.
Verification
- Confirm wilson_score_interval(12, 400) returns lower 0.017243 and
upper 0.051699, within 1e-4 of the spec anchors.
- Confirm clopper_pearson_interval(12, 400) returns lower 0.015596 and
upper 0.051817, and clopper_pearson_interval(0, 30) upper 0.115703.
- Confirm both intervals contain phat = k / n at several (k, n), and
that the width shrinks as n grows at fixed phat.
- Confirm the two-proportion width for (5, 100) versus (1, 100) is
0.04696 within 1e-3.
- Confirm k outside [0, n], n <= 0 and cl outside (0, 1) raise
ValueError in every interval function.
- Confirm the dict keys are exactly lower/upper/width (plus diff for
the two-proportion function) and that every bound stays in [0, 1]
for the single-proportion methods.
- Run the contract test offline: python3
scripts/test_proportion_confidence_interval.py (35 tests,
deterministic, under 1 second).
Related leaves
- cross-cutting/numerics/confidence-interval-estimation: parametric
intervals for a mean, a difference of means and a variance from
continuous measurement data.
- cross-cutting/numerics/hypothesis-testing: significance tests that
consume the same attribute data and produce the verdict layer.
- cross-cutting/numerics/descriptive-statistics: the sample statistics
(counts, rates) that feed the proportion intervals.
- cross-cutting/numerics/probability-distributions: binomial and beta
distribution context behind the inversion methods.
Pitfalls
- Reporting a Wald interval for a small or edge sample: the
normal-approximation (Wald) interval phat +/- z * sqrt(phat(1 -
phat) / n) collapses to a point at k = 0 or k = n and is badly
anti-conservative at small n; the Wilson interval in this leaf does
not have that failure and is the default.
- Quoting the continuity-corrected Wilson interval without saying so:
the Newcombe form is visibly wider than the plain Wilson interval
(0/30 upper 0.1413 versus 0.1135), so mixing the two variants in one
report inflates or deflates the claim silently.
- Treating Clopper-Pearson as the tightest bound: it is exact in
coverage but conservative, so its width at small n can exceed the
Wilson width by a wide margin (3/7 gives lower 0.099 versus 0.158);
pick it for guaranteed coverage, not for tightness.
- Reading a difference-of-proportions interval that straddles zero as a
verdict: a 95% interval containing zero means the observed rates are
not separated at that level, but the verdict layer belongs to
cross-cutting/numerics/hypothesis-testing, not to this leaf.
- Forgetting the k = 0 and k = n endpoint handling: the incomplete beta
inversion is degenerate at the edges, so the module forces lower 0 at
k = 0 and upper 1 at k = n; without it the exact interval would
misreport a zero-failure claim.
- Inverting the beta without the symmetry transform: a continued
fraction run only in one direction loses accuracy above the crossover
(a + 1) / (a + b + 2) and the Clopper-Pearson upper bounds drift;
the transform branch is required for the exact bounds.
Behavior contract (gate 3)
Run the deterministic contract test (stdlib unittest, offline):
python3 scripts/test_proportion_confidence_interval.py
The test covers the spec anchors (Wilson 12/400 [0.017243, 0.051699],
0/30 [0.0, 0.113513], 30/30 [0.886487, 1.0]; Clopper-Pearson 12/400
[0.015596, 0.051817], 0/30 [0.0, 0.115703], 30/30 [0.884297, 1.0];
two-proportion diff 0.04 with width 0.04696), phat containment, width
shrinkage with n, the closed-form Wilson center identity, the exact
binomial-tail inversion identity for Clopper-Pearson, beta symmetry
across the crossover, the rule-of-three bound, convergence of
Clopper-Pearson to Wilson at large n, dict key contracts, determinism,
and ValueError rejection of every non-physical input class.
Compliance
- Standards referenced, not reproduced: NACA TR-824 is the aeronautics
numerics reference id per standards-map.yaml; the interval relations
above are standard engineering methodology, summary-only.
- compliance: STANDARDS-REF, gated: false.
1---2name: proportion-confidence-interval3description: Use when you must estimate a confidence interval for a binomial proportion: compute the Wilson score interval and its continuity-corrected variant, compute the exact Clopper-Pearson interval by inverting the binomial tail through an in-leaf regularized incomplete beta, and compute the confidence interval for the difference of two proportions from attribute data at a stated confidence level. Produces the lower and upper bounds by each method, the interval width, and the method recommendation that gates pass-rate, yield, and fraction-defective claims. Trigger: wilson score interval, clopper pearson interval, binomial proportion confidence bound, exact proportion interval, two proportion difference, pass fail rate.4license: Apache-2.05---67# Proportion Confidence Interval (cross-cutting/numerics/proportion-confidence-interval)89Use when you must estimate a confidence interval for a binomial10proportion from attribute data (pass-fail, go-no-go, defect counts):11the Wilson score interval, its continuity-corrected variant, the exact12Clopper-Pearson interval computed by inverting the binomial tail through13an in-leaf regularized incomplete beta, and the normal-approximation14interval for the difference of two proportions. All quantiles and beta15values come from the module's own inversion machinery, pure Python16stdlib with no external statistics packages. It pairs with17cross-cutting/numerics/confidence-interval-estimation for intervals on18continuous measurements and with cross-cutting/numerics/hypothesis-testing19for the verdict layer on the same attribute data.2021## Domain quick reference2223- Conventions: k successes in n trials, confidence level cl in (0, 1)24 (default 0.95), z = normal_quantile((1 + cl) / 2) computed in-leaf25 (1.959964 for 0.95). All single-proportion bounds live in [0, 1].26- Wilson score interval: center = (phat + z^2 / (2n)) / (1 + z^2 / n),27 half-width = z * sqrt(phat(1 - phat) / n + z^2 / (4n^2)) /28 (1 + z^2 / n), bounds = center +/- half-width with k = 0 forcing lower29 0 and k = n forcing upper 1. Good default for most attribute claims.30- Continuity-corrected Wilson: the Newcombe form, which moves the31 interval outward by the 1/(2n) correction term; it is always at least32 as wide as the plain Wilson interval and keeps the same endpoints at33 k = 0 and k = n.34- Clopper-Pearson exact interval: lower solves I_p(k, n - k + 1) =35 alpha / 2 and upper solves I_p(k + 1, n - k) = 1 - alpha / 2, where36 I_p is the regularized incomplete beta evaluated in-leaf with the37 symmetry transform (below the crossover (a + 1) / (a + b + 2) use38 betacf(a, b, x) / a, at or above it use 1 - bt * betacf(b, a, 1 - x) /39 b) and inverted by bisection to 1e-10. k = 0 gives lower 0 and k = n40 gives upper 1 by endpoint handling.41- Two-proportion difference (normal approximation): diff = p1 - p2 with42 standard error sqrt(p1(1 - p1) / n1 + p2(1 - p2) / n2); the interval43 is diff +/- z * se and the reported width is the margin z * se of the44 worked example.45- The Clopper-Pearson interval is exact but conservative (guaranteed46 coverage at or above the level); Wilson is approximate with good47 coverage that is closer to nominal, so Wilson is the default and48 Clopper-Pearson the audit-grade check.49- NACA TR-824 frames the aeronautics numerics context; the relations50 above are standard engineering methodology, summary-only.5152## Workflow53541. State the attribute data: k successes in n trials (or the defect55 count for a fraction-defective claim) and the confidence level cl.562. Compute the Wilson score interval with wilson_score_interval(k, n,57 cl) as the default estimate for the proportion.583. If the sample is small or the claim is audit-grade, cross-check with59 the exact clopper_pearson_interval(k, n, cl) and report the exact60 bound when it differs materially.614. When a continuity correction is wanted for conservatism near the62 edges, run wilson_score_cc_interval(k, n, cl) alongside the plain63 Wilson interval.645. For a difference between two groups (configuration A versus B,65 batch versus batch), run two_proportion_diff_interval(k1, n1, k2,66 n2, cl) and report diff, the bounds, and the width.676. Form the method recommendation: Wilson for the default gate,68 Clopper-Pearson when the claim must hold at the stated confidence,69 and the two-proportion interval when the comparison is the claim.707. Confirm every result with the deterministic checks and the contract71 test scripts/test_proportion_confidence_interval.py.7273## Worked example7475Attribute data with 12 successes in 400 trials at cl = 0.9576(phat = 0.03), module output in brackets:7778- Wilson: [0.017243, 0.051699], width 0.034456; a 95% claim on the79 pass rate of 3% runs from 1.72% to 5.17%.80- Clopper-Pearson: [0.015596, 0.051817], width 0.036222; the exact81 interval is wider, as expected at finite n.82- Zero-success edge: k = 0, n = 30 gives Wilson [0.0, 0.113513] and83 Clopper-Pearson [0.0, 0.115703]; the Clopper-Pearson upper obeys the84 rule of three: 1 - 0.025^(1/30) = 0.1157.85- All-success edge: k = 30, n = 30 gives Wilson [0.886487, 1.0] and86 Clopper-Pearson [0.884297, 1.0].87- Two-proportion: k1 = 5, n1 = 100 versus k2 = 1, n2 = 100 gives diff88 0.04, width 0.046957 and interval [-0.006957, 0.086957]; the interval89 contains zero, so the 5% versus 1% observed rates are not separated90 at 95% confidence with these sample sizes.9192## Verification9394- Confirm wilson_score_interval(12, 400) returns lower 0.017243 and95 upper 0.051699, within 1e-4 of the spec anchors.96- Confirm clopper_pearson_interval(12, 400) returns lower 0.015596 and97 upper 0.051817, and clopper_pearson_interval(0, 30) upper 0.115703.98- Confirm both intervals contain phat = k / n at several (k, n), and99 that the width shrinks as n grows at fixed phat.100- Confirm the two-proportion width for (5, 100) versus (1, 100) is101 0.04696 within 1e-3.102- Confirm k outside [0, n], n <= 0 and cl outside (0, 1) raise103 ValueError in every interval function.104- Confirm the dict keys are exactly lower/upper/width (plus diff for105 the two-proportion function) and that every bound stays in [0, 1]106 for the single-proportion methods.107- Run the contract test offline: python3108 scripts/test_proportion_confidence_interval.py (35 tests,109 deterministic, under 1 second).110111## Related leaves112113- cross-cutting/numerics/confidence-interval-estimation: parametric114 intervals for a mean, a difference of means and a variance from115 continuous measurement data.116- cross-cutting/numerics/hypothesis-testing: significance tests that117 consume the same attribute data and produce the verdict layer.118- cross-cutting/numerics/descriptive-statistics: the sample statistics119 (counts, rates) that feed the proportion intervals.120- cross-cutting/numerics/probability-distributions: binomial and beta121 distribution context behind the inversion methods.122123## Pitfalls124125- Reporting a Wald interval for a small or edge sample: the126 normal-approximation (Wald) interval phat +/- z * sqrt(phat(1 -127 phat) / n) collapses to a point at k = 0 or k = n and is badly128 anti-conservative at small n; the Wilson interval in this leaf does129 not have that failure and is the default.130- Quoting the continuity-corrected Wilson interval without saying so:131 the Newcombe form is visibly wider than the plain Wilson interval132 (0/30 upper 0.1413 versus 0.1135), so mixing the two variants in one133 report inflates or deflates the claim silently.134- Treating Clopper-Pearson as the tightest bound: it is exact in135 coverage but conservative, so its width at small n can exceed the136 Wilson width by a wide margin (3/7 gives lower 0.099 versus 0.158);137 pick it for guaranteed coverage, not for tightness.138- Reading a difference-of-proportions interval that straddles zero as a139 verdict: a 95% interval containing zero means the observed rates are140 not separated at that level, but the verdict layer belongs to141 cross-cutting/numerics/hypothesis-testing, not to this leaf.142- Forgetting the k = 0 and k = n endpoint handling: the incomplete beta143 inversion is degenerate at the edges, so the module forces lower 0 at144 k = 0 and upper 1 at k = n; without it the exact interval would145 misreport a zero-failure claim.146- Inverting the beta without the symmetry transform: a continued147 fraction run only in one direction loses accuracy above the crossover148 (a + 1) / (a + b + 2) and the Clopper-Pearson upper bounds drift;149 the transform branch is required for the exact bounds.150151## Behavior contract (gate 3)152153Run the deterministic contract test (stdlib unittest, offline):154155 python3 scripts/test_proportion_confidence_interval.py156157The test covers the spec anchors (Wilson 12/400 [0.017243, 0.051699],1580/30 [0.0, 0.113513], 30/30 [0.886487, 1.0]; Clopper-Pearson 12/400159[0.015596, 0.051817], 0/30 [0.0, 0.115703], 30/30 [0.884297, 1.0];160two-proportion diff 0.04 with width 0.04696), phat containment, width161shrinkage with n, the closed-form Wilson center identity, the exact162binomial-tail inversion identity for Clopper-Pearson, beta symmetry163across the crossover, the rule-of-three bound, convergence of164Clopper-Pearson to Wilson at large n, dict key contracts, determinism,165and ValueError rejection of every non-physical input class.166167## Compliance168169- Standards referenced, not reproduced: NACA TR-824 is the aeronautics170 numerics reference id per standards-map.yaml; the interval relations171 above are standard engineering methodology, summary-only.172- compliance: STANDARDS-REF, gated: false.