Last active
July 27, 2017 15:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bin_prop_jeffreys_interval(trials, successes, α=0.05): | |
"""Binomial proportions confidence Jeffrey's interval. | |
Parameters | |
---------- | |
trials : np.ndarray | |
number of trials | |
successes : int | |
number of successes | |
α : float, 0<α<1 | |
width of confidence interval | |
Returns | |
------- | |
low, high : tuple of floats | |
The lower and upper bounds of the proportions confidence interval | |
See | |
--- | |
https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Jeffreys_interval | |
""" | |
import scipy.stats | |
n = int(trials) | |
x = int(successes) | |
assert 0 <= x <= n | |
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