ndarray_glm/response/
binomial.rs

1//! Regression with a binomial response function. The N parameter must be known ahead of time.
2use crate::{
3    error::{RegressionError, RegressionResult},
4    glm::{DispersionType, Glm},
5    math::prod_log,
6    num::Float,
7    response::Response,
8};
9
10/// Use a fixed type of u16 for the domain of the binomial distribution.
11type BinDom = u16;
12
13/// Binomial regression with a fixed N. Non-canonical link functions are not
14/// possible at this time due to the awkward ergonomics with the const trait
15/// parameter N.
16pub struct Binomial<const N: BinDom>;
17
18impl<const N: BinDom> Response<Binomial<N>> for BinDom {
19    fn into_float<F: Float>(self) -> RegressionResult<F> {
20        F::from(self).ok_or_else(|| RegressionError::InvalidY(self.to_string()))
21    }
22}
23
24impl<const N: BinDom> Glm for Binomial<N> {
25    /// Only the canonical link function is available for binomial regression.
26    type Link = link::Logit;
27    const DISPERSED: DispersionType = DispersionType::NoDispersion;
28
29    /// The log-partition function for the binomial distribution is similar to
30    /// that for logistic regression, but it is adjusted for the maximum value.
31    fn log_partition<F: Float>(nat_par: F) -> F {
32        let n: F = F::from(N).unwrap();
33        n * num_traits::Float::exp(nat_par).ln_1p()
34    }
35
36    fn variance<F: Float>(mean: F) -> F {
37        let n_float: F = F::from(N).unwrap();
38        mean * (n_float - mean) / n_float
39    }
40
41    fn log_like_sat<F: Float>(y: F) -> F {
42        let n: F = F::from(N).unwrap();
43        prod_log(y) + prod_log(n - y) - prod_log(n)
44    }
45}
46
47pub mod link {
48    use super::*;
49    use crate::link::{Canonical, Link};
50    use num_traits::Float;
51
52    pub struct Logit {}
53    impl Canonical for Logit {}
54    impl<const N: BinDom> Link<Binomial<N>> for Logit {
55        fn func<F: Float>(y: F) -> F {
56            let n_float: F = F::from(N).unwrap();
57            Float::ln(y / (n_float - y))
58        }
59        fn func_inv<F: Float>(lin_pred: F) -> F {
60            let n_float: F = F::from(N).unwrap();
61            n_float / (F::one() + (-lin_pred).exp())
62        }
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::Binomial;
69    use crate::{error::RegressionResult, model::ModelBuilder};
70    use approx::assert_abs_diff_eq;
71    use ndarray::array;
72
73    #[test]
74    fn bin_reg() -> RegressionResult<()> {
75        const N: u16 = 12;
76        let ln2 = f64::ln(2.);
77        let beta = array![0., 1.];
78        let data_x = array![[0.], [0.], [ln2], [ln2], [ln2]];
79        // the first two data points should average to 6 and the last 3 should average to 8.
80        let data_y = array![5, 7, 9, 6, 9];
81        let model = ModelBuilder::<Binomial<N>>::data(&data_y, &data_x).build()?;
82        let fit = model.fit()?;
83        dbg!(&fit.result);
84        dbg!(&fit.n_iter);
85        assert_abs_diff_eq!(beta, fit.result, epsilon = 0.05 * f32::EPSILON as f64);
86        Ok(())
87    }
88}