Statistical trends additional functions and unit tests (#1896)

* PEARSON() and CORREL() are identical functions
* Unit tests for GROWTH() function
* Move GROWTH() function into Statistical\Trends Class
This commit is contained in:
Mark Baker 2021-03-03 23:18:56 +01:00 committed by GitHub
parent 000e6088c9
commit d2a83b404a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 18 deletions

View File

@ -1206,7 +1206,7 @@ class Calculation
],
'GROWTH' => [
'category' => Category::CATEGORY_STATISTICAL,
'functionCall' => [Statistical::class, 'GROWTH'],
'functionCall' => [Statistical\Trends::class, 'GROWTH'],
'argumentCount' => '1-4',
],
'HARMEAN' => [
@ -1897,7 +1897,7 @@ class Calculation
],
'PEARSON' => [
'category' => Category::CATEGORY_STATISTICAL,
'functionCall' => [Statistical::class, 'CORREL'],
'functionCall' => [Statistical\Trends::class, 'CORREL'],
'argumentCount' => '2',
],
'PERCENTILE' => [

View File

@ -1544,6 +1544,11 @@ class Statistical
*
* Returns values along a predicted exponential Trend
*
* @Deprecated 1.18.0
*
* @see Statistical\Trends::GROWTH()
* Use the GROWTH() method in the Statistical\Trends class instead
*
* @param mixed[] $yValues Data Series Y
* @param mixed[] $xValues Data Series X
* @param mixed[] $newValues Values of X for which we want to find Y
@ -1553,22 +1558,7 @@ class Statistical
*/
public static function GROWTH($yValues, $xValues = [], $newValues = [], $const = true)
{
$yValues = Functions::flattenArray($yValues);
$xValues = Functions::flattenArray($xValues);
$newValues = Functions::flattenArray($newValues);
$const = ($const === null) ? true : (bool) Functions::flattenSingleValue($const);
$bestFitExponential = Trend::calculate(Trend::TREND_EXPONENTIAL, $yValues, $xValues, $const);
if (empty($newValues)) {
$newValues = $bestFitExponential->getXValues();
}
$returnArray = [];
foreach ($newValues as $xValue) {
$returnArray[0][] = $bestFitExponential->getValueOfYForX($xValue);
}
return $returnArray;
return Trends::GROWTH($yValues, $xValues, $newValues, $const);
}
/**

View File

@ -132,6 +132,38 @@ class Trends
return $bestFitLinear->getValueOfYForX($xValue);
}
/**
* GROWTH.
*
* Returns values along a predicted exponential Trend
*
* @param mixed[] $yValues Data Series Y
* @param mixed[] $xValues Data Series X
* @param mixed[] $newValues Values of X for which we want to find Y
* @param bool $const a logical value specifying whether to force the intersect to equal 0
*
* @return array of float
*/
public static function GROWTH($yValues, $xValues = [], $newValues = [], $const = true)
{
$yValues = Functions::flattenArray($yValues);
$xValues = Functions::flattenArray($xValues);
$newValues = Functions::flattenArray($newValues);
$const = ($const === null) ? true : (bool) Functions::flattenSingleValue($const);
$bestFitExponential = Trend::calculate(Trend::TREND_EXPONENTIAL, $yValues, $xValues, $const);
if (empty($newValues)) {
$newValues = $bestFitExponential->getXValues();
}
$returnArray = [];
foreach ($newValues as $xValue) {
$returnArray[0][] = [$bestFitExponential->getValueOfYForX($xValue)];
}
return $returnArray;
}
/**
* INTERCEPT.
*

View File

@ -0,0 +1,32 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
use PHPUnit\Framework\TestCase;
class GrowthTest extends TestCase
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerGROWTH
*
* @param mixed $expectedResult
*/
public function testGROWTH($expectedResult, ...$args): void
{
$result = Statistical::GROWTH(...$args);
self::assertEqualsWithDelta($expectedResult, $result[0], 1E-12);
}
public function providerGROWTH()
{
return require 'tests/data/Calculation/Statistical/GROWTH.php';
}
}

View File

@ -0,0 +1,25 @@
<?php
return [
[
[
[32618.203773539713],
[47729.42261474774],
[69841.30085621739],
[102197.07337883231],
[149542.48674004572],
[218821.87621459525],
],
[33100, 47300, 69000, 102000, 150000, 220000],
[11, 12, 13, 14, 15, 16],
],
[
[
[320196.71836347197],
[468536.05418404756],
],
[33100, 47300, 69000, 102000, 150000, 220000],
[11, 12, 13, 14, 15, 16],
[17, 18],
],
];