Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difference in variance calculations between Excel/Gnumeric and Open/LibreOffice #1959

Merged
merged 5 commits into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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