Skip to main content

lib_jsl_special_functions/
incomplete_beta.rs

1//! Incomplete beta function.
2//!
3//! See the book entry [Incomplete gamma and beta functions and the error function](https://jwkunz.github.io/lib_jsl/book/special_functions/incomplete.html#the-incomplete-beta-function).
4
5use crate::gamma::ln_gamma;
6
7const EPS: f64 = 1e-16;
8const FPMIN: f64 = f64::MIN_POSITIVE / EPS;
9
10// ANCHOR: beta_cf
11/// Continued fraction for the incomplete beta function (modified Lentz).
12fn beta_cf(a: f64, b: f64, x: f64) -> f64 {
13    let qab = a + b;
14    let qap = a + 1.0;
15    let qam = a - 1.0;
16    let mut c = 1.0;
17    let mut d = 1.0 - qab * x / qap;
18    if d.abs() < FPMIN {
19        d = FPMIN;
20    }
21    d = 1.0 / d;
22    let mut h = d;
23    let max_iter = 1000 + (10.0 * a.max(b).sqrt()) as usize * 10;
24    for m in 1..=max_iter {
25        let m = m as f64;
26        let m2 = 2.0 * m;
27        let aa = m * (b - m) * x / ((qam + m2) * (a + m2));
28        d = 1.0 + aa * d;
29        if d.abs() < FPMIN {
30            d = FPMIN;
31        }
32        c = 1.0 + aa / c;
33        if c.abs() < FPMIN {
34            c = FPMIN;
35        }
36        d = 1.0 / d;
37        h *= d * c;
38        let aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2));
39        d = 1.0 + aa * d;
40        if d.abs() < FPMIN {
41            d = FPMIN;
42        }
43        c = 1.0 + aa / c;
44        if c.abs() < FPMIN {
45            c = FPMIN;
46        }
47        d = 1.0 / d;
48        let del = d * c;
49        h *= del;
50        if (del - 1.0).abs() < EPS {
51            break;
52        }
53    }
54    h
55}
56// ANCHOR_END: beta_cf
57
58/// The regularized incomplete beta function I_x(a, b) for `a, b > 0` and `0 <= x <= 1`.
59///
60/// Returns `NaN` for invalid arguments.
61pub fn incomplete_beta(a: f64, b: f64, x: f64) -> f64 {
62    if !(a > 0.0) || !(b > 0.0) || x.is_nan() || !(0.0..=1.0).contains(&x) {
63        return f64::NAN;
64    }
65    if x == 0.0 {
66        return 0.0;
67    }
68    if x == 1.0 {
69        return 1.0;
70    }
71    let bt = (ln_gamma(a + b) - ln_gamma(a) - ln_gamma(b) + a * x.ln() + b * (-x).ln_1p()).exp();
72    if x < (a + 1.0) / (a + b + 2.0) {
73        bt * beta_cf(a, b, x) / a
74    } else {
75        1.0 - bt * beta_cf(b, a, 1.0 - x) / b
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn closed_forms() {
85        // I_x(1, 1) = x; I_x(a, 1) = x^a; I_x(1, b) = 1 - (1-x)^b
86        assert!((incomplete_beta(1.0, 1.0, 0.37) - 0.37).abs() < 1e-14);
87        assert!((incomplete_beta(2.5, 1.0, 0.6) - 0.6f64.powf(2.5)).abs() < 1e-14);
88        assert!((incomplete_beta(1.0, 3.0, 0.2) - (1.0 - 0.8f64.powi(3))).abs() < 1e-14);
89        // I_x(2, 2) = 3x^2 - 2x^3
90        let x = 0.3;
91        assert!((incomplete_beta(2.0, 2.0, x) - (3.0 * x * x - 2.0 * x * x * x)).abs() < 1e-14);
92    }
93
94    #[test]
95    fn symmetry_and_median() {
96        for &(a, b, x) in &[(2.0, 5.0, 0.3), (0.5, 0.5, 0.9), (10.0, 3.0, 0.8), (40.0, 60.0, 0.45)] {
97            let s = incomplete_beta(a, b, x) + incomplete_beta(b, a, 1.0 - x);
98            assert!((s - 1.0).abs() < 1e-13);
99        }
100        assert!((incomplete_beta(3.0, 3.0, 0.5) - 0.5).abs() < 1e-14);
101        // arcsine distribution: I_x(1/2, 1/2) = 2/pi asin(sqrt(x))
102        let x: f64 = 0.7;
103        let want = 2.0 / std::f64::consts::PI * x.sqrt().asin();
104        assert!((incomplete_beta(0.5, 0.5, x) - want).abs() < 1e-14);
105    }
106
107    #[test]
108    fn invalid() {
109        assert!(incomplete_beta(-1.0, 1.0, 0.5).is_nan());
110        assert!(incomplete_beta(1.0, 1.0, 1.5).is_nan());
111        assert_eq!(incomplete_beta(1.0, 1.0, 0.0), 0.0);
112        assert_eq!(incomplete_beta(1.0, 1.0, 1.0), 1.0);
113    }
114}