From b50b488fc22d54ddcdf83a6a14ee7ecbece294ae Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 3 Nov 2019 23:18:10 +0100 Subject: [PATCH] add tests --- tests/Math/Statistic/Forecast/ErrorTest.php | 21 +++++++- .../Regression/LevelLevelRegressionTest.php | 52 +++++++++++++++++++ .../Statistic/MeasureOfDispersionTest.php | 21 ++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/tests/Math/Statistic/Forecast/ErrorTest.php b/tests/Math/Statistic/Forecast/ErrorTest.php index 0a94cf3c5..ca40e3432 100644 --- a/tests/Math/Statistic/Forecast/ErrorTest.php +++ b/tests/Math/Statistic/Forecast/ErrorTest.php @@ -14,7 +14,9 @@ declare(strict_types=1); namespace phpOMS\tests\Math\Statistic\Forecast; +use phpOMS\Math\Statistic\Correlation; use phpOMS\Math\Statistic\Forecast\Error; +use phpOMS\Math\Statistic\MeasureOfDispersion; /** * @internal @@ -60,7 +62,7 @@ class ErrorTest extends \PHPUnit\Framework\TestCase ); } - public function testMeanError() : void + public function testMeanErrors() : void { $errors = [ 400 - 300, @@ -99,4 +101,21 @@ class ErrorTest extends \PHPUnit\Framework\TestCase Error::getScaledErrorArray([Error::getForecastError(1000, 700)], [1000, 800]) ); } + + public function testSSE() : void + { + $errors = MeasureOfDispersion::meanDeviationArray([99.0, 98.6, 98.5, 101.1, 98.3, 98.6, 97.9, 98.4, 99.2, 99.1]); + + self::assertEqualsWithDelta(6.921, Error::getSumSquaredError($errors), 0.001); + } + + public function testCoefficientOfDetermination() : void + { + self::assertEqualsWithDelta(0.9729, Error::getCoefficientOfDetermination( + [3, 8, 10, 17, 24, 27], + [2, 8, 10, 13, 18, 20] + ), 0.001); + + self::assertEqualsWithDelta(0.922085138, Error::getAdjustedCoefficientOfDetermination(0.944346527, 8, 2), 0.001); + } } diff --git a/tests/Math/Statistic/Forecast/Regression/LevelLevelRegressionTest.php b/tests/Math/Statistic/Forecast/Regression/LevelLevelRegressionTest.php index bb9dbb475..558d5e677 100644 --- a/tests/Math/Statistic/Forecast/Regression/LevelLevelRegressionTest.php +++ b/tests/Math/Statistic/Forecast/Regression/LevelLevelRegressionTest.php @@ -15,6 +15,8 @@ declare(strict_types=1); namespace phpOMS\tests\Math\Statistic\Forecast\Regression; use phpOMS\Math\Statistic\Forecast\Regression\LevelLevelRegression; +use phpOMS\Math\Statistic\Forecast\Error; +use phpOMS\Math\Stochastic\Distribution\TDistribution; /** * @internal @@ -47,6 +49,56 @@ class LevelLevelRegressionTest extends \PHPUnit\Framework\TestCase self::assertEqualsWithDelta(0.7273, LevelLevelRegression::getElasticity($this->reg['b1'], 11, 2), 0.01); } + public function testStandardErrorOfRegressionPopulation() : void + { + $x = [1, 2, 3, 4, 5]; + $y = [1, 2, 1.3, 3.75, 2.25]; + $reg = LevelLevelRegression::getRegression($x, $y); + + $forecast = []; + foreach ($x as $value) { + $forecast[] = $reg['b0'] + $reg['b1'] * $value; + } + + $errors = Error::getForecastErrorArray($y, $forecast); + self::assertEqualsWithDelta(0.747, LevelLevelRegression::getStandardErrorOfRegressionPopulation($errors), 0.001); + } + + public function testStandardErrorOfRegressionSample() : void + { + $x = [1, 2, 3, 4, 5]; + $y = [1, 2, 1.3, 3.75, 2.25]; + $reg = LevelLevelRegression::getRegression($x, $y); + + $forecast = []; + foreach ($x as $value) { + $forecast[] = $reg['b0'] + $reg['b1'] * $value; + } + + $errors = Error::getForecastErrorArray($y, $forecast); + self::assertEqualsWithDelta(0.964, LevelLevelRegression::getStandardErrorOfRegressionSample($errors), 0.001); + } + + public function testPredictionInterval() : void + { + $x = [1, 2, 3, 4, 5]; + $y = [1, 2, 1.3, 3.75, 2.25]; + $reg = LevelLevelRegression::getRegression($x, $y); + + $forecast = []; + foreach ($x as $value) { + $forecast[] = $reg['b0'] + $reg['b1'] * $value; + } + + $errors = Error::getForecastErrorArray($y, $forecast); + $mse = Error::getMeanSquaredError($errors, 2); + self::assertEqualsWithDelta( + [-1.1124355, 7.7824355], + LevelLevelRegression::getPredictionIntervalMSE(6, $reg['b0'] + $reg['b1'] * 6, $x, $mse, TDistribution::TABLE[3]['0.95']), + 0.001 + ); + } + public function testInvalidDimension() : void { self::expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); diff --git a/tests/Math/Statistic/MeasureOfDispersionTest.php b/tests/Math/Statistic/MeasureOfDispersionTest.php index c2226b52e..ce9165663 100644 --- a/tests/Math/Statistic/MeasureOfDispersionTest.php +++ b/tests/Math/Statistic/MeasureOfDispersionTest.php @@ -55,6 +55,27 @@ class MeasureOfDispersionTest extends \PHPUnit\Framework\TestCase self::assertEqualsWithDelta((12.96 + 2.56 + 0.36 + 5.76 + 11.56) / 5, MeasureOfDispersion::squaredMeanDeviation([1, 3, 4, 7, 8]), 0.01); } + public function testDeviationArray() : void + { + self::assertEqualsWithDelta( + [0.13, -0.27, -0.37, 2.23, -0.57, -0.27, -0.97, -0.47, 0.33, 0.23], + MeasureOfDispersion::meanDeviationArray([99.0, 98.6, 98.5, 101.1, 98.3, 98.6, 97.9, 98.4, 99.2, 99.1]), + 0.01 + ); + + self::assertEqualsWithDelta( + [0.13, 0.27, 0.37, 2.23, 0.57, 0.27, 0.97, 0.47, 0.33, 0.23], + MeasureOfDispersion::meanAbsoluteDeviationArray([99.0, 98.6, 98.5, 101.1, 98.3, 98.6, 97.9, 98.4, 99.2, 99.1]), + 0.01 + ); + + self::assertEqualsWithDelta( + [0.0169, 0.0729, 0.1369, 4.9729, 0.3249, 0.0729, 0.9409, 0.2209, 0.1089, 0.0529], + MeasureOfDispersion::squaredMeanDeviationArray([99.0, 98.6, 98.5, 101.1, 98.3, 98.6, 97.9, 98.4, 99.2, 99.1]), + 0.01 + ); + } + public function testEmpiricalVariationCoefficient() : void { self::assertEqualsWithDelta(0.5400, MeasureOfDispersion::empiricalVariationCoefficient([1, 2, 3, 4, 5, 6, 7]), 0.01);