irithyll 10.0.1

Streaming ML in Rust -- gradient boosted trees, neural architectures (TTT/KAN/MoE/Mamba/SNN), AutoML, kernel methods, and composable pipelines
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
//! Multinomial and Bernoulli Naive Bayes classifiers.
//!
//! Two complementary streaming Naive Bayes variants:
//!
//! - [`MultinomialNB`] -- for count/frequency features (e.g., word counts in text
//!   classification). Learns `P(feature | class)` as smoothed relative frequencies.
//! - [`BernoulliNB`] -- for binary/boolean features. Explicitly models both
//!   feature presence and absence, unlike Multinomial which ignores zeros.
//!
//! Both support Laplace smoothing, automatic class discovery, and implement
//! [`StreamingLearner`] for polymorphic composition.

use crate::learner::StreamingLearner;

// ---------------------------------------------------------------------------
// MultinomialNB
// ---------------------------------------------------------------------------

/// Multinomial Naive Bayes classifier for streaming count/frequency data.
///
/// Maintains per-class feature count sums with Laplace smoothing. Prediction
/// selects the class maximizing:
///
/// ```text
/// argmax_c { log P(c) + sum_f { x_f * log P(f|c) } }
/// ```
///
/// where `P(f|c) = (count(f,c) + alpha) / (total(c) + alpha * n_features)`.
///
/// # Example
///
/// ```
/// use irithyll::MultinomialNB;
///
/// let mut nb = MultinomialNB::new();
/// // Class 0: high counts in features 0-1
/// nb.train_one(&[5.0, 3.0, 0.0, 0.0], 0);
/// // Class 1: high counts in features 2-3
/// nb.train_one(&[0.0, 0.0, 4.0, 6.0], 1);
///
/// assert_eq!(nb.predict_class(&[4.0, 2.0, 0.0, 0.0]), 0);
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde-json", derive(serde::Serialize, serde::Deserialize))]
pub struct MultinomialNB {
    /// Laplace smoothing parameter.
    alpha: f64,
    /// Per-class sample counts.
    class_counts: Vec<u64>,
    /// Per-class, per-feature sum of feature values. `[class][feature]`
    feature_counts: Vec<Vec<f64>>,
    /// Per-class total of all feature value sums.
    class_total: Vec<f64>,
    /// Number of features (0 = lazy init).
    n_features: usize,
    /// Total samples seen.
    n_samples: u64,
}

impl MultinomialNB {
    /// Create a new Multinomial NB with default Laplace smoothing (alpha = 1.0).
    pub fn new() -> Self {
        Self {
            alpha: 1.0,
            class_counts: Vec::new(),
            feature_counts: Vec::new(),
            class_total: Vec::new(),
            n_features: 0,
            n_samples: 0,
        }
    }

    /// Create with a custom smoothing parameter.
    pub fn with_alpha(alpha: f64) -> Self {
        let mut nb = Self::new();
        nb.alpha = alpha;
        nb
    }

    /// Ensure vectors are large enough for the given class label.
    fn ensure_class(&mut self, class: usize) {
        while self.class_counts.len() <= class {
            self.class_counts.push(0);
            self.feature_counts.push(vec![0.0; self.n_features]);
            self.class_total.push(0.0);
        }
    }

    /// Train on a single sample.
    pub fn train_one(&mut self, features: &[f64], class: usize) {
        // Lazy feature init.
        if self.n_features == 0 {
            self.n_features = features.len();
            // Resize existing class vectors (if any).
            for fc in &mut self.feature_counts {
                fc.resize(self.n_features, 0.0);
            }
        }
        self.ensure_class(class);
        self.n_samples += 1;

        self.class_counts[class] += 1;
        let total: f64 = features.iter().sum();
        self.class_total[class] += total;
        for (j, &x) in features.iter().enumerate() {
            self.feature_counts[class][j] += x;
        }
    }

    /// Compute unnormalized log-posterior for each class.
    fn log_posteriors(&self, features: &[f64]) -> Vec<f64> {
        let n_total = self.n_samples as f64;
        let n_feat = self.n_features as f64;
        let alpha = self.alpha;

        self.class_counts
            .iter()
            .enumerate()
            .map(|(c, &count)| {
                let log_prior = (count as f64 / n_total).ln();
                let denom = self.class_total[c] + alpha * n_feat;
                let log_likelihood: f64 = features
                    .iter()
                    .enumerate()
                    .map(|(j, &x)| {
                        let p = (self.feature_counts[c][j] + alpha) / denom;
                        x * p.ln()
                    })
                    .sum();
                log_prior + log_likelihood
            })
            .collect()
    }

    /// Predict the most likely class.
    pub fn predict_class(&self, features: &[f64]) -> usize {
        let logs = self.log_posteriors(features);
        logs.iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(i, _)| i)
            .unwrap_or(0)
    }

    /// Log-probability for each class (unnormalized).
    pub fn predict_log_proba(&self, features: &[f64]) -> Vec<f64> {
        self.log_posteriors(features)
    }

    /// Normalized probability for each class.
    pub fn predict_proba(&self, features: &[f64]) -> Vec<f64> {
        let logs = self.log_posteriors(features);
        let max_log = logs.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
        let exps: Vec<f64> = logs.iter().map(|&l| (l - max_log).exp()).collect();
        let sum: f64 = exps.iter().sum();
        if sum == 0.0 {
            vec![1.0 / logs.len() as f64; logs.len()]
        } else {
            exps.iter().map(|&e| e / sum).collect()
        }
    }

    /// Number of discovered classes.
    pub fn n_classes(&self) -> usize {
        self.class_counts.len()
    }

    /// Total samples seen.
    pub fn n_samples_seen(&self) -> u64 {
        self.n_samples
    }

    /// Reset all state.
    pub fn reset(&mut self) {
        self.class_counts.clear();
        self.feature_counts.clear();
        self.class_total.clear();
        self.n_features = 0;
        self.n_samples = 0;
    }
}

impl Default for MultinomialNB {
    fn default() -> Self {
        Self::new()
    }
}

impl StreamingLearner for MultinomialNB {
    fn train_one(&mut self, features: &[f64], target: f64, _weight: f64) {
        self.train_one(features, target as usize);
    }

    fn predict(&self, features: &[f64]) -> f64 {
        self.predict_class(features) as f64
    }

    fn n_samples_seen(&self) -> u64 {
        self.n_samples
    }

    fn reset(&mut self) {
        self.reset();
    }
}

// ---------------------------------------------------------------------------
// BernoulliNB
// ---------------------------------------------------------------------------

/// Bernoulli Naive Bayes classifier for streaming binary feature data.
///
/// Each feature is binarized: values above `threshold` are "present" (1),
/// values at or below are "absent" (0). Unlike [`MultinomialNB`], Bernoulli NB
/// explicitly models feature absence:
///
/// ```text
/// argmax_c { log P(c) + sum_f { b_f * log P(f|c) + (1 - b_f) * log(1 - P(f|c)) } }
/// ```
///
/// where `b_f = 1 if x_f > threshold, else 0` and
/// `P(f|c) = (present(f,c) + alpha) / (count(c) + 2 * alpha)`.
///
/// # Example
///
/// ```
/// use irithyll::BernoulliNB;
///
/// let mut nb = BernoulliNB::new();
/// nb.train_one(&[1.0, 1.0, 0.0, 0.0], 0);
/// nb.train_one(&[0.0, 0.0, 1.0, 1.0], 1);
///
/// assert_eq!(nb.predict_class(&[1.0, 1.0, 0.0, 0.0]), 0);
/// ```
#[derive(Debug, Clone)]
pub struct BernoulliNB {
    /// Laplace smoothing parameter.
    alpha: f64,
    /// Binarization threshold.
    threshold: f64,
    /// Per-class sample counts.
    class_counts: Vec<u64>,
    /// Per-class, per-feature presence counts. `[class][feature]`
    feature_present: Vec<Vec<u64>>,
    /// Number of features (0 = lazy init).
    n_features: usize,
    /// Total samples seen.
    n_samples: u64,
}

impl BernoulliNB {
    /// Create with default settings (alpha = 1.0, threshold = 0.0).
    pub fn new() -> Self {
        Self {
            alpha: 1.0,
            threshold: 0.0,
            class_counts: Vec::new(),
            feature_present: Vec::new(),
            n_features: 0,
            n_samples: 0,
        }
    }

    /// Create with custom smoothing parameter.
    pub fn with_alpha(alpha: f64) -> Self {
        let mut nb = Self::new();
        nb.alpha = alpha;
        nb
    }

    /// Create with custom binarization threshold.
    pub fn with_threshold(threshold: f64) -> Self {
        let mut nb = Self::new();
        nb.threshold = threshold;
        nb
    }

    /// Create with custom smoothing and threshold.
    pub fn with_alpha_and_threshold(alpha: f64, threshold: f64) -> Self {
        let mut nb = Self::new();
        nb.alpha = alpha;
        nb.threshold = threshold;
        nb
    }

    fn ensure_class(&mut self, class: usize) {
        while self.class_counts.len() <= class {
            self.class_counts.push(0);
            self.feature_present.push(vec![0; self.n_features]);
        }
    }

    /// Train on a single sample.
    pub fn train_one(&mut self, features: &[f64], class: usize) {
        if self.n_features == 0 {
            self.n_features = features.len();
            for fp in &mut self.feature_present {
                fp.resize(self.n_features, 0);
            }
        }
        self.ensure_class(class);
        self.n_samples += 1;
        self.class_counts[class] += 1;

        for (j, &x) in features.iter().enumerate() {
            if x > self.threshold {
                self.feature_present[class][j] += 1;
            }
        }
    }

    fn log_posteriors(&self, features: &[f64]) -> Vec<f64> {
        let n_total = self.n_samples as f64;
        let alpha = self.alpha;

        self.class_counts
            .iter()
            .enumerate()
            .map(|(c, &count)| {
                let log_prior = (count as f64 / n_total).ln();
                let denom = count as f64 + 2.0 * alpha;
                let log_likelihood: f64 = features
                    .iter()
                    .enumerate()
                    .map(|(j, &x)| {
                        let p = (self.feature_present[c][j] as f64 + alpha) / denom;
                        let b = if x > self.threshold { 1.0 } else { 0.0 };
                        b * p.ln() + (1.0 - b) * (1.0 - p).ln()
                    })
                    .sum();
                log_prior + log_likelihood
            })
            .collect()
    }

    /// Predict the most likely class.
    pub fn predict_class(&self, features: &[f64]) -> usize {
        let logs = self.log_posteriors(features);
        logs.iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(i, _)| i)
            .unwrap_or(0)
    }

    /// Log-probability for each class (unnormalized).
    pub fn predict_log_proba(&self, features: &[f64]) -> Vec<f64> {
        self.log_posteriors(features)
    }

    /// Normalized probability for each class.
    pub fn predict_proba(&self, features: &[f64]) -> Vec<f64> {
        let logs = self.log_posteriors(features);
        let max_log = logs.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
        let exps: Vec<f64> = logs.iter().map(|&l| (l - max_log).exp()).collect();
        let sum: f64 = exps.iter().sum();
        if sum == 0.0 {
            vec![1.0 / logs.len() as f64; logs.len()]
        } else {
            exps.iter().map(|&e| e / sum).collect()
        }
    }

    /// Number of discovered classes.
    pub fn n_classes(&self) -> usize {
        self.class_counts.len()
    }

    /// Total samples seen.
    pub fn n_samples_seen(&self) -> u64 {
        self.n_samples
    }

    /// Reset all state.
    pub fn reset(&mut self) {
        self.class_counts.clear();
        self.feature_present.clear();
        self.n_features = 0;
        self.n_samples = 0;
    }
}

impl Default for BernoulliNB {
    fn default() -> Self {
        Self::new()
    }
}

impl StreamingLearner for BernoulliNB {
    fn train_one(&mut self, features: &[f64], target: f64, _weight: f64) {
        self.train_one(features, target as usize);
    }

    fn predict(&self, features: &[f64]) -> f64 {
        self.predict_class(features) as f64
    }

    fn n_samples_seen(&self) -> u64 {
        self.n_samples
    }

    fn reset(&mut self) {
        self.reset();
    }
}

// ---------------------------------------------------------------------------
// DiagnosticSource impls
// ---------------------------------------------------------------------------

impl crate::automl::DiagnosticSource for MultinomialNB {
    fn config_diagnostics(&self) -> Option<crate::automl::ConfigDiagnostics> {
        None
    }
}

