stats-claw 0.1.0

Data science on the hot path: in-process, zero-dependency statistical computing for Rust (distributions, hypothesis tests, resampling) validated against scipy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Gaussian mixture model fit by expectation–maximisation.
//!
//! A GMM models the data as a weighted sum of `k` multivariate Gaussians and is fit
//! by EM: the E-step computes each point's responsibility (posterior probability) for
//! every component using the [`NormalDistribution`](crate::distributions::NormalDistribution)
//! family's Gaussian density generalised to full covariance; the M-step re-estimates
//! the mixture weights, means, and covariances from those responsibilities. Iteration
//! continues until the average log-likelihood stops improving by more than `tol`.
//! Initialisation reuses the framework's k-means (which itself uses the deterministic
//! [`SplitMix64`](crate::rng::SplitMix64) RNG), matching `scikit-learn`'s
//! `init_params="kmeans"`.
//!
//! The Bayesian and Akaike information criteria are identifiable (free of label
//! permutation), so the equivalence suite compares them to `sklearn.mixture.
//! GaussianMixture` directly, alongside an adjusted-Rand-index check on the hard
//! assignments and a verification that every responsibility row is a probability
//! distribution.

use crate::algorithms::clustering::kmeans;
use crate::algorithms::decomposition::{at, count_to_f64, jacobi_eigen, put, symmetric_inverse};

/// Diagonal regularisation added to each covariance, matching `scikit-learn`'s
/// `reg_covar` default; keeps covariances positive-definite.
const REG_COVAR: f64 = 1e-6;

/// Outcome of a Gaussian-mixture EM fit.
#[derive(Debug, Clone)]
pub struct GmmResult {
    /// Hard cluster assignment (arg-max responsibility) per point, in input order.
    pub labels: Vec<usize>,
    /// Soft assignments: `responsibilities[i][c]` is point `i`'s posterior for
    /// component `c`; each row sums to one.
    pub responsibilities: Vec<Vec<f64>>,
    /// Bayesian information criterion `−2·logL + p·ln(n)` (lower is better).
    pub bic: f64,
    /// Akaike information criterion `−2·logL + 2p` (lower is better).
    pub aic: f64,
}

/// Fits a `k`-component Gaussian mixture to `data` by EM.
///
/// # Arguments
///
/// * `data` — observations; each inner slice is one point of equal dimension. Empty
///   input yields an empty result.
/// * `k` — number of mixture components, clamped to the number of points.
/// * `max_iter` — maximum EM iterations.
/// * `tol` — convergence tolerance on the per-sample average log-likelihood change.
/// * `seed` — RNG seed for the k-means initialisation (determinism contract).
///
/// # Returns
///
/// A [`GmmResult`] with hard/soft assignments and the BIC/AIC information criteria.
///
/// # Examples
///
/// ```
/// use stats_claw::algorithms::clustering::gmm_em;
///
/// let data = vec![
///     vec![0.0, 0.0],
///     vec![0.2, 0.1],
///     vec![10.0, 10.0],
///     vec![10.1, 9.9],
/// ];
/// let r = gmm_em(&data, 2, 100, 1e-3, 42);
/// // The two tight pairs fall into different components.
/// assert_ne!(r.labels.first(), r.labels.get(2));
/// ```
#[must_use]
pub fn gmm_em(data: &[Vec<f64>], k: usize, max_iter: usize, tol: f64, seed: u64) -> GmmResult {
    let n = data.len();
    let dim = data.first().map_or(0, Vec::len);
    let k = k.min(n);
    if n == 0 || dim == 0 || k == 0 {
        return GmmResult {
            labels: Vec::new(),
            responsibilities: Vec::new(),
            bic: 0.0,
            aic: 0.0,
        };
    }
    let mut params = initialise(data, k, dim, seed);
    let mut prev_ll = f64::NEG_INFINITY;
    let mut responsibilities = vec![vec![0.0_f64; k]; n];

    for _ in 0..max_iter {
        let avg_ll = e_step(data, &params, &mut responsibilities, k, dim);
        m_step(data, &responsibilities, &mut params, k, dim);
        let converged = (avg_ll - prev_ll).abs() < tol;
        prev_ll = avg_ll;
        if converged {
            break;
        }
    }
    // Final E-step so responsibilities and the log-likelihood reflect the last M-step.
    let avg_ll = e_step(data, &params, &mut responsibilities, k, dim);

    let labels = hard_labels(&responsibilities);
    let total_ll = avg_ll * count_to_f64(n);
    let free = free_parameters(k, dim);
    let bic = free.mul_add(count_to_f64(n).ln(), -2.0 * total_ll);
    let aic = 2.0_f64.mul_add(free, -2.0 * total_ll);
    GmmResult {
        labels,
        responsibilities,
        bic,
        aic,
    }
}

