Improve coverage

This commit is contained in:
Dennis Eichhorn 2019-10-19 13:12:50 +02:00
parent 2e67a95b89
commit 70f1662929
7 changed files with 40 additions and 116 deletions

View File

@ -14,9 +14,9 @@ declare(strict_types=1);
namespace phpOMS\Math\Statistic\Forecast;
use phpOMS\Math\Functions\Functions;
use phpOMS\Math\Statistic\Average;
use phpOMS\Math\Statistic\MeasureOfDispersion;
use phpOMS\Utils\ArrayUtils;
/**
* Basic forecast functions.
@ -139,7 +139,7 @@ class Error
*/
public static function getRootMeanSquaredError(array $errors) : float
{
return \sqrt(Average::arithmeticMean(Functions::powerInt($errors, 2)));
return \sqrt(Average::arithmeticMean(ArrayUtils::powerInt($errors, 2)));
}
/**
@ -273,7 +273,7 @@ class Error
*/
public static function getMeanAbsolutePercentageError(array $observed, array $forecasted) : float
{
return Average::arithmeticMean(Functions::abs(self::getPercentageErrorArray(self::getForecastErrorArray($observed, $forecasted), $observed)));
return Average::arithmeticMean(ArrayUtils::abs(self::getPercentageErrorArray(self::getForecastErrorArray($observed, $forecasted), $observed)));
}
/**
@ -352,7 +352,7 @@ class Error
*/
public static function getMeanAbsoluteScaledError(array $scaledErrors) : float
{
return Average::arithmeticMean(Functions::abs($scaledErrors));
return Average::arithmeticMean(ArrayUtils::abs($scaledErrors));
}
/**
@ -366,7 +366,7 @@ class Error
*/
public static function getMeanSquaredScaledError(array $scaledErrors) : float
{
return Average::arithmeticMean(Functions::powerInt($scaledErrors, 2));
return Average::arithmeticMean(ArrayUtils::powerInt($scaledErrors, 2));
}
/**

View File

@ -15,6 +15,8 @@ declare(strict_types=1);
namespace phpOMS\Math\Topology;
use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
/**
* Metrics.
*
@ -204,7 +206,7 @@ final class Metrics2D
public static function hamming(array $a, array $b) : int
{
if (($size = \count($a)) !== \count($b)) {
throw new \Exception();
throw new InvalidDimensionException(\count($a) . 'x' . \count($b));
}
$dist = 0;
@ -234,7 +236,7 @@ final class Metrics2D
public static function ulam(array $a, array $b) : int
{
if (($size = \count($a)) !== \count($b)) {
throw new \Exception('Invalid dimensions');
throw new InvalidDimensionException(\count($a) . 'x' . \count($b));
}
$mp = [];

View File

@ -1,109 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Math\Matrix;
use phpOMS\Math\Matrix\Matrix;
use phpOMS\Math\Matrix\SingularValueDecomposition;
/**
* @testdox phpOMS\tests\Math\Matrix\SingularValueDecompositionTest: Singular Value Decomposition
*
* @internal
*/
class SingularValueDecompositionTest extends \PHPUnit\Framework\TestCase
{
/**
* @testdox Test the correct rank calculation
*/
public function testRankCalculation() : void
{
$A = new Matrix();
$A->setMatrix([
[1, 2, 3],
[-2, 3, 8],
[5, 1, -3],
]);
self::markTestIncomplete();
return;
$svd = new SingularValueDecomposition($A);
self::assertEquals(3, $svd->rank());
}
/**
* @testdox Test the correct calculation of U, S and V
*/
public function testSUVCalculation() : void
{
$A = new Matrix();
$A->setMatrix([
[1, 2, 3],
[-2, 3, 8],
[5, 1, -3],
]);
self::markTestIncomplete();
return;
$svd = new SingularValueDecomposition($A);
self::assertEquals([
[0.2871, -0.4773, -0.8305],
[0.8640, -0.2453, 0.4397],
[-0.4136, -0.8438, 0.3420],
], $svd->getU()->toArray(), '', 0.2);
self::assertEquals([
[10.0571, 0, 0],
[0, 4.9855, 0],
[0, 0, 0],
], $svd->getS()->toArray(), '', 0.2);
self::assertEquals([
[-0.3489, -0.8436, -0.4082],
[0.2737, -0.5084, 0.8165],
[0.8963, -0.1731, -0.4082],
], $svd->getV()->toArray(), '', 0.2);
}
/**
* @testdox Test A = S * U * V'
*/
public function testComposition() : void
{
$A = new Matrix();
$A->setMatrix([
[1, 2, 3],
[-2, 3, 8],
[5, 1, -3],
]);
self::markTestIncomplete();
return;
$svd = new SingularValueDecomposition($A);
self::assertEquals(
$A->toArray(),
$svd->getU()
->mult($svd->getS())
->mult($svd->getV()->transpose())
->toArray(),
'', 0.2
);
}
}

View File

@ -93,6 +93,7 @@ class ComplexTest extends \PHPUnit\Framework\TestCase
self::assertEquals('7.00 + 24.00i', $cpl->square()->render());
self::assertEquals('7.00 + 24.00i', $cpl->pow(2)->render());
self::assertEquals('-44.00 + 117.00i', $cpl->pow(3)->render());
self::assertEquals('1.00', $cpl->pow(0)->render());
}
public function testAbs() : void

View File

@ -31,6 +31,8 @@ class LinearInterpolationTest extends \PHPUnit\Framework\TestCase
['x' => 50.0, 'y' => 7.0],
]);
self::assertEqualsWithDelta(0.45, $interpolation->interpolate(9.0), 0.1);
self::assertEqualsWithDelta(4.4, $interpolation->interpolate(37.0), 0.1);
self::assertEqualsWithDelta(10, $interpolation->interpolate(55.0), 0.1);
}
}

View File

@ -84,6 +84,20 @@ class AverageTest extends \PHPUnit\Framework\TestCase
Average::arithmeticMean([]);
}
public function testInvalidMovingAverageZeroDevision() : void
{
self::expectException(\Exception::class);
Average::movingAverage([], 4, 2);
}
public function testInvalidHarmonicMeanZeroDevision() : void
{
self::expectException(\phpOMS\Math\Exception\ZeroDevisionException::class);
Average::harmonicMean([]);
}
public function testInvalidGeometricMean() : void
{
self::expectException(\phpOMS\Math\Exception\ZeroDevisionException::class);

View File

@ -97,4 +97,18 @@ class Metrics2DTest extends \PHPUnit\Framework\TestCase
Metrics2D::ulam([3, 6, 4, 8], [4, 6, 8, 3])
);
}
public function testInvalidHammingDimension() : void
{
self::expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class);
Metrics2D::hamming([1, 1, 1, 1], [0, 1, 0]);
}
public function testInvalidUlamDimension() : void
{
self::expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class);
Metrics2D::ulam([3, 6, 4], [4, 6, 8, 3]);
}
}