Conditions | 3 |
Paths | 3 |
Total Lines | 26 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
19 | public static function pearson(array $x, array $y) |
||
20 | { |
||
21 | if (count($x) !== count($y)) { |
||
22 | throw InvalidArgumentException::arraySizeNotMatch(); |
||
23 | } |
||
24 | |||
25 | $count = count($x); |
||
26 | $meanX = Mean::arithmetic($x); |
||
27 | $meanY = Mean::arithmetic($y); |
||
28 | |||
29 | $axb = 0; |
||
30 | $a2 = 0; |
||
31 | $b2 = 0; |
||
32 | |||
33 | for ($i = 0;$i < $count;++$i) { |
||
34 | $a = $x[$i] - $meanX; |
||
35 | $b = $y[$i] - $meanY; |
||
36 | $axb = $axb + ($a * $b); |
||
37 | $a2 = $a2 + pow($a, 2); |
||
38 | $b2 = $b2 + pow($b, 2); |
||
39 | } |
||
40 | |||
41 | $corr = $axb / sqrt($a2 * $b2); |
||
42 | |||
43 | return $corr; |
||
44 | } |
||
45 | } |
||
46 |