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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With