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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.special;
19
import org.apache.commons.math.MathException;
20
import org.apache.commons.math.util.ContinuedFraction;
23
* This is a utility class that provides computation methods related to the
24
* Beta family of functions.
26
* @version $Revision: 780933 $ $Date: 2009-06-02 00:39:12 -0400 (Tue, 02 Jun 2009) $
30
/** Maximum allowed numerical error. */
31
private static final double DEFAULT_EPSILON = 10e-15;
34
* Default constructor. Prohibit instantiation.
42
* <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
43
* regularized beta function</a> I(x, a, b).
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.
51
public static double regularizedBeta(double x, double a, double b)
54
return regularizedBeta(x, a, b, DEFAULT_EPSILON, Integer.MAX_VALUE);
59
* <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
60
* regularized beta function</a> I(x, a, b).
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.
71
public static double regularizedBeta(double x, double a, double b,
72
double epsilon) throws MathException
74
return regularizedBeta(x, a, b, epsilon, Integer.MAX_VALUE);
78
* Returns the regularized beta function I(x, a, b).
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.
87
public static double regularizedBeta(double x, double a, double b,
88
int maxIterations) throws MathException
90
return regularizedBeta(x, a, b, DEFAULT_EPSILON, maxIterations);
94
* Returns the regularized beta function I(x, a, b).
96
* The implementation of this method is based on:
99
* <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
100
* Regularized Beta Function</a>.</li>
102
* <a href="http://functions.wolfram.com/06.21.10.0001.01">
103
* Regularized Beta Function</a>.</li>
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.
116
public static double regularizedBeta(double x, final double a,
117
final double b, double epsilon, int maxIterations) throws MathException
121
if (Double.isNaN(x) || Double.isNaN(a) || Double.isNaN(b) || (x < 0) ||
122
(x > 1) || (a <= 0.0) || (b <= 0.0))
125
} else if (x > (a + 1.0) / (a + b + 2.0)) {
126
ret = 1.0 - regularizedBeta(1.0 - x, b, a, epsilon, maxIterations);
128
ContinuedFraction fraction = new ContinuedFraction() {
131
protected double getB(int n, double x) {
134
if (n % 2 == 0) { // even
136
ret = (m * (b - m) * x) /
137
((a + (2 * m) - 1) * (a + (2 * m)));
140
ret = -((a + m) * (a + b + m) * x) /
141
((a + (2 * m)) * (a + (2 * m) + 1.0));
147
protected double getA(int n, double x) {
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);
160
* Returns the natural logarithm of the beta function B(a, b).
162
* @param a the a parameter.
163
* @param b the b parameter.
164
* @return log(B(a, b))
166
public static double logBeta(double a, double b) {
167
return logBeta(a, b, DEFAULT_EPSILON, Integer.MAX_VALUE);
171
* Returns the natural logarithm of the beta function B(a, b).
173
* The implementation of this method is based on:
175
* <li><a href="http://mathworld.wolfram.com/BetaFunction.html">
176
* Beta Function</a>, equation (1).</li>
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))
187
public static double logBeta(double a, double b, double epsilon,
192
if (Double.isNaN(a) || Double.isNaN(b) || (a <= 0.0) || (b <= 0.0)) {
195
ret = Gamma.logGamma(a) + Gamma.logGamma(b) -
196
Gamma.logGamma(a + b);