/// The fitted mixture parameters.
struct Params {
    /// Mixture weights `π_c`, summing to one.
    weights: Vec<f64>,
    /// Component means, one length-`dim` vector per component.
    means: Vec<Vec<f64>>,
    /// Component covariances, each a row-major `dim×dim` buffer.
    covariances: Vec<Vec<f64>>,
}

/// Initialises the mixture from a k-means partition: uniform weights, the cluster
/// centroids as means, and per-cluster empirical covariances.
fn initialise(data: &[Vec<f64>], k: usize, dim: usize, seed: u64) -> Params {
    let assignment = kmeans(data, k, 100, seed);
    let mut weights = vec![0.0_f64; k];
    let mut means = assignment.centers.clone();
    means.resize(k, vec![0.0_f64; dim]);
    let mut covariances = vec![diagonal(dim, 1.0); k];

    for cluster in 0..k {
        let members: Vec<&Vec<f64>> = data
            .iter()
            .zip(&assignment.labels)
            .filter(|&(_, &l)| l == cluster)
            .map(|(p, _)| p)
            .collect();
        let count = count_to_f64(members.len());
        if let Some(slot) = weights.get_mut(cluster) {
            *slot = if data.is_empty() {
                0.0
            } else {
                count / count_to_f64(data.len())
            };
        }
        if let (Some(mean), Some(cov)) = (means.get(cluster), covariances.get_mut(cluster)) {
            *cov = empirical_covariance(&members, mean, dim);
        }
    }
    Params {
        weights,
        means,
        covariances,
    }
}

/// E-step: fills `responsibilities` with each point's posterior over components and
/// returns the average per-sample log-likelihood.
fn e_step(
    data: &[Vec<f64>],
    params: &Params,
    responsibilities: &mut [Vec<f64>],
    k: usize,
    dim: usize,
) -> f64 {
    let log_consts: Vec<(Vec<f64>, f64)> =
        (0..k).map(|c| gaussian_log_const(params, c, dim)).collect();
    let mut total = 0.0_f64;
    for (point, resp) in data.iter().zip(responsibilities.iter_mut()) {
        // log(π_c) + log N(x | μ_c, Σ_c) per component.
        let log_weighted: Vec<f64> = (0..k)
            .map(|c| {
                let weight = params.weights.get(c).copied().unwrap_or(0.0).max(1e-300);
                weight.ln() + gaussian_log_density(point, params, &log_consts, c, dim)
            })
            .collect();
        let max = log_weighted
            .iter()
            .copied()
            .fold(f64::NEG_INFINITY, f64::max);
        let sum_exp: f64 = log_weighted.iter().map(|&v| (v - max).exp()).sum();
        let log_norm = max + sum_exp.ln();
        total += log_norm;
        for (slot, &lw) in resp.iter_mut().zip(&log_weighted) {
            *slot = (lw - log_norm).exp();
        }
    }
    if data.is_empty() {
        0.0
    } else {
        total / count_to_f64(data.len())
    }
}

