Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/GammaInvTest.php
T
Vincent Gao a0e67af496 Update the now-reachable extreme-tail case for GAMMA.INV
Merging #4946 (bracket expansion) with #4945 (accurate incomplete
gamma) means p=0.9999999 no longer hits the alpha*beta*5 safety
ceiling -- the corrected regularizedGammaP/Q resolve the true root.
Fold it into the extreme-tail data provider against the closed-form
-ln(1-p) check instead of asserting the stale clamped bound.
2026-08-04 08:28:27 +02:00

84 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
class GammaInvTest extends AllSetupTeardown
{
private function gammaInvFormulaResult(float $probability, float $alpha, float $beta): mixed
{
$sheet = $this->getSheet();
$sheet->getCell('A1')->setValue($probability);
$sheet->getCell('A2')->setValue($alpha);
$sheet->getCell('A3')->setValue($beta);
$sheet->getCell('B1')->setValue('=GAMMA.INV(A1, A2, A3)');
return $sheet->getCell('B1')->getCalculatedValue();
}
/**
* Upper-tail quantiles whose true root exceeds the old fixed alpha*beta*5
* bracket ceiling that used to clamp the result to it. Reference values from
* mpmath findroot on the regularized gammainc.
*/
#[\PHPUnit\Framework\Attributes\DataProvider('providerGammaInvExtremeTail')]
public function testGammaInvExtremeTail(float $expected, float $probability, float $alpha, float $beta): void
{
self::assertEqualsWithDelta($expected, $this->gammaInvFormulaResult($probability, $alpha, $beta), 1.0e-3);
}
public static function providerGammaInvExtremeTail(): array
{
return [
'p=0.9999 alpha=1 beta=1' => [9.210340371976, 0.9999, 1.0, 1.0],
'p=0.99995 alpha=1 beta=1' => [9.903487552536, 0.99995, 1.0, 1.0],
'p=0.9999 alpha=0.5 beta=2' => [15.136705226623, 0.9999, 0.5, 2.0],
'p=0.9999 alpha=1 beta=2' => [18.420680743952, 0.9999, 1.0, 2.0],
// Past #4945, regularizedGammaP/Q are accurate at this depth, so the
// bracket now reaches the true root instead of the old alpha*beta*5
// ceiling; -ln(1e-7) is the closed-form check (alpha=1).
'p=0.9999999 alpha=1 beta=1' => [16.118095650958, 0.9999999, 1.0, 1.0],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerGAMMAINV')]
public function testGAMMAINV(mixed $expectedResult, mixed ...$args): void
{
$this->runTestCases('GAMMA.INV', $expectedResult, ...$args);
}
public static function providerGAMMAINV(): array
{
return require 'tests/data/Calculation/Statistical/GAMMAINV.php';
}
#[\PHPUnit\Framework\Attributes\DataProvider('providerGammaInvArray')]
public function testGammaInvArray(array $expectedResult, string $values, string $alpha, string $beta): void
{
$calculation = Calculation::getInstance();
$formula = "=GAMMA.INV({$values}, {$alpha}, {$beta})";
$result = $calculation->calculateFormula($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public static function providerGammaInvArray(): array
{
return [
'row/column vectors' => [
[
[2.772588722239782, 5.38526905777939, 12.548861396889375],
[5.545177444479561, 10.770538115558788, 25.097722793778765],
[6.931471805599456, 13.463172644448484, 31.372153492223447],
],
'0.75',
'{1, 2, 5}',
'{2; 4; 5}',
],
];
}
}