Skip to content

Instantly share code, notes, and snippets.

@yoavram
Last active July 27, 2017 15:09
Show Gist options
  • Select an option

  • Save yoavram/963892760c83e6a984e10bd1ee2bf762 to your computer and use it in GitHub Desktop.

Select an option

Save yoavram/963892760c83e6a984e10bd1ee2bf762 to your computer and use it in GitHub Desktop.
Function to calculate Jefferey's Interval for Binomial proportion confidence interval. See https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Jeffreys_interval
def jeffreys_interval(trials, successes, α=0.05):
n = int(trials)
x = int(successes)
beta = scipy.stats.beta(x + 0.5, n-x+0.5)
if x == 0:
low = 0
else:
low = beta.ppf(α/2)
if x == n:
high = 1
else:
high = beta.ppf(1-α/2)
return x/n-low, high-x/n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment