[Math] Average in a rating between 1 and 5

average

A user can vote on these numbers:

1, 2, 3, 4, 5

Then it's displayed as groups like this:

1 - 5 votes
2 - 3 votes
3 - 1 vote
4 - 17 votes
5 - 2 votes

Now I want the avarage rating score, like 3.9 or whatever it will be.

What I tried and failed:

Multiply all rows in the groups like:

1 - 1 * 5 votes
2 - 2 * 3 votes
3 - 3 * 1 vote
4 - 4 * 17 votes
5 - 5 * 2 votes

and then a sum of them where I get 52. Then I divide it with 15 which is the sum of all the valid scores. 52/15 = 3.46.

If I do that with the same votes on each, I can see how this is not correct:

1 - 1 * 1 votes
2 - 2 * 1 votes
3 - 3 * 1 vote
4 - 4 * 1 votes
5 - 5 * 1 votes

The result with my way would be 15/15 and that's 1 but it should be 3 in this case.

Any ideas?

Best Answer

Everybody is showing you how to compute the weighted average, but not why.

What you have is 28 votes. The average of 28 values is the sum of those values, divided by 28. In this case, that means:

$$\frac{1+1+1+1+1+2+2+2+3+4+\cdots+4+5+5}{28}$$

This, we see, is the same as:

$$\frac{1\cdot 5 + 2\cdot 3+ 3\cdot 1 + 4\cdot 17+5\cdot 2}{28}$$