Skip to content

Instantly share code, notes, and snippets.

@mattdennewitz
Created November 4, 2009 19:26
Show Gist options
  • Save mattdennewitz/226309 to your computer and use it in GitHub Desktop.
Save mattdennewitz/226309 to your computer and use it in GitHub Desktop.
import sys
from math import log, sqrt
def pnormaldist(qn):
b = [1.570796288, 0.03706987906, -0.8364353589e-3,
-0.2250947176e-3, 0.6841218299e-5, 0.5824238515e-5,
-0.104527497e-5, 0.8360937017e-7, -0.3231081277e-8,
0.3657763036e-10, 0.6936233982e-12]
if qn < 0.0 or 1.0 < qn:
sys.stderr.write("Error : qn <= 0 or qn >= 1 in pnorm()!\n")
return 0.0
if qn == 0.5:
return 0.0
w1 = qn
if qn > 0.5:
w1 = 1.0 - w1
w3 = -log(4.0 * w1 * (1.0 - w1))
w1 = b[0]
for i in range(1, 10):
w1 += b[i] * w3**i
if qn > 0.5:
return sqrt(w1 * w3)
return -sqrt(w1 * w3)
def ci_lower_bound(pos, n, power):
"""Calculate the lower bound of Wilson score confidence interval
for a Bernoulli parameter, whatever that means!
"""
if n == 0:
return 0
z = pnormaldist(1-power/2)
phat = 1.0*pos/n
return (phat + z*z/(2*n) - z * sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
@larojas
Copy link

larojas commented Jan 28, 2014

Isn't the for loop supposed to be over range(1, 11)?

Reasons:

  • b has 11 elements. If we're not accessing b[10] ever, then why is it there.
  • the original Ruby code does "1.upto 10 do |i|", which includes the 10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment