Skip to content

Instantly share code, notes, and snippets.

@loisaidasam loisaidasam/wilson.py
Last active Jul 1, 2018

Embed
What would you like to do?
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):
z = 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

This comment has been minimized.

Copy link

commented Nov 9, 2017

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

This comment has been minimized.

Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.