I need to calculate 5-star ratings like the one on Amazon website. I have done enough search to find what is the best algorithm, but I am not able to get a proper answer. For example, if these are the ratings
5 star - 252
4 star - 124
3 star - 40
2 star - 29
1 star - 33
totally 478 reviews
Amazon has calculated this to be "4.1 out of 5 stars". Can anyone tell me how this figure is arrived at? I am not able to get this just by doing average.
Yes, you can average them out:
(5 * 252 + 4 * 124 + 3 * 40 + 2 * 29 + 1 * 33) / 478 = 4.11
This rating system is based on a weighted average or weighted mean. That is, they used the weight in terms of stars to compute a decimal value which rounds to 4.1. For example:
You may want to check this algorithm out: Calculating Average Rating the Right Way using PHP and MySQL - there's no need of saving each "star" (aka rating grade) and its corresponding number of votes and then having to retrieve thousands of rows from the database every time you need to calculate their average. (unless you want to display exactly how many people rated the given item with 1, 2, 3, 4 & 5 stars)
If you are start calculation of overall rating from beginning then this formula will help you.
Formula
Example
suppose you have no ratings till now then formula is like, overall rating is "0" till now. total rating "0" and given rating is "4"
If overall rating is "4.11" Total rating is "478" And new rating giving by one user is "2"
then formula is like
That's a weighted average, where you weigh each rating with the number of votes it got:
There is a really great write up on this topic at evanmiller.org. He goes through and discusses the pros and cons of a few approaches and suggests a mathematically reliable way of weighting and calculating votes.
http://evanmiller.org/ranking-items-with-star-ratings.html