Skip to main content

sliding_features/sliding_windows/
cyber_cycle.rs

1//! John Ehlers Cyber Cycle Indicator
2//! from: <https://www.mesasoftware.com/papers/TheInverseFisherTransform.pdf>
3
4use std::{
5    collections::VecDeque,
6    num::NonZeroUsize,
7};
8
9use getset::CopyGetters;
10use num::Float;
11
12use crate::View;
13
14/// John Ehlers Cyber Cycle Indicator
15/// from: <https://www.mesasoftware.com/papers/TheInverseFisherTransform.pdf>
16#[derive(Clone, Debug, CopyGetters)]
17pub struct CyberCycle<T, V> {
18    view: V,
19    /// The sliding window length
20    #[getset(get_copy = "pub")]
21    window_len: NonZeroUsize,
22    alpha: T,
23    vals: VecDeque<T>,
24    out: VecDeque<T>,
25    // avoid allocation in `update` step by re-using this buffer.
26    smooth: Vec<T>,
27}
28
29impl<T, V> CyberCycle<T, V>
30where
31    V: View<T>,
32    T: Float,
33{
34    /// Create a new Cyber Cycle Indicator with a chained View
35    /// and a given window length
36    #[inline]
37    pub fn new(view: V, window_len: NonZeroUsize) -> Self {
38        CyberCycle {
39            view,
40            window_len,
41            alpha: T::from(2.0).expect("can convert")
42                / (T::from(window_len.get()).expect("can convert") + T::one()),
43            vals: VecDeque::with_capacity(window_len.get()),
44            out: VecDeque::with_capacity(window_len.get()),
45            smooth: vec![T::zero(); window_len.get()],
46        }
47    }
48}
49
50impl<T, V> View<T> for CyberCycle<T, V>
51where
52    V: View<T>,
53    T: Float,
54{
55    fn update(&mut self, val: T) {
56        debug_assert!(val.is_finite(), "value must be finite");
57        self.view.update(val);
58        let Some(val) = self.view.last() else { return };
59        debug_assert!(val.is_finite(), "value must be finite");
60
61        if self.vals.len() >= self.window_len.get() {
62            self.vals.pop_front();
63            self.out.pop_front();
64        }
65        self.vals.push_back(val);
66
67        if self.vals.len() < self.window_len.get() {
68            self.out.push_back(T::zero());
69            return;
70        }
71        let last = self.vals.len() - 1;
72        let two = T::from(2.0).expect("can convert");
73        for (i, v) in self
74            .smooth
75            .iter_mut()
76            .enumerate()
77            .take(self.vals.len())
78            .skip(3)
79        {
80            *v = (val
81                + two * *self.vals.get(i - 1).unwrap()
82                + two * *self.vals.get(i - 2).unwrap()
83                + *self.vals.get(i - 3).unwrap())
84                / T::from(6.0).expect("can convert")
85        }
86        let cc = (T::one() - T::from(0.5).expect("can convert") * self.alpha).powi(2)
87            * (self.smooth[last] - two * self.smooth[last - 1] + self.smooth[last - 2])
88            + two * (T::one() - self.alpha) * *self.out.get(last - 1).unwrap()
89            - (T::one() - self.alpha).powi(2) * *self.out.get(last - 2).unwrap();
90        debug_assert!(cc.is_finite(), "value must be finite");
91        self.out.push_back(cc);
92    }
93
94    #[inline(always)]
95    fn last(&self) -> Option<T> {
96        self.out.back().copied()
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103    use crate::{
104        plot::plot_values,
105        pure_functions::Echo,
106        test_data::TEST_DATA,
107    };
108
109    #[test]
110    fn cyber_cycle_plot() {
111        let mut cc = CyberCycle::new(Echo::new(), NonZeroUsize::new(16).unwrap());
112        let mut out: Vec<f64> = Vec::new();
113        for v in &TEST_DATA {
114            cc.update(*v);
115            out.push(cc.last().unwrap());
116        }
117        let filename = "img/cyber_cycle.png";
118        plot_values(out, filename).unwrap();
119    }
120}