Star rating, implementing Wilson score interval

2020-06-04 07:27发布

class Rating
{
  public static function ratingAverage($positive, $total, $power = '0.05')
{
if ($total == 0)
  return 0;

$z = Rating::pnormaldist(1-$power/2,0,1);
$p = 1.0 * $positive / $total;
$s = ($p + $z*$z/(2*$total) - $z * sqrt(($p*(1-$p)+$z*$z/(4*$total))/$total))/(1+$z*$z/$total);
return $s;
  } 

 public static function pnormaldist($qn)
{
  $b = array(
  1.570796288, 0.03706987906, -0.8364353589e-3,
  -0.2250947176e-3, 0.6841218299e-5, 0.5824238515e-5,
  -0.104527497e-5, 0.8360937017e-7, -0.3231081277e-8,
  0.3657763036e-10, 0.6936233982e-12);

if ($qn < 0.0 || 1.0 < $qn)
  return 0.0;

if ($qn == 0.5)
  return 0.0;

$w1 = $qn;

if ($qn > 0.5)
  $w1 = 1.0 - $w1;

$w3 = - log(4.0 * $w1 * (1.0 - $w1));
$w1 = $b[0];

for ($i = 1;$i <= 10; $i++)
  $w1 += $b[$i] * pow($w3,$i);

if ($qn > 0.5)
  return sqrt($w1 * $w3);

return - sqrt($w1 * $w3);
 }
}

Can I make use of this when calculating the average rating based on a 5 star rating system (where 1 star = 1 point)? And if so, I would like an example using the linked class. Or should I go for something like the "Bayesian estimate"?

标签: php math rating
1条回答
叼着烟拽天下
2楼-- · 2020-06-04 08:10

I don't think this is possible. Wilson intervall is for 2 variables only. But instead of using the positive or successes in a binominal distribution you can calculate the variance of the 5-star rating: http://www.mathsisfun.com/data/standard-deviation.html. Here is an adapted wilson function: http://www.goproblems.com/test/wilson/wilson.php?v1=0&v2=0&v3=3&v4=0&v5=0

查看更多
登录 后发表回答