This is a small package that provides functions to compute the confidence interval for a binomial proportion. I made it because I spend altogether too much time staring at the Binomial proportion confidence interval wiki page.
Presently, the package implements:
- The Normal Approximation
- The Wilson Interval (no continuity correction)
- Jeffrey's interval (via scipy.stats.beta)
- Clopper-Pearson interval (also via scipy.stats.beta)
If you haven't spent a lot of time thinking about which interval you should use (and why would you want to?), I suggest using the Wilson interval or Jeffrey's interval. Jeffrey's interval is returned by default by the binomial_confidence
function in this package.
You oughtn't use the normal approximation if you don't have to. It produces patently inaccurate values with low/high probabilities at low Ns. The plot at the top of this readme shows the normal approximation producing lower lower bounds of less than 0 in these cases.
pip install binoculars
from binoculars import binomial_confidence
N, p = 100, 0.2
binomial_confidence(p, N) # default to jeffrey's interval
# (0.1307892803998113, 0.28628125447599173)
binomial_confidence(p, N, tail='lower') # grab one tail
# 0.1307892803998113
# set Z value
binomial_confidence(p, N, tail='lower', z=2.58)
# 0.11212431621448567
# choose your method
binomial_confidence(p, N, method='normal')
# (0.12160000000000001, 0.2784)
binomial_confidence(p, N, method='wilson')
# (0.1333659225590988, 0.28883096192650237)
binomial_confidence(p, N, method='clopper-pearson')
# (0.1266544343411484, 0.291844378634278
I honestly do not imagine touching this a lot. But maybe you want to add one of the other interval methods?
- Make a python 3.6+ venv
pip install -e .[test]
black lib --check
pytest
- Add confidence intervals for odds ratios, differences
- Add the unimplemented intervals
- Add plots comparing the intervals to readme.