impl crate::automl::DiagnosticSource for BernoulliNB {
    fn config_diagnostics(&self) -> Option<crate::automl::ConfigDiagnostics> {
        None
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn approx_eq(a: f64, b: f64, tol: f64) -> bool {
        (a - b).abs() < tol
    }

    // -- MultinomialNB tests --

    #[test]
    fn multinomial_predict_simple() {
        let mut nb = MultinomialNB::new();
        // Class 0: heavy on features 0-1
        for _ in 0..20 {
            nb.train_one(&[5.0, 3.0, 0.0, 0.0], 0);
        }
        // Class 1: heavy on features 2-3
        for _ in 0..20 {
            nb.train_one(&[0.0, 0.0, 4.0, 6.0], 1);
        }
        assert_eq!(nb.predict_class(&[4.0, 2.0, 0.0, 0.0]), 0);
        assert_eq!(nb.predict_class(&[0.0, 0.0, 3.0, 5.0]), 1);
    }

    #[test]
    fn multinomial_predict_proba_sums_to_one() {
        let mut nb = MultinomialNB::new();
        nb.train_one(&[1.0, 0.0], 0);
        nb.train_one(&[0.0, 1.0], 1);
        let proba = nb.predict_proba(&[0.5, 0.5]);
        let sum: f64 = proba.iter().sum();
        assert!(approx_eq(sum, 1.0, 1e-10), "sum = {}", sum);
    }

    #[test]
    fn multinomial_auto_discovers_classes() {
        let mut nb = MultinomialNB::new();
        nb.train_one(&[1.0], 0);
        nb.train_one(&[1.0], 2); // skip class 1
        assert_eq!(nb.n_classes(), 3); // 0, 1 (empty), 2
    }

    #[test]
    fn multinomial_smoothing_prevents_zero_prob() {
        let mut nb = MultinomialNB::new();
        nb.train_one(&[1.0, 0.0], 0);
        nb.train_one(&[0.0, 1.0], 1);
        // Even though class 0 never saw feature 1, smoothing prevents -inf
        let log_proba = nb.predict_log_proba(&[0.0, 10.0]);
        assert!(log_proba[0].is_finite(), "log_proba[0] = {}", log_proba[0]);
    }

    #[test]
    fn multinomial_streaming_learner_trait() {
        let mut nb = MultinomialNB::new();
        let learner: &mut dyn StreamingLearner = &mut nb;
        learner.train(&[5.0, 0.0], 0.0);
        learner.train(&[0.0, 5.0], 1.0);
        let pred = learner.predict(&[4.0, 0.0]);
        assert_eq!(pred, 0.0);
    }

    #[test]
    fn multinomial_reset() {
        let mut nb = MultinomialNB::new();
        nb.train_one(&[1.0, 0.0], 0);
        nb.reset();
        assert_eq!(nb.n_samples_seen(), 0);
        assert_eq!(nb.n_classes(), 0);
    }

    // -- BernoulliNB tests --

    #[test]
    fn bernoulli_predict_simple() {
        let mut nb = BernoulliNB::new();
        for _ in 0..20 {
            nb.train_one(&[1.0, 1.0, 0.0, 0.0], 0);
        }
        for _ in 0..20 {
            nb.train_one(&[0.0, 0.0, 1.0, 1.0], 1);
        }
        assert_eq!(nb.predict_class(&[1.0, 1.0, 0.0, 0.0]), 0);
        assert_eq!(nb.predict_class(&[0.0, 0.0, 1.0, 1.0]), 1);
    }

    #[test]
    fn bernoulli_predict_proba_sums_to_one() {
        let mut nb = BernoulliNB::new();
        nb.train_one(&[1.0, 0.0], 0);
        nb.train_one(&[0.0, 1.0], 1);
        let proba = nb.predict_proba(&[1.0, 0.0]);
        let sum: f64 = proba.iter().sum();
        assert!(approx_eq(sum, 1.0, 1e-10), "sum = {}", sum);
    }

    #[test]
    fn bernoulli_models_absence() {
        let mut nb = BernoulliNB::new();
        // Class 0: feature 0 always present, feature 1 always absent
        for _ in 0..20 {
            nb.train_one(&[1.0, 0.0], 0);
        }
        // Class 1: feature 0 always absent, feature 1 always present
        for _ in 0..20 {
            nb.train_one(&[0.0, 1.0], 1);
        }
        // Absence of feature 1 should strongly indicate class 0
        assert_eq!(nb.predict_class(&[0.5, 0.0]), 0);
        // Absence of feature 0 should strongly indicate class 1
        assert_eq!(nb.predict_class(&[0.0, 0.5]), 1);
    }

    #[test]
    fn bernoulli_threshold_binarization() {
        let mut nb = BernoulliNB::with_threshold(0.5);
        for _ in 0..20 {
            nb.train_one(&[1.0, 0.0], 0);
        }
        for _ in 0..20 {
            nb.train_one(&[0.0, 1.0], 1);
        }
        // 0.3 is below threshold 0.5 → absent; 0.8 is above → present
        assert_eq!(nb.predict_class(&[0.8, 0.3]), 0);
        assert_eq!(nb.predict_class(&[0.3, 0.8]), 1);
    }

    #[test]
    fn bernoulli_streaming_learner_trait() {
        let mut nb = BernoulliNB::new();
        let learner: &mut dyn StreamingLearner = &mut nb;
        learner.train(&[1.0, 0.0], 0.0);
        learner.train(&[0.0, 1.0], 1.0);
        assert_eq!(learner.n_samples_seen(), 2);
    }

    #[test]
    fn bernoulli_reset() {
        let mut nb = BernoulliNB::new();
        nb.train_one(&[1.0, 0.0], 0);
        nb.reset();
        assert_eq!(nb.n_samples_seen(), 0);
        assert_eq!(nb.n_classes(), 0);
    }
}