Skip to main content

wickra_core/indicators/
z_score.rs

1//! Z-Score.
2
3use std::collections::VecDeque;
4
5use crate::error::{Error, Result};
6use crate::traits::Indicator;
7
8/// Z-Score — how many standard deviations the latest price sits from its
9/// rolling mean.
10///
11/// ```text
12/// ZScore = (price − SMA(price, n)) / population_stddev(price, n)
13/// ```
14///
15/// A reading of `+2` means price is two standard deviations above its recent
16/// average — statistically stretched to the upside; `−2` is the mirror. It is
17/// the standard normalisation behind mean-reversion strategies: a large
18/// magnitude flags an extension, a return toward `0` flags reversion. A window
19/// with zero dispersion (a flat series) yields `0`.
20///
21/// # Example
22///
23/// ```
24/// use wickra_core::{Indicator, ZScore};
25///
26/// let mut indicator = ZScore::new(20).unwrap();
27/// let mut last = None;
28/// for i in 0..80 {
29///     last = indicator.update(f64::from(i));
30/// }
31/// assert!(last.is_some());
32/// ```
33#[derive(Debug, Clone)]
34pub struct ZScore {
35    period: usize,
36    window: VecDeque<f64>,
37    sum: f64,
38    sum_sq: f64,
39}
40
41impl ZScore {
42    /// Construct a new Z-Score over a rolling window of `period` prices.
43    ///
44    /// # Errors
45    /// Returns [`Error::PeriodZero`] if `period == 0`.
46    pub fn new(period: usize) -> Result<Self> {
47        if period == 0 {
48            return Err(Error::PeriodZero);
49        }
50        Ok(Self {
51            period,
52            window: VecDeque::with_capacity(period),
53            sum: 0.0,
54            sum_sq: 0.0,
55        })
56    }
57
58    /// Configured period.
59    pub const fn period(&self) -> usize {
60        self.period
61    }
62}
63
64impl Indicator for ZScore {
65    type Input = f64;
66    type Output = f64;
67
68    fn update(&mut self, value: f64) -> Option<f64> {
69        if self.window.len() == self.period {
70            let old = self.window.pop_front().expect("non-empty");
71            self.sum -= old;
72            self.sum_sq -= old * old;
73        }
74        self.window.push_back(value);
75        self.sum += value;
76        self.sum_sq += value * value;
77        if self.window.len() < self.period {
78            return None;
79        }
80        let n = self.period as f64;
81        let mean = self.sum / n;
82        // Population variance E[x²] − E[x]²; clamp away tiny negative drift.
83        let variance = (self.sum_sq / n - mean * mean).max(0.0);
84        let std = variance.sqrt();
85        if std == 0.0 {
86            // A window with no dispersion: the price is exactly its own mean.
87            return Some(0.0);
88        }
89        Some((value - mean) / std)
90    }
91
92    fn reset(&mut self) {
93        self.window.clear();
94        self.sum = 0.0;
95        self.sum_sq = 0.0;
96    }
97
98    fn warmup_period(&self) -> usize {
99        self.period
100    }
101
102    fn is_ready(&self) -> bool {
103        self.window.len() == self.period
104    }
105
106    fn name(&self) -> &'static str {
107        "ZScore"
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114    use crate::traits::BatchExt;
115    use approx::assert_relative_eq;
116
117    #[test]
118    fn reference_values() {
119        // Window [1, 3]: mean 2, population variance (1 + 9)/2 − 4 = 1,
120        // stddev 1; the latest price 3 is (3 − 2) / 1 = 1 stddev above.
121        let mut z = ZScore::new(2).unwrap();
122        let out = z.batch(&[1.0, 3.0]);
123        assert!(out[0].is_none());
124        assert_relative_eq!(out[1].unwrap(), 1.0, epsilon = 1e-12);
125    }
126
127    #[test]
128    fn constant_series_yields_zero() {
129        let mut z = ZScore::new(10).unwrap();
130        for v in z.batch(&[42.0; 30]).into_iter().flatten() {
131            assert_relative_eq!(v, 0.0, epsilon = 1e-12);
132        }
133    }
134
135    #[test]
136    fn rising_price_is_above_its_mean() {
137        // A monotonically rising series always sits above its trailing mean.
138        let prices: Vec<f64> = (0..40).map(f64::from).collect();
139        let mut z = ZScore::new(10).unwrap();
140        for v in z.batch(&prices).into_iter().flatten() {
141            assert!(
142                v > 0.0,
143                "a rising price should score above its mean, got {v}"
144            );
145        }
146    }
147
148    #[test]
149    fn first_value_on_period_th_input() {
150        let mut z = ZScore::new(5).unwrap();
151        let out = z.batch(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
152        for (i, v) in out.iter().enumerate().take(4) {
153            assert!(v.is_none(), "index {i} must be None during warmup");
154        }
155        assert!(out[4].is_some(), "first value lands at index period - 1");
156        assert_eq!(z.warmup_period(), 5);
157    }
158
159    #[test]
160    fn rejects_zero_period() {
161        assert!(ZScore::new(0).is_err());
162    }
163
164    /// Cover the const accessor `period` (59-61) and the Indicator-impl
165    /// `name` body (106-108). `warmup_period` is exercised elsewhere.
166    #[test]
167    fn accessors_and_metadata() {
168        let z = ZScore::new(20).unwrap();
169        assert_eq!(z.period(), 20);
170        assert_eq!(z.name(), "ZScore");
171    }
172
173    #[test]
174    fn reset_clears_state() {
175        let mut z = ZScore::new(5).unwrap();
176        z.batch(&[1.0, 2.0, 3.0, 4.0, 5.0]);
177        assert!(z.is_ready());
178        z.reset();
179        assert!(!z.is_ready());
180        assert_eq!(z.update(1.0), None);
181    }
182
183    #[test]
184    fn batch_equals_streaming() {
185        let prices: Vec<f64> = (0..60)
186            .map(|i| 50.0 + (f64::from(i) * 0.3).sin() * 10.0)
187            .collect();
188        let mut a = ZScore::new(20).unwrap();
189        let mut b = ZScore::new(20).unwrap();
190        assert_eq!(
191            a.batch(&prices),
192            prices.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
193        );
194    }
195}