Python implementation of the Wilson Score Interval?

前端 未结 5 585
-上瘾入骨i
-上瘾入骨i 2021-01-30 04:18

After reading How Not to Sort by Average Rating, I was curious if anyone has a Python implementation of a Lower bound of Wilson score confidence interval for a Bernoulli paramet

5条回答
  •  无人共我
    2021-01-30 05:06

    The accepted solution seems to use a hard-coded z-value (best for performance).

    In the event that you wanted a direct python equivalent of the ruby formula from the blogpost with a dynamic z-value (based on the confidence interval):

    import math
    
    import scipy.stats as st
    
    
    def ci_lower_bound(pos, n, confidence):
        if n == 0:
            return 0
        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)
    

提交回复
热议问题