Instantly share code, notes, and snippets.

# loisaidasam/wilson.py Last active Jul 23, 2019

Python implementation - Lower bound of Wilson score confidence interval for a Bernoulli parameter
 """ Python implementation - Lower bound of Wilson score confidence interval for a Bernoulli parameter - http://www.evanmiller.org/how-not-to-sort-by-average-rating.html - https://news.ycombinator.com/item?id=15131611 - https://stackoverflow.com/questions/10029588/python-implementation-of-the-wilson-score-interval/45965534 - https://stackoverflow.com/questions/10029588/python-implementation-of-the-wilson-score-interval/45965534#45965534 """ import math import scipy.stats as st def get_z(confidence): return st.norm.ppf(1 - (1 - confidence) / 2) def ci_lower_bound(pos, n, confidence=None, z=None): if n == 0: return 0 if z is None: z = get_z(confidence) 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)

### pierre-vr commented Nov 9, 2017 • edited

 You current get_z method does not return anything. I would suggest to put this : `def get_z(confidence): return st.norm.ppf(1 - (1 - confidence) / 2)`

### funk44 commented Jul 1, 2018

 ``````def ci_lower_bound(pos, n, confidence=None, z=None): if n == 0: return 0 if z is None: z = st.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) ``````
Owner Author

### loisaidasam commented Jul 23, 2019

 Doh!!! Thanks @pierre-vr