IncompleteBeta.java

  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.numbers.gamma;

  18. /**
  19.  * <a href="https://mathworld.wolfram.com/IncompleteBetaFunction.html">
  20.  * Incomplete Beta function</a>.
  21.  *
  22.  * <p>\[ B_x(a,b) = \int_0^x t^{a-1}\,(1-t)^{b-1}\,dt \]
  23.  *
  24.  * <p>This code has been adapted from the <a href="https://www.boost.org/">Boost</a>
  25.  * {@code c++} implementation {@code <boost/math/special_functions/beta.hpp>}.
  26.  *
  27.  * @see
  28.  * <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/math_toolkit/sf_beta/ibeta_function.html">
  29.  * Boost C++ Incomplete Beta functions</a>
  30.  * @since 1.1
  31.  */
  32. public final class IncompleteBeta {

  33.     /** Private constructor. */
  34.     private IncompleteBeta() {
  35.         // intentionally empty.
  36.     }

  37.     /**
  38.      * Computes the value of the
  39.      * <a href="https://mathworld.wolfram.com/IncompleteBetaFunction.html">
  40.      * incomplete beta function</a> B(x, a, b).
  41.      *
  42.      * <p>\[ B_x(a,b) = \int_0^x t^{a-1}\,(1-t)^{b-1}\,dt \]
  43.      *
  44.      * @param x Value.
  45.      * @param a Parameter {@code a}.
  46.      * @param b Parameter {@code b}.
  47.      * @return the incomplete beta function \( B_x(a, b) \).
  48.      * @throws ArithmeticException if the series evaluation fails to converge.
  49.      */
  50.     public static double value(double x,
  51.                                double a,
  52.                                double b) {
  53.         return BoostBeta.beta(a, b, x);
  54.     }

  55.     /**
  56.      * Computes the value of the
  57.      * <a href="https://mathworld.wolfram.com/IncompleteBetaFunction.html">
  58.      * incomplete beta function</a> B(x, a, b).
  59.      *
  60.      * <p>\[ B_x(a,b) = \int_0^x t^{a-1}\,(1-t)^{b-1}\,dt \]
  61.      *
  62.      * @param x the value.
  63.      * @param a Parameter {@code a}.
  64.      * @param b Parameter {@code b}.
  65.      * @param epsilon Tolerance in series evaluation.
  66.      * @param maxIterations Maximum number of iterations in series evaluation.
  67.      * @return the incomplete beta function \( B_x(a, b) \).
  68.      * @throws ArithmeticException if the series evaluation fails to converge.
  69.      */
  70.     public static double value(double x,
  71.                                final double a,
  72.                                final double b,
  73.                                double epsilon,
  74.                                int maxIterations) {
  75.         return BoostBeta.beta(a, b, x, new Policy(epsilon, maxIterations));
  76.     }

  77.     /**
  78.      * Computes the complement of the
  79.      * <a href="https://mathworld.wolfram.com/IncompleteBetaFunction.html">
  80.      * incomplete beta function</a> B(x, a, b).
  81.      *
  82.      * <p>\[ B(a, b) - B_x(a,b) = B_{1-x}(b, a) \]
  83.      *
  84.      * <p>where \( B(a, b) \) is the beta function.
  85.      *
  86.      * @param x Value.
  87.      * @param a Parameter {@code a}.
  88.      * @param b Parameter {@code b}.
  89.      * @return the complement of the incomplete beta function \( B(a, b) - B_x(a, b) \).
  90.      * @throws ArithmeticException if the series evaluation fails to converge.
  91.      */
  92.     public static double complement(double x,
  93.                                     double a,
  94.                                     double b) {
  95.         return BoostBeta.betac(a, b, x);
  96.     }

  97.     /**
  98.      * Computes the complement of the
  99.      * <a href="https://mathworld.wolfram.com/IncompleteBetaFunction.html">
  100.      * incomplete beta function</a> B(x, a, b).
  101.      *
  102.      * <p>\[ B(a, b) - B_x(a,b) = B_{1-x}(b, a) \]
  103.      *
  104.      * <p>where \( B(a, b) \) is the beta function.
  105.      *
  106.      * @param x the value.
  107.      * @param a Parameter {@code a}.
  108.      * @param b Parameter {@code b}.
  109.      * @param epsilon Tolerance in series evaluation.
  110.      * @param maxIterations Maximum number of iterations in series evaluation.
  111.      * @return the complement of the incomplete beta function \( B(a, b) - B_x(a, b) \).
  112.      * @throws ArithmeticException if the series evaluation fails to converge.
  113.      */
  114.     public static double complement(double x,
  115.                                     final double a,
  116.                                     final double b,
  117.                                     double epsilon,
  118.                                     int maxIterations) {
  119.         return BoostBeta.betac(a, b, x, new Policy(epsilon, maxIterations));
  120.     }
  121. }