mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-10 00:37:55 +00:00
pearson correlation function
This commit is contained in:
parent
66dcfcf2b7
commit
cbec77d247
45
src/Phpml/Math/Statistic/Correlation.php
Normal file
45
src/Phpml/Math/Statistic/Correlation.php
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\Math\Statistic;
|
||||
|
||||
@ -7,10 +8,9 @@ use Phpml\Exception\InvalidArgumentException;
|
||||
|
||||
class StandardDeviation
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array|float[] $a
|
||||
* @param bool $sample
|
||||
* @param bool $sample
|
||||
*
|
||||
* @return float
|
||||
*
|
||||
@ -18,16 +18,16 @@ class StandardDeviation
|
||||
*/
|
||||
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) {
|
||||
@ -35,11 +35,10 @@ class StandardDeviation
|
||||
$carry += $d * $d;
|
||||
};
|
||||
|
||||
if($sample) {
|
||||
if ($sample) {
|
||||
--$n;
|
||||
}
|
||||
|
||||
return sqrt($carry / $n);
|
||||
}
|
||||
|
||||
}
|
||||
|
33
tests/Phpml/Math/Statistic/CorrelationTest.php
Normal file
33
tests/Phpml/Math/Statistic/CorrelationTest.php
Normal file
@ -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]);
|
||||
}
|
||||
}
|
@ -39,5 +39,4 @@ class StandardDeviationTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
StandardDeviation::population([1]);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user