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
use crate::sliding_window::View;
use crate::{Echo, WelfordOnline};

/// Variance Stabilizing Transform uses the standard deviation to normalize values
#[derive(Clone)]
pub struct VST {
    view: Box<dyn View>,
    last: f64,
    welford_online: WelfordOnline,
}

impl VST {
    /// Create a new Variance Stabilizing Transform with a chained View
    pub fn new(view: Box<dyn View>) -> Self {
        Self {
            view,
            last: 0.0,
            welford_online: WelfordOnline::new_final(),
        }
    }

    /// Create a new Variance Stabilizing Transform with the default Echo View
    pub fn new_final() -> Self {
        Self::new(Box::new(Echo::new()))
    }
}

impl View for VST {
    fn update(&mut self, val: f64) {
        self.view.update(val);
        let view_last: f64 = self.view.last();

        self.welford_online.update(view_last);
        self.last = view_last;
    }

    fn last(&self) -> f64 {
        let std_dev = self.welford_online.last();
        if std_dev == 0.0 {
            return 0.0;
        }
        self.last / std_dev
    }
}