Java tutorial
//package com.java2s; /* * Copyright bzewdu * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { public static double betai(final double a, final double b, final double x) { double bt = 0.0; if (x == 0.0 || x == 1.0) { bt = 0.0; } else if (x > 0.0 && x < 1.0) { bt = gamma(a + b) * Math.pow(x, a) * Math.pow(1.0 - x, b) / (gamma(a) * gamma(b)); } double beta; if (x < (a + 1.0) / (a + b + 2.0)) { beta = bt * betacf(a, b, x) / a; } else { beta = 1.0 - bt * betacf(b, a, 1.0 - x) / b; } return beta; } public static double gamma(double x) { double g = 1.0; double f; if (x > 0.0) { while (x < 3.0) { g *= x; ++x; } f = (1.0 - 2.0 / (7.0 * Math.pow(x, 2.0)) * (1.0 - 2.0 / (3.0 * Math.pow(x, 2.0)))) / (30.0 * Math.pow(x, 2.0)); f = (1.0 - f) / (12.0 * x) + x * (Math.log(x) - 1.0); f = Math.exp(f) / g * Math.pow(6.283185307179586 / x, 0.5); } else { f = Double.POSITIVE_INFINITY; } return f; } static double betacf(final double a, final double b, final double x) { final int maxIterations = 50; int m = 1; final double eps = 3.0E-5; double am = 1.0; double bm = 1.0; double az = 1.0; final double qab = a + b; final double qap = a + 1.0; final double qam = a - 1.0; double bz = 1.0 - qab * x / qap; double ap; double bp; double app; double bpp; for (double aold = 0.0; m < 50 && Math.abs(az - aold) >= 3.0E-5 * Math.abs(az); aold = az, am = ap / bpp, bm = bp / bpp, az = app / bpp, bz = 1.0, ++m) { final double em = m; final double tem = em + em; double d = em * (b - m) * x / ((qam + tem) * (a + tem)); ap = az + d * am; bp = bz + d * bm; d = -(a + em) * (qab + em) * x / ((a + tem) * (qap + tem)); app = ap + d * az; bpp = bp + d * bz; } return az; } }