Skip to content

Commit

Permalink
Difference in variance calculations between Excel/Gnumeric and Open/L…
Browse files Browse the repository at this point in the history
…ibreOffice (#1959)

* Difference in variance calculations between Excel/Gnumeric and Open/LibreOffice
* Simplify STDEV() function logic by remembering that STDEV() is simply the square root of VAR(), so we can simply use the VAR() calculaion rather than duplicating the basic logic... and also allow for the differences between Excel/Gnumeric and Open/LibreOffice
  • Loading branch information
Mark Baker authored Mar 27, 2021
1 parent ec25314 commit a34dd71
Show file tree
Hide file tree
Showing 30 changed files with 446 additions and 104 deletions.
120 changes: 17 additions & 103 deletions src/PhpSpreadsheet/Calculation/Statistical/StandardDeviations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;

class StandardDeviations extends VarianceBase
class StandardDeviations
{
/**
* STDEV.
Expand All @@ -21,34 +19,12 @@ class StandardDeviations extends VarianceBase
*/
public static function STDEV(...$args)
{
$aArgs = Functions::flattenArrayIndexed($args);

$aMean = Averages::average($aArgs);

if (!is_string($aMean)) {
$returnValue = 0.0;
$aCount = -1;

foreach ($aArgs as $k => $arg) {
if (
(is_bool($arg)) &&
((!Functions::isCellValue($k)) || (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE))
) {
$arg = (int) $arg;
}
// Is it a numeric value?
if ((is_numeric($arg)) && (!is_string($arg))) {
$returnValue += ($arg - $aMean) ** 2;
++$aCount;
}
}

if ($aCount > 0) {
return sqrt($returnValue / $aCount);
}
$result = Variances::VAR(...$args);
if (!is_numeric($result)) {
return $result;
}

return Functions::DIV0();
return sqrt((float) $result);
}

/**
Expand All @@ -65,32 +41,12 @@ public static function STDEV(...$args)
*/
public static function STDEVA(...$args)
{
$aArgs = Functions::flattenArrayIndexed($args);

$aMean = Averages::averageA($aArgs);

if (!is_string($aMean)) {
$returnValue = 0.0;
$aCount = -1;

foreach ($aArgs as $k => $arg) {
if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) {
} else {
// Is it a numeric value?
if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
$arg = self::datatypeAdjustmentAllowStrings($arg);
$returnValue += ($arg - $aMean) ** 2;
++$aCount;
}
}
}

if ($aCount > 0) {
return sqrt($returnValue / $aCount);
}
$result = Variances::VARA(...$args);
if (!is_numeric($result)) {
return $result;
}

return Functions::DIV0();
return sqrt((float) $result);
}

/**
Expand All @@ -107,34 +63,12 @@ public static function STDEVA(...$args)
*/
public static function STDEVP(...$args)
{
$aArgs = Functions::flattenArrayIndexed($args);

$aMean = Averages::average($aArgs);

if (!is_string($aMean)) {
$returnValue = 0.0;
$aCount = 0;

foreach ($aArgs as $k => $arg) {
if (
(is_bool($arg)) &&
((!Functions::isCellValue($k)) || (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE))
) {
$arg = (int) $arg;
}
// Is it a numeric value?
if ((is_numeric($arg)) && (!is_string($arg))) {
$returnValue += ($arg - $aMean) ** 2;
++$aCount;
}
}

if ($aCount > 0) {
return sqrt($returnValue / $aCount);
}
$result = Variances::VARP(...$args);
if (!is_numeric($result)) {
return $result;
}

return Functions::DIV0();
return sqrt((float) $result);
}

/**
Expand All @@ -151,31 +85,11 @@ public static function STDEVP(...$args)
*/
public static function STDEVPA(...$args)
{
$aArgs = Functions::flattenArrayIndexed($args);

$aMean = Averages::averageA($aArgs);

if (!is_string($aMean)) {
$returnValue = 0.0;
$aCount = 0;

foreach ($aArgs as $k => $arg) {
if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) {
} else {
// Is it a numeric value?
if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg != '')))) {
$arg = self::datatypeAdjustmentAllowStrings($arg);
$returnValue += ($arg - $aMean) ** 2;
++$aCount;
}
}
}

