Skip to content

Instantly share code, notes, and snippets.

@axiak
Created June 14, 2012 20:24
Show Gist options
  • Save axiak/2932725 to your computer and use it in GitHub Desktop.
Save axiak/2932725 to your computer and use it in GitHub Desktop.
Weight a vote for an object based on a user's previous voting history
from math import sqrt
def confidence_bounds(ups, downs):
#stolen from reddit!
n = ups + downs
if n == 0:
return 0, 0
z = 1.0 #1.0 = 85%, 1.6 = 95%
phat = float(ups) / n
confidence_positive = sqrt(phat+z*z/(2*n)-z*((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
phat = float(downs) / n
confidence_negative = sqrt(phat+z*z/(2*n)-z*((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
return confidence_positive, confidence_negative
UP_VOTE, DOWN_VOTE = 0, 1
def score_weight(user_ups, user_downs, vote_type):
# vote_type is either UP_VOTE or DOWN_VOTE
vote_index = 0 if vote_type == UP_VOTE else 1
bounds = confidence_bounds(user_ups, user_downs)
# if less than 50% probability of current vote type,
# then return the whole vote
if bounds[vote_index] <= .50:
return 1
other_weight = max(bounds[1 - vote_index], 0.5)
return 1 - 2 * (bounds[vote_index] - other_weight)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment