Last active
July 23, 2019 19:18
-
-
Save loisaidasam/4e174fb9f56b05ae549b7b5798cc7f90 to your computer and use it in GitHub Desktop.
Python implementation - Lower bound of Wilson score confidence interval for a Bernoulli parameter
This file contains hidden or 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
| """ | |
| 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doh!!! Thanks @pierre-vr