u-insight 0.7.0

Statistical analysis and data profiling engine with C FFI bindings.
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! Isolation Forest for multivariate anomaly detection.
//!
//! Detects anomalies by measuring how easily points can be isolated
//! via random recursive partitioning. Anomalies require fewer splits
//! to isolate, yielding shorter path lengths and higher anomaly scores.
//!
//! # Algorithm
//!
//! Reference: Liu, Ting & Zhou (2008). "Isolation Forest", ICDM.
//!
//! 1. Build an ensemble of isolation trees, each on a random subsample
//! 2. For each point, compute average path length across all trees
//! 3. Normalize using c(n) = 2H(n-1) - 2(n-1)/n (expected BST search depth)
//! 4. Anomaly score: s(x,n) = 2^(-E(h(x))/c(n))
//!
//! Scores range from 0 to 1:
//! - Close to 1: anomaly (short paths)
//! - Close to 0.5: normal (average path length)
//! - Close to 0: very normal (long paths)
//!
//! # Example
//!
//! ```
//! use u_insight::isolation_forest::{isolation_forest, IsolationForestConfig};
//!
//! // Normal cluster + one outlier
//! let mut data = Vec::new();
//! for i in 0..50 {
//!     data.push(vec![i as f64 * 0.1, i as f64 * 0.1]);
//! }
//! data.push(vec![100.0, 100.0]); // outlier
//!
//! let result = isolation_forest(&data, &IsolationForestConfig::default()).unwrap();
//! // The outlier (last point) should have the highest anomaly score
//! let max_idx = result.scores.iter()
//!     .enumerate()
//!     .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
//!     .unwrap().0;
//! assert_eq!(max_idx, 50);
//! ```

use crate::error::InsightError;

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

/// Configuration for Isolation Forest.
#[derive(Debug, Clone)]
pub struct IsolationForestConfig {
    /// Number of isolation trees. Default: 100.
    pub n_estimators: usize,
    /// Subsample size per tree. Default: min(256, n).
    /// Set to 0 for automatic (min(256, n)).
    pub max_samples: usize,
    /// Expected proportion of anomalies (0.0–1.0).
    /// Used to determine the anomaly threshold.
    /// Default: 0.1.
    pub contamination: f64,
    /// Random seed. Default: Some(42).
    pub seed: Option<u64>,
}

impl Default for IsolationForestConfig {
    fn default() -> Self {
        Self {
            n_estimators: 100,
            max_samples: 0, // auto
            contamination: 0.1,
            seed: Some(42),
        }
    }
}

impl IsolationForestConfig {
    /// Sets the number of trees.
    pub fn n_estimators(mut self, n: usize) -> Self {
        self.n_estimators = n;
        self
    }

    /// Sets the contamination rate.
    pub fn contamination(mut self, c: f64) -> Self {
        self.contamination = c;
        self
    }

    /// Sets the random seed.
    pub fn seed(mut self, seed: Option<u64>) -> Self {
        self.seed = seed;
        self
    }

    /// Sets the max_samples per tree.
    pub fn max_samples(mut self, m: usize) -> Self {
        self.max_samples = m;
        self
    }
}

// ── Result ────────────────────────────────────────────────────────────

/// Result of Isolation Forest anomaly detection.
#[derive(Debug, Clone)]
pub struct IsolationForestResult {
    /// Anomaly score for each data point (0.0–1.0). Higher = more anomalous.
    pub scores: Vec<f64>,
    /// Binary anomaly labels: true = anomaly.
    pub anomalies: Vec<bool>,
    /// Score threshold used for classification.
    pub threshold: f64,
    /// Number of anomalies detected.
    pub anomaly_count: usize,
    /// Fraction of points classified as anomalies.
    pub anomaly_fraction: f64,
}

// ── Isolation Forest algorithm ────────────────────────────────────────

