Skip to content

Instantly share code, notes, and snippets.

@paulgb
Created September 19, 2013 17:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paulgb/6627336 to your computer and use it in GitHub Desktop.
Save paulgb/6627336 to your computer and use it in GitHub Desktop.
Compute two-sided binomial confidence interval in Python. Based on R's binom.test.
from scipy.stats import beta
def binom_interval(success, total, confint=0.95):
quantile = (1 - confint) / 2.
lower = beta.ppf(quantile, success, total - success + 1)
upper = beta.ppf(1 - quantile, success + 1, total - success)
return (lower, upper)
@dkirkby
Copy link

dkirkby commented Oct 21, 2015

I don't think this works if success equals zero or total. You need to calculate a one-sided interval in those cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment