# Is this bayesian average calculation right?

I have asked it already on math.stackexchange.com and stackoverflow.com but they said it was off-topic and could not help me. They pointed me to cs.stackexchange.com so hopefully somebody here can help me or atleast point me in the right direction.

We have been using a bayesian average on a website of ours for a couple of years now and it seems to have working great but when using the same formula with other (good) data on another website the output was a bit disturbing.

We use this PHP code:

<?php
$$avg_num_reviews = 6264.3636363636;$$avg_rating = 4.527272727272727;
$$this_num_review = 4;$$this_average_rating = 4;
$$bayesian_average = (($$avg_num_reviews * $$avg_rating) + ($$this_num_reviews * $$this_average_rating)) / ($$avg_num_reviews + $$this_num_reviews); echo$$bayesian_average;
?>


This normaly is filled in dynamicly but to give you an idea we will put in the data manualy.

So this data:

$$avg_num_reviews = 6264.3636363636;$$avg_rating = 4.527272727272727;


is correct.

If we then calculate the Bayesian average, then in my opinion something is going wrong. It would help us enormously if someone could say something meaningful about this.

$$this_num_reviews = total number over reviews for this company;$$this_average_rating = the average rating of this company;


Now we get to the weird part.:

Company A

$$avg_num_reviews = 6264.3636363636;$$avg_rating = 4.527272727272727;
$$this_num_review = 4;$$this_average_rating = 4;
$$bayesian_average = (($$avg_num_reviews * $$avg_rating) + ($$this_num_reviews * $$this_average_rating)) / ($$avg_num_reviews + $this_num_reviews);$bayesian_average = 4.5269362613254;


Company B

$$avg_num_reviews = 6264.3636363636;$$avg_rating = 4.527272727272727;
$$this_num_review = 111;$$this_average_rating = 4.2;
$$bayesian_average = (($$avg_num_reviews * $$avg_rating) + ($$this_num_reviews * $$this_average_rating)) / ($$avg_num_reviews + $this_num_reviews);$bayesian_average = 4.5215746565744;


Company C

$$avg_num_reviews = 6264.3636363636;$$avg_rating = 4.527272727272727;
$$this_num_review = 15468;$$this_average_rating = 4.4;
$$bayesian_average = (($$avg_num_reviews * $$avg_rating) + ($$this_num_reviews * $$this_average_rating)) / ($$avg_num_reviews + $this_num_reviews);$bayesian_average = 4.4366864211353;


If we would sort this based on the bayesian average it will be like this:

1. Company A
2. Company B
3. Company C

But how can it happen that a company (Company C) with 15468 reviews and an average of 4.4 has a lower Bayesian average than a company (Company A) with 4 reviews and an average of 4?

The Bayesian average should ensure that these kinds of things are prevented.

The logical sequence should be as follows:

1. Company C
2. Company B
3. Company A

When we re-calculate it by hand the outcome is the same.

Can someone explain to me why this happens and whether there might be something wrong in the formula?

Am I missing something or should I use a different formula to do this better?

If anyone can say something meaningful about this, I would be very grateful to that person because I am a bit off track at the moment.

By setting $avg_num_reviews to a huge number like 6264, you've basically told the Bayesian average that you have extremely high confidence that the average rating should be 4.527. It takes a gigantic number of reviews to override this prior. So of course your formula assigns Company A an average of close to 4.527, because the number of reviews of Company A is so vastly smaller than 6264 that the prior dominates; but your formula assigns Company C an average close to the average of C's ratings, because 15468 is significantly larger than 6264 and this is enough to overcome the strong prior. I would expect more typical values for $avg_num_reviews would be vastly smaller, like 10. It's basically a parameter you can set, which controls how strongly you want the prior to be. Perhaps your mistake is setting it to be equal to the average number of reviews per company -- that doesn't seem like a good choice to me.