/// Runs Isolation Forest anomaly detection.
///
/// ```
/// use u_insight::isolation_forest::{isolation_forest, IsolationForestConfig};
///
/// let data = vec![
///     vec![1.0, 1.0], vec![1.1, 1.1], vec![0.9, 0.9],
///     vec![1.0, 1.2], vec![1.2, 1.0],
///     vec![50.0, 50.0], // outlier
/// ];
/// let config = IsolationForestConfig::default().contamination(0.2);
/// let result = isolation_forest(&data, &config).unwrap();
///
/// // The outlier should be detected
/// assert!(result.anomalies[5]);
/// assert!(result.scores[5] > 0.5);
/// ```
pub fn isolation_forest(
    data: &[Vec<f64>],
    config: &IsolationForestConfig,
) -> Result<IsolationForestResult, InsightError> {
    let n = data.len();
    if n < 2 {
        return Err(InsightError::InsufficientData {
            min_required: 2,
            actual: n,
        });
    }

    let d = data[0].len();
    if d == 0 {
        return Err(InsightError::DegenerateData {
            reason: "data has 0 features".into(),
        });
    }

    // Validate
    for (i, point) in data.iter().enumerate() {
        if point.len() != d {
            return Err(InsightError::DimensionMismatch {
                expected: d,
                actual: point.len(),
            });
        }
        for &v in point {
            if v.is_nan() || v.is_infinite() {
                return Err(InsightError::NonNumericColumn {
                    column: format!("point[{i}]"),
                });
            }
        }
    }

    if config.n_estimators == 0 {
        return Err(InsightError::InvalidParameter {
            name: "n_estimators".into(),
            message: "must be at least 1".into(),
        });
    }
    if !(0.0..=1.0).contains(&config.contamination) {
        return Err(InsightError::InvalidParameter {
            name: "contamination".into(),
            message: format!("must be in [0.0, 1.0], got {}", config.contamination),
        });
    }

    let max_samples = if config.max_samples == 0 {
        n.min(256)
    } else {
        config.max_samples.min(n)
    };
    let max_depth = (max_samples as f64).log2().ceil() as usize;

    // Build forest
    let mut rng_state = config.seed.unwrap_or(12345);
    let mut trees = Vec::with_capacity(config.n_estimators);

    for _ in 0..config.n_estimators {
        // Subsample indices
        let indices = sample_indices(n, max_samples, &mut rng_state);
        let subsample: Vec<&[f64]> = indices.iter().map(|&i| data[i].as_slice()).collect();
        let tree = build_itree(&subsample, d, max_depth, &mut rng_state);
        trees.push(tree);
    }

    // Score each point
    let cn = c_factor(max_samples);
    let mut scores = Vec::with_capacity(n);

    for point in data {
        let avg_path: f64 = trees
            .iter()
            .map(|tree| path_length(point, tree, 0))
            .sum::<f64>()
            / config.n_estimators as f64;

        let score = if cn > 0.0 {
            2.0f64.powf(-avg_path / cn)
        } else {
            0.5 // degenerate case
        };
        scores.push(score);
    }

    // Determine threshold from contamination
    let mut sorted_scores: Vec<f64> = scores.clone();
    sorted_scores.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
    let threshold_idx = ((n as f64 * config.contamination).ceil() as usize)
        .min(n)
        .max(1)
        - 1;
    let threshold = sorted_scores[threshold_idx];

    let anomalies: Vec<bool> = scores.iter().map(|&s| s >= threshold).collect();
    let anomaly_count = anomalies.iter().filter(|&&a| a).count();

    Ok(IsolationForestResult {
        scores,
        anomalies,
        threshold,
        anomaly_count,
        anomaly_fraction: anomaly_count as f64 / n as f64,
    })
}

// ── Isolation Tree internals ──────────────────────────────────────────

/// Node in an isolation tree.
enum ITreeNode {
    /// Internal split node.
    Internal {
        feature: usize,
        split_value: f64,
        left: Box<ITreeNode>,
        right: Box<ITreeNode>,
    },
    /// External (leaf) node.
    External { size: usize },
}

/// Builds a single isolation tree from a subsample.
fn build_itree(data: &[&[f64]], d: usize, max_depth: usize, rng: &mut u64) -> ITreeNode {
    let n = data.len();

    // Termination conditions
    if n <= 1 || max_depth == 0 {
        return ITreeNode::External { size: n };
    }

    // Random feature
    let feature = lcg_next_usize(rng, d);

    // Find min/max for this feature
    let mut min_val = f64::INFINITY;
    let mut max_val = f64::NEG_INFINITY;
    for point in data {
        let v = point[feature];
        if v < min_val {
            min_val = v;
        }
        if v > max_val {
            max_val = v;
        }
    }

    // All values identical for this feature
    if (max_val - min_val).abs() < 1e-15 {
        return ITreeNode::External { size: n };
    }

    // Random split point between min and max
    let split_value = min_val + lcg_next_f64(rng) * (max_val - min_val);

    // Partition
    let mut left_data = Vec::new();
    let mut right_data = Vec::new();
    for &point in data {
        if point[feature] < split_value {
            left_data.push(point);
        } else {
            right_data.push(point);
        }
    }

    // Avoid degenerate splits where all data goes to one side
    if left_data.is_empty() || right_data.is_empty() {
        return ITreeNode::External { size: n };
    }

    ITreeNode::Internal {
        feature,
        split_value,
        left: Box::new(build_itree(&left_data, d, max_depth - 1, rng)),
        right: Box::new(build_itree(&right_data, d, max_depth - 1, rng)),
    }
}

/// Computes the path length for a point traversing the tree.
fn path_length(point: &[f64], node: &ITreeNode, current_depth: usize) -> f64 {
    match node {
        ITreeNode::External { size } => current_depth as f64 + c_factor(*size),
        ITreeNode::Internal {
            feature,
            split_value,
            left,
            right,
        } => {
            if point[*feature] < *split_value {
                path_length(point, left, current_depth + 1)
            } else {
                path_length(point, right, current_depth + 1)
            }
        }
    }
}

/// Average path length of unsuccessful search in BST of size n.
///
/// c(n) = 2*H(n-1) - 2*(n-1)/n
/// where H(i) ≈ ln(i) + γ (Euler-Mascheroni constant).
fn c_factor(n: usize) -> f64 {
    if n <= 1 {
        return 0.0;
    }
    if n == 2 {
        return 1.0;
    }
    let n_f = n as f64;
    let harmonic = (n_f - 1.0).ln() + 0.5772156649;
    2.0 * harmonic - 2.0 * (n_f - 1.0) / n_f
}

// ── RNG helpers ───────────────────────────────────────────────────────

/// LCG random: returns [0, 1).
fn lcg_next_f64(state: &mut u64) -> f64 {
    *state = state
        .wrapping_mul(6364136223846793005)
        .wrapping_add(1442695040888963407);
    (*state >> 33) as f64 / (1u64 << 31) as f64
}

/// LCG random: returns [0, max).
fn lcg_next_usize(state: &mut u64, max: usize) -> usize {
    (lcg_next_f64(state) * max as f64) as usize % max
}

