statrs/function/
logistic.rs

1//! Provides the [logistic](http://en.wikipedia.org/wiki/Logistic_function) and
2//! related functions
3
4/// Computes the logistic function
5pub fn logistic(p: f64) -> f64 {
6    1.0 / ((-p).exp() + 1.0)
7}
8
9/// Computes the logit function
10///
11/// # Panics
12///
13/// If `p < 0.0` or `p > 1.0`
14pub fn logit(p: f64) -> f64 {
15    checked_logit(p).unwrap()
16}
17
18/// Computes the logit function, returning `None` if `p < 0.0` or `p > 1.0`.
19pub fn checked_logit(p: f64) -> Option<f64> {
20    if (0.0..=1.0).contains(&p) {
21        Some((p / (1.0 - p)).ln())
22    } else {
23        None
24    }
25}
26
27#[rustfmt::skip]
28#[cfg(test)]
29mod tests {
30    use std::f64;
31
32    #[test]
33    fn test_logistic() {
34        assert_eq!(super::logistic(f64::NEG_INFINITY), 0.0);
35        assert_eq!(super::logistic(-11.512915464920228103874353849992239636376994324587), 0.00001);
36        assert_almost_eq!(super::logistic(-6.9067547786485535272274487616830597875179908939086), 0.001, 1e-18);
37        assert_almost_eq!(super::logistic(-2.1972245773362193134015514347727700402304323440139), 0.1, 1e-16);
38        assert_eq!(super::logistic(0.0), 0.5);
39        assert_almost_eq!(super::logistic(2.1972245773362195801634726294284168954491240598975), 0.9, 1e-15);
40        assert_almost_eq!(super::logistic(6.9067547786485526081487245019905638981131702804661), 0.999, 1e-15);
41        assert_eq!(super::logistic(11.512915464924779098232747799811946290419057060965), 0.99999);
42        assert_eq!(super::logistic(f64::INFINITY), 1.0);
43    }
44
45    #[test]
46    fn test_logit() {
47        assert_eq!(super::logit(0.0), f64::NEG_INFINITY);
48        assert_eq!(super::logit(0.00001), -11.512915464920228103874353849992239636376994324587);
49        assert_eq!(super::logit(0.001), -6.9067547786485535272274487616830597875179908939086);
50        assert_eq!(super::logit(0.1), -2.1972245773362193134015514347727700402304323440139);
51        assert_eq!(super::logit(0.5), 0.0);
52        assert_eq!(super::logit(0.9), 2.1972245773362195801634726294284168954491240598975);
53        assert_eq!(super::logit(0.999), 6.9067547786485526081487245019905638981131702804661);
54        assert_eq!(super::logit(0.99999), 11.512915464924779098232747799811946290419057060965);
55        assert_eq!(super::logit(1.0), f64::INFINITY);
56    }
57
58    #[test]
59    #[should_panic]
60    fn test_logit_p_lt_0() {
61        super::logit(-1.0);
62    }
63
64    #[test]
65    #[should_panic]
66    fn test_logit_p_gt_1() {
67        super::logit(2.0);
68    }
69
70    #[test]
71    fn test_checked_logit_p_lt_0() {
72        assert!(super::checked_logit(-1.0).is_none());
73    }
74
75    #[test]
76    fn test_checked_logit_p_gt_1() {
77        assert!(super::checked_logit(2.0).is_none());
78    }
79}