Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement the Bayesian average algorithm for a binary rating system

I have a system where people can up vote or down vote an item and I want to display the results of that as a 5 star rating.

I have been trying use the Bayesian Rating algorithm explained here and here with no success.

For example: I have three items (A, B and C) in my database:

A = 500 UP and 500 down votes B = 0 UP and 1000 down votes C = 0 UP and 1000 down votes

How do i calculate the Bayesian average rating for each item so that it has a score on a scale of 1 to 5?

like image 452
gath Avatar asked Jul 01 '10 14:07

gath


2 Answers

This blog post, How Not To Sort By Average Rating, describes exactly your situation, and how to solve it using a Wilson Score confidence interval. Reddit used this to good effect.

like image 101
Nick Johnson Avatar answered Nov 17 '22 15:11

Nick Johnson


Simple Algebra:

AvgVotes = Sum of all votes / Sum of all items

AvgRating = Sum of up votes for all items * 5 / Sum of all votes

CurVotes = Number of votes on current item

CurRating = Sum of up votes on current item * 5/ Number of votes on current item

TotalVotes = Sum of all votes + Sum of votes on current item

((AvgVotes * AvgRating) + (CurVotes * CurRating)) * 5 / TotalVotes

So plugging in your numbers evaluating the weight for A...

AvgVotes = 1000

AvgRating = 0 (Remember do not include numbers for the item you are evaluating in this calculation)

CurVotes = 1000

CurRating = 500 * 5 / 1000 = 2.5

Total Votes = 2000 + 1000 = 3000

((1000 * 0) + (1000 * 2.5)) * 5 / 3000 = 4.166

I forgot to add, do NOT include any items in any calculation or sum above that have no votes or it will throw the weights off.

EDIT - Simplified Solution:

I should note that there is a simplified solution to the problem that can be performed. I only demonstrated longhand form for comprehension. The compressed algorithm looks like:

Definitions:

SET = Anything not related to the current evaluation target where votes is greater than zero.

TARGET = The element you are currently trying to evaluate

25*(((Sum of SET up-votes)/(Sum of SET items)) + (Sum of TARGET up-votes)) / (Sum of TARGET votes + Sum of SET votes)

Again plugging in with your numbers evaluating 'A' for clarification and proof:

(25*((0/2)+500)) / (1000+2000) = 4.166

like image 33
Peter Hanneman Avatar answered Nov 17 '22 15:11

Peter Hanneman