mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-31 04:28:51 +00:00
e88cd615cc
The incomplete gamma primitive used a fixed 32-term power series with no convergence test, so GAMMA.DIST, GAMMADIST, CHISQ.DIST(.RT), GAMMAINV and CHISQ.INV were grossly wrong once the series argument reached ~32 (e.g. CHISQ.DIST.RT(80, 4) returned 0.806 instead of 1.74e-16). Replace it with the standard convergence-tested regularized incomplete gamma: series P(a,x) for x < a+1, continued fraction Q(a,x) for x >= a+1. CHISQ.DIST.RT now uses Q directly so the right tail stays free of 1 - P cancellation. Consolidates the duplicate copy that already existed privately in ChiSquared onto the shared primitive.
61 lines
2.0 KiB
PHP
61 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
|
|
class ChiInvRightTailTest extends AllSetupTeardown
|
|
{
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerCHIINV')]
|
|
public function testCHIINV(mixed $expectedResult, mixed ...$args): void
|
|
{
|
|
$this->runTestCases('CHISQ.INV.RT', $expectedResult, ...$args);
|
|
}
|
|
|
|
public static function providerCHIINV(): array
|
|
{
|
|
return require 'tests/data/Calculation/Statistical/CHIINVRightTail.php';
|
|
}
|
|
|
|
public function invVersusDistTest(): void
|
|
{
|
|
$expectedResult = 8.383430828608;
|
|
$probability = 0.3;
|
|
$degrees = 7;
|
|
$calculation = Calculation::getInstance();
|
|
$formula = "=CHISQ.INV.RT($probability, $degrees)";
|
|
/** @var float|int|string */
|
|
$result = $calculation->calculateFormula($formula);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-8);
|
|
$formula = "=CHISQ.DIST.RT($result, $degrees)";
|
|
$result = $calculation->calculateFormula($formula);
|
|
self::assertEqualsWithDelta($probability, $result, 1.0e-8);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerChiInvRightTailArray')]
|
|
public function testChiInvRightTailArray(array $expectedResult, string $probabilities, string $degrees): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
|
|
$formula = "=CHISQ.INV.RT({$probabilities}, {$degrees})";
|
|
$result = $calculation->calculateFormula($formula);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
|
|
}
|
|
|
|
public static function providerChiInvRightTailArray(): array
|
|
{
|
|
return [
|
|
'row/column vectors' => [
|
|
[
|
|
[7.8061229155968075, 6.345811195521517, 16.907871682617596],
|
|
[13.266097125199924, 11.340322377424133, 24.388802639997067],
|
|
],
|
|
'{0.35, 0.5, 0.018}',
|
|
'{7; 12}',
|
|
],
|
|
];
|
|
}
|
|
}
|