betaInc function

double betaInc(
  1. double a,
  2. double b,
  3. double x
)

正则化不完全 Beta I_x(a,b)

Implementation

double betaInc(double a, double b, double x) {
  if (x <= 0) return 0;
  if (x >= 1) return 1;
  final lnBeta = lnGamma(a) + lnGamma(b) - lnGamma(a + b);
  final front = math.exp(math.log(x) * a + math.log(1 - x) * b - lnBeta) / a;
  double f = 1, c = 1, d = 0;
  for (var i = 0; i <= 250; i++) {
    final m = i ~/ 2;
    double num;
    if (i == 0) {
      num = 1;
    } else if (i.isOdd) {
      num = -(a + m) * (a + b + m) * x / ((a + 2 * m) * (a + 2 * m + 1));
    } else {
      num = m * (b - m) * x / ((a + 2 * m - 1) * (a + 2 * m));
    }
    d = 1 + num * d;
    if (d.abs() < 1e-30) d = 1e-30;
    d = 1 / d;
    c = 1 + num / c;
    if (c.abs() < 1e-30) c = 1e-30;
    f *= c * d;
    if ((1 - c * d).abs() < 1e-13) break;
  }
  return front * (f - 1);
}