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;
}
}
}