001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.special;
017:
018: import java.io.Serializable;
019:
020: import org.apache.commons.math.MathException;
021: import org.apache.commons.math.util.ContinuedFraction;
022:
023: /**
024: * This is a utility class that provides computation methods related to the
025: * Beta family of functions.
026: *
027: * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
028: */
029: public class Beta implements Serializable {
030: /** Maximum allowed numerical error. */
031: private static final double DEFAULT_EPSILON = 10e-9;
032:
033: /**
034: * Default constructor. Prohibit instantiation.
035: */
036: private Beta() {
037: super ();
038: }
039:
040: /**
041: * Returns the
042: * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
043: * regularized beta function</a> I(x, a, b).
044: *
045: * @param x the value.
046: * @param a the a parameter.
047: * @param b the b parameter.
048: * @return the regularized beta function I(x, a, b)
049: * @throws MathException if the algorithm fails to converge.
050: */
051: public static double regularizedBeta(double x, double a, double b)
052: throws MathException {
053: return regularizedBeta(x, a, b, DEFAULT_EPSILON,
054: Integer.MAX_VALUE);
055: }
056:
057: /**
058: * Returns the
059: * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
060: * regularized beta function</a> I(x, a, b).
061: *
062: * @param x the value.
063: * @param a the a parameter.
064: * @param b the b parameter.
065: * @param epsilon When the absolute value of the nth item in the
066: * series is less than epsilon the approximation ceases
067: * to calculate further elements in the series.
068: * @return the regularized beta function I(x, a, b)
069: * @throws MathException if the algorithm fails to converge.
070: */
071: public static double regularizedBeta(double x, double a, double b,
072: double epsilon) throws MathException {
073: return regularizedBeta(x, a, b, epsilon, Integer.MAX_VALUE);
074: }
075:
076: /**
077: * Returns the regularized beta function I(x, a, b).
078: *
079: * @param x the value.
080: * @param a the a parameter.
081: * @param b the b parameter.
082: * @param maxIterations Maximum number of "iterations" to complete.
083: * @return the regularized beta function I(x, a, b)
084: * @throws MathException if the algorithm fails to converge.
085: */
086: public static double regularizedBeta(double x, double a, double b,
087: int maxIterations) throws MathException {
088: return regularizedBeta(x, a, b, DEFAULT_EPSILON, maxIterations);
089: }
090:
091: /**
092: * Returns the regularized beta function I(x, a, b).
093: *
094: * The implementation of this method is based on:
095: * <ul>
096: * <li>
097: * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
098: * Regularized Beta Function</a>.</li>
099: * <li>
100: * <a href="http://functions.wolfram.com/06.21.10.0001.01">
101: * Regularized Beta Function</a>.</li>
102: * </ul>
103: *
104: * @param x the value.
105: * @param a the a parameter.
106: * @param b the b parameter.
107: * @param epsilon When the absolute value of the nth item in the
108: * series is less than epsilon the approximation ceases
109: * to calculate further elements in the series.
110: * @param maxIterations Maximum number of "iterations" to complete.
111: * @return the regularized beta function I(x, a, b)
112: * @throws MathException if the algorithm fails to converge.
113: */
114: public static double regularizedBeta(double x, final double a,
115: final double b, double epsilon, int maxIterations)
116: throws MathException {
117: double ret;
118:
119: if (Double.isNaN(x) || Double.isNaN(a) || Double.isNaN(b)
120: || (x < 0) || (x > 1) || (a <= 0.0) || (b <= 0.0)) {
121: ret = Double.NaN;
122: } else if (x > (a + 1.0) / (a + b + 2.0)) {
123: ret = 1.0 - regularizedBeta(1.0 - x, b, a, epsilon,
124: maxIterations);
125: } else {
126: ContinuedFraction fraction = new ContinuedFraction() {
127: protected double getB(int n, double x) {
128: double ret;
129: double m;
130: if (n % 2 == 0) { // even
131: m = n / 2.0;
132: ret = (m * (b - m) * x)
133: / ((a + (2 * m) - 1) * (a + (2 * m)));
134: } else {
135: m = (n - 1.0) / 2.0;
136: ret = -((a + m) * (a + b + m) * x)
137: / ((a + (2 * m)) * (a + (2 * m) + 1.0));
138: }
139: return ret;
140: }
141:
142: protected double getA(int n, double x) {
143: return 1.0;
144: }
145: };
146: ret = Math.exp((a * Math.log(x)) + (b * Math.log(1.0 - x))
147: - Math.log(a)
148: - logBeta(a, b, epsilon, maxIterations))
149: * 1.0
150: / fraction.evaluate(x, epsilon, maxIterations);
151: }
152:
153: return ret;
154: }
155:
156: /**
157: * Returns the natural logarithm of the beta function B(a, b).
158: *
159: * @param a the a parameter.
160: * @param b the b parameter.
161: * @return log(B(a, b))
162: */
163: public static double logBeta(double a, double b) {
164: return logBeta(a, b, DEFAULT_EPSILON, Integer.MAX_VALUE);
165: }
166:
167: /**
168: * Returns the natural logarithm of the beta function B(a, b).
169: *
170: * The implementation of this method is based on:
171: * <ul>
172: * <li><a href="http://mathworld.wolfram.com/BetaFunction.html">
173: * Beta Function</a>, equation (1).</li>
174: * </ul>
175: *
176: * @param a the a parameter.
177: * @param b the b parameter.
178: * @param epsilon When the absolute value of the nth item in the
179: * series is less than epsilon the approximation ceases
180: * to calculate further elements in the series.
181: * @param maxIterations Maximum number of "iterations" to complete.
182: * @return log(B(a, b))
183: */
184: public static double logBeta(double a, double b, double epsilon,
185: int maxIterations) {
186:
187: double ret;
188:
189: if (Double.isNaN(a) || Double.isNaN(b) || (a <= 0.0)
190: || (b <= 0.0)) {
191: ret = Double.NaN;
192: } else {
193: ret = Gamma.logGamma(a) + Gamma.logGamma(b)
194: - Gamma.logGamma(a + b);
195: }
196:
197: return ret;
198: }
199: }
|