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
//! Variance Stabilizing Transform uses the standard deviation to normalize values

use crate::View;
use crate::{Echo, WelfordOnline};

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

impl<V> std::fmt::Debug for VST<V>
where
    V: View,
{
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(fmt, "VST.last: {}", self.last)
    }
}

impl<V> VST<V>
where
    V: View,
{
    /// Create a new Variance Stabilizing Transform with a chained View
    /// and a given window length for computing standard deviation
    #[inline]
    pub fn new(view: V, window_len: usize) -> Self {
        Self {
            view,
            last: 0.0,
            welford_online: WelfordOnline::new(Echo::new(), window_len),
        }
    }
}

impl<V> View for VST<V>
where
    V: View,
{
    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 self.last;
        }
        self.last / std_dev
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::plot::plot_values;
    use crate::test_data::TEST_DATA;

    #[test]
    fn variance_stabilizing_transform_plot() {
        let mut tf = VST::new(Echo::new(), 16);
        let mut out: Vec<f64> = Vec::new();
        for v in &TEST_DATA {
            tf.update(*v);
            out.push(tf.last());
        }
        let filename = "img/trend_flex.png";
        plot_values(out, filename).unwrap();
    }
}