RegularizedBeta.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/RegularizedBetaFunction.html">
  20.  * Regularized Beta function</a>.
  21.  *
  22.  * <p>\[ I_x(a,b) = \frac{1}{B(a, b)} \int_0^x t^{a-1}\,(1-t)^{b-1}\,dt \]
  23.  *
  24.  * <p>where \( B(a, b) \) is the beta function.
  25.  *
  26.  * <p>This code has been adapted from the <a href="https://www.boost.org/">Boost</a>
  27.  * {@code c++} implementation {@code <boost/math/special_functions/beta.hpp>}.
  28.  *
  29.  * @see
  30.  * <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/math_toolkit/sf_beta/ibeta_function.html">
  31.  * Boost C++ Incomplete Beta functions</a>
  32.  */
  33. public final class RegularizedBeta {

  34.     /** Private constructor. */
  35.     private RegularizedBeta() {
  36.         // intentionally empty.
  37.     }

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

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

  82.     /**
  83.      * Computes the complement of the
  84.      * <a href="https://mathworld.wolfram.com/RegularizedBetaFunction.html">
  85.      * regularized beta function</a> I(x, a, b).
  86.      *
  87.      * <p>\[ 1 - I_x(a,b) = I_{1-x}(b, a) \]
  88.      *
  89.      * @param x Value.
  90.      * @param a Parameter {@code a}.
  91.      * @param b Parameter {@code b}.
  92.      * @return the complement of the regularized beta function \( 1 - I_x(a, b) \).
  93.      * @throws ArithmeticException if the series evaluation fails to converge.
  94.      * @since 1.1
  95.      */
  96.     public static double complement(double x,
  97.                                     double a,
  98.                                     double b) {
  99.         return BoostBeta.ibetac(a, b, x);
  100.     }

  101.     /**
  102.      * Computes the complement of the
  103.      * <a href="https://mathworld.wolfram.com/RegularizedBetaFunction.html">
  104.      * regularized beta function</a> I(x, a, b).
  105.      *
  106.      * <p>\[ 1 - I_x(a,b) = I_{1-x}(b, a) \]
  107.      *
  108.      * @param x the value.
  109.      * @param a Parameter {@code a}.
  110.      * @param b Parameter {@code b}.
  111.      * @param epsilon Tolerance in series evaluation.
  112.      * @param maxIterations Maximum number of iterations in series evaluation.
  113.      * @return the complement of the regularized beta function \( 1 - I_x(a, b) \).
  114.      * @throws ArithmeticException if the series evaluation fails to converge.
  115.      * @since 1.1
  116.      */
  117.     public static double complement(double x,
  118.                                     final double a,
  119.                                     final double b,
  120.                                     double epsilon,
  121.                                     int maxIterations) {
  122.         return BoostBeta.ibetac(a, b, x, new Policy(epsilon, maxIterations));
  123.     }

  124.     /**
  125.      * Computes the derivative of the
  126.      * <a href="https://mathworld.wolfram.com/RegularizedBetaFunction.html">
  127.      * regularized beta function</a> I(x, a, b).
  128.      *
  129.      * <p>\[ \frac{\delta}{\delta x} I_x(a,b) = \frac{(1-x)^{b-1} x^{a-1}}{B(a, b)} \]
  130.      *
  131.      * <p>where \( B(a, b) \) is the beta function.
  132.      *
  133.      * <p>This function has uses in some statistical distributions.
  134.      *
  135.      * @param x Value.
  136.      * @param a Parameter {@code a}.
  137.      * @param b Parameter {@code b}.
  138.      * @return the derivative of the regularized beta function \( I_x(a, b) \).
  139.      * @throws ArithmeticException if the series evaluation fails to converge.
  140.      * @since 1.1
  141.      */
  142.     public static double derivative(double x,
  143.                                     double a,
  144.                                     double b) {
  145.         return BoostBeta.ibetaDerivative(a, b, x);
  146.     }
  147. }