Skip to content

Instantly share code, notes, and snippets.

@fdcore
Last active December 29, 2021 20:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fdcore/a4dd72580244ffeac3039741b4904b31 to your computer and use it in GitHub Desktop.
Save fdcore/a4dd72580244ffeac3039741b4904b31 to your computer and use it in GitHub Desktop.
Php simple pearson correlation Function
<?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