我正在尝试应用贝叶斯评级公式,但如果我在 5000 分中给 1 分打分,则最终评级大于 5。
例如,给定项目没有投票,在投票 170,000 次并获得 1 星后,其最终评分为 5.23。如果我给 100 分,它有一个正常值。
这是我在 PHP 中所拥有的。
<?php
// these values came from DB
$total_votes = 2936; // total of votes for all items
$total_rating = 582.955; // sum of all ratings
$total_items = 202;
// now the specific item, it has no votes yet
$this_num_votes = 0;
$this_score = 0;
$this_rating = 0;
// simulating a lot of votes with 1 star
for ($i=0; $i < 170000; $i++) {
$rating_sent = 1; // the new rating, always 1
$total_votes++; // adding 1 to total
$total_rating = $total_rating+$rating_sent; // adding 1 to total
$avg_num_votes = ($total_votes/$total_items); // Average number of votes in all items
$avg_rating = ($total_rating/$total_items); // Average rating for all items
$this_num_votes = $this_num_votes+1; // Number of votes for this item
$this_score = $this_score+$rating_sent; // Sum of all votes for this item
$this_rating = $this_score/$this_num_votes; // Rating for this item
$bayesian_rating = ( ($avg_num_votes * $avg_rating) + ($this_num_votes * $this_rating) ) / ($avg_num_votes + $this_num_votes);
}
echo $bayesian_rating;
?>
即使我用 1 或 2 泛滥:
$rating_sent = rand(1,2)
100,000票后的最终评分超过5。
我刚刚做了一个新的测试
$rating_sent = rand(1,5)
在 100,000 之后,我得到了一个完全超出范围范围(10.53)的值。我知道在正常情况下,没有一个项目会获得 170,000 票,而所有其他项目都没有投票。但我想知道我的代码是否有问题,或者考虑到大量选票,这是否是贝叶斯公式的预期行为。
编辑
为了清楚起见,这里是对一些变量的更好解释。
$avg_num_votes // SUM(votes given to all items)/COUNT(all items)
$avg_rating // SUM(rating of all items)/COUNT(all items)
$this_num_votes // COUNT(votes given for this item)
$this_score // SUM(rating for this item)
$bayesian_rating // is the formula itself
公式为:( (avg_num_votes * avg_rating) + (this_num_votes * this_rating) ) / (avg_num_votes + this_num_votes)
。取自这里