public static double betainv(double x, double p, double q) { // ALGORITHM AS 63 APPL. STATIST. VOL.32, NO.1 // Computes P(Beta>x) double beta = Maths.logBeta(p, q), acu = 1E-14; double cx, psq, pp, qq, x2, term, ai, betain, ns, rx, temp; boolean indx; if (p <= 0 || q <= 0) return (-1.0); if (x <= 0 || x >= 1) return (-1.0); psq = p + q; cx = 1 - x; if (p < psq * x) { x2 = cx; cx = x; pp = q; qq = p; indx = true; } else { x2 = x; pp = p; qq = q; indx = false; } term = 1; ai = 1; betain = 1; ns = qq + cx * psq; rx = x2 / cx; temp = qq - ai; if (ns == 0) rx = x2; while (temp > acu && temp > acu * betain) { term = term * temp * rx / (pp + ai); betain = betain + term; temp = Math.abs(term); if (temp > acu && temp > acu * betain) { ai++; ns--; if (ns >= 0) { temp = qq - ai; if (ns == 0) rx = x2; } else { temp = psq; psq += 1; } } } betain *= Math.exp(pp * Math.log(x2) + (qq - 1) * Math.log(cx) - beta) / pp; if (indx) betain = 1 - betain; return (betain); }