Skip to main content

wickra_core/indicators/
detrended_std_dev.rs

1//! Population standard deviation of residuals from a rolling OLS detrend.
2
3use std::collections::VecDeque;
4
5use crate::error::{Error, Result};
6use crate::traits::Indicator;
7
8/// Detrended (residual) standard deviation over the last `period` inputs.
9///
10/// Over the trailing window indexed `x = 0, 1, …, period − 1` the OLS line
11/// `y = a + b·x` is fitted and the residual sum of squares is then divided
12/// by `n` (population convention):
13///
14/// ```text
15/// slope     = (n·Σxy − Σx·Σy) / (n·Σxx − (Σx)²)
16/// SS_total  = Σy² − n·ȳ²
17/// RSS       = SS_total − slope² · ( denom / n )
18/// DetrendedStdDev = √( RSS / n )
19/// ```
20///
21/// Unlike [`crate::StdDev`], which measures dispersion around the rolling
22/// **mean**, `DetrendedStdDev` measures dispersion around the rolling
23/// **linear trend** — the portion of the price action that is *not*
24/// explained by the local slope. On a strongly trending series this is
25/// much smaller than `StdDev`; on a sideways, mean-reverting series the
26/// two converge.
27///
28/// The divisor is `n` (population), matching the convention of
29/// [`crate::StdDev`]; use [`crate::StandardError`] when you want the
30/// textbook standard error of estimate with `n − 2` residual degrees of
31/// freedom.
32///
33/// Each `update` is O(1) via the same rolling sums as
34/// [`crate::LinearRegression`], plus a running `Σy²`. Floating-point
35/// cancellation noise in the residual is clamped to zero before the square
36/// root.
37///
38/// # Example
39///
40/// ```
41/// use wickra_core::{DetrendedStdDev, Indicator};
42///
43/// let mut indicator = DetrendedStdDev::new(14).unwrap();
44/// let mut last = None;
45/// for i in 0..40 {
46///     last = indicator.update(100.0 + f64::from(i) + (f64::from(i) * 0.3).sin());
47/// }
48/// assert!(last.is_some());
49/// ```
50#[derive(Debug, Clone)]
51pub struct DetrendedStdDev {
52    period: usize,
53    window: VecDeque<f64>,
54    sum_x: f64,
55    /// `n·Σxx − (Σx)²` — OLS denominator, constant in `period`.
56    denom: f64,
57    sum_y: f64,
58    sum_xy: f64,
59    sum_y_sq: f64,
60}
61
62impl DetrendedStdDev {
63    /// Construct a new rolling detrended standard deviation.
64    ///
65    /// # Errors
66    /// Returns [`Error::InvalidPeriod`] if `period < 2` — a regression line
67    /// is undefined for fewer than two points.
68    pub fn new(period: usize) -> Result<Self> {
69        if period < 2 {
70            return Err(Error::InvalidPeriod {
71                message: "detrended stddev needs period >= 2",
72            });
73        }
74        let n = period as f64;
75        let sum_x = n * (n - 1.0) / 2.0;
76        let sum_xx = (n - 1.0) * n * (2.0 * n - 1.0) / 6.0;
77        Ok(Self {
78            period,
79            window: VecDeque::with_capacity(period),
80            sum_x,
81            denom: n * sum_xx - sum_x * sum_x,
82            sum_y: 0.0,
83            sum_xy: 0.0,
84            sum_y_sq: 0.0,
85        })
86    }
87
88    /// Configured period.
89    pub const fn period(&self) -> usize {
90        self.period
91    }
92}
93
94impl Indicator for DetrendedStdDev {
95    type Input = f64;
96    type Output = f64;
97
98    fn update(&mut self, value: f64) -> Option<f64> {
99        if self.window.len() == self.period {
100            let y0 = self.window.pop_front().expect("non-empty");
101            self.sum_xy = self.sum_xy - self.sum_y + y0;
102            self.sum_y -= y0;
103            self.sum_y_sq -= y0 * y0;
104        }
105        let k = self.window.len() as f64;
106        self.window.push_back(value);
107        self.sum_y += value;
108        self.sum_xy += k * value;
109        self.sum_y_sq += value * value;
110
111        if self.window.len() < self.period {
112            return None;
113        }
114        let n = self.period as f64;
115        let slope = (n * self.sum_xy - self.sum_x * self.sum_y) / self.denom;
116        let mean_y = self.sum_y / n;
117        let ss_total = self.sum_y_sq - n * mean_y * mean_y;
118        let s_xx = self.denom / n;
119        let rss = (ss_total - slope * slope * s_xx).max(0.0);
120        Some((rss / n).sqrt())
121    }
122
123    fn reset(&mut self) {
124        self.window.clear();
125        self.sum_y = 0.0;
126        self.sum_xy = 0.0;
127        self.sum_y_sq = 0.0;
128    }
129
130    fn warmup_period(&self) -> usize {
131        self.period
132    }
133
134    fn is_ready(&self) -> bool {
135        self.window.len() == self.period
136    }
137
138    fn name(&self) -> &'static str {
139        "DetrendedStdDev"
140    }
141}
142
143#[cfg(test)]
144mod tests {
145    use super::*;
146    use crate::traits::BatchExt;
147    use approx::assert_relative_eq;
148
149    #[test]
150    fn rejects_period_below_two() {
151        assert!(DetrendedStdDev::new(0).is_err());
152        assert!(DetrendedStdDev::new(1).is_err());
153        assert!(DetrendedStdDev::new(2).is_ok());
154    }
155
156    #[test]
157    fn accessors_and_metadata() {
158        let d = DetrendedStdDev::new(14).unwrap();
159        assert_eq!(d.period(), 14);
160        assert_eq!(d.warmup_period(), 14);
161        assert_eq!(d.name(), "DetrendedStdDev");
162    }
163
164    #[test]
165    fn perfect_line_has_zero_residual() {
166        // Residuals are zero on a perfectly linear series.
167        let prices: Vec<f64> = (0..30).map(|i| 2.0 * f64::from(i) + 5.0).collect();
168        let mut d = DetrendedStdDev::new(10).unwrap();
169        for v in d.batch(&prices).into_iter().flatten() {
170            assert_relative_eq!(v, 0.0, epsilon = 1e-9);
171        }
172    }
173
174    #[test]
175    fn constant_series_yields_zero() {
176        let mut d = DetrendedStdDev::new(5).unwrap();
177        for v in d.batch(&[42.0; 20]).into_iter().flatten() {
178            assert_relative_eq!(v, 0.0, epsilon = 1e-9);
179        }
180    }
181
182    #[test]
183    fn never_exceeds_stddev() {
184        // The detrended residual is the projection of (y - ȳ) orthogonal to
185        // the trend axis, so its norm cannot exceed the raw stddev. Equality
186        // holds iff the OLS slope is exactly zero.
187        let prices: Vec<f64> = (0..60)
188            .map(|i| 50.0 + f64::from(i) * 0.5 + (f64::from(i) * 0.7).sin() * 4.0)
189            .collect();
190        let mut d = DetrendedStdDev::new(14).unwrap();
191        let mut sd = crate::StdDev::new(14).unwrap();
192        for &p in &prices {
193            let (dv, sv) = (d.update(p), sd.update(p));
194            assert_eq!(dv.is_some(), sv.is_some());
195            if let (Some(dv), Some(sv)) = (dv, sv) {
196                assert!(dv <= sv + 1e-9, "detrended {dv} should be <= stddev {sv}");
197            }
198        }
199    }
200
201    #[test]
202    fn reset_clears_state() {
203        let mut d = DetrendedStdDev::new(5).unwrap();
204        d.batch(&[1.0, 2.0, 3.0, 4.0, 5.0]);
205        assert!(d.is_ready());
206        d.reset();
207        assert!(!d.is_ready());
208        assert_eq!(d.update(1.0), None);
209    }
210
211    #[test]
212    fn batch_equals_streaming() {
213        let prices: Vec<f64> = (0..60)
214            .map(|i| 100.0 + (f64::from(i) * 0.4).sin() * 10.0)
215            .collect();
216        let batch = DetrendedStdDev::new(14).unwrap().batch(&prices);
217        let mut b = DetrendedStdDev::new(14).unwrap();
218        let streamed: Vec<_> = prices.iter().map(|p| b.update(*p)).collect();
219        assert_eq!(batch, streamed);
220    }
221}