```  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
```
```
use core::fmt::Debug;

use num_traits::{float::Float, identities::Zero, identities::One, cast::FromPrimitive};

use serde::{Serialize, Deserialize};

/// Stats object calculates continuous min/max/mean/deviation for tracking of time varying statistics.
///
/// See: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_Online_algorithm for
/// Details of the underlying algorithm.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Stats<T: Float + Zero + One + AddAssign + FromPrimitive + PartialEq + Debug> {
/// Minimum value
pub min:     T,
/// Maximum value
pub max:     T,
/// Mean of sample set
pub mean:    T,
/// Standard deviation of sample
pub std_dev: T,

/// Number of values collected
#[serde(skip)]
pub count: usize,

/// Internal mean squared for algo
#[serde(skip)]
mean2:   T,
}

use core::fmt;

impl <T> fmt::Display for Stats<T>
where
T: fmt::Display + Float + Zero + One + AddAssign + FromPrimitive + PartialEq + Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let precision = f.precision().unwrap_or(2);

write!(f, "(avg: {:.precision\$}, std_dev: {:.precision\$}, min: {:.precision\$}, max: {:.precision\$}, count: {})", self.mean, self.std_dev, self.min, self.max, self.count, precision=precision)
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Info {

}

impl <T> Stats<T>
where
T: Float + Zero + One + AddAssign + FromPrimitive + PartialEq + Debug,
{
/// Create a new stats object
pub fn new() -> Stats<T> {
Stats{count: 0, min: T::zero(), max: T::zero(), mean: T::zero(), std_dev: T::zero(), mean2: T::zero()}
}

/// Update the stats object
pub fn update(&mut self, value: T) {
// Track min and max
if value > self.max || self.count == 0 {
self.max = value;
}
if value < self.min || self.count == 0 {
self.min = value;
}

// Increment counter
self.count += 1;
let count = T::from_usize(self.count).unwrap();

// Calculate mean
let delta: T = value - self.mean;
self.mean += delta / count;

// Mean2 used internally for standard deviation calculation
let delta2: T = value - self.mean;
self.mean2 += delta * delta2;

// Calculate standard deviation
if self.count > 1 {
self.std_dev = (self.mean2 / (count - T::one())).sqrt();
}
}

/// Merge a set of stats objects for analysis
/// This performs a weighted averaging across the provided stats object, the output
/// object should not be updated further.
pub fn merge<S: Iterator<Item=Stats<T>>>(stats: S) -> Stats<T> {
let mut merged = Stats::new();

for s in stats {
// Track min and max
if s.max > merged.max || merged.count == 0 {
merged.max = s.max;
}
if s.min < merged.min || merged.count == 0 {
merged.min = s.min;
}

let merged_count = T::from_usize(merged.count).unwrap();
let s_count = T::from_usize(s.count).unwrap();

if merged.count > 0 {
merged.mean = (merged.mean * merged_count + s.mean * s_count) / (merged_count + s_count);
merged.std_dev = (merged.std_dev * merged_count + s.std_dev * s_count) / (merged_count + s_count);
merged.count += s.count;
} else {
merged.mean = s.mean;
merged.std_dev = s.std_dev;
merged.count = s.count;
}
}

merged
}
}

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

use float_cmp::ApproxEqUlps;

#[test]
fn it_works() {
let mut s: Stats<f32> = Stats::new();

let vals: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0];
for v in &vals {
s.update(*v);
}

assert_eq!(s.count, vals.len());

assert_eq!(s.min, 1.0);
assert_eq!(s.max, 5.0);

assert!(s.mean.approx_eq_ulps(&3.0, 2));
assert!(s.std_dev.approx_eq_ulps(&1.5811388, 2));
}
}
```