wickra_core/indicators/
cybernetic_cycle.rs1#![allow(clippy::doc_markdown)]
3
4use crate::error::{Error, Result};
5use crate::traits::Indicator;
6
7#[derive(Debug, Clone)]
39pub struct CyberneticCycle {
40 period: usize,
41 alpha: f64,
42 in_buf: [Option<f64>; 4],
43 smooth_buf: [Option<f64>; 3],
44 cycle_buf: [Option<f64>; 3],
45 count: usize,
46 last_value: Option<f64>,
47}
48
49impl CyberneticCycle {
50 pub fn new(period: usize) -> Result<Self> {
56 if period == 0 {
57 return Err(Error::PeriodZero);
58 }
59 let alpha = 2.0 / (period as f64 + 1.0);
60 Ok(Self {
61 period,
62 alpha,
63 in_buf: [None; 4],
64 smooth_buf: [None; 3],
65 cycle_buf: [None; 3],
66 count: 0,
67 last_value: None,
68 })
69 }
70
71 pub const fn period(&self) -> usize {
73 self.period
74 }
75
76 pub const fn alpha(&self) -> f64 {
78 self.alpha
79 }
80
81 pub const fn value(&self) -> Option<f64> {
83 self.last_value
84 }
85
86 fn push3(buf: &mut [Option<f64>; 3], x: f64) {
88 buf[2] = buf[1];
89 buf[1] = buf[0];
90 buf[0] = Some(x);
91 }
92 fn push4(buf: &mut [Option<f64>; 4], x: f64) {
93 buf[3] = buf[2];
94 buf[2] = buf[1];
95 buf[1] = buf[0];
96 buf[0] = Some(x);
97 }
98}
99
100impl Indicator for CyberneticCycle {
101 type Input = f64;
102 type Output = f64;
103
104 fn update(&mut self, input: f64) -> Option<f64> {
105 if !input.is_finite() {
106 return self.last_value;
107 }
108 self.count += 1;
109 Self::push4(&mut self.in_buf, input);
110
111 let smooth = if let (Some(a), Some(b), Some(c), Some(d)) = (
113 self.in_buf[0],
114 self.in_buf[1],
115 self.in_buf[2],
116 self.in_buf[3],
117 ) {
118 (a + 2.0 * b + 2.0 * c + d) / 6.0
119 } else {
120 input
122 };
123 Self::push3(&mut self.smooth_buf, smooth);
124
125 let one_minus_half_alpha = 1.0 - self.alpha / 2.0;
127 let one_minus_alpha = 1.0 - self.alpha;
128 let drv = one_minus_half_alpha * one_minus_half_alpha;
129
130 let cycle = if let (Some(s0), Some(s1), Some(s2), Some(c1), Some(c2)) = (
136 self.smooth_buf[0],
137 self.smooth_buf[1],
138 self.smooth_buf[2],
139 self.cycle_buf[0],
140 self.cycle_buf[1],
141 ) {
142 drv * (s0 - 2.0 * s1 + s2) + 2.0 * one_minus_alpha * c1
143 - one_minus_alpha * one_minus_alpha * c2
144 } else {
145 let (x0, x1, x2) = (
146 self.in_buf[0].unwrap_or(input),
147 self.in_buf[1].unwrap_or(input),
148 self.in_buf[2].unwrap_or(input),
149 );
150 (x0 - 2.0 * x1 + x2) / 4.0
151 };
152
153 Self::push3(&mut self.cycle_buf, cycle);
154 self.last_value = Some(cycle);
155 Some(cycle)
156 }
157
158 fn reset(&mut self) {
159 self.in_buf = [None; 4];
160 self.smooth_buf = [None; 3];
161 self.cycle_buf = [None; 3];
162 self.count = 0;
163 self.last_value = None;
164 }
165
166 fn warmup_period(&self) -> usize {
167 1
168 }
169
170 fn is_ready(&self) -> bool {
171 self.last_value.is_some()
172 }
173
174 fn name(&self) -> &'static str {
175 "CyberneticCycle"
176 }
177}
178
179#[cfg(test)]
180mod tests {
181 use super::*;
182 use crate::traits::BatchExt;
183 use approx::assert_relative_eq;
184
185 #[test]
186 fn new_rejects_zero_period() {
187 assert!(matches!(CyberneticCycle::new(0), Err(Error::PeriodZero)));
188 }
189
190 #[test]
191 fn accessors_and_metadata() {
192 let mut cc = CyberneticCycle::new(10).unwrap();
193 assert_eq!(cc.period(), 10);
194 assert_relative_eq!(cc.alpha(), 2.0 / 11.0, epsilon = 1e-15);
195 assert_eq!(cc.warmup_period(), 1);
196 assert_eq!(cc.name(), "CyberneticCycle");
197 assert!(!cc.is_ready());
198 cc.update(100.0);
199 assert!(cc.is_ready());
200 }
201
202 #[test]
203 fn constant_series_converges_to_zero() {
204 let mut cc = CyberneticCycle::new(10).unwrap();
205 let out = cc.batch(&[50.0_f64; 200]);
206 for x in out.iter().skip(50).flatten() {
207 assert_relative_eq!(*x, 0.0, epsilon = 1e-9);
208 }
209 }
210
211 #[test]
212 fn batch_equals_streaming() {
213 let prices: Vec<f64> = (0..120)
214 .map(|i| 100.0 + (f64::from(i) * 0.25).sin() * 5.0)
215 .collect();
216 let mut a = CyberneticCycle::new(15).unwrap();
217 let mut b = CyberneticCycle::new(15).unwrap();
218 let batch = a.batch(&prices);
219 let streamed: Vec<_> = prices.iter().map(|p| b.update(*p)).collect();
220 assert_eq!(batch, streamed);
221 }
222
223 #[test]
224 fn ignores_non_finite_input() {
225 let mut cc = CyberneticCycle::new(10).unwrap();
226 cc.batch(&(1..=30).map(f64::from).collect::<Vec<_>>());
227 let before = cc.value();
228 assert!(before.is_some());
229 assert_eq!(cc.update(f64::NAN), before);
230 }
231
232 #[test]
233 fn reset_clears_state() {
234 let mut cc = CyberneticCycle::new(10).unwrap();
235 cc.batch(&(1..=30).map(f64::from).collect::<Vec<_>>());
236 assert!(cc.is_ready());
237 cc.reset();
238 assert!(!cc.is_ready());
239 }
240}