#467Wilson Confidence Interval (Proportion)EasyStatisticsAsked atMicrosoft · Meta · Google
Wilson Confidence Interval (Proportion)
Background
The Wilson score interval is the proper confidence interval for a proportion. It corrects the canonical Wald interval's pathologies at small and near or , where Wald can give nonsensical bounds outside .
Problem statement
Implement wilson_ci(successes, n, alpha=0.05, z=None):
Return (lo, hi) clipped to .
Input
successes-int >= 0.n-int >= 1, total trials, successes.alpha- significance (default → 95% CI).z- optional override of the z-score (default:1.96foralpha=0.05).
Output
(lo, hi)- tuple of plain Python floats.
Examples
Input: successes=8, n=10, alpha=0.05
Output: roughly (0.49, 0.94)
Constraints
- ValueError on bad inputs (n < 1, successes < 0, successes > n).
- Output bounded in .
Notes
- vs Wald. Wald = . At Wald gives ; Wilson gives a non-degenerate upper bound.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •example: successes=8, n=10, 95% CI
- •reference: symmetric interval for s=50, n=100
- •sample: zero successes gives non-degenerate upper bound