/// M-step: re-estimates weights, means, and covariances from `responsibilities`.
fn m_step(
    data: &[Vec<f64>],
    responsibilities: &[Vec<f64>],
    params: &mut Params,
    k: usize,
    dim: usize,
) {
    let n = count_to_f64(data.len());
    for c in 0..k {
        let nk: f64 = responsibilities
            .iter()
            .map(|r| r.get(c).copied().unwrap_or(0.0))
            .sum::<f64>()
            .max(1e-300);
        // Weight.
        if let Some(w) = params.weights.get_mut(c) {
            *w = nk / n;
        }
        // Mean.
        let mut mean = vec![0.0_f64; dim];
        for (point, resp) in data.iter().zip(responsibilities) {
            let r = resp.get(c).copied().unwrap_or(0.0);
            for (m, &x) in mean.iter_mut().zip(point) {
                *m = r.mul_add(x, *m);
            }
        }
        for m in &mut mean {
            *m /= nk;
        }
        // Covariance with reg_covar on the diagonal.
        let mut cov = vec![0.0_f64; dim * dim];
        for (point, resp) in data.iter().zip(responsibilities) {
            let r = resp.get(c).copied().unwrap_or(0.0);
            for a in 0..dim {
                let da = point.get(a).copied().unwrap_or(0.0) - mean.get(a).copied().unwrap_or(0.0);
                for b in 0..dim {
                    let db =
                        point.get(b).copied().unwrap_or(0.0) - mean.get(b).copied().unwrap_or(0.0);
                    let cur = at(&cov, dim, a, b);
                    put(&mut cov, dim, a, b, (r * da).mul_add(db, cur));
                }
            }
        }
        for a in 0..dim {
            for b in 0..dim {
                let cur = at(&cov, dim, a, b) / nk;
                put(
                    &mut cov,
                    dim,
                    a,
                    b,
                    cur + if a == b { REG_COVAR } else { 0.0 },
                );
            }
        }
        if let Some(slot) = params.means.get_mut(c) {
            *slot = mean;
        }
        if let Some(slot) = params.covariances.get_mut(c) {
            *slot = cov;
        }
    }
}

/// Precomputes component `c`'s precision matrix and the log-normaliser
/// `−½(d·ln(2π) + ln|Σ|)` of its Gaussian density.
fn gaussian_log_const(params: &Params, c: usize, dim: usize) -> (Vec<f64>, f64) {
    let cov = params
        .covariances
        .get(c)
        .cloned()
        .unwrap_or_else(|| diagonal(dim, 1.0));
    let precision = symmetric_inverse(&cov, dim);
    let (values, _) = jacobi_eigen(&cov, dim);
    let log_det: f64 = values.iter().map(|&v| v.max(1e-300).ln()).sum();
    let constant = -0.5 * count_to_f64(dim).mul_add((2.0 * std::f64::consts::PI).ln(), log_det);
    (precision, constant)
}

/// Evaluates the multivariate Gaussian log-density of `point` under component `c`.
fn gaussian_log_density(
    point: &[f64],
    params: &Params,
    log_consts: &[(Vec<f64>, f64)],
    c: usize,
    dim: usize,
) -> f64 {
    let Some((precision, constant)) = log_consts.get(c) else {
        return f64::NEG_INFINITY;
    };
    let mean = params.means.get(c);
    let diff: Vec<f64> = (0..dim)
        .map(|i| {
            point.get(i).copied().unwrap_or(0.0)
                - mean.and_then(|m| m.get(i)).copied().unwrap_or(0.0)
        })
        .collect();
    // Mahalanobis term diffᵀ · precision · diff.
    let mut maha = 0.0_f64;
    for a in 0..dim {
        for b in 0..dim {
            maha = (diff.get(a).copied().unwrap_or(0.0) * at(precision, dim, a, b))
                .mul_add(diff.get(b).copied().unwrap_or(0.0), maha);
        }
    }
    constant - 0.5 * maha
}

