Confidence Interval for a Conversion Rate (Wilson)

medium
Unit 13 · Statistics & Time Series0/3 to milestone

"3 out of 4 users clicked" isn't the same as "750 out of 1000". The textbook interval p^±zp^(1−p^)/n\hat p \pm z\sqrt{\hat p(1-\hat p)/n} breaks down for small samples and rates near 0 or 1, and can even go below 0. The Wilson score interval fixes that:

centre=p^+z22n1+z2n,half-width=z1+z2np^(1−p^)n+z24n2\text{centre} = \frac{\hat p + \frac{z^2}{2n}}{1 + \frac{z^2}{n}}, \qquad \text{half-width} = \frac{z}{1 + \frac{z^2}{n}}\sqrt{\frac{\hat p(1-\hat p)}{n} + \frac{z^2}{4n^2}}

Implement wilson_interval(successes, n, z=1.96) returning (low, high), and wald_interval(successes, n, z=1.96) (the naive version, clipped to [0, 1]) for comparison. Then implement rank_by_lower_bound(items), which takes (name, successes, n) and sorts by the Wilson lower bound, highest first. This is the classic way to rank items by rating without letting a single 5-star review win.

#statistics#confidence-intervals#experimentation2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Confidence Interval for a Conversion Rate (Wilson) · TechGradify