Trend unit tests (#1899)

- Move TREND() functions into the Statistical Trends class
- Unit tests for TREND()
- Create Confidence class for Statistical Confidence functions
This commit is contained in:
Mark Baker
2021-03-06 22:50:19 +01:00
committed by GitHub
parent a79a4ddbab
commit 2d8c8c8ecf
15 changed files with 344 additions and 111 deletions
@@ -19,7 +19,7 @@ class LogEstTest extends TestCase
public function testLOGEST($expectedResult, $yValues, $xValues, $const, $stats): void
{
$result = Statistical::LOGEST($yValues, $xValues, $const, $stats);
//var_dump($result);
$elements = count($expectedResult);
for ($element = 0; $element < $elements; ++$element) {
self::assertEqualsWithDelta($expectedResult[$element], $result[$element], 1E-12);
@@ -0,0 +1,49 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Shared\Trend;
use PhpOffice\PhpSpreadsheet\Shared\Trend\ExponentialBestFit;
use PHPUnit\Framework\TestCase;
class ExponentialBestFitTest extends TestCase
{
/**
* @dataProvider providerExponentialBestFit
*
* @param mixed $expectedSlope
* @param mixed $expectedIntersect
* @param mixed $expectedGoodnessOfFit
* @param mixed $yValues
* @param mixed $xValues
* @param mixed $expectedEquation
*/
public function testExponentialBestFit(
$expectedSlope,
$expectedIntersect,
$expectedGoodnessOfFit,
$expectedEquation,
$yValues,
$xValues
): void {
$bestFit = new ExponentialBestFit($yValues, $xValues);
$slope = $bestFit->getSlope(1);
self::assertEquals($expectedSlope[0], $slope);
$slope = $bestFit->getSlope();
self::assertEquals($expectedSlope[1], $slope);
$intersect = $bestFit->getIntersect(1);
self::assertEquals($expectedIntersect[0], $intersect);
$intersect = $bestFit->getIntersect();
self::assertEquals($expectedIntersect[1], $intersect);
$equation = $bestFit->getEquation(2);
self::assertEquals($expectedEquation, $equation);
self::assertSame($expectedGoodnessOfFit[0], $bestFit->getGoodnessOfFit(6));
self::assertSame($expectedGoodnessOfFit[1], $bestFit->getGoodnessOfFit());
}
public function providerExponentialBestFit()
{
return require 'tests/data/Shared/Trend/ExponentialBestFit.php';
}
}
@@ -0,0 +1,49 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Shared\Trend;
use PhpOffice\PhpSpreadsheet\Shared\Trend\LinearBestFit;
use PHPUnit\Framework\TestCase;
class LinearBestFitTest extends TestCase
{
/**
* @dataProvider providerLinearBestFit
*
* @param mixed $expectedSlope
* @param mixed $expectedIntersect
* @param mixed $expectedGoodnessOfFit
* @param mixed $yValues
* @param mixed $xValues
* @param mixed $expectedEquation
*/
public function testLinearBestFit(
$expectedSlope,
$expectedIntersect,
$expectedGoodnessOfFit,
$expectedEquation,
$yValues,
$xValues
): void {
$bestFit = new LinearBestFit($yValues, $xValues);
$slope = $bestFit->getSlope(1);
self::assertEquals($expectedSlope[0], $slope);
$slope = $bestFit->getSlope();
self::assertEquals($expectedSlope[1], $slope);
$intersect = $bestFit->getIntersect(1);
self::assertEquals($expectedIntersect[0], $intersect);
$intersect = $bestFit->getIntersect();
self::assertEquals($expectedIntersect[1], $intersect);
$equation = $bestFit->getEquation(2);
self::assertEquals($expectedEquation, $equation);
self::assertSame($expectedGoodnessOfFit[0], $bestFit->getGoodnessOfFit(6));
self::assertSame($expectedGoodnessOfFit[1], $bestFit->getGoodnessOfFit());
}
public function providerLinearBestFit()
{
return require 'tests/data/Shared/Trend/LinearBestFit.php';
}
}
+80 -24
View File
@@ -1,6 +1,22 @@
<?php
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
return [
[
[1.0, 0.0],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
false,
false,
],
[
[2.310344827586207, 0.0],
[1, 9, 5, 7],
[0, 4, 2, 3],
false,
false,
],
[
[2.0, 1.0],
[1, 9, 5, 7],
@@ -8,6 +24,46 @@ return [
true,
false,
],
[
[0.600378787879, 0.0],
[3, 10, 3, 6, 8, 12, 1, 4, 9, 14],
[8, 2, 11, 6, 5, 4, 12, 9, 6, 1],
false,
false,
],
[
[-1.1064189189190, 14.081081081081],
[3, 10, 3, 6, 8, 12, 1, 4, 9, 14],
[8, 2, 11, 6, 5, 4, 12, 9, 6, 1],
true,
false,
],
[
[
[0.600378787879, 0.0],
[0.3130441135917, Functions::NA()],
[0.2901220667036, 7.193206086629],
[3.6782360429317, 9],
[190.3200757575760, 465.679924242424],
],
[3, 10, 3, 6, 8, 12, 1, 4, 9, 14],
[8, 2, 11, 6, 5, 4, 12, 9, 6, 1],
false,
true,
],
[
[
[-1.1064189189190, 14.081081081081],
[0.1491074289251, 1.083468383961],
[0.8731378215565, 1.622464237727],
[55.0605598780781, 8],
[144.9408783783780, 21.059121621622],
],
[3, 10, 3, 6, 8, 12, 1, 4, 9, 14],
[8, 2, 11, 6, 5, 4, 12, 9, 6, 1],
true,
true,
],
[
[
[56.837944664032, 11704.347826086974],
@@ -21,29 +77,29 @@ return [
true,
true,
],
// [
// [
// [-234.2371645, 2553.21066, 12529.76817, 27.64138737, 52317.83051],
// [13.26801148, 530.6691519, 400.0668382, 5.429374042, 12237.3616],
// [0.996747993, 970.5784629, '#N/A', '#N/A', '#N/A'],
// [459.7536742, 6, '#N/A', '#N/A', '#N/A'],
// [1732393319, 5652135.316, '#N/A', '#N/A', '#N/A'],
// 'multi-series' => [
// [
// [-234.2371645, 2553.21066, 12529.76817, 27.64138737, 52317.83051],
// [13.26801148, 530.6691519, 400.0668382, 5.429374042, 12237.3616],
// [0.996747993, 970.5784629, '#N/A', '#N/A', '#N/A'],
// [459.7536742, 6, '#N/A', '#N/A', '#N/A'],
// [1732393319, 5652135.316, '#N/A', '#N/A', '#N/A'],
// ],
// [142000, 144000, 151000, 150000, 139000, 169000, 126000, 142900, 163000, 169000, 149000],
// [
// [2310, 2, 2, 20],
// [2333, 2, 2, 12],
// [2356, 3, 1.5, 33],
// [2379, 3, 2, 43],
// [2402, 2, 3, 53],
// [2425, 4, 2, 23],
// [2448, 2, 1.5, 99],
// [2471, 2, 2, 34],
// [2494, 3, 3, 23],
// [2517, 4, 4, 55],
// [2540, 2, 3, 22],
// ],
// true,
// true,
// ],
// [142000, 144000, 151000, 150000, 139000, 169000, 126000, 142900, 163000, 169000, 149000],
// [
// [2310, 2, 2, 20],
// [2333, 2, 2, 12],
// [2356, 3, 1.5, 33],
// [2379, 3, 2, 43],
// [2402, 2, 3, 53],
// [2425, 4, 2, 23],
// [2448, 2, 1.5, 99],
// [2471, 2, 2, 34],
// [2494, 3, 3, 23],
// [2517, 4, 4, 55],
// [2540, 2, 3, 22],
// ],
// true,
// true,
// ],
];
@@ -1,6 +1,20 @@
<?php
return [
[
[1.000174230092, 1.0],
[1, 2, 3, 4, 5],
[1, 10, 100, 1000, 10000],
false,
false,
],
[
[1.000091183030, 2.127357620703],
[1, 2, 3, 4, 5],
[1, 10, 100, 1000, 10000],
true,
false,
],
[
[1.463275628116, 495.304770158727],
[33100, 47300, 69000, 102000, 150000, 220000],
@@ -15,4 +29,44 @@ return [
true,
false,
],
[
[1.1743674215053, 1.0],
[3, 10, 3, 6, 8, 12, 1, 4, 9, 14],
[8, 2, 11, 6, 5, 4, 12, 9, 6, 1],
false,
false,
],
[
[0.8135120728565, 20.671878197178],
[3, 10, 3, 6, 8, 12, 1, 4, 9, 14],
[8, 2, 11, 6, 5, 4, 12, 9, 6, 1],
true,
false,
],
// [
// [
// [1.174367421505266, 1.0],
// [0.0672620306083, Functions::NA()],
// [0.3881799938732, 1.545563794251],
// [5.7102087376569, 9],
// [13.6403607201119, 21.498906978904],
// ],
// [3, 10, 3, 6, 8, 12, 1, 4, 9, 14],
// [8, 2, 11, 6, 5, 4, 12, 9, 6, 1],
// false,
// true,
// ],
// [
// [
// [0.8135120728565, 20.671878197178],
// [0.0313021171611, 0.227452478657],
// [0.8445875527654, 0.340603858743],
// [43.4759283593386, 8],
// [5.0436854288511, 0.928087908723],
// ],
// [3, 10, 3, 6, 8, 12, 1, 4, 9, 14],
// [8, 2, 11, 6, 5, 4, 12, 9, 6, 1],
// true,
// true,
// ],
];
@@ -0,0 +1,12 @@
<?php
return [
[
'slope' => [0.8, 0.813512072856517],
'intersect' => [20.7, 20.671878197177865],
'goodnessOfFit' => [0.904868, 0.9048681877346413],
'equation' => 'Y = 20.67 * 0.81^X',
[3, 10, 3, 6, 8, 12, 1, 4, 9, 14],
[8, 2, 11, 6, 5, 4, 12, 9, 6, 1],
],
];
+20
View File
@@ -0,0 +1,20 @@
<?php
return [
[
'slope' => [-1.1, -1.1064189189190],
'intersect' => [14.1, 14.081081081081],
'goodnessOfFit' => [0.873138, 0.8731378215564962],
'equation' => 'Y = 14.08 + -1.11 * X',
[3, 10, 3, 6, 8, 12, 1, 4, 9, 14],
[8, 2, 11, 6, 5, 4, 12, 9, 6, 1],
],
[
'slope' => [1.0, 1.0],
'intersect' => [-2.0, -2.0],
'goodnessOfFit' => [1.0, 1.0],
'equation' => 'Y = -2 + 1 * X',
[1, 2, 3, 4, 5],
[3, 4, 5, 6, 7],
],
];