public static regularizedIncompleteBeta ( $x, $a, $b ) : number | ||
$x | Upper limit of the integration 0 ≦ x ≦ 1 | |
$a | Shape parameter a > 0 | |
$b | Shape parameter b > 0 | |
return | number |
public static function regularizedIncompleteBeta($x, $a, $b)
{
$limits = ['x' => '[0, 1]', 'a' => '(0,∞)', 'b' => '(0,∞)'];
Support::checkLimits($limits, ['x' => $x, 'a' => $a, 'b' => $b]);
if ($x == 1 || $x == 0) {
return $x;
}
if ($a == 1) {
return 1 - (1 - $x) ** $b;
}
if ($b == 1) {
return $x ** $a;
}
if ($x > 0.9 || $b > $a && $x > 0.5) {
$y = 1 - $x;
return 1 - self::regularizedIncompleteBeta($y, $b, $a);
}
if ($a > 1 && $b > 1) {
// Tolerance on evaluating the continued fraction.
$tol = 1.0E-15;
$dif = $tol + 1;
// Initialize
// We will calculate the continuous fraction with a minimum depth of 10.
$m = 10;
// Counter
do {
$I_new = self::iBetaCF($m, $x, $a, $b);
if ($m > 10) {
$dif = abs(($I - $I_new) / $I_new);
}
$I = $I_new;
$m++;
} while ($dif > $tol);
return $I;
} else {
if ($a <= 1) {
// We shift a up by one, to the region that the continuous fraction works best.
$offset = $x ** $a * (1 - $x) ** $b / $a / self::beta($a, $b);
return self::regularizedIncompleteBeta($x, $a + 1, $b) + $offset;
} else {
// $b <= 1
// We shift a up by one, to the region that the continuous fraction works best.
$offset = $x ** $a * (1 - $x) ** $b / $b / self::beta($a, $b);
return self::regularizedIncompleteBeta($x, $a, $b + 1) - $offset;
}
}
}
/** * Cumulative distribution function * Calculate the cumulative t value up to a point, left tail. * * cdf = 1 - ½Iₓ₍t₎(ν/2, ½) * * ν * where x(t) = ------ * t² + ν * * Iₓ₍t₎(ν/2, ½) is the regularized incomplete beta function * * @param number $t t score * @param int $ν degrees of freedom > 0 */ public static function CDF($t, int $ν) { Support::checkLimits(self::LIMITS, ['t' => $t, 'ν' => $ν]); if ($t == 0) { return 0.5; } $x⟮t⟯ = $ν / ($t ** 2 + $ν); $ν/2 = $ν / 2; $½ = 0.5; $Iₓ = Special::regularizedIncompleteBeta($x⟮t⟯, $ν/2, $½); if ($t < 0) { return $½ * $Iₓ; } // $t ≥ 0 return 1 - $½ * $Iₓ; }