Gamma { public static double incompleteBeta(double alpha, double beta, double xx) { if (alpha <= 0.0) { throw new ArithmeticException("incompleteBeta: Domain error! alpha must be > 0, but was " + alpha); } if (beta <= 0.0) { throw new ArithmeticException("incompleteBeta: Domain error! beta must be > 0, but was " + beta); } if (xx <= 0.0) { return 0.0; } if (xx >= 1.0) { return 1.0; } double t; if ((beta * xx) <= 1.0 && xx <= 0.95) { t = powerSeries(alpha, beta, xx); return t; } double w = 1.0 - xx; double xc; double x; double b; double a; boolean flag = false; if (xx > (alpha / (alpha + beta))) { flag = true; a = beta; b = alpha; xc = xx; x = w; } else { a = alpha; b = beta; xc = w; x = xx; } if (flag && (b * x) <= 1.0 && x <= 0.95) { t = powerSeries(a, b, x); t = t <= Constants.MACHEP ? 1.0 - Constants.MACHEP : 1.0 - t; return t; } double y = x * (a + b - 2.0) - (a - 1.0); w = y < 0.0 ? incompleteBetaFraction1(a, b, x) : incompleteBetaFraction2(a, b, x) / xc; y = a * Math.log(x); t = b * Math.log(xc); if ((a + b) < Constants.MAXGAM && Math.abs(y) < Constants.MAXLOG && Math.abs(t) < Constants.MAXLOG) { t = Math.pow(xc, b); t *= Math.pow(x, a); t /= a; t *= w; t *= gamma(a + b) / (gamma(a) * gamma(b)); if (flag) { t = t <= Constants.MACHEP ? 1.0 - Constants.MACHEP : 1.0 - t; } return t; } y += t + logGamma(a + b) - logGamma(a) - logGamma(b); y += Math.log(w / a); t = y < Constants.MINLOG ? 0.0 : Math.exp(y); if (flag) { t = t <= Constants.MACHEP ? 1.0 - Constants.MACHEP : 1.0 - t; } return t; } private  Gamma(); static double beta(double alpha, double beta); static double gamma(double x); static double incompleteBeta(double alpha, double beta, double xx); static double incompleteGamma(double alpha, double x); static double incompleteGammaComplement(double alpha, double x); static double logGamma(double x);  }
@Test public void incompleteBeta() throws IOException { Splitter onComma = Splitter.on(",").trimResults(); InputSupplier<InputStreamReader> input = Resources.newReaderSupplier(Resources.getResource("beta-test-data.csv"), Charsets.UTF_8); boolean header = true; for (String line : CharStreams.readLines(input)) { if (header) { header = false; } else { Iterable<String> values = onComma.split(line); double alpha = Double.parseDouble(Iterables.get(values, 0)); double beta = Double.parseDouble(Iterables.get(values, 1)); double x = Double.parseDouble(Iterables.get(values, 2)); double ref = Double.parseDouble(Iterables.get(values, 3)); double actual = Gamma.incompleteBeta(alpha, beta, x); assertEquals(alpha + "," + beta + ',' + x, ref, actual, ref * 1.0e-5); } } }