Skip to main content

qualia_core_db/solvers/statistics/regression/
discrete.rs

1//! Discrete dependent variable: logit and 2-class LDA wrappers for Civics receipts.
2
3use serde::Serialize;
4
5use crate::solvers::learning::classification::discriminant::LdaModel;
6use crate::solvers::learning::glm::{fit_logistic, GlmModel};
7
8#[derive(Debug, Clone, Serialize)]
9pub struct LogitFitSummary {
10    pub coefficients: Vec<f64>,
11    pub std_errors: Vec<f64>,
12    pub z_values: Vec<f64>,
13    pub p_values: Vec<f64>,
14    pub deviance: f64,
15    pub converged: bool,
16    pub n: usize,
17    pub fit_intercept: bool,
18}
19
20/// Logit fit.
21pub fn fit_logit(
22    x: &[f64],
23    y: &[f64],
24    n: usize,
25    p: usize,
26    fit_intercept: bool,
27) -> Option<LogitFitSummary> {
28    let m: GlmModel = fit_logistic(x, y, n, p, fit_intercept).ok()?;
29    Some(LogitFitSummary {
30        coefficients: m.coefficients,
31        std_errors: m.std_errors,
32        z_values: m.z_values,
33        p_values: m.p_values,
34        deviance: m.deviance,
35        converged: m.converged,
36        n: m.n,
37        fit_intercept: m.fit_intercept,
38    })
39}
40
41#[derive(Debug, Clone, Serialize)]
42pub struct LdaFitSummary {
43    pub classes: Vec<usize>,
44    pub predictions: Vec<usize>,
45    pub n: usize,
46    pub p: usize,
47}
48
49/// Two-or-more class LDA; `y` are integer class labels.
50pub fn fit_lda_2class(x: &[f64], y: &[usize], n: usize, p: usize) -> Option<LdaFitSummary> {
51    let m = LdaModel::fit(x, y, n, p).ok()?;
52    let predictions = m.predict(x, n);
53    Some(LdaFitSummary {
54        classes: m.classes.clone(),
55        predictions,
56        n,
57        p,
58    })
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn logit_separates_simple() {
67        // Mild overlap so IRLS does not hit perfect separation.
68        let x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0];
69        let y = [0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0];
70        let m = fit_logit(&x, &y, 12, 1, true).expect("logit should converge");
71        assert!(m.converged);
72        assert!(m.coefficients[1] > 0.0);
73    }
74
75    #[test]
76    fn lda_two_blobs() {
77        let x = [
78            0.0, 0.0, 0.1, 0.0, -0.1, 0.1, 0.0, -0.1, 5.0, 5.0, 5.1, 4.9, 4.9, 5.1, 5.0, 5.2,
79        ];
80        let y = [0usize, 0, 0, 0, 1, 1, 1, 1];
81        let m = fit_lda_2class(&x, &y, 8, 2).unwrap();
82        assert_eq!(m.classes.len(), 2);
83        let ok = m
84            .predictions
85            .iter()
86            .zip(y.iter())
87            .filter(|(a, b)| a == b)
88            .count();
89        assert!(ok >= 6, "pred={:?}", m.predictions);
90    }
91}