/// Returns the arg-max-responsibility hard label for each point.
fn hard_labels(responsibilities: &[Vec<f64>]) -> Vec<usize> {
    responsibilities
        .iter()
        .map(|row| {
            let mut best = 0_usize;
            let mut best_v = f64::NEG_INFINITY;
            for (c, &r) in row.iter().enumerate() {
                if r > best_v {
                    best_v = r;
                    best = c;
                }
            }
            best
        })
        .collect()
}

/// Empirical covariance of `members` about `mean` (`n` denominator), with a tiny
/// diagonal ridge so a singleton cluster stays invertible.
fn empirical_covariance(members: &[&Vec<f64>], mean: &[f64], dim: usize) -> Vec<f64> {
    let mut cov = vec![0.0_f64; dim * dim];
    for &point in members {
        for a in 0..dim {
            let da = point.get(a).copied().unwrap_or(0.0) - mean.get(a).copied().unwrap_or(0.0);
            for b in 0..dim {
                let db = point.get(b).copied().unwrap_or(0.0) - mean.get(b).copied().unwrap_or(0.0);
                let cur = at(&cov, dim, a, b);
                put(&mut cov, dim, a, b, da.mul_add(db, cur));
            }
        }
    }
    let count = count_to_f64(members.len()).max(1.0);
    for a in 0..dim {
        for b in 0..dim {
            let cur = at(&cov, dim, a, b) / count;
            put(
                &mut cov,
                dim,
                a,
                b,
                cur + if a == b { REG_COVAR } else { 0.0 },
            );
        }
    }
    cov
}

/// Builds a `dim×dim` diagonal matrix with `value` on the diagonal.
fn diagonal(dim: usize, value: f64) -> Vec<f64> {
    let mut m = vec![0.0_f64; dim * dim];
    for i in 0..dim {
        put(&mut m, dim, i, i, value);
    }
    m
}

/// Free-parameter count for a full-covariance `k`-component mixture in `dim`
/// dimensions: covariances + means + weights, matching scikit-learn's `_n_parameters`.
fn free_parameters(k: usize, dim: usize) -> f64 {
    let cov = count_to_f64(k) * count_to_f64(dim) * (count_to_f64(dim) + 1.0) / 2.0;
    let means = count_to_f64(dim) * count_to_f64(k);
    cov + means + count_to_f64(k) - 1.0
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Two well-separated 2-D clusters.
    fn two_clusters() -> Vec<Vec<f64>> {
        let mut data = Vec::new();
        for i in 0..10 {
            let jitter = count_to_f64(i) * 0.05;
            data.push(vec![jitter, jitter]);
            data.push(vec![10.0 + jitter, 10.0 - jitter]);
        }
        data
    }

    #[test]
    fn separates_two_clusters() {
        let data = two_clusters();
        let r = gmm_em(&data, 2, 200, 1e-3, 42);
        assert_eq!(r.labels.len(), data.len(), "label count");
        // The first point of each interleaved pair differs in component.
        assert_ne!(r.labels.first(), r.labels.get(1), "adjacent pair merged");
    }

    #[test]
    fn responsibilities_sum_to_one() {
        let data = two_clusters();
        let r = gmm_em(&data, 2, 200, 1e-3, 7);
        for (i, row) in r.responsibilities.iter().enumerate() {
            let sum: f64 = row.iter().sum();
            assert!((sum - 1.0).abs() < 1e-9, "row {i} summed to {sum}");
        }
    }

    #[test]
    fn empty_input_is_empty() {
        let r = gmm_em(&[], 3, 10, 1e-3, 0);
        assert!(r.labels.is_empty(), "labels not empty");
        assert!((r.bic - 0.0).abs() < 1e-12, "bic was {}", r.bic);
    }
}