Skip to content

Commit

Permalink
pearson correlation function
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed Apr 27, 2016
1 parent 66dcfcf commit cbec77d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
45 changes: 45 additions & 0 deletions src/Phpml/Math/Statistic/Correlation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare (strict_types = 1);

namespace Phpml\Math\Statistic;

use Phpml\Exception\InvalidArgumentException;

class Correlation
{
/**
* @param array|int[]|float[] $x
* @param array|int[]|float[] $y
*
* @return float
*
* @throws InvalidArgumentException
*/
public static function pearson(array $x, array $y)
{
if (count($x) !== count($y)) {
throw InvalidArgumentException::arraySizeNotMatch();
}

$count = count($x);
$meanX = array_sum($x) / $count;
$meanY = array_sum($y) / $count;

$axb = 0;
$a2 = 0;
$b2 = 0;

for ($i = 0;$i < $count;++$i) {
$a = $x[$i] - $meanX;
$b = $y[$i] - $meanY;
$axb = $axb + ($a * $b);
$a2 = $a2 + pow($a, 2);
$b2 = $b2 + pow($b, 2);
}

$corr = $axb / sqrt($a2 * $b2);

return $corr;
}
}
15 changes: 7 additions & 8 deletions src/Phpml/Math/Statistic/StandardDeviation.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
<?php
declare(strict_types = 1);

declare (strict_types = 1);

namespace Phpml\Math\Statistic;

use Phpml\Exception\InvalidArgumentException;

class StandardDeviation
{

/**
* @param array|float[] $a
* @param bool $sample
* @param bool $sample
*
* @return float
*
* @throws InvalidArgumentException
*/
public static function population(array $a, $sample = true)
{
if(empty($a)) {
if (empty($a)) {
throw InvalidArgumentException::arrayCantBeEmpty();
}

$n = count($a);

if ($sample && $n === 1) {
throw InvalidArgumentException::arraySizeToSmall(2);
}

$mean = array_sum($a) / $n;
$carry = 0.0;
foreach ($a as $val) {
$d = $val - $mean;
$carry += $d * $d;
};

if($sample) {
if ($sample) {
--$n;
}

return sqrt($carry / $n);
}

}
33 changes: 33 additions & 0 deletions tests/Phpml/Math/Statistic/CorrelationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare (strict_types = 1);

namespace test\Phpml\Math\StandardDeviation;

use Phpml\Math\Statistic\Correlation;

class CorrelationTest extends \PHPUnit_Framework_TestCase
{
public function testPearsonCorrelation()
{
//http://www.stat.wmich.edu/s216/book/node126.html
$delta = 0.001;
$x = [9300, 10565, 15000, 15000, 17764, 57000, 65940, 73676, 77006, 93739, 146088, 153260];
$y = [7100, 15500, 4400, 4400, 5900, 4600, 8800, 2000, 2750, 2550, 960, 1025];
$this->assertEquals(-0.641, Correlation::pearson($x, $y), '', $delta);

//http://www.statisticshowto.com/how-to-compute-pearsons-correlation-coefficients/
$delta = 0.001;
$x = [43, 21, 25, 42, 57, 59];
$y = [99, 65, 79, 75, 87, 82];
$this->assertEquals(0.549, Correlation::pearson($x, $y), '', $delta);
}

/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidArgumentsForPearsonCorrelation()
{
Correlation::pearson([1, 2, 4], [3, 5]);
}
}
1 change: 0 additions & 1 deletion tests/Phpml/Math/Statistic/StandardDeviationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ public function testThrowExceptionOnToSmallArray()
{
StandardDeviation::population([1]);
}

}

0 comments on commit cbec77d

Please sign in to comment.