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 */
017package org.apache.commons.numbers.gamma;
018
019/**
020 * <a href="https://mathworld.wolfram.com/IncompleteBetaFunction.html">
021 * Incomplete Beta function</a>.
022 *
023 * <p>\[ B_x(a,b) = \int_0^x t^{a-1}\,(1-t)^{b-1}\,dt \]
024 *
025 * <p>This code has been adapted from the <a href="https://www.boost.org/">Boost</a>
026 * {@code c++} implementation {@code <boost/math/special_functions/beta.hpp>}.
027 *
028 * @see
029 * <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/math_toolkit/sf_beta/ibeta_function.html">
030 * Boost C++ Incomplete Beta functions</a>
031 * @since 1.1
032 */
033public final class IncompleteBeta {
034
035    /** Private constructor. */
036    private IncompleteBeta() {
037        // intentionally empty.
038    }
039
040    /**
041     * Computes the value of the
042     * <a href="https://mathworld.wolfram.com/IncompleteBetaFunction.html">
043     * incomplete beta function</a> B(x, a, b).
044     *
045     * <p>\[ B_x(a,b) = \int_0^x t^{a-1}\,(1-t)^{b-1}\,dt \]
046     *
047     * @param x Value.
048     * @param a Parameter {@code a}.
049     * @param b Parameter {@code b}.
050     * @return the incomplete beta function \( B_x(a, b) \).
051     * @throws ArithmeticException if the series evaluation fails to converge.
052     */
053    public static double value(double x,
054                               double a,
055                               double b) {
056        return BoostBeta.beta(a, b, x);
057    }
058
059    /**
060     * Computes the value of the
061     * <a href="https://mathworld.wolfram.com/IncompleteBetaFunction.html">
062     * incomplete beta function</a> B(x, a, b).
063     *
064     * <p>\[ B_x(a,b) = \int_0^x t^{a-1}\,(1-t)^{b-1}\,dt \]
065     *
066     * @param x the value.
067     * @param a Parameter {@code a}.
068     * @param b Parameter {@code b}.
069     * @param epsilon Tolerance in series evaluation.
070     * @param maxIterations Maximum number of iterations in series evaluation.
071     * @return the incomplete beta function \( B_x(a, b) \).
072     * @throws ArithmeticException if the series evaluation fails to converge.
073     */
074    public static double value(double x,
075                               final double a,
076                               final double b,
077                               double epsilon,
078                               int maxIterations) {
079        return BoostBeta.beta(a, b, x, new Policy(epsilon, maxIterations));
080    }
081
082    /**
083     * Computes the complement of the
084     * <a href="https://mathworld.wolfram.com/IncompleteBetaFunction.html">
085     * incomplete beta function</a> B(x, a, b).
086     *
087     * <p>\[ B(a, b) - B_x(a,b) = B_{1-x}(b, a) \]
088     *
089     * <p>where \( B(a, b) \) is the beta function.
090     *
091     * @param x Value.
092     * @param a Parameter {@code a}.
093     * @param b Parameter {@code b}.
094     * @return the complement of the incomplete beta function \( B(a, b) - B_x(a, b) \).
095     * @throws ArithmeticException if the series evaluation fails to converge.
096     */
097    public static double complement(double x,
098                                    double a,
099                                    double b) {
100        return BoostBeta.betac(a, b, x);
101    }
102
103    /**
104     * Computes the complement of the
105     * <a href="https://mathworld.wolfram.com/IncompleteBetaFunction.html">
106     * incomplete beta function</a> B(x, a, b).
107     *
108     * <p>\[ B(a, b) - B_x(a,b) = B_{1-x}(b, a) \]
109     *
110     * <p>where \( B(a, b) \) is the beta function.
111     *
112     * @param x the value.
113     * @param a Parameter {@code a}.
114     * @param b Parameter {@code b}.
115     * @param epsilon Tolerance in series evaluation.
116     * @param maxIterations Maximum number of iterations in series evaluation.
117     * @return the complement of the incomplete beta function \( B(a, b) - B_x(a, b) \).
118     * @throws ArithmeticException if the series evaluation fails to converge.
119     */
120    public static double complement(double x,
121                                    final double a,
122                                    final double b,
123                                    double epsilon,
124                                    int maxIterations) {
125        return BoostBeta.betac(a, b, x, new Policy(epsilon, maxIterations));
126    }
127}