Skip to main content

lib_jsl_special_functions/
error_function.rs

1//! Error 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-error-function).
4
5use crate::incomplete_gamma::{incomplete_gamma, incomplete_gamma_upper};
6use std::f64::consts::PI;
7
8/// The error function erf(x) = 2/√π ∫₀ˣ e^(-t²) dt.
9pub fn error_function(x: f64) -> f64 {
10    if x.is_nan() {
11        return f64::NAN;
12    }
13    let r = incomplete_gamma(0.5, x * x);
14    if x < 0.0 { -r } else { r }
15}
16
17/// The complementary error function erfc(x) = 1 - erf(x), accurate in the upper tail.
18pub fn erfc(x: f64) -> f64 {
19    if x.is_nan() {
20        return f64::NAN;
21    }
22    if x >= 0.0 {
23        incomplete_gamma_upper(0.5, x * x)
24    } else {
25        2.0 - incomplete_gamma_upper(0.5, x * x)
26    }
27}
28
29/// The inverse error function: returns `x` such that `erf(x) = y`, for `y` in `(-1, 1)`.
30///
31/// Returns ±infinity at ±1 and `NaN` outside `[-1, 1]`.
32pub fn erf_inv(y: f64) -> f64 {
33    if y.is_nan() || y.abs() > 1.0 {
34        return f64::NAN;
35    }
36    if y == 1.0 {
37        return f64::INFINITY;
38    }
39    if y == -1.0 {
40        return f64::NEG_INFINITY;
41    }
42    if y == 0.0 {
43        return 0.0;
44    }
45    if y < 0.0 {
46        return -erf_inv(-y);
47    }
48    // Winitzki initial guess, refined with Halley's method.
49    let a = 0.147;
50    let l = (1.0 - y * y).ln();
51    let t = 2.0 / (PI * a) + 0.5 * l;
52    let mut x = ((t * t - l / a).sqrt() - t).sqrt();
53    for _ in 0..8 {
54        // Solve in the better-conditioned form for the tail.
55        let f = if y > 0.5 { (1.0 - y) - erfc(x) } else { error_function(x) - y };
56        let d = 2.0 / PI.sqrt() * (-x * x).exp();
57        if d == 0.0 {
58            break;
59        }
60        let step = f / (d + x * f); // Halley: f / (f' - f f''/(2 f')), with f''/f' = -2x
61        x -= step;
62        if step.abs() < 1e-16 * x.abs() {
63            break;
64        }
65    }
66    x
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn erf_values() {
75        assert_eq!(error_function(0.0), 0.0);
76        assert!((error_function(0.5) - 0.5204998778130465).abs() < 1e-15);
77        assert!((error_function(1.0) - 0.8427007929497149).abs() < 1e-15);
78        assert!((error_function(2.0) - 0.9953222650189527).abs() < 1e-15);
79        assert!((error_function(-1.0) + 0.8427007929497149).abs() < 1e-15);
80        assert_eq!(error_function(30.0), 1.0);
81    }
82
83    #[test]
84    fn erfc_tail() {
85        assert!((erfc(1.0) - 0.15729920705028513).abs() < 1e-15);
86        assert!((erfc(5.0) / 1.537_459_794_428_035e-12 - 1.0).abs() < 1e-12);
87        assert!((erfc(-1.0) - 1.8427007929497148).abs() < 1e-15);
88    }
89
90    #[test]
91    fn inverse_round_trip() {
92        for &y in &[-0.999, -0.7, -0.1, 1e-8, 0.3, 0.5, 0.9, 0.999999, 1.0 - 1e-12] {
93            let x = erf_inv(y);
94            assert!((error_function(x) - y).abs() < 1e-14, "y={y}");
95        }
96        assert!((erf_inv(0.5) - 0.4769362762044699).abs() < 1e-14);
97        assert!(erf_inv(1.0).is_infinite());
98        assert!(erf_inv(1.5).is_nan());
99    }
100}