if ($aCount > 0) {
return sqrt($returnValue / $aCount);
}
$result = Variances::VARPA(...$args);
if (!is_numeric($result)) {
return $result;
}

return Functions::DIV0();
return sqrt((float) $result);
}
}
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Calculation/Statistical/VarianceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;

abstract class VarianceBase
{
protected static function datatypeAdjustmentAllowStrings($value)
Expand All @@ -17,7 +19,7 @@ protected static function datatypeAdjustmentAllowStrings($value)

protected static function datatypeAdjustmentBooleans($value)
{
if (is_bool($value)) {
if (is_bool($value) && (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE)) {
return (int) $value;
}

Expand Down
2 changes: 2 additions & 0 deletions src/PhpSpreadsheet/Calculation/Statistical/Variances.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static function VAR(...$args)
$aCount = 0;
foreach ($aArgs as $arg) {
$arg = self::datatypeAdjustmentBooleans($arg);

// Is it a numeric value?
if ((is_numeric($arg)) && (!is_string($arg))) {
$summerA += ($arg * $arg);
Expand Down Expand Up @@ -117,6 +118,7 @@ public static function VARP(...$args)
$aCount = 0;
foreach ($aArgs as $arg) {
$arg = self::datatypeAdjustmentBooleans($arg);

// Is it a numeric value?
if ((is_numeric($arg)) && (!is_string($arg))) {
$summerA += ($arg * $arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
use PHPUnit\Framework\TestCase;

class StDevATest extends TestCase
{
protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

/**
* @dataProvider providerSTDEVA
*
Expand All @@ -23,4 +29,23 @@ public function providerSTDEVA()
{
return require 'tests/data/Calculation/Statistical/STDEVA.php';
}

/**
* @dataProvider providerOdsSTDEVA
*
* @param mixed $expectedResult
* @param mixed $values
*/
public function testOdsSTDEVA($expectedResult, $values): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);

$result = Statistical::STDEVA($values);
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

public function providerOdsSTDEVA()
{
return require 'tests/data/Calculation/Statistical/STDEVA_ODS.php';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
use PHPUnit\Framework\TestCase;

class StDevPATest extends TestCase
{
protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

/**
* @dataProvider providerSTDEVPA
*
Expand All @@ -23,4 +29,23 @@ public function providerSTDEVPA()
{
return require 'tests/data/Calculation/Statistical/STDEVPA.php';
}

/**
* @dataProvider providerOdsSTDEVPA
*
* @param mixed $expectedResult
* @param mixed $values
*/
public function testOdsSTDEVPA($expectedResult, $values): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);

$result = Statistical::STDEVPA($values);
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

public function providerOdsSTDEVPA()
{
return require 'tests/data/Calculation/Statistical/STDEVPA_ODS.php';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
use PHPUnit\Framework\TestCase;

class StDevPTest extends TestCase
{
protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

/**
* @dataProvider providerSTDEVP
*
Expand All @@ -23,4 +29,23 @@ public function providerSTDEVP()
{
return require 'tests/data/Calculation/Statistical/STDEVP.php';
}

/**
* @dataProvider providerOdsSTDEVP
*
* @param mixed $expectedResult
* @param mixed $values
*/
public function testOdsSTDEVP($expectedResult, $values): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);

$result = Statistical::STDEVP($values);
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

public function providerOdsSTDEVP()
{
return require 'tests/data/Calculation/Statistical/STDEVP_ODS.php';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;

use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
use PHPUnit\Framework\TestCase;

class StDevTest extends TestCase
{
protected function tearDown(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

/**
* @dataProvider providerSTDEV
*
Expand All @@ -23,4 +29,23 @@ public function providerSTDEV()
{
return require 'tests/data/Calculation/Statistical/STDEV.php';
}

/**
* @dataProvider providerOdsSTDEV
*
* @param mixed $expectedResult
* @param mixed $values
*/
public function testOdsSTDEV($expectedResult, $values): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);

$result = Statistical::STDEV($values);
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

public function providerOdsSTDEV()
{
return require 'tests/data/Calculation/Statistical/STDEV_ODS.php';
}
}
Loading

0 comments on commit a34dd71

Please sign in to comment.