1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.math.special; 18 19 import org.apache.commons.math.MathException; 20 import org.apache.commons.math.util.ContinuedFraction; 21 22 /** 23 * This is a utility class that provides computation methods related to the 24 * Beta family of functions. 25 * 26 * @version $Revision: 780933 $ $Date: 2009-06-02 00:39:12 -0400 (Tue, 02 Jun 2009) $ 27 */ 28 public class Beta { 29 30 /** Maximum allowed numerical error. */ 31 private static final double DEFAULT_EPSILON = 10e-15; 32 33 /** 34 * Default constructor. Prohibit instantiation. 35 */ 36 private Beta() { 37 super(); 38 } 39 40 /** 41 * Returns the 42 * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html"> 43 * regularized beta function</a> I(x, a, b). 44 * 45 * @param x the value. 46 * @param a the a parameter. 47 * @param b the b parameter. 48 * @return the regularized beta function I(x, a, b) 49 * @throws MathException if the algorithm fails to converge. 50 */ 51 public static double regularizedBeta(double x, double a, double b) 52 throws MathException 53 { 54 return regularizedBeta(x, a, b, DEFAULT_EPSILON, Integer.MAX_VALUE); 55 } 56 57 /** 58 * Returns the 59 * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html"> 60 * regularized beta function</a> I(x, a, b). 61 * 62 * @param x the value. 63 * @param a the a parameter. 64 * @param b the b parameter. 65 * @param epsilon When the absolute value of the nth item in the 66 * series is less than epsilon the approximation ceases 67 * to calculate further elements in the series. 68 * @return the regularized beta function I(x, a, b) 69 * @throws MathException if the algorithm fails to converge. 70 */ 71 public static double regularizedBeta(double x, double a, double b, 72 double epsilon) throws MathException 73 { 74 return regularizedBeta(x, a, b, epsilon, Integer.MAX_VALUE); 75 } 76 77 /** 78 * Returns the regularized beta function I(x, a, b). 79 * 80 * @param x the value. 81 * @param a the a parameter. 82 * @param b the b parameter. 83 * @param maxIterations Maximum number of "iterations" to complete. 84 * @return the regularized beta function I(x, a, b) 85 * @throws MathException if the algorithm fails to converge. 86 */ 87 public static double regularizedBeta(double x, double a, double b, 88 int maxIterations) throws MathException 89 { 90 return regularizedBeta(x, a, b, DEFAULT_EPSILON, maxIterations); 91 } 92 93 /** 94 * Returns the regularized beta function I(x, a, b). 95 * 96 * The implementation of this method is based on: 97 * <ul> 98 * <li> 99 * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html"> 100 * Regularized Beta Function</a>.</li> 101 * <li> 102 * <a href="http://functions.wolfram.com/06.21.10.0001.01"> 103 * Regularized Beta Function</a>.</li> 104 * </ul> 105 * 106 * @param x the value. 107 * @param a the a parameter. 108 * @param b the b parameter. 109 * @param epsilon When the absolute value of the nth item in the 110 * series is less than epsilon the approximation ceases 111 * to calculate further elements in the series. 112 * @param maxIterations Maximum number of "iterations" to complete. 113 * @return the regularized beta function I(x, a, b) 114 * @throws MathException if the algorithm fails to converge. 115 */ 116 public static double regularizedBeta(double x, final double a, 117 final double b, double epsilon, int maxIterations) throws MathException 118 { 119 double ret; 120 121 if (Double.isNaN(x) || Double.isNaN(a) || Double.isNaN(b) || (x < 0) || 122 (x > 1) || (a <= 0.0) || (b <= 0.0)) 123 { 124 ret = Double.NaN; 125 } else if (x > (a + 1.0) / (a + b + 2.0)) { 126 ret = 1.0 - regularizedBeta(1.0 - x, b, a, epsilon, maxIterations); 127 } else { 128 ContinuedFraction fraction = new ContinuedFraction() { 129 130 @Override 131 protected double getB(int n, double x) { 132 double ret; 133 double m; 134 if (n % 2 == 0) { // even 135 m = n / 2.0; 136 ret = (m * (b - m) * x) / 137 ((a + (2 * m) - 1) * (a + (2 * m))); 138 } else { 139 m = (n - 1.0) / 2.0; 140 ret = -((a + m) * (a + b + m) * x) / 141 ((a + (2 * m)) * (a + (2 * m) + 1.0)); 142 } 143 return ret; 144 } 145 146 @Override 147 protected double getA(int n, double x) { 148 return 1.0; 149 } 150 }; 151 ret = Math.exp((a * Math.log(x)) + (b * Math.log(1.0 - x)) - 152 Math.log(a) - logBeta(a, b, epsilon, maxIterations)) * 153 1.0 / fraction.evaluate(x, epsilon, maxIterations); 154 } 155 156 return ret; 157 } 158 159 /** 160 * Returns the natural logarithm of the beta function B(a, b). 161 * 162 * @param a the a parameter. 163 * @param b the b parameter. 164 * @return log(B(a, b)) 165 */ 166 public static double logBeta(double a, double b) { 167 return logBeta(a, b, DEFAULT_EPSILON, Integer.MAX_VALUE); 168 } 169 170 /** 171 * Returns the natural logarithm of the beta function B(a, b). 172 * 173 * The implementation of this method is based on: 174 * <ul> 175 * <li><a href="http://mathworld.wolfram.com/BetaFunction.html"> 176 * Beta Function</a>, equation (1).</li> 177 * </ul> 178 * 179 * @param a the a parameter. 180 * @param b the b parameter. 181 * @param epsilon When the absolute value of the nth item in the 182 * series is less than epsilon the approximation ceases 183 * to calculate further elements in the series. 184 * @param maxIterations Maximum number of "iterations" to complete. 185 * @return log(B(a, b)) 186 */ 187 public static double logBeta(double a, double b, double epsilon, 188 int maxIterations) { 189 190 double ret; 191 192 if (Double.isNaN(a) || Double.isNaN(b) || (a <= 0.0) || (b <= 0.0)) { 193 ret = Double.NaN; 194 } else { 195 ret = Gamma.logGamma(a) + Gamma.logGamma(b) - 196 Gamma.logGamma(a + b); 197 } 198 199 return ret; 200 } 201 }