Raymond | dee0849 | 2015-04-02 10:43:13 -0700 | [diff] [blame] | 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 | import org.apache.commons.math.util.FastMath; |
| 22 | |
| 23 | /** |
| 24 | * This is a utility class that provides computation methods related to the |
| 25 | * Beta family of functions. |
| 26 | * |
| 27 | * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 août 2010) $ |
| 28 | */ |
| 29 | public class Beta { |
| 30 | |
| 31 | /** Maximum allowed numerical error. */ |
| 32 | private static final double DEFAULT_EPSILON = 10e-15; |
| 33 | |
| 34 | /** |
| 35 | * Default constructor. Prohibit instantiation. |
| 36 | */ |
| 37 | private Beta() { |
| 38 | super(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns the |
| 43 | * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html"> |
| 44 | * regularized beta function</a> I(x, a, b). |
| 45 | * |
| 46 | * @param x the value. |
| 47 | * @param a the a parameter. |
| 48 | * @param b the b parameter. |
| 49 | * @return the regularized beta function I(x, a, b) |
| 50 | * @throws MathException if the algorithm fails to converge. |
| 51 | */ |
| 52 | public static double regularizedBeta(double x, double a, double b) |
| 53 | throws MathException |
| 54 | { |
| 55 | return regularizedBeta(x, a, b, DEFAULT_EPSILON, Integer.MAX_VALUE); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns the |
| 60 | * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html"> |
| 61 | * regularized beta function</a> I(x, a, b). |
| 62 | * |
| 63 | * @param x the value. |
| 64 | * @param a the a parameter. |
| 65 | * @param b the b parameter. |
| 66 | * @param epsilon When the absolute value of the nth item in the |
| 67 | * series is less than epsilon the approximation ceases |
| 68 | * to calculate further elements in the series. |
| 69 | * @return the regularized beta function I(x, a, b) |
| 70 | * @throws MathException if the algorithm fails to converge. |
| 71 | */ |
| 72 | public static double regularizedBeta(double x, double a, double b, |
| 73 | double epsilon) throws MathException |
| 74 | { |
| 75 | return regularizedBeta(x, a, b, epsilon, Integer.MAX_VALUE); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns the regularized beta function I(x, a, b). |
| 80 | * |
| 81 | * @param x the value. |
| 82 | * @param a the a parameter. |
| 83 | * @param b the b parameter. |
| 84 | * @param maxIterations Maximum number of "iterations" to complete. |
| 85 | * @return the regularized beta function I(x, a, b) |
| 86 | * @throws MathException if the algorithm fails to converge. |
| 87 | */ |
| 88 | public static double regularizedBeta(double x, double a, double b, |
| 89 | int maxIterations) throws MathException |
| 90 | { |
| 91 | return regularizedBeta(x, a, b, DEFAULT_EPSILON, maxIterations); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the regularized beta function I(x, a, b). |
| 96 | * |
| 97 | * The implementation of this method is based on: |
| 98 | * <ul> |
| 99 | * <li> |
| 100 | * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html"> |
| 101 | * Regularized Beta Function</a>.</li> |
| 102 | * <li> |
| 103 | * <a href="http://functions.wolfram.com/06.21.10.0001.01"> |
| 104 | * Regularized Beta Function</a>.</li> |
| 105 | * </ul> |
| 106 | * |
| 107 | * @param x the value. |
| 108 | * @param a the a parameter. |
| 109 | * @param b the b parameter. |
| 110 | * @param epsilon When the absolute value of the nth item in the |
| 111 | * series is less than epsilon the approximation ceases |
| 112 | * to calculate further elements in the series. |
| 113 | * @param maxIterations Maximum number of "iterations" to complete. |
| 114 | * @return the regularized beta function I(x, a, b) |
| 115 | * @throws MathException if the algorithm fails to converge. |
| 116 | */ |
| 117 | public static double regularizedBeta(double x, final double a, |
| 118 | final double b, double epsilon, int maxIterations) throws MathException |
| 119 | { |
| 120 | double ret; |
| 121 | |
| 122 | if (Double.isNaN(x) || Double.isNaN(a) || Double.isNaN(b) || (x < 0) || |
| 123 | (x > 1) || (a <= 0.0) || (b <= 0.0)) |
| 124 | { |
| 125 | ret = Double.NaN; |
| 126 | } else if (x > (a + 1.0) / (a + b + 2.0)) { |
| 127 | ret = 1.0 - regularizedBeta(1.0 - x, b, a, epsilon, maxIterations); |
| 128 | } else { |
| 129 | ContinuedFraction fraction = new ContinuedFraction() { |
| 130 | |
| 131 | @Override |
| 132 | protected double getB(int n, double x) { |
| 133 | double ret; |
| 134 | double m; |
| 135 | if (n % 2 == 0) { // even |
| 136 | m = n / 2.0; |
| 137 | ret = (m * (b - m) * x) / |
| 138 | ((a + (2 * m) - 1) * (a + (2 * m))); |
| 139 | } else { |
| 140 | m = (n - 1.0) / 2.0; |
| 141 | ret = -((a + m) * (a + b + m) * x) / |
| 142 | ((a + (2 * m)) * (a + (2 * m) + 1.0)); |
| 143 | } |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | protected double getA(int n, double x) { |
| 149 | return 1.0; |
| 150 | } |
| 151 | }; |
| 152 | ret = FastMath.exp((a * FastMath.log(x)) + (b * FastMath.log(1.0 - x)) - |
| 153 | FastMath.log(a) - logBeta(a, b, epsilon, maxIterations)) * |
| 154 | 1.0 / fraction.evaluate(x, epsilon, maxIterations); |
| 155 | } |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns the natural logarithm of the beta function B(a, b). |
| 162 | * |
| 163 | * @param a the a parameter. |
| 164 | * @param b the b parameter. |
| 165 | * @return log(B(a, b)) |
| 166 | */ |
| 167 | public static double logBeta(double a, double b) { |
| 168 | return logBeta(a, b, DEFAULT_EPSILON, Integer.MAX_VALUE); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Returns the natural logarithm of the beta function B(a, b). |
| 173 | * |
| 174 | * The implementation of this method is based on: |
| 175 | * <ul> |
| 176 | * <li><a href="http://mathworld.wolfram.com/BetaFunction.html"> |
| 177 | * Beta Function</a>, equation (1).</li> |
| 178 | * </ul> |
| 179 | * |
| 180 | * @param a the a parameter. |
| 181 | * @param b the b parameter. |
| 182 | * @param epsilon When the absolute value of the nth item in the |
| 183 | * series is less than epsilon the approximation ceases |
| 184 | * to calculate further elements in the series. |
| 185 | * @param maxIterations Maximum number of "iterations" to complete. |
| 186 | * @return log(B(a, b)) |
| 187 | */ |
| 188 | public static double logBeta(double a, double b, double epsilon, |
| 189 | int maxIterations) { |
| 190 | |
| 191 | double ret; |
| 192 | |
| 193 | if (Double.isNaN(a) || Double.isNaN(b) || (a <= 0.0) || (b <= 0.0)) { |
| 194 | ret = Double.NaN; |
| 195 | } else { |
| 196 | ret = Gamma.logGamma(a) + Gamma.logGamma(b) - |
| 197 | Gamma.logGamma(a + b); |
| 198 | } |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | } |