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
pub struct EWMA { val: f64, alpha: f64, } #[allow(dead_code)] impl EWMA { pub fn new(alpha: f64) -> EWMA { EWMA{ val: 0.0, alpha: alpha } } pub fn value(&self) -> f64 { return self.val; } pub fn add(&mut self, new_val: f64) -> f64 { self.val = self.alpha*new_val + (1.0-self.alpha)*self.val; return self.val; } } #[cfg(test)] mod tests { #[test] fn ewma() { let mut ewma = ::EWMA::new(0.1); assert_eq!(ewma.value(), 0.0); assert_eq!(ewma.add(0.5), 0.05); } }