oxicuda-anomaly 0.2.0

Anomaly detection primitives for OxiCUDA — DeepSVDD, AE/VAE reconstruction, LOF, COPOD, isolation scoring, statistical methods, ensemble
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! INNE — Isolation using Nearest-Neighbour Ensembles (Bandaragoda et al. 2018).
//!
//! INNE builds an ensemble of `n_estimators` models. Each model draws a random
//! sub-sample `D` of `sample_size` (ψ) points without replacement. Within the
//! sub-sample, every reference point `c` defines a **hypersphere** `B(c)`
//! centred at `c` whose radius `τ(c)` equals the distance from `c` to its
//! nearest neighbour inside `D`:
//!
//! ```text
//! τ(c) = min_{c' ∈ D, c' ≠ c} ‖c − c'‖
//! ```
//!
//! For a query `x` the **isolation score** of one model is the relative radius
//! of the smallest hypersphere that contains `x`:
//!
//! ```text
//! cnn(x) = argmin_{ c ∈ D : ‖x − c‖ ≤ τ(c) } τ(c)
//! I(x)   = 1 − τ(η(cnn(x))) / τ(cnn(x))        if x is covered by some ball
//!        = 1                                    if x is covered by no ball
//! ```
//!
//! where `η(c)` is the nearest neighbour of `c` inside `D` (so `τ(η(c))` is the
//! radius of that neighbour's ball). The final anomaly score averages `I(x)`
//! over the ensemble. Because every per-model term is `≤ 1`, the averaged score
//! is also `≤ 1`; larger values indicate stronger isolation (anomaly).
//!
//! # Reference
//! Bandaragoda, T. R., Ting, K. M., Albrecht, D., Liu, F. T., Zhu, Y., &
//! Wells, J. R. (2018). *Isolation-based anomaly detection using
//! nearest-neighbour ensembles*. Computational Intelligence, 34(4), 968–998.

use crate::error::{AnomalyError, AnomalyResult};
use crate::handle::LcgRng;

/// Numerical floor used to keep radii strictly positive.
const RADIUS_FLOOR: f32 = 1e-12;

// ─── Configuration ────────────────────────────────────────────────────────────

/// Hyper-parameters for the INNE detector.
#[derive(Debug, Clone)]
pub struct InneConfig {
    /// Number of ensemble members `t` (default 200).
    pub n_estimators: usize,
    /// Sub-sample size `ψ` per estimator (default 16). Must be `≥ 2`.
    pub sample_size: usize,
    /// RNG seed for reproducible sub-sampling (default 42).
    pub seed: u64,
}

impl Default for InneConfig {
    fn default() -> Self {
        Self {
            n_estimators: 200,
            sample_size: 16,
            seed: 42,
        }
    }
}

// ─── Per-estimator hypersphere set ────────────────────────────────────────────

/// One fitted INNE estimator: the sub-sample centroids together with each
/// centroid's hypersphere radius and the precomputed radius ratio.
#[derive(Debug, Clone)]
struct InneEstimator {
    /// Sub-sample centroids, row-major `[sample_size * n_features]`.
    centroids: Vec<f32>,
    /// Hypersphere radius `τ(c)` per centroid `[sample_size]`.
    radii: Vec<f32>,
    /// Precomputed `τ(η(c)) / τ(c)` per centroid `[sample_size]`.
    ratio: Vec<f32>,
}

impl InneEstimator {
    /// Isolation score `I(x)` for one query against this estimator.
    fn isolation_score(&self, x: &[f32], n_features: usize) -> f32 {
        let mut best_radius = f32::INFINITY;
        let mut covered_ratio: Option<f32> = None;
        for (c, centroid) in self.centroids.chunks_exact(n_features).enumerate() {
            let dist = euclidean(x, centroid);
            // `x` falls inside ball B(c) when dist ≤ radius. Among all covering
            // balls keep the one with the smallest radius (the "nearest" ball).
            if dist <= self.radii[c] && self.radii[c] < best_radius {
                best_radius = self.radii[c];
                covered_ratio = Some(self.ratio[c]);
            }
        }
        match covered_ratio {
            Some(ratio) => 1.0 - ratio,
            None => 1.0,
        }
    }
}

// ─── Euclidean distance (internal, equal length assumed) ──────────────────────

