From cbec77d2478ca5b6c0452cac28829a58f516e775 Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Wed, 27 Apr 2016 23:28:01 +0200 Subject: [PATCH] pearson correlation function --- src/Phpml/Math/Statistic/Correlation.php | 45 +++++++++++++++++++ .../Math/Statistic/StandardDeviation.php | 15 +++---- .../Phpml/Math/Statistic/CorrelationTest.php | 33 ++++++++++++++ .../Math/Statistic/StandardDeviationTest.php | 1 - 4 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 src/Phpml/Math/Statistic/Correlation.php create mode 100644 tests/Phpml/Math/Statistic/CorrelationTest.php diff --git a/src/Phpml/Math/Statistic/Correlation.php b/src/Phpml/Math/Statistic/Correlation.php new file mode 100644 index 0000000..a6047a8 --- /dev/null +++ b/src/Phpml/Math/Statistic/Correlation.php @@ -0,0 +1,45 @@ +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]); + } +} diff --git a/tests/Phpml/Math/Statistic/StandardDeviationTest.php b/tests/Phpml/Math/Statistic/StandardDeviationTest.php index ff69f7c..299c979 100644 --- a/tests/Phpml/Math/Statistic/StandardDeviationTest.php +++ b/tests/Phpml/Math/Statistic/StandardDeviationTest.php @@ -39,5 +39,4 @@ class StandardDeviationTest extends \PHPUnit_Framework_TestCase { StandardDeviation::population([1]); } - }