001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.special;
018    
019    import org.apache.commons.math.MathException;
020    import org.apache.commons.math.util.ContinuedFraction;
021    
022    /**
023     * This is a utility class that provides computation methods related to the
024     * Beta family of functions.
025     *
026     * @version $Revision: 811685 $ $Date: 2009-09-05 13:36:48 -0400 (Sat, 05 Sep 2009) $
027     */
028    public class Beta {
029    
030        /** Maximum allowed numerical error. */
031        private static final double DEFAULT_EPSILON = 10e-15;
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        {
054            return regularizedBeta(x, a, b, DEFAULT_EPSILON, 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        {
074            return regularizedBeta(x, a, b, epsilon, Integer.MAX_VALUE);
075        }
076    
077        /**
078         * Returns the regularized beta function I(x, a, b).
079         *
080         * @param x the value.
081         * @param a the a parameter.
082         * @param b the b parameter.
083         * @param maxIterations Maximum number of "iterations" to complete.
084         * @return the regularized beta function I(x, a, b)
085         * @throws MathException if the algorithm fails to converge.
086         */
087        public static double regularizedBeta(double x, double a, double b,
088            int maxIterations) throws MathException
089        {
090            return regularizedBeta(x, a, b, DEFAULT_EPSILON, maxIterations);
091        }
092    
093        /**
094         * Returns the regularized beta function I(x, a, b).
095         *
096         * The implementation of this method is based on:
097         * <ul>
098         * <li>
099         * <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    }