Skip to content

Instantly share code, notes, and snippets.

@mhnatiuk
Last active August 29, 2015 14:24
Show Gist options
  • Save mhnatiuk/a0f45a344830abbab6c5 to your computer and use it in GitHub Desktop.
Save mhnatiuk/a0f45a344830abbab6c5 to your computer and use it in GitHub Desktop.
Calculate lower bound of Wilson score confidence interval for a Bernoulli parameter
import math
import scipy.stats as stats
"""
Algorithms desgin:
http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
What: Calculate lower bound of Wilson score confidence interval for a Bernoulli parameter
Why: Sort data by binomial votes score
"""
def ci_lower_bound(pos, n, confidence):
if n == 0:
return 0
z = stats.norm.ppf(1-(1-confidence)/2)
phat = 1.0*pos/n
return (phat + z*z/(2*n) - z * math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
if __name__ == "__main__":
from sys import argv
#pos, n , confidence = (sys.argv[1], sys.argv[2], sys.argv[3])
print ci_lower_bound(float(argv[1]), float(argv[2]), float(argv[3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment