irithyll 10.0.0

Streaming ML in Rust -- gradient boosted trees, neural architectures (TTT/KAN/MoE/Mamba/SNN), AutoML, kernel methods, and composable pipelines
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
//! Progressive validation with configurable holdout strategies.
//!
//! Unlike prequential evaluation (which trains on every sample), progressive
//! validation can hold out some samples for pure evaluation -- they are never
//! trained on. This provides a more conservative estimate of generalisation
//! performance at the cost of reduced training data.
//!
//! # Holdout Strategies
//!
//! - [`HoldoutStrategy::None`] -- train on everything (equivalent to prequential).
//! - [`HoldoutStrategy::Periodic`] -- every N-th sample is test-only.
//! - [`HoldoutStrategy::Random`] -- each sample has probability *p* of being
//!   held out, using a deterministic xorshift64 PRNG for reproducibility.

use crate::learner::StreamingLearner;
use crate::metrics::regression::RegressionMetrics;
use crate::sample::Observation;

// ---------------------------------------------------------------------------
// Holdout Strategy
// ---------------------------------------------------------------------------

/// Strategy for deciding which samples are held out (test-only, never trained on).
///
/// All strategies evaluate every sample; the distinction is whether the model
/// is trained on it afterward.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum HoldoutStrategy {
    /// Every sample is used for both test and train (equivalent to prequential).
    None,
    /// Every N-th sample is test-only (not trained on).
    ///
    /// The remaining `(N-1)/N` fraction of samples are used for training.
    Periodic {
        /// Holdout period. Every `period`-th sample is test-only.
        period: usize,
    },
    /// Each sample has probability `holdout_fraction` of being test-only.
    ///
    /// Uses a deterministic xorshift64 PRNG seeded with `seed` for
    /// reproducibility. The same seed always produces the same holdout pattern.
    Random {
        /// Probability that a sample is held out (not trained on).
        holdout_fraction: f64,
        /// Seed for the deterministic PRNG.
        seed: u64,
    },
}

use irithyll_core::rng::xorshift64;

// ---------------------------------------------------------------------------
// Progressive Validator
// ---------------------------------------------------------------------------

/// Progressive validator with configurable holdout.
///
/// Every sample is always evaluated (predict + record metrics). The holdout
/// strategy controls which samples the model trains on afterward. This lets
/// you measure model performance on data it will never learn from.
///
/// # Semantics
///
/// For each sample:
/// 1. **Predict** -- query the model.
/// 2. **Record** -- update regression metrics with (target, prediction).
/// 3. **Holdout check** -- decide whether to train based on the strategy.
/// 4. **Train** (conditionally) -- feed the sample to the model if not held out.
///
/// # Example
///
/// ```
/// use irithyll::evaluation::{ProgressiveValidator, HoldoutStrategy};
/// use irithyll::learner::{SGBTLearner, StreamingLearner};
/// use irithyll::SGBTConfig;
///
/// let config = SGBTConfig::builder()
///     .n_steps(5)
///     .learning_rate(0.1)
///     .grace_period(10)
///     .build()
///     .unwrap();
/// let mut model = SGBTLearner::from_config(config);
///
/// let holdout = HoldoutStrategy::Periodic { period: 5 };
/// let mut validator = ProgressiveValidator::new(holdout);
///
/// for i in 0..200 {
///     let x = [i as f64, (i as f64).sin()];
///     let y = i as f64 * 0.5 + 1.0;
///     validator.step(&mut model, &x, y);
/// }
///
/// // ~80% of samples were trained on, ~20% were holdout-only
/// assert!(validator.holdout_fraction_actual() > 0.15);
/// assert!(validator.holdout_fraction_actual() < 0.25);
/// ```
#[derive(Debug, Clone)]
pub struct ProgressiveValidator {
    holdout: HoldoutStrategy,
    regression: RegressionMetrics,
    samples_seen: u64,
    samples_evaluated: u64,
    samples_trained: u64,
    rng_state: u64,
}

