kam*_*lot 1 php algorithm math correlation
所以我试图在PHP中实现Sample Pearson Correlation Coefficient:
(转到http://en.wikipedia.org/wiki/Pearson_correlation_coefficient并针对我试图实施的特定公式搜索"样本Pearson相关系数的替代公式")
$sum = 0;
$TF1 = 0;
$TF2 = 0;
$wSquare1 = 0;
$wSquare2 = 0;
$m = sizeof($sample);
foreach($sample as $x){
if(!isset($obj1[$x])){
$obj1[$x]['count'] = 0;
}
if(!isset($obj2[$x])){
$obj2[$x]['count'] = 0;
}
$sum += $obj1[$x]['count'] * $obj2[$x]['count'];
$TF1 += $obj1[$x]['count'];
$TF2 += $obj2[$x]['count'];
$wSquare1 += $obj1[$x]['count']^2;
$wSquare2 += $obj2[$x]['count']^2;
}
$numer = $sum * $m - $TF1 * $TF2;
$denom_left = $m*$wSquare1 - $TF1^2;
$denom_right = $m*$wSquare2 - $TF2^2;
$denom = sqrt($denom_left) * sqrt($denom_right);
$pears = $numer / $denom;
return $pears;
Run Code Online (Sandbox Code Playgroud)
但有时我的代码会返回一个大于1的值,而PCC不应该超过1 ....
难道我做错了什么?