Created
June 14, 2012 20:24
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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