impl ProgressiveValidator {
    /// Create a new progressive validator with the given holdout strategy.
    ///
    /// # Panics
    ///
    /// - Panics if `HoldoutStrategy::Periodic { period }` has `period == 0`.
    /// - Panics if `HoldoutStrategy::Random { holdout_fraction, .. }` has
    ///   `holdout_fraction` outside `[0.0, 1.0]`.
    /// - Panics if `HoldoutStrategy::Random { seed, .. }` has `seed == 0`
    ///   (xorshift64 requires a non-zero seed).
    pub fn new(holdout: HoldoutStrategy) -> Self {
        let rng_state = match &holdout {
            HoldoutStrategy::None => 1, // unused, but non-zero
            HoldoutStrategy::Periodic { period } => {
                assert!(*period > 0, "holdout period must be > 0");
                1 // unused
            }
            HoldoutStrategy::Random {
                holdout_fraction,
                seed,
            } => {
                assert!(
                    *holdout_fraction >= 0.0 && *holdout_fraction <= 1.0,
                    "holdout_fraction must be in [0.0, 1.0], got {}",
                    holdout_fraction
                );
                assert!(*seed != 0, "xorshift64 seed must be non-zero");
                *seed
            }
        };

        Self {
            holdout,
            regression: RegressionMetrics::new(),
            samples_seen: 0,
            samples_evaluated: 0,
            samples_trained: 0,
            rng_state,
        }
    }

    /// Process a single sample: always evaluate, conditionally train.
    ///
    /// Returns the prediction (made before any potential training).
    ///
    /// The model is always queried for a prediction and metrics are always
    /// recorded. Whether the model trains on this sample depends on the
    /// holdout strategy:
    ///
    /// - `None`: always train.
    /// - `Periodic { period }`: skip training every `period`-th sample.
    /// - `Random { holdout_fraction, .. }`: skip training with probability
    ///   `holdout_fraction`.
    pub fn step(&mut self, model: &mut dyn StreamingLearner, features: &[f64], target: f64) -> f64 {
        // 1. Predict
        let prediction = model.predict(features);

        // 2. Always record metrics
        self.regression.update(target, prediction);
        self.samples_evaluated += 1;

        // 3. Decide whether to train based on holdout strategy
        let should_train = match &self.holdout {
            HoldoutStrategy::None => true,
            HoldoutStrategy::Periodic { period } => {
                // Every period-th sample is held out (not trained on)
                self.samples_seen % *period as u64 != 0
            }
            HoldoutStrategy::Random {
                holdout_fraction, ..
            } => {
                let rand_val = xorshift64(&mut self.rng_state) as f64 / u64::MAX as f64;
                rand_val >= *holdout_fraction
            }
        };

        // 4. Conditionally train
        if should_train {
            model.train(features, target);
            self.samples_trained += 1;
        }

        // 5. Increment counter
        self.samples_seen += 1;

        prediction
    }

    /// Run the full evaluation protocol over a slice of observations.
    ///
    /// Iterates over `data`, calling [`step`](Self::step) for each observation.
    pub fn evaluate<O: Observation>(&mut self, model: &mut dyn StreamingLearner, data: &[O]) {
        for obs in data {
            self.step(model, obs.features(), obs.target());
        }
    }

    /// Reference to the cumulative regression metrics.
    pub fn regression_metrics(&self) -> &RegressionMetrics {
        &self.regression
    }

    /// Total number of samples processed.
    pub fn samples_seen(&self) -> u64 {
        self.samples_seen
    }

    /// Number of samples evaluated (always equals `samples_seen`).
    pub fn samples_evaluated(&self) -> u64 {
        self.samples_evaluated
    }

    /// Number of samples the model was trained on.
    pub fn samples_trained(&self) -> u64 {
        self.samples_trained
    }

    /// Actual holdout fraction: `1.0 - (samples_trained / samples_seen)`.
    ///
    /// Returns 0.0 if no samples have been seen yet.
    pub fn holdout_fraction_actual(&self) -> f64 {
        if self.samples_seen == 0 {
            return 0.0;
        }
        1.0 - (self.samples_trained as f64 / self.samples_seen as f64)
    }

    /// Reset all metrics and counters to the initial state.
    ///
    /// The holdout strategy is preserved. For `Random`, the PRNG state is
    /// reset to the original seed.
    pub fn reset(&mut self) {
        self.regression.reset();
        self.samples_seen = 0;
        self.samples_evaluated = 0;
        self.samples_trained = 0;

        // Reset PRNG to original seed for reproducibility
        if let HoldoutStrategy::Random { seed, .. } = &self.holdout {
            self.rng_state = *seed;
        }
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::learner::StreamingLearner;
    use crate::sample::Sample;

    /// Mock learner that always predicts a fixed constant.
    struct MockLearner {
        prediction: f64,
        n: u64,
    }

    impl MockLearner {
        fn new(prediction: f64) -> Self {
            Self { prediction, n: 0 }
        }
    }

    impl StreamingLearner for MockLearner {
        fn train_one(&mut self, _features: &[f64], _target: f64, _weight: f64) {
            self.n += 1;
        }

        fn predict(&self, _features: &[f64]) -> f64 {
            self.prediction
        }

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

        fn reset(&mut self) {
            self.n = 0;
        }
    }

    #[test]
    fn holdout_none_trains_on_everything() {
        let mut model = MockLearner::new(0.0);
        let mut validator = ProgressiveValidator::new(HoldoutStrategy::None);

        for i in 0..100 {
            validator.step(&mut model, &[i as f64], i as f64);
        }

        assert_eq!(validator.samples_seen(), 100);
        assert_eq!(validator.samples_evaluated(), 100);
        assert_eq!(validator.samples_trained(), 100);
        assert!(
            (validator.holdout_fraction_actual() - 0.0).abs() < 1e-12,
            "holdout fraction should be 0.0 for None strategy"
        );
        assert_eq!(model.n_samples_seen(), 100);
    }

    #[test]
    fn holdout_periodic_holds_out_every_nth() {
        let mut model = MockLearner::new(0.0);
        let holdout = HoldoutStrategy::Periodic { period: 5 };
        let mut validator = ProgressiveValidator::new(holdout);

        for i in 0..100 {
            validator.step(&mut model, &[i as f64], i as f64);
        }

        assert_eq!(validator.samples_seen(), 100);
        assert_eq!(validator.samples_evaluated(), 100);
        // Held out at samples_seen=0,5,10,...,95 => 20 held out, 80 trained
        assert_eq!(validator.samples_trained(), 80);
        assert_eq!(model.n_samples_seen(), 80);

        let actual = validator.holdout_fraction_actual();
        assert!(
            (actual - 0.2).abs() < 0.01,
            "expected holdout ~0.2, got {}",
            actual
        );
    }

    #[test]
    fn holdout_random_approximates_target_fraction() {
        let mut model = MockLearner::new(0.0);
        let holdout = HoldoutStrategy::Random {
            holdout_fraction: 0.3,
            seed: 42,
        };
        let mut validator = ProgressiveValidator::new(holdout);

        for i in 0..10_000 {
            validator.step(&mut model, &[i as f64], i as f64);
        }

        assert_eq!(validator.samples_seen(), 10_000);
        assert_eq!(validator.samples_evaluated(), 10_000);

        let actual = validator.holdout_fraction_actual();
        assert!(
            (actual - 0.3).abs() < 0.05,
            "expected holdout ~0.3, got {} (tolerance 0.05)",
            actual
        );
    }

    #[test]
    fn holdout_random_deterministic_with_same_seed() {
        let run = |seed: u64| -> Vec<bool> {
            let mut model = MockLearner::new(0.0);
            let holdout = HoldoutStrategy::Random {
                holdout_fraction: 0.5,
                seed,
            };
            let mut validator = ProgressiveValidator::new(holdout);
            let mut pattern = Vec::new();
            for i in 0..50 {
                let prev_trained = validator.samples_trained();
                validator.step(&mut model, &[i as f64], i as f64);
                // Record whether this specific sample was trained on
                pattern.push(validator.samples_trained() > prev_trained);
            }
            pattern
        };

        let run1 = run(12345);
        let run2 = run(12345);
        assert_eq!(
            run1, run2,
            "same seed should produce identical holdout patterns"
        );

        // Different seed should (very likely) produce different pattern
        let run3 = run(99999);
        assert_ne!(
            run1, run3,
            "different seeds should produce different holdout patterns"
        );
    }

    #[test]
    fn step_returns_prediction() {
        let mut model = MockLearner::new(7.5);
        let mut validator = ProgressiveValidator::new(HoldoutStrategy::None);

        let pred = validator.step(&mut model, &[1.0], 5.0);
        assert!(
            (pred - 7.5).abs() < 1e-12,
            "step should return the model's prediction, got {}",
            pred
        );
    }

    #[test]
    fn evaluate_over_observations() {
        let mut model = MockLearner::new(0.0);
        let mut validator = ProgressiveValidator::new(HoldoutStrategy::None);

        let data: Vec<Sample> = (0..30)
            .map(|i| Sample::new(vec![i as f64], i as f64))
            .collect();

        validator.evaluate(&mut model, &data);

        assert_eq!(validator.samples_seen(), 30);
        assert_eq!(validator.samples_evaluated(), 30);
        assert_eq!(validator.samples_trained(), 30);
    }

    #[test]
    fn reset_clears_all_state() {
        let mut model = MockLearner::new(0.0);
        let holdout = HoldoutStrategy::Random {
            holdout_fraction: 0.2,
            seed: 42,
        };
        let mut validator = ProgressiveValidator::new(holdout);

        for i in 0..50 {
            validator.step(&mut model, &[i as f64], i as f64);
        }

        assert!(validator.samples_seen() > 0);
        validator.reset();

        assert_eq!(validator.samples_seen(), 0);
        assert_eq!(validator.samples_evaluated(), 0);
        assert_eq!(validator.samples_trained(), 0);
        assert_eq!(validator.regression_metrics().n_samples(), 0);
        assert!(
            (validator.holdout_fraction_actual() - 0.0).abs() < 1e-12,
            "holdout fraction should be 0.0 after reset"
        );
    }

    #[test]
    fn reset_restores_prng_seed() {
        let holdout = HoldoutStrategy::Random {
            holdout_fraction: 0.5,
            seed: 42,
        };

        let mut model1 = MockLearner::new(0.0);
        let mut validator = ProgressiveValidator::new(holdout);

        // Run once
        for i in 0..20 {
            validator.step(&mut model1, &[i as f64], i as f64);
        }
        let trained_first_run = validator.samples_trained();

        // Reset and run again
        validator.reset();
        let mut model2 = MockLearner::new(0.0);
        for i in 0..20 {
            validator.step(&mut model2, &[i as f64], i as f64);
        }
        let trained_second_run = validator.samples_trained();

        assert_eq!(
            trained_first_run, trained_second_run,
            "reset should restore PRNG state, producing identical holdout patterns"
        );
    }

    #[test]
    fn metrics_always_recorded_even_for_holdout() {
        let mut model = MockLearner::new(0.0);
        let holdout = HoldoutStrategy::Periodic { period: 2 };
        let mut validator = ProgressiveValidator::new(holdout);

        for i in 0..10 {
            validator.step(&mut model, &[i as f64], i as f64);
        }

        // All 10 samples should be evaluated
        assert_eq!(validator.samples_evaluated(), 10);
        assert_eq!(validator.regression_metrics().n_samples(), 10);

        // But only ~half are trained on
        // Held out: 0, 2, 4, 6, 8 => 5 held out, 5 trained
        assert_eq!(validator.samples_trained(), 5);
    }

    #[test]
    #[should_panic(expected = "holdout period must be > 0")]
    fn periodic_zero_period_panics() {
        ProgressiveValidator::new(HoldoutStrategy::Periodic { period: 0 });
    }

    #[test]
    #[should_panic(expected = "holdout_fraction must be in [0.0, 1.0]")]
    fn random_invalid_fraction_panics() {
        ProgressiveValidator::new(HoldoutStrategy::Random {
            holdout_fraction: 1.5,
            seed: 42,
        });
    }

    #[test]
    #[should_panic(expected = "xorshift64 seed must be non-zero")]
    fn random_zero_seed_panics() {
        ProgressiveValidator::new(HoldoutStrategy::Random {
            holdout_fraction: 0.5,
            seed: 0,
        });
    }

    #[test]
    fn holdout_fraction_zero_means_all_train() {
        let mut model = MockLearner::new(0.0);
        let holdout = HoldoutStrategy::Random {
            holdout_fraction: 0.0,
            seed: 42,
        };
        let mut validator = ProgressiveValidator::new(holdout);

        for i in 0..100 {
            validator.step(&mut model, &[i as f64], i as f64);
        }

        assert_eq!(validator.samples_trained(), 100);
    }

    #[test]
    fn holdout_fraction_one_means_none_train() {
        let mut model = MockLearner::new(0.0);
        let holdout = HoldoutStrategy::Random {
            holdout_fraction: 1.0,
            seed: 42,
        };
        let mut validator = ProgressiveValidator::new(holdout);

        for i in 0..100 {
            validator.step(&mut model, &[i as f64], i as f64);
        }

        assert_eq!(validator.samples_trained(), 0);
        assert_eq!(model.n_samples_seen(), 0);
    }
}