Last active
December 29, 2021 20:26
-
-
Save fdcore/a4dd72580244ffeac3039741b4904b31 to your computer and use it in GitHub Desktop.
Php simple pearson correlation Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function pearson_correlation($x,$y){ | |
if(count($x)!==count($y)){return -1;} | |
$x=array_values($x); | |
$y=array_values($y); | |
$xs=array_sum($x)/count($x); | |
$ys=array_sum($y)/count($y); | |
$a=0;$bx=0;$by=0; | |
for($i=0;$i<count($x);$i++){ | |
$xr=$x[$i]-$xs; | |
$yr=$y[$i]-$ys; | |
$a+=$xr*$yr; | |
$bx+=pow($xr,2); | |
$by+=pow($yr,2); | |
} | |
$b = sqrt($bx*$by); | |
return $a/$b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment