irox_stats/
streaming.rs

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
// SPDX-License-Identifier: MIT
// Copyright 2024 IROX Contributors
//

//!
//! Streaming Statistics
//!

use core::ops::{Add, Div, Mul, Sub};
use irox_tools::f64::FloatExt;

///
/// A statistic type that can be incrementally calculated after each sample is added.  Does not
/// necessarily have to have the full set of data available to it, and as such, should be fast.
pub trait StreamingStatistic {
    type Type;

    /// adds a sample to this streaming statistic, returning a result of the operation.
    fn add_sample(&mut self, sample: Self::Type) -> Self::Type;
    /// Returns a copy of the last sample added
    fn get_last_sample(&self) -> Self::Type;
    /// Returns the last value returned by 'add_sample'
    fn get_last_result(&self) -> Self::Type;
    /// Returns the total number of samples pushed in
    fn get_num_samples(&self) -> u64;
}

///
/// Standard rolling average/mean of all the samples pushed into it.
#[derive(Default, Debug, Clone, Copy)]
pub struct Mean<Type> {
    sample_count: u64,
    last_sample: Option<Type>,
    last_mean: Option<Type>,
}

impl<T> Mean<T>
where
    T: Copy,
{
    pub fn get_mean(&self) -> Option<T> {
        self.last_mean
    }
}

///
/// Standard rolling average/mean of all the f32 samples pushed into it.
pub type MeanF32 = Mean<f32>;
///
/// Standard rolling average/mean of all the f64 samples pushed into it.
pub type MeanF64 = Mean<f64>;

impl<Type> StreamingStatistic for Mean<Type>
where
    Type: Sub<Type, Output = Type>
        + Copy
        + Div<f64, Output = Type>
        + Add<Type, Output = Type>
        + Default,
{
    type Type = Type;

    fn add_sample(&mut self, sample: Self::Type) -> Self::Type {
        self.sample_count += 1;
        self.last_sample = Some(sample);
        let last = self.last_mean.get_or_insert(sample);
        let mean = *last + (sample - *last) / self.sample_count as f64;
        *last = mean;
        mean
    }

    fn get_last_sample(&self) -> Self::Type {
        self.last_sample.unwrap_or_default()
    }

    fn get_last_result(&self) -> Self::Type {
        self.last_mean.unwrap_or_default()
    }

    fn get_num_samples(&self) -> u64 {
        self.sample_count
    }
}

/// Streaming maximum function
#[derive(Default, Debug, Clone, Copy)]
pub struct Max<T> {
    max_val: Option<T>,
    last_sample: Option<T>,
    num_samples: u64,
}
impl<T: Copy> Max<T> {
    pub fn get_max_val(&self) -> Option<T> {
        self.max_val
    }
}

impl<T: Default> StreamingStatistic for Max<T>
where
    T: PartialOrd + Copy,
{
    type Type = T;

    fn add_sample(&mut self, sample: Self::Type) -> Self::Type {
        self.num_samples += 1;
        let mut max = self.max_val.get_or_insert(sample);
        if sample.ge(max) {
            max = self.max_val.insert(sample);
        }
        *max
    }

    fn get_last_sample(&self) -> Self::Type {
        self.last_sample.unwrap_or_default()
    }

    fn get_last_result(&self) -> Self::Type {
        self.max_val.unwrap_or_default()
    }

    fn get_num_samples(&self) -> u64 {
        self.num_samples
    }
}

/// Streaming minimum function
#[derive(Default, Debug, Clone, Copy)]
pub struct Min<T> {
    min_val: Option<T>,
    last_sample: Option<T>,
    num_samples: u64,
}

impl<T: Copy> Min<T> {
    pub fn get_min_val(&self) -> Option<T> {
        self.min_val
    }
}

impl<T: Default> StreamingStatistic for Min<T>
where
    T: PartialOrd + Copy,
{
    type Type = T;

    fn add_sample(&mut self, sample: Self::Type) -> Self::Type {
        self.num_samples += 1;
        let mut min = self.min_val.get_or_insert(sample);
        if sample.le(min) {
            min = self.min_val.insert(sample);
        }
        *min
    }

    fn get_last_sample(&self) -> Self::Type {
        self.last_sample.unwrap_or_default()
    }

    fn get_last_result(&self) -> Self::Type {
        self.min_val.unwrap_or_default()
    }

    fn get_num_samples(&self) -> u64 {
        self.num_samples
    }
}

///
/// Unweighted sum of squares / [Total Sum of Squares](https://en.wikipedia.org/wiki/Total_sum_of_squares) / SST
/// using Chan, Golub, LeVeque's algorithm in TR222 1.3b
#[derive(Default, Debug, Clone, Copy)]
pub struct UnweightedSumOfSquares<T> {
    means: Mean<T>,
    last_ssq: Option<T>,
}

impl<T> UnweightedSumOfSquares<T>
where
    T: Copy + Default,
{
    pub fn get_mean(&self) -> T {
        self.means.last_mean.unwrap_or_default()
    }
    pub fn get_unweighted_sum_of_squares(&self) -> T {
        self.last_ssq.unwrap_or_default()
    }
}

impl<Type> StreamingStatistic for UnweightedSumOfSquares<Type>
where
    Type: Sub<Type, Output = Type>
        + Copy
        + Default
        + Div<f64, Output = Type>
        + Add<Type, Output = Type>
        + Mul<f64, Output = Type>
        + Mul<Type, Output = Type>,
{
    type Type = Type;

    fn add_sample(&mut self, sample: Self::Type) -> Self::Type {
        let count = self.means.get_num_samples() as f64;
        let var = sample - self.means.get_last_result();
        let last = self.last_ssq.unwrap_or_default();
        let ussq = last + var * (var / (count + 1.0)) * count;
        self.last_ssq = Some(ussq);
        let _ = self.means.add_sample(sample);
        ussq
    }

    fn get_last_sample(&self) -> Self::Type {
        self.means.get_last_sample()
    }

    fn get_last_result(&self) -> Self::Type {
        self.last_ssq.unwrap_or_default()
    }

    fn get_num_samples(&self) -> u64 {
        self.means.get_num_samples()
    }
}

///
/// Returns the [Biased Variance](https://en.wikipedia.org/wiki/Variance#Biased_sample_variance),
/// which is the Sum of the Squares (SST) scaled by 1/N (the current count)
#[derive(Default, Debug, Copy, Clone)]
pub struct BiasedVariance<T> {
    inner: UnweightedSumOfSquares<T>,
    last_result: Option<T>,
}

impl<T> BiasedVariance<T>
where
    T: Copy + Default,
{
    pub fn get_mean(&self) -> T {
        self.inner.get_mean()
    }
    pub fn get_unweighted_sum_of_squares(&self) -> Option<T> {
        self.inner.last_ssq
    }
    pub fn get_biased_variance(&self) -> Option<T> {
        self.last_result
    }
}

impl<T> StreamingStatistic for BiasedVariance<T>
where
    T: Sub<T, Output = T>
        + Copy
        + Default
        + Div<f64, Output = T>
        + Add<T, Output = T>
        + Mul<f64, Output = T>
        + Mul<T, Output = T>,
{
    type Type = T;

    fn add_sample(&mut self, sample: Self::Type) -> Self::Type {
        let cnt = self.get_num_samples() + 1; // not incremented until add_sample
        let variance = self.inner.add_sample(sample) / cnt as f64;
        self.last_result = Some(variance);
        variance
    }

    fn get_last_sample(&self) -> Self::Type {
        self.inner.get_last_sample()
    }

    fn get_last_result(&self) -> Self::Type {
        self.last_result.unwrap_or_default()
    }

    fn get_num_samples(&self) -> u64 {
        self.inner.get_num_samples()
    }
}

///
/// Returns the [Unbiased Variance](https://en.wikipedia.org/wiki/Variance#Unbiased_sample_variance),
/// which is the Sum of the Squares (SST) scaled by 1/(N-1) (the last count)
#[derive(Default, Debug, Copy, Clone)]
pub struct UnbiasedVariance<T> {
    inner: UnweightedSumOfSquares<T>,
    last_result: Option<T>,
}

impl<T> UnbiasedVariance<T>
where
    T: Copy + Default,
{
    pub fn get_mean(&self) -> T {
        self.inner.get_mean()
    }
    pub fn get_unweighted_sum_of_squares(&self) -> Option<T> {
        self.inner.last_ssq
    }
    pub fn get_unbiased_variance(&self) -> Option<T> {
        self.last_result
    }
}

impl<T> StreamingStatistic for UnbiasedVariance<T>
where
    T: Sub<T, Output = T>
        + Copy
        + Default
        + Div<f64, Output = T>
        + Add<T, Output = T>
        + Mul<f64, Output = T>
        + Mul<T, Output = T>,
{
    type Type = T;

    fn add_sample(&mut self, sample: Self::Type) -> Self::Type {
        let cnt = self.get_num_samples();
        let variance = self.inner.add_sample(sample) / cnt as f64;
        self.last_result = Some(variance);
        variance
    }

    fn get_last_sample(&self) -> Self::Type {
        self.inner.get_last_sample()
    }

    fn get_last_result(&self) -> Self::Type {
        self.last_result.unwrap_or_default()
    }

    fn get_num_samples(&self) -> u64 {
        self.inner.get_num_samples()
    }
}

///
/// Returns the Unbiased Standard Deviation, which is the square root of the unbiased Variance,
/// also known as the 'Sample Standard Deviation'
#[derive(Default, Debug, Copy, Clone)]
pub struct UnbiasedStandardDeviation<T> {
    inner: UnbiasedVariance<T>,
    last_result: Option<T>,
}

impl<T> UnbiasedStandardDeviation<T>
where
    T: Copy + Default,
{
    pub fn get_mean(&self) -> T {
        self.inner.get_mean()
    }
    pub fn get_unweighted_sum_of_squares(&self) -> Option<T> {
        self.inner.get_unweighted_sum_of_squares()
    }
    pub fn get_unbiased_variance(&self) -> Option<T> {
        self.inner.last_result
    }
    pub fn get_unbiased_stdev(&self) -> Option<T> {
        self.last_result
    }
}

impl<T> StreamingStatistic for UnbiasedStandardDeviation<T>
where
    T: Sub<T, Output = T>
        + Copy
        + Default
        + Div<f64, Output = T>
        + Add<T, Output = T>
        + Mul<f64, Output = T>
        + Mul<T, Output = T>
        + FloatExt<Type = T>,
{
    type Type = T;

    fn add_sample(&mut self, sample: Self::Type) -> Self::Type {
        let val = self.inner.add_sample(sample).sqrt();
        self.last_result = Some(val);
        val
    }

    fn get_last_sample(&self) -> Self::Type {
        self.inner.get_last_sample()
    }

    fn get_last_result(&self) -> Self::Type {
        self.last_result.unwrap_or_default()
    }

    fn get_num_samples(&self) -> u64 {
        self.inner.get_num_samples()
    }
}

///
/// Returns the Biased Standard Deviation, which is the square root of the biased Variance,
/// also known as the 'Population Standard Deviation'
#[derive(Default, Debug, Copy, Clone)]
pub struct BiasedStandardDeviation<T> {
    inner: BiasedVariance<T>,
    last_result: Option<T>,
}

impl<T> BiasedStandardDeviation<T>
where
    T: Copy + Default,
{
    pub fn get_mean(&self) -> T {
        self.inner.get_mean()
    }
    pub fn get_unweighted_sum_of_squares(&self) -> Option<T> {
        self.inner.get_unweighted_sum_of_squares()
    }
    pub fn get_biased_variance(&self) -> Option<T> {
        self.inner.get_biased_variance()
    }
    pub fn get_biased_stdev(&self) -> Option<T> {
        self.last_result
    }
}

impl<T> StreamingStatistic for BiasedStandardDeviation<T>
where
    T: Sub<T, Output = T>
        + Copy
        + Default
        + Div<f64, Output = T>
        + Add<T, Output = T>
        + Mul<f64, Output = T>
        + Mul<T, Output = T>
        + FloatExt<Type = T>,
{
    type Type = T;

    fn add_sample(&mut self, sample: Self::Type) -> Self::Type {
        let val = self.inner.add_sample(sample).sqrt();
        self.last_result = Some(val);
        val
    }

    fn get_last_sample(&self) -> Self::Type {
        self.inner.get_last_sample()
    }

    fn get_last_result(&self) -> Self::Type {
        self.last_result.unwrap_or_default()
    }

    fn get_num_samples(&self) -> u64 {
        self.inner.get_num_samples()
    }
}

#[derive(Copy, Clone)]
pub struct Summary<T> {
    mean: Mean<T>,
    min: Min<T>,
    max: Max<T>,
    stdev: UnbiasedStandardDeviation<T>,
}
impl<T: Default> Default for Summary<T> {
    fn default() -> Self {
        Summary {
            mean: Mean::default(),
            min: Min::default(),
            max: Max::default(),
            stdev: UnbiasedStandardDeviation::default(),
        }
    }
}
impl<T> Summary<T>
where
    T: Sub<T, Output = T>
        + PartialOrd
        + Copy
        + Default
        + Div<f64, Output = T>
        + Add<T, Output = T>
        + Mul<f64, Output = T>
        + Mul<T, Output = T>
        + FloatExt<Type = T>,
{
    pub fn add_sample(&mut self, value: T) {
        self.min.add_sample(value);
        self.max.add_sample(value);
        self.stdev.add_sample(value);
        self.mean.add_sample(value);
    }
    #[must_use]
    pub fn mean(&self) -> Option<T> {
        self.mean.get_mean()
    }
    #[must_use]
    pub fn min(&self) -> Option<T> {
        self.min.get_min_val()
    }
    #[must_use]
    pub fn max(&self) -> Option<T> {
        self.max.get_max_val()
    }
    #[must_use]
    pub fn stddev(&self) -> Option<T> {
        self.stdev.get_unbiased_stdev()
    }
    #[must_use]
    pub fn num_samples(&self) -> u64 {
        self.mean.get_num_samples()
    }
}
#[cfg(feature = "std")]
pub struct OneSecondWindows {
    epoch: irox_time::epoch::Epoch,
    windows: alloc::collections::BTreeMap<irox_time::Time64, Summary<f64>>,
}
#[cfg(feature = "std")]
impl OneSecondWindows {
    pub fn new(epoch: irox_time::epoch::Epoch) -> Self {
        Self {
            epoch,
            windows: alloc::collections::BTreeMap::new(),
        }
    }
    pub fn add_sample(&mut self, time: irox_time::Time64, value: f64) {
        let seconds = time.as_epoch(self.epoch).as_only_seconds();
        self.windows.entry(seconds).or_default().add_sample(value);
    }

    pub fn iter(&self) -> impl Iterator<Item = (&irox_time::Time64, &Summary<f64>)> {
        self.windows.iter()
    }
}

#[cfg(test)]
mod test {
    use crate::streaming::*;
    use irox_tools::assert_eq_eps;
    use irox_units::units::duration::Duration;

    #[test]
    pub fn test() {
        let mut mean = MeanF64::default();
        let v = mean.add_sample(0.0);
        assert_eq!(v, 0.0);
        let v = mean.add_sample(1.0);
        assert_eq!(v, 0.5);

        let mut mean = Mean::<Duration>::default();
        let v = mean.add_sample(Duration::from_seconds(1));
        assert_eq!(v, Duration::from_seconds_f64(1.0));
        let v = mean.add_sample(Duration::from_seconds(2));
        assert_eq!(v, Duration::from_seconds_f64(1.5));

        let mut samp_stddev = UnbiasedStandardDeviation::default();
        let mut pop_stddev = BiasedStandardDeviation::default();
        for val in [2, 4, 4, 4, 5, 5, 7, 9] {
            let _ = samp_stddev.add_sample(val as f64);
            let _ = pop_stddev.add_sample(val as f64);
        }
        assert_eq!(5.0, samp_stddev.get_mean());
        assert_eq_eps!(
            4.571428571428571,
            samp_stddev.get_unbiased_variance().unwrap_or_default(),
            1e-15
        );
        assert_eq_eps!(
            2.1380899352993947,
            samp_stddev.get_unbiased_stdev().unwrap_or_default(),
            1e-15
        );

        assert_eq!(5.0, pop_stddev.get_mean());
        assert_eq_eps!(
            4.,
            pop_stddev.get_biased_variance().unwrap_or_default(),
            1e-15
        );
        assert_eq_eps!(2., pop_stddev.get_biased_stdev().unwrap_or_default(), 1e-15);
    }
}