/// Euclidean distance between two equal-length slices.
#[inline]
fn euclidean(a: &[f32], b: &[f32]) -> f32 {
    a.iter()
        .zip(b.iter())
        .map(|(x, y)| {
            let d = x - y;
            d * d
        })
        .sum::<f32>()
        .sqrt()
}

// ─── InneDetector ─────────────────────────────────────────────────────────────

/// Isolation using Nearest-Neighbour Ensembles anomaly detector.
///
/// # Usage
///
/// ```rust,ignore
/// let mut det = InneDetector::new(InneConfig::default());
/// det.fit(&train, n_samples, n_features)?;
/// let s = det.score(&query)?;
/// ```
#[derive(Debug, Clone)]
pub struct InneDetector {
    config: InneConfig,
    estimators: Vec<InneEstimator>,
    n_features: usize,
    fitted: bool,
}

impl InneDetector {
    /// Create an unfitted detector from the supplied configuration.
    #[must_use]
    pub fn new(config: InneConfig) -> Self {
        Self {
            config,
            estimators: Vec::new(),
            n_features: 0,
            fitted: false,
        }
    }

    /// Fit the ensemble on `data` (`n_samples × n_features`, row-major).
    ///
    /// # Errors
    /// * [`AnomalyError::EmptyInput`] if `n_samples == 0`.
    /// * [`AnomalyError::InvalidFeatureCount`] if `n_features == 0`.
    /// * [`AnomalyError::DimensionMismatch`] if `data.len() != n_samples * n_features`.
    /// * [`AnomalyError::InsufficientSamples`] if `sample_size < 2` or
    ///   `sample_size > n_samples`.
    /// * [`AnomalyError::Internal`] if `n_estimators == 0`.
    pub fn fit(&mut self, data: &[f32], n_samples: usize, n_features: usize) -> AnomalyResult<()> {
        if n_samples == 0 {
            return Err(AnomalyError::EmptyInput);
        }
        if n_features == 0 {
            return Err(AnomalyError::InvalidFeatureCount { n: 0 });
        }
        if data.len() != n_samples * n_features {
            return Err(AnomalyError::DimensionMismatch {
                expected: n_samples * n_features,
                got: data.len(),
            });
        }
        if self.config.n_estimators == 0 {
            return Err(AnomalyError::Internal {
                msg: "n_estimators must be > 0".into(),
            });
        }
        let psi = self.config.sample_size;
        if psi < 2 {
            return Err(AnomalyError::InsufficientSamples { need: 2, got: psi });
        }
        if psi > n_samples {
            return Err(AnomalyError::InsufficientSamples {
                need: psi,
                got: n_samples,
            });
        }

        let mut rng = LcgRng::new(self.config.seed);
        let mut estimators = Vec::with_capacity(self.config.n_estimators);
        let mut index_pool: Vec<usize> = (0..n_samples).collect();

        for _ in 0..self.config.n_estimators {
            // Partial Fisher–Yates: select `psi` distinct indices without replacement.
            for k in 0..psi {
                let j = k + rng.next_usize(n_samples - k);
                index_pool.swap(k, j);
            }

            // Gather the sub-sample centroids.
            let mut centroids = vec![0.0_f32; psi * n_features];
            for (slot, &src) in index_pool[..psi].iter().enumerate() {
                let dst = &mut centroids[slot * n_features..(slot + 1) * n_features];
                dst.copy_from_slice(&data[src * n_features..(src + 1) * n_features]);
            }

            // Radius τ(c) = NN distance within the sub-sample; record NN index.
            let radius_nn: Vec<(f32, usize)> = (0..psi)
                .map(|c| {
                    let row_c = &centroids[c * n_features..(c + 1) * n_features];
                    let mut best = f32::INFINITY;
                    let mut best_j = c;
                    for d in 0..psi {
                        if d == c {
                            continue;
                        }
                        let dist =
                            euclidean(row_c, &centroids[d * n_features..(d + 1) * n_features]);
                        if dist < best {
                            best = dist;
                            best_j = d;
                        }
                    }
                    (best, best_j)
                })
                .collect();

            let radii: Vec<f32> = radius_nn.iter().map(|&(r, _)| r).collect();
            // ratio(c) = τ(η(c)) / τ(c); floor the denominator to stay finite.
            let ratio: Vec<f32> = radius_nn
                .iter()
                .map(|&(r, nn)| radii[nn] / r.max(RADIUS_FLOOR))
                .collect();

            estimators.push(InneEstimator {
                centroids,
                radii,
                ratio,
            });
        }

        self.estimators = estimators;
        self.n_features = n_features;
        self.fitted = true;
        Ok(())
    }

    /// Anomaly score for one query `x` (averaged isolation score, `≤ 1`).
    ///
    /// Higher values indicate stronger isolation (more anomalous).
    ///
    /// # Errors
    /// * [`AnomalyError::NotFitted`] if [`InneDetector::fit`] has not been called.
    /// * [`AnomalyError::FeatureCountMismatch`] if `x.len() != n_features`.
    pub fn score(&self, x: &[f32]) -> AnomalyResult<f32> {
        if !self.fitted {
            return Err(AnomalyError::NotFitted);
        }
        if x.len() != self.n_features {
            return Err(AnomalyError::FeatureCountMismatch {
                expected: self.n_features,
                got: x.len(),
            });
        }
        let sum: f32 = self
            .estimators
            .iter()
            .map(|est| est.isolation_score(x, self.n_features))
            .sum();
        Ok(sum / self.estimators.len() as f32)
    }

    /// Batch scoring; `x` is `[n × n_features]` row-major, returns `[n]`.
    ///
    /// # Errors
    /// * [`AnomalyError::NotFitted`] if the detector is unfitted.
    /// * [`AnomalyError::DimensionMismatch`] if `x.len() != n * n_features`.
    pub fn score_batch(&self, x: &[f32], n: usize) -> AnomalyResult<Vec<f32>> {
        if !self.fitted {
            return Err(AnomalyError::NotFitted);
        }
        if x.len() != n * self.n_features {
            return Err(AnomalyError::DimensionMismatch {
                expected: n * self.n_features,
                got: x.len(),
            });
        }
        let mut scores = Vec::with_capacity(n);
        for i in 0..n {
            let sample = &x[i * self.n_features..(i + 1) * self.n_features];
            scores.push(self.score(sample)?);
        }
        Ok(scores)
    }

    /// Number of features the detector was fitted on (0 if unfitted).
    #[inline]
    #[must_use]
    pub fn n_features(&self) -> usize {
        self.n_features
    }

