mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-01 13:08:50 +00:00
52da37f4a7
GAMMA.INV silently returned the first bisection midpoint for alpha in ~[143, 171.62] (e.g. GAMMA.INV(0.5, 143, 1) gave 358.0 instead of 142.667) and #NUM! above that, because the Newton-step pdf evaluates Gamma(a), b**a and value**(a-1) in linear domain, all of which overflow even though the density itself is a small representable number. The same pattern breaks the GAMMA.DIST, CHISQ.DIST and F.DIST densities and GAMMALN, which computed log(Gamma(x)) through Gamma(x). Evaluate these in log domain via the existing logGamma, and scale the incomplete-gamma series/continued-fraction iteration cap as O(sqrt(a)), which both expansions need to converge near x ~ a once the shape is large (GAMMA.INV drifted from the true quantile above alpha ~5000 and returned alpha+1 by alpha=10000; same for CHISQ.INV at high df).
81 lines
2.8 KiB
PHP
81 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
|
|
class ChiInvLeftTailTest extends AllSetupTeardown
|
|
{
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerCHIINV')]
|
|
public function testCHIINV(mixed $expectedResult, mixed ...$args): void
|
|
{
|
|
$this->runTestCaseReference('CHISQ.INV', $expectedResult, ...$args);
|
|
}
|
|
|
|
public static function providerCHIINV(): array
|
|
{
|
|
return require 'tests/data/Calculation/Statistical/CHIINVLeftTail.php';
|
|
}
|
|
|
|
/**
|
|
* Degrees of freedom whose cdf used to under-iterate near the median.
|
|
* References from scipy.stats.chi2.ppf.
|
|
*/
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerChiInvLeftTailLargeDegrees')]
|
|
public function testChiInvLeftTailLargeDegrees(float $expected, float $probability, int $degrees): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
$result = $calculation->calculateFormula("=CHISQ.INV($probability, $degrees)");
|
|
self::assertEqualsWithDelta($expected, $result, 1.0e-6);
|
|
}
|
|
|
|
public static function providerChiInvLeftTailLargeDegrees(): array
|
|
{
|
|
return [
|
|
'df=10000' => [9999.333341235144, 0.5, 10000],
|
|
'df=200000' => [199999.3333337284, 0.5, 200000],
|
|
];
|
|
}
|
|
|
|
public function invVersusDistTest(): void
|
|
{
|
|
$expectedResult = 4.671330448981;
|
|
$probability = 0.3;
|
|
$degrees = 7;
|
|
$calculation = Calculation::getInstance();
|
|
$formula = "=CHISQ.INV($probability, $degrees)";
|
|
/** @var float|int|string */
|
|
$result = $calculation->calculateFormula($formula);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-8);
|
|
$formula = "=CHISQ.DIST($result, $degrees)";
|
|
$result = $calculation->calculateFormula($formula);
|
|
self::assertEqualsWithDelta($probability, $result, 1.0e-8);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerChiInvLeftTailArray')]
|
|
public function testChiInvLeftTailArray(array $expectedResult, string $probabilities, string $degrees): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
|
|
$formula = "=CHISQ.INV({$probabilities}, {$degrees})";
|
|
$result = $calculation->calculateFormula($formula);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
|
|
}
|
|
|
|
public static function providerChiInvLeftTailArray(): array
|
|
{
|
|
return [
|
|
'row/column vectors' => [
|
|
[
|
|
[5.0816470296362795, 6.345811195521517, 1.508898337422818],
|
|
[9.61151737996991, 11.34032237742413, 4.0773397083341045],
|
|
],
|
|
'{0.35, 0.5, 0.018}',
|
|
'{7; 12}',
|
|
],
|
|
];
|
|
}
|
|
}
|