gpt4 book ai didi

php - 我的 PHP PIL 逊相关系数代码有什么问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:16:32 24 4
gpt4 key购买 nike

所以我试图在 PHP 中实现样本 Pearson 相关系数:

(转到 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;

但有时我的代码会返回一个大于 1 的值,而 PCC 不应超过 1....

我做错了什么吗?

最佳答案

我还没有完全检查你的数学,但我突然想到的一件事是$TF1^2$obj1[$x]['count']^2。他们正在使用 bitwise XOR operator .

我相信你想要 pow($TF1, 2)pow($obj1[$x]['count'], 2)

或者:$TF1 * $TF1$obj1[$x]['count'] * $obj1[$x]['count']

这是一个常见的错误。

另请注意文章中的免责声明:

The above formula suggests a convenient single-pass algorithm for calculating sample correlations, but, depending on the numbers involved, it can sometimes be numerically unstable.

关于php - 我的 PHP PIL 逊相关系数代码有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7440810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com