    /// Number of fitted ensemble members.
    #[inline]
    #[must_use]
    pub fn n_estimators(&self) -> usize {
        self.estimators.len()
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

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

    /// Dense 2-D cluster of `n` points near the origin (deterministic jitter).
    fn dense_cluster(n: usize, seed: u64) -> Vec<f32> {
        let mut rng = LcgRng::new(seed);
        let mut data = Vec::with_capacity(n * 2);
        for _ in 0..n {
            data.push(rng.next_f32() * 0.2);
            data.push(rng.next_f32() * 0.2);
        }
        data
    }

    // ── Test (a): a clear outlier scores higher than inliers ──────────────────
    #[test]
    fn outlier_scores_higher_than_inliers() {
        let n = 40_usize;
        let data = dense_cluster(n, 1);
        let cfg = InneConfig {
            n_estimators: 100,
            sample_size: 8,
            seed: 7,
        };
        let mut det = InneDetector::new(cfg);
        det.fit(&data, n, 2).expect("fit");

        let outlier = det.score(&[50.0_f32, 50.0]).expect("outlier");
        // A handful of inliers sampled from the cluster region.
        let inliers = [[0.05_f32, 0.1], [0.1, 0.05], [0.15, 0.15], [0.08, 0.12]];
        for inlier in &inliers {
            let s_in = det.score(inlier).expect("inlier");
            assert!(
                outlier > s_in,
                "outlier {outlier} should exceed inlier {s_in}"
            );
        }
    }

    // ── Test (b): scores are finite and bounded above by 1 ────────────────────
    #[test]
    fn scores_finite_and_bounded() {
        let n = 30_usize;
        let data = dense_cluster(n, 2);
        let mut det = InneDetector::new(InneConfig {
            n_estimators: 64,
            sample_size: 8,
            seed: 11,
        });
        det.fit(&data, n, 2).expect("fit");

        let queries = [
            [0.1_f32, 0.1],
            [0.5, 0.2],
            [10.0, -10.0],
            [0.0, 0.0],
            [-3.0, 4.0],
        ];
        for q in &queries {
            let s = det.score(q).expect("score");
            assert!(s.is_finite(), "score must be finite, got {s}");
            assert!(s <= 1.0 + 1e-5, "score {s} must be ≤ 1");
            assert!(s >= -5.0, "score {s} unexpectedly small");
        }
    }

    // ── Test (c): larger ensemble → lower score variance across seeds ──────────
    #[test]
    fn larger_ensemble_reduces_variance() {
        let n = 36_usize;
        let data = dense_cluster(n, 3);
        let query = [0.12_f32, 0.09]; // an inlier-ish point with a non-trivial score

        let variance_for = |n_estimators: usize| -> f32 {
            let mut scores = Vec::new();
            for seed in 0..8_u64 {
                let mut det = InneDetector::new(InneConfig {
                    n_estimators,
                    sample_size: 6,
                    seed,
                });
                det.fit(&data, n, 2).expect("fit");
                scores.push(det.score(&query).expect("score"));
            }
            let mean = scores.iter().sum::<f32>() / scores.len() as f32;
            scores.iter().map(|s| (s - mean).powi(2)).sum::<f32>() / scores.len() as f32
        };

        let var_small = variance_for(2);
        let var_large = variance_for(64);
        assert!(
            var_large < var_small,
            "variance should shrink with more estimators: small={var_small}, large={var_large}"
        );
    }

    // ── Test (d): invalid sample_size / empty data → error ────────────────────
    #[test]
    fn invalid_sample_size_and_empty_errors() {
        let n = 10_usize;
        let data = dense_cluster(n, 4);

        // sample_size > n
        let mut det = InneDetector::new(InneConfig {
            n_estimators: 10,
            sample_size: 50,
            seed: 1,
        });
        assert!(matches!(
            det.fit(&data, n, 2),
            Err(AnomalyError::InsufficientSamples { .. })
        ));

        // n = 0 → EmptyInput
        let mut det2 = InneDetector::new(InneConfig::default());
        assert!(matches!(det2.fit(&[], 0, 2), Err(AnomalyError::EmptyInput)));

        // sample_size < 2 → InsufficientSamples
        let mut det3 = InneDetector::new(InneConfig {
            n_estimators: 10,
            sample_size: 1,
            seed: 1,
        });
        assert!(matches!(
            det3.fit(&data, n, 2),
            Err(AnomalyError::InsufficientSamples { need: 2, got: 1 })
        ));

        // score before fit → NotFitted
        let det4 = InneDetector::new(InneConfig::default());
        assert!(matches!(
            det4.score(&[0.0_f32, 0.0]),
            Err(AnomalyError::NotFitted)
        ));
    }

    // ── Test (e): determinism with a fixed seed ───────────────────────────────
    #[test]
    fn deterministic_with_fixed_seed() {
        let n = 30_usize;
        let data = dense_cluster(n, 5);
        let cfg = InneConfig {
            n_estimators: 50,
            sample_size: 8,
            seed: 123,
        };
        let mut det_a = InneDetector::new(cfg.clone());
        let mut det_b = InneDetector::new(cfg);
        det_a.fit(&data, n, 2).expect("fit a");
        det_b.fit(&data, n, 2).expect("fit b");

        for q in &[[0.1_f32, 0.1], [5.0, 5.0], [0.3, 0.0]] {
            let sa = det_a.score(q).expect("score a");
            let sb = det_b.score(q).expect("score b");
            assert!((sa - sb).abs() < 1e-6, "scores differ: {sa} vs {sb}");
        }
    }

    // ── Extra: dimension mismatch + batch scoring ─────────────────────────────
    #[test]
    fn feature_mismatch_and_batch() {
        let n = 20_usize;
        let data = dense_cluster(n, 6);
        let mut det = InneDetector::new(InneConfig {
            n_estimators: 20,
            sample_size: 5,
            seed: 9,
        });
        det.fit(&data, n, 2).expect("fit");

        assert!(matches!(
            det.score(&[0.1_f32, 0.2, 0.3]),
            Err(AnomalyError::FeatureCountMismatch {
                expected: 2,
                got: 3
            })
        ));

        let batch = det.score_batch(&data, n).expect("batch");
        assert_eq!(batch.len(), n);
        assert!(batch.iter().all(|s| s.is_finite()));
    }
}