Skip to content

Instantly share code, notes, and snippets.

@amix
Created March 24, 2013 02:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amix/5230165 to your computer and use it in GitHub Desktop.
Save amix/5230165 to your computer and use it in GitHub Desktop.
The confidence sort algorithm is implemented in _sorts.pyx, I have rewritten their Pyrex implementation into pure Python (do also note that I have removed their caching optimization)
#Rewritten code from /r2/r2/lib/db/_sorts.pyx
from math import sqrt
def _confidence(ups, downs):
n = ups + downs
if n == 0:
return 0
z = 1.0 #1.0 = 85%, 1.6 = 95%
phat = float(ups) / n
return sqrt(phat+z*z/(2*n)-z*((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
def confidence(ups, downs):
if ups + downs == 0:
return 0
else:
return _confidence(ups, downs)
@oliverw1
Copy link

quick note (little time): The sqrt should only be over the 3rd term. In your line 13, it's over all three terms. You're doing it correctly in sorts.py though.

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