rill-ml 1.5.1

RillML adaptive intelligence core library — lightweight, serializable online machine learning for native and edge applications.
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
//! Additive weighted-learning APIs and Preview weighted accumulators.
//!
//! The frozen unweighted traits and state layouts are unchanged. A zero
//! weight validates inputs but performs no state update and does not increment
//! `samples_seen`.

use crate::error::{RillError, checked_finite_add, checked_increment, ensure_finite};
#[cfg(feature = "serde")]
use crate::persistence::ValidateState;
use crate::traits::{OnlineBinaryClassifier, OnlineRegressor};

/// Online statistic accepting finite, non-negative sample weights.
pub trait WeightedStatistic {
    /// Incorporate a value with a caller-defined weight.
    fn update_weighted(&mut self, value: f64, weight: f64) -> Result<(), RillError>;
    /// Sum of accepted positive weights.
    fn total_weight(&self) -> f64;
    /// Number of accepted positive-weight observations.
    fn samples_seen(&self) -> u64;
    /// Reset values, total weight, and sample count.
    fn reset(&mut self);
}

/// Additive weighted update contract for online regressors.
pub trait WeightedOnlineRegressor: OnlineRegressor {
    /// Learn from a finite non-negative weighted sample.
    fn learn_weighted(
        &mut self,
        features: &[f64],
        target: f64,
        weight: f64,
    ) -> Result<(), RillError>;
}

/// Additive weighted update contract for online binary classifiers.
pub trait WeightedOnlineBinaryClassifier: OnlineBinaryClassifier {
    /// Learn from a finite non-negative weighted sample.
    fn learn_weighted(
        &mut self,
        features: &[f64],
        target: bool,
        weight: f64,
    ) -> Result<(), RillError>;
}

/// Validate the shared weight contract.
pub fn validate_weight(weight: f64) -> Result<(), RillError> {
    ensure_finite("weight", weight)?;
    if weight < 0.0 {
        return Err(RillError::InvalidParameter {
            name: "weight",
            value: weight,
        });
    }
    Ok(())
}

/// Preview O(1)-space weighted arithmetic mean.
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct WeightedMean {
    total_weight: f64,
    mean: f64,
    samples_seen: u64,
}

impl WeightedMean {
    /// Create an empty weighted mean.
    pub const fn new() -> Self {
        Self {
            total_weight: 0.0,
            mean: 0.0,
            samples_seen: 0,
        }
    }

    /// Current weighted mean, or `None` before a positive weight is seen.
    pub fn value(&self) -> Option<f64> {
        (self.total_weight > 0.0).then_some(self.mean)
    }
}

impl WeightedStatistic for WeightedMean {
    fn update_weighted(&mut self, value: f64, weight: f64) -> Result<(), RillError> {
        ensure_finite("value", value)?;
        validate_weight(weight)?;
        if weight == 0.0 {
            return Ok(());
        }
        let next_weight = checked_finite_add(self.total_weight, weight, "total weight")?;
        let delta = value - self.mean;
        ensure_finite("weighted mean delta", delta)?;
        let next_mean = self.mean + delta * (weight / next_weight);
        ensure_finite("weighted mean", next_mean)?;
        let next_samples = checked_increment(self.samples_seen, "weighted mean sample")?;
        self.total_weight = next_weight;
        self.mean = next_mean;
        self.samples_seen = next_samples;
        Ok(())
    }

    fn total_weight(&self) -> f64 {
        self.total_weight
    }

    fn samples_seen(&self) -> u64 {
        self.samples_seen
    }

    fn reset(&mut self) {
        *self = Self::new();
    }
}

/// Preview O(1)-space weighted population variance (West's update).
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct WeightedVariance {
    total_weight: f64,
    mean: f64,
    m2: f64,
    samples_seen: u64,
}

impl WeightedVariance {
    /// Create an empty weighted variance.
    pub const fn new() -> Self {
        Self {
            total_weight: 0.0,
            mean: 0.0,
            m2: 0.0,
            samples_seen: 0,
        }
    }

    /// Weighted mean, if any positive weight has been observed.
    pub fn mean(&self) -> Option<f64> {
        (self.total_weight > 0.0).then_some(self.mean)
    }

    /// Population variance `Σw(x-μ)² / Σw`.
    pub fn value(&self) -> Option<f64> {
        (self.total_weight > 0.0).then_some(self.m2 / self.total_weight)
    }
}

impl WeightedStatistic for WeightedVariance {
    fn update_weighted(&mut self, value: f64, weight: f64) -> Result<(), RillError> {
        ensure_finite("value", value)?;
        validate_weight(weight)?;
        if weight == 0.0 {
            return Ok(());
        }
        let next_weight = checked_finite_add(self.total_weight, weight, "total weight")?;
        let delta = value - self.mean;
        ensure_finite("weighted variance delta", delta)?;
        let ratio = weight / next_weight;
        let next_mean = self.mean + ratio * delta;
        ensure_finite("weighted variance mean", next_mean)?;
        let m2_delta = weight * delta * (value - next_mean);
        ensure_finite("weighted variance m2 delta", m2_delta)?;
        let next_m2 = checked_finite_add(self.m2, m2_delta, "weighted variance m2")?;
        if next_m2 < -f64::EPSILON * next_weight.max(1.0) {
            return Err(RillError::InvalidState(
                "weighted variance became negative".to_owned(),
            ));
        }
        let next_samples = checked_increment(self.samples_seen, "weighted variance sample")?;
        self.total_weight = next_weight;
        self.mean = next_mean;
        self.m2 = next_m2.max(0.0);
        self.samples_seen = next_samples;
        Ok(())
    }

    fn total_weight(&self) -> f64 {
        self.total_weight
    }

    fn samples_seen(&self) -> u64 {
        self.samples_seen
    }

    fn reset(&mut self) {
        *self = Self::new();
    }
}

/// Preview weighted exponentially decayed mean.
///
/// A sample weight `w` uses effective smoothing
/// `1 - (1 - alpha)^w`, matching repeated integer-weight updates while also
/// supporting fractional weights.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct WeightedExponentiallyWeightedMean {
    alpha: f64,
    total_weight: f64,
    mean: f64,
    samples_seen: u64,
}

impl WeightedExponentiallyWeightedMean {
    /// Create an accumulator with `alpha` in `(0, 1]`.
    pub fn new(alpha: f64) -> Result<Self, RillError> {
        ensure_finite("alpha", alpha)?;
        if !(0.0 < alpha && alpha <= 1.0) {
            return Err(RillError::InvalidParameter {
                name: "alpha",
                value: alpha,
            });
        }
        Ok(Self {
            alpha,
            total_weight: 0.0,
            mean: 0.0,
            samples_seen: 0,
        })
    }

    /// Current value, or `None` before a positive weight is observed.
    pub fn value(&self) -> Option<f64> {
        (self.total_weight > 0.0).then_some(self.mean)
    }

    /// Configured base smoothing factor.
    pub fn alpha(&self) -> f64 {
        self.alpha
    }
}

impl WeightedStatistic for WeightedExponentiallyWeightedMean {
    fn update_weighted(&mut self, value: f64, weight: f64) -> Result<(), RillError> {
        ensure_finite("value", value)?;
        validate_weight(weight)?;
        if weight == 0.0 {
            return Ok(());
        }
        let next_weight = checked_finite_add(self.total_weight, weight, "total weight")?;
        let next_samples = checked_increment(self.samples_seen, "weighted EW mean sample")?;
        let next_mean = if self.total_weight == 0.0 {
            value
        } else {
            let effective_alpha = 1.0 - (1.0 - self.alpha).powf(weight);
            ensure_finite("effective alpha", effective_alpha)?;
            self.mean + effective_alpha * (value - self.mean)
        };
        ensure_finite("weighted EW mean", next_mean)?;
        self.total_weight = next_weight;
        self.samples_seen = next_samples;
        self.mean = next_mean;
        Ok(())
    }

    fn total_weight(&self) -> f64 {
        self.total_weight
    }

    fn samples_seen(&self) -> u64 {
        self.samples_seen
    }

    fn reset(&mut self) {
        self.total_weight = 0.0;
        self.mean = 0.0;
        self.samples_seen = 0;
    }
}

/// Preview weighted mean absolute error.
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct WeightedMae {
    errors: WeightedMean,
}

impl WeightedMae {
    /// Update with a weighted truth/prediction pair.
    pub fn update(&mut self, truth: f64, prediction: f64, weight: f64) -> Result<(), RillError> {
        ensure_finite("truth", truth)?;
        ensure_finite("prediction", prediction)?;
        let error = (truth - prediction).abs();
        ensure_finite("absolute error", error)?;
        self.errors.update_weighted(error, weight)
    }

    /// Current weighted MAE.
    pub fn value(&self) -> Option<f64> {
        self.errors.value()
    }

    /// Sum of weights.
    pub fn total_weight(&self) -> f64 {
        self.errors.total_weight()
    }

    /// Number of positive-weight observations.
    pub fn samples_seen(&self) -> u64 {
        self.errors.samples_seen()
    }

    /// Reset all metric state.
    pub fn reset(&mut self) {
        self.errors.reset();
    }
}

/// Preview weighted mean squared error.
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct WeightedMse {
    errors: WeightedMean,
}

impl WeightedMse {
    /// Update with a weighted truth/prediction pair.
    pub fn update(&mut self, truth: f64, prediction: f64, weight: f64) -> Result<(), RillError> {
        ensure_finite("truth", truth)?;
        ensure_finite("prediction", prediction)?;
        let error = truth - prediction;
        let squared = error * error;
        ensure_finite("squared error", squared)?;
        self.errors.update_weighted(squared, weight)
    }

    /// Current weighted MSE.
    pub fn value(&self) -> Option<f64> {
        self.errors.value()
    }

    /// Sum of weights.
    pub fn total_weight(&self) -> f64 {
        self.errors.total_weight()
    }

    /// Number of positive-weight observations.
    pub fn samples_seen(&self) -> u64 {
        self.errors.samples_seen()
    }

    /// Reset all metric state.
    pub fn reset(&mut self) {
        self.errors.reset();
    }
}

#[cfg(feature = "serde")]
fn validate_weighted_moments(
    total_weight: f64,
    samples_seen: u64,
    values: &[(&'static str, f64)],
) -> Result<(), RillError> {
    ensure_finite("total weight", total_weight)?;
    if total_weight < 0.0 || (samples_seen == 0) != (total_weight == 0.0) {
        return Err(RillError::InvalidState(
            "weighted state has inconsistent count and total weight".to_owned(),
        ));
    }
    for &(name, value) in values {
        ensure_finite(name, value)?;
    }
    Ok(())
}

#[cfg(feature = "serde")]
impl ValidateState for WeightedMean {
    fn validate_state(&self) -> Result<(), RillError> {
        validate_weighted_moments(self.total_weight, self.samples_seen, &[("mean", self.mean)])
    }
}

#[cfg(feature = "serde")]
impl ValidateState for WeightedVariance {
    fn validate_state(&self) -> Result<(), RillError> {
        validate_weighted_moments(
            self.total_weight,
            self.samples_seen,
            &[("mean", self.mean), ("m2", self.m2)],
        )?;
        if self.m2 < 0.0 {
            return Err(RillError::InvalidState(
                "weighted variance m2 must be non-negative".to_owned(),
            ));
        }
        Ok(())
    }
}

#[cfg(feature = "serde")]
impl ValidateState for WeightedExponentiallyWeightedMean {
    fn validate_state(&self) -> Result<(), RillError> {
        if !(self.alpha.is_finite() && 0.0 < self.alpha && self.alpha <= 1.0) {
            return Err(RillError::InvalidState(
                "weighted EW mean alpha must be in (0, 1]".to_owned(),
            ));
        }
        validate_weighted_moments(self.total_weight, self.samples_seen, &[("mean", self.mean)])
    }
}

#[cfg(feature = "serde")]
impl ValidateState for WeightedMae {
    fn validate_state(&self) -> Result<(), RillError> {
        self.errors.validate_state()
    }
}

#[cfg(feature = "serde")]
impl ValidateState for WeightedMse {
    fn validate_state(&self) -> Result<(), RillError> {
        self.errors.validate_state()
    }
}

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

    #[test]
    fn weighted_mean_and_variance_match_offline() {
        let samples = [(1.0, 0.5), (3.0, 2.0), (10.0, 1.5)];
        let total = samples.iter().map(|(_, weight)| weight).sum::<f64>();
        let offline_mean = samples.iter().map(|(x, w)| x * w).sum::<f64>() / total;
        let offline_variance = samples
            .iter()
            .map(|(x, w)| w * (x - offline_mean).powi(2))
            .sum::<f64>()
            / total;
        let mut mean = WeightedMean::new();
        let mut variance = WeightedVariance::new();
        for (x, weight) in samples {
            mean.update_weighted(x, weight).unwrap();
            variance.update_weighted(x, weight).unwrap();
        }
        assert_abs_diff_eq!(mean.value().unwrap(), offline_mean, epsilon = 1e-12);
        assert_abs_diff_eq!(variance.value().unwrap(), offline_variance, epsilon = 1e-12);
    }

    #[test]
    fn integer_weight_ew_mean_matches_repeated_samples() {
        let mut weighted = WeightedExponentiallyWeightedMean::new(0.2).unwrap();
        let mut repeated = WeightedExponentiallyWeightedMean::new(0.2).unwrap();
        weighted.update_weighted(1.0, 1.0).unwrap();
        repeated.update_weighted(1.0, 1.0).unwrap();
        weighted.update_weighted(5.0, 3.0).unwrap();
        for _ in 0..3 {
            repeated.update_weighted(5.0, 1.0).unwrap();
        }
        assert_abs_diff_eq!(
            weighted.value().unwrap(),
            repeated.value().unwrap(),
            epsilon = 1e-12
        );
    }

    #[test]
    fn zero_weight_is_validated_noop_and_bad_weights_are_atomic() {
        let mut mean = WeightedMean::new();
        mean.update_weighted(3.0, 0.0).unwrap();
        assert_eq!(mean, WeightedMean::new());
        let before = mean.clone();
        assert!(mean.update_weighted(1.0, -1.0).is_err());
        assert!(mean.update_weighted(1.0, f64::NAN).is_err());
        assert_eq!(mean, before);
    }

    #[test]
    fn weighted_errors_match_offline_calculation_and_reset() {
        let samples: [(f64, f64, f64); 3] = [(2.0, 1.0, 0.5), (5.0, 2.0, 2.0), (-1.0, 1.0, 1.5)];
        let total_weight = samples.iter().map(|sample| sample.2).sum::<f64>();
        let expected_mae = samples
            .iter()
            .map(|(truth, prediction, weight)| weight * (truth - prediction).abs())
            .sum::<f64>()
            / total_weight;
        let expected_mse = samples
            .iter()
            .map(|(truth, prediction, weight)| weight * (truth - prediction).powi(2))
            .sum::<f64>()
            / total_weight;
        let mut mae = WeightedMae::default();
        let mut mse = WeightedMse::default();
        for (truth, prediction, weight) in samples {
            mae.update(truth, prediction, weight).unwrap();
            mse.update(truth, prediction, weight).unwrap();
        }
        assert_abs_diff_eq!(mae.value().unwrap(), expected_mae, epsilon = 1e-12);
        assert_abs_diff_eq!(mse.value().unwrap(), expected_mse, epsilon = 1e-12);
        assert_eq!(mae.samples_seen(), 3);
        assert_eq!(mse.total_weight(), total_weight);
        mae.reset();
        mse.reset();
        assert_eq!(mae.value(), None);
        assert_eq!(mse.samples_seen(), 0);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn weighted_state_roundtrip_and_corruption_rejected() {
        let mut variance = WeightedVariance::new();
        variance.update_weighted(2.0, 0.5).unwrap();
        let json = serde_json::to_string(&variance).unwrap();
        let restored: WeightedVariance = serde_json::from_str(&json).unwrap();
        restored.validate_state().unwrap();
        let corrupt = json.replace("\"total_weight\":0.5", "\"total_weight\":-1.0");
        let restored: WeightedVariance = serde_json::from_str(&corrupt).unwrap();
        assert!(restored.validate_state().is_err());
    }
}