/// Sample `k` unique indices from `0..n` using Fisher-Yates partial shuffle.
fn sample_indices(n: usize, k: usize, rng: &mut u64) -> Vec<usize> {
    let k = k.min(n);
    let mut indices: Vec<usize> = (0..n).collect();
    for i in 0..k {
        let j = i + lcg_next_usize(rng, n - i);
        indices.swap(i, j);
    }
    indices.truncate(k);
    indices
}

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

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

    fn make_normal_with_outliers() -> Vec<Vec<f64>> {
        let mut data = Vec::new();
        // Normal cluster around (5, 5)
        for i in 0..40 {
            let x = 5.0 + (i % 7) as f64 * 0.2 - 0.6;
            let y = 5.0 + (i % 5) as f64 * 0.3 - 0.6;
            data.push(vec![x, y]);
        }
        // Outliers far from cluster
        data.push(vec![50.0, 50.0]);
        data.push(vec![-40.0, -40.0]);
        data.push(vec![50.0, -40.0]);
        data
    }

    // ── Basic detection ───────────────────────────────────────────

    #[test]
    fn detects_obvious_outliers() {
        let data = make_normal_with_outliers();
        let result =
            isolation_forest(&data, &IsolationForestConfig::default().contamination(0.1)).unwrap();

        // The last 3 points are outliers — they should have high scores
        let n = data.len();
        for i in (n - 3)..n {
            assert!(
                result.scores[i] > 0.5,
                "outlier point {i} score = {} should be > 0.5",
                result.scores[i]
            );
        }
    }

    #[test]
    fn outlier_scores_higher_than_normal() {
        let data = make_normal_with_outliers();
        let result = isolation_forest(&data, &IsolationForestConfig::default()).unwrap();

        let normal_max = data[..40]
            .iter()
            .enumerate()
            .map(|(i, _)| result.scores[i])
            .fold(0.0f64, f64::max);

        let outlier_min = data[40..]
            .iter()
            .enumerate()
            .map(|(i, _)| result.scores[40 + i])
            .fold(1.0f64, f64::min);

        assert!(
            outlier_min > normal_max,
            "outlier min score ({outlier_min}) should exceed normal max ({normal_max})"
        );
    }

    #[test]
    fn anomaly_count_matches_contamination() {
        let data = make_normal_with_outliers();
        let n = data.len();
        let contamination = 0.1;
        let result = isolation_forest(
            &data,
            &IsolationForestConfig::default().contamination(contamination),
        )
        .unwrap();

        let expected_max = (n as f64 * contamination).ceil() as usize + 1;
        assert!(
            result.anomaly_count <= expected_max,
            "anomaly_count {} should be <= {}",
            result.anomaly_count,
            expected_max
        );
    }

    // ── Score properties ──────────────────────────────────────────

    #[test]
    fn scores_in_range() {
        let data = make_normal_with_outliers();
        let result = isolation_forest(&data, &IsolationForestConfig::default()).unwrap();

        for (i, &score) in result.scores.iter().enumerate() {
            assert!(
                (0.0..=1.0).contains(&score),
                "score[{i}] = {score} out of [0, 1] range"
            );
        }
    }

    #[test]
    fn scores_reproducible_with_seed() {
        let data = make_normal_with_outliers();
        let config = IsolationForestConfig::default().seed(Some(123));
        let r1 = isolation_forest(&data, &config).unwrap();
        let r2 = isolation_forest(&data, &config).unwrap();

        for (i, (&s1, &s2)) in r1.scores.iter().zip(r2.scores.iter()).enumerate() {
            assert!((s1 - s2).abs() < 1e-10, "score[{i}] differs: {s1} vs {s2}");
        }
    }

    // ── No outliers ───────────────────────────────────────────────

    #[test]
    fn uniform_data_low_scores() {
        // All points are similar — scores should be near 0.5
        let data: Vec<Vec<f64>> = (0..50)
            .map(|i| vec![i as f64 * 0.1, i as f64 * 0.1])
            .collect();
        let result = isolation_forest(&data, &IsolationForestConfig::default()).unwrap();

        let mean_score: f64 = result.scores.iter().sum::<f64>() / result.scores.len() as f64;
        assert!(
            mean_score < 0.7,
            "mean score for uniform data should be moderate: {mean_score}"
        );
    }

    // ── Error cases ──────────────────────────────────────────────

    #[test]
    fn empty_data() {
        let data: Vec<Vec<f64>> = vec![];
        assert!(isolation_forest(&data, &IsolationForestConfig::default()).is_err());
    }

    #[test]
    fn single_point() {
        let data = vec![vec![1.0, 2.0]];
        assert!(isolation_forest(&data, &IsolationForestConfig::default()).is_err());
    }

    #[test]
    fn nan_rejected() {
        let data = vec![vec![1.0, f64::NAN], vec![2.0, 3.0]];
        assert!(isolation_forest(&data, &IsolationForestConfig::default()).is_err());
    }

    #[test]
    fn dimension_mismatch() {
        let data = vec![vec![1.0, 2.0], vec![3.0]];
        assert!(isolation_forest(&data, &IsolationForestConfig::default()).is_err());
    }

    // ── 1D data ──────────────────────────────────────────────────

    #[test]
    fn one_dimensional_outlier() {
        let mut data: Vec<Vec<f64>> = (0..30).map(|i| vec![i as f64 * 0.1]).collect();
        data.push(vec![100.0]); // outlier

        let result = isolation_forest(&data, &IsolationForestConfig::default()).unwrap();

        let outlier_score = result.scores[30];
        let normal_mean: f64 = result.scores[..30].iter().sum::<f64>() / 30.0;

        assert!(
            outlier_score > normal_mean,
            "outlier score ({outlier_score}) should exceed normal mean ({normal_mean})"
        );
    }

    // ── High-dimensional ──────────────────────────────────────────

    #[test]
    fn high_dim_detection() {
        // 10D: normal cluster at origin, one outlier at (100, 100, ...)
        let mut data = Vec::new();
        for i in 0..30 {
            let mut point = vec![0.0; 10];
            point[i % 10] = (i % 5) as f64 * 0.2;
            data.push(point);
        }
        data.push(vec![100.0; 10]); // outlier

        let result =
            isolation_forest(&data, &IsolationForestConfig::default().n_estimators(200)).unwrap();

        // Outlier should have the highest score
        let max_idx = result
            .scores
            .iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap()
            .0;
        assert_eq!(max_idx, 30, "outlier should have highest anomaly score");
    }

    // ── c_factor ──────────────────────────────────────────────────

    #[test]
    fn c_factor_known_values() {
        assert_eq!(c_factor(1), 0.0);
        assert_eq!(c_factor(2), 1.0);
        // c(256) ≈ 2*(ln(255) + 0.5772) - 2*255/256 ≈ 2*6.118 - 1.992 ≈ 10.244
        let c256 = c_factor(256);
        assert!(
            (c256 - 10.244).abs() < 0.1,
            "c(256) = {c256}, expected ~10.244"
        );
    }

    // ── Config ────────────────────────────────────────────────────

    #[test]
    fn different_n_estimators() {
        let data = make_normal_with_outliers();
        // Fewer trees should still detect obvious outliers
        let result =
            isolation_forest(&data, &IsolationForestConfig::default().n_estimators(10)).unwrap();

        // Outlier at index 40 should still be detectable
        let outlier_score = result.scores[40];
        assert!(
            outlier_score > 0.5,
            "even with 10 trees, outlier score = {outlier_score}"
        );
    }
}