Skip to main content

wickra_core/indicators/
distance_ssd.rs

1//! Gatev distance (sum of squared deviations) between two normalised series.
2
3use std::collections::VecDeque;
4
5use crate::error::{Error, Result};
6use crate::traits::Indicator;
7
8/// Sum of squared deviations between two price series, normalised to a common
9/// start — the classic Gatev et al. pairs-selection distance.
10///
11/// Each `update` takes one `(a, b)` price pair. Over the trailing window of
12/// `period` pairs each series is rebased to `1` at the window's first bar and
13/// the squared gap between the two normalised paths is summed:
14///
15/// ```text
16/// ãᵢ = aᵢ / a_first        b̃ᵢ = bᵢ / b_first
17/// SSD = Σ (ãᵢ − b̃ᵢ)²
18/// ```
19///
20/// Rebasing puts the two series on the same scale (both start at `1`), so the
21/// distance measures how far their *relative* paths drift apart. A **small**
22/// SSD means the two assets track each other tightly — the screen Gatev,
23/// Goetzmann and Rouwenhorst use to pick tradeable pairs; a large SSD means
24/// they have decoupled. The output is always `≥ 0`. If either series is `0` at
25/// the start of the window the normalisation is undefined and the indicator
26/// returns `0`.
27///
28/// Each `update` is `O(period)`, bounded by the fixed window.
29///
30/// # Example
31///
32/// ```
33/// use wickra_core::{DistanceSsd, Indicator};
34///
35/// let mut d = DistanceSsd::new(20).unwrap();
36/// let mut last = None;
37/// for t in 0..40 {
38///     let base = 100.0 + f64::from(t);
39///     // Two near-identical paths ⇒ tiny distance.
40///     last = d.update((base, base * 1.0001));
41/// }
42/// assert!(last.unwrap() < 1e-3);
43/// ```
44#[derive(Debug, Clone)]
45pub struct DistanceSsd {
46    period: usize,
47    window: VecDeque<(f64, f64)>,
48}
49
50impl DistanceSsd {
51    /// Construct a new Gatev distance estimator.
52    ///
53    /// # Errors
54    /// Returns [`Error::InvalidPeriod`] if `period < 2` — a distance needs at
55    /// least two points.
56    pub fn new(period: usize) -> Result<Self> {
57        if period < 2 {
58            return Err(Error::InvalidPeriod {
59                message: "distance SSD needs period >= 2",
60            });
61        }
62        Ok(Self {
63            period,
64            window: VecDeque::with_capacity(period),
65        })
66    }
67
68    /// Configured look-back window.
69    pub const fn period(&self) -> usize {
70        self.period
71    }
72}
73
74impl Indicator for DistanceSsd {
75    type Input = (f64, f64);
76    type Output = f64;
77
78    fn update(&mut self, input: (f64, f64)) -> Option<f64> {
79        if self.window.len() == self.period {
80            self.window.pop_front();
81        }
82        self.window.push_back(input);
83        if self.window.len() < self.period {
84            return None;
85        }
86        let &(a_first, b_first) = self.window.front().expect("window is full");
87        if a_first == 0.0 || b_first == 0.0 {
88            // Cannot rebase a series that starts at zero.
89            return Some(0.0);
90        }
91        let ssd = self
92            .window
93            .iter()
94            .map(|&(a, b)| {
95                let gap = a / a_first - b / b_first;
96                gap * gap
97            })
98            .sum();
99        Some(ssd)
100    }
101
102    fn reset(&mut self) {
103        self.window.clear();
104    }
105
106    fn warmup_period(&self) -> usize {
107        self.period
108    }
109
110    fn is_ready(&self) -> bool {
111        self.window.len() == self.period
112    }
113
114    fn name(&self) -> &'static str {
115        "DistanceSsd"
116    }
117}
118
119#[cfg(test)]
120mod tests {
121    use super::*;
122    use crate::traits::BatchExt;
123    use approx::assert_relative_eq;
124
125    #[test]
126    fn rejects_period_below_two() {
127        assert!(DistanceSsd::new(1).is_err());
128        assert!(DistanceSsd::new(2).is_ok());
129    }
130
131    #[test]
132    fn accessors_and_metadata() {
133        let d = DistanceSsd::new(20).unwrap();
134        assert_eq!(d.period(), 20);
135        assert_eq!(d.warmup_period(), 20);
136        assert_eq!(d.name(), "DistanceSsd");
137        assert!(!d.is_ready());
138    }
139
140    #[test]
141    fn warmup_returns_none() {
142        let mut d = DistanceSsd::new(3).unwrap();
143        assert_eq!(d.update((1.0, 1.0)), None);
144        assert_eq!(d.update((2.0, 2.0)), None);
145        assert!(d.update((3.0, 3.0)).is_some());
146        assert!(d.is_ready());
147    }
148
149    #[test]
150    fn identical_normalised_paths_have_zero_distance() {
151        // b = 2·a ⇒ both rebase to the same path ⇒ SSD = 0.
152        let pairs: Vec<(f64, f64)> = (0..20)
153            .map(|t| {
154                let a = 100.0 + f64::from(t);
155                (a, 2.0 * a)
156            })
157            .collect();
158        let last = DistanceSsd::new(10)
159            .unwrap()
160            .batch(&pairs)
161            .into_iter()
162            .flatten()
163            .last()
164            .unwrap();
165        assert_relative_eq!(last, 0.0, epsilon = 1e-12);
166    }
167
168    #[test]
169    fn diverging_paths_have_positive_distance() {
170        let pairs: Vec<(f64, f64)> = (0..20)
171            .map(|t| (100.0 + f64::from(t), 100.0 + 3.0 * f64::from(t)))
172            .collect();
173        let last = DistanceSsd::new(10)
174            .unwrap()
175            .batch(&pairs)
176            .into_iter()
177            .flatten()
178            .last()
179            .unwrap();
180        assert!(last > 0.0, "ssd {last}");
181    }
182
183    #[test]
184    fn hand_computed_value() {
185        // Window of three pairs, a_first = b_first = 1:
186        //   (1,1) → 0; (2,4) → (2−4)² = 4; (3,9) → (3−9)² = 36 ⇒ SSD = 40.
187        let pairs = [(1.0, 1.0), (2.0, 4.0), (3.0, 9.0)];
188        let last = DistanceSsd::new(3)
189            .unwrap()
190            .batch(&pairs)
191            .into_iter()
192            .flatten()
193            .last()
194            .unwrap();
195        assert_relative_eq!(last, 40.0, epsilon = 1e-12);
196    }
197
198    #[test]
199    fn zero_start_returns_zero() {
200        // First bar of the window has a = 0 ⇒ rebasing undefined ⇒ 0.
201        let pairs = [(0.0, 1.0), (2.0, 2.0), (3.0, 3.0)];
202        let last = DistanceSsd::new(3)
203            .unwrap()
204            .batch(&pairs)
205            .into_iter()
206            .flatten()
207            .last()
208            .unwrap();
209        assert_eq!(last, 0.0);
210    }
211
212    #[test]
213    fn reset_clears_state() {
214        let mut d = DistanceSsd::new(4).unwrap();
215        d.batch(&[(1.0, 1.0), (2.0, 2.0), (3.0, 4.0), (4.0, 5.0), (5.0, 6.0)]);
216        assert!(d.is_ready());
217        d.reset();
218        assert!(!d.is_ready());
219        assert_eq!(d.update((1.0, 1.0)), None);
220    }
221
222    #[test]
223    fn batch_equals_streaming() {
224        let pairs: Vec<(f64, f64)> = (0..60)
225            .map(|t| {
226                let a = 100.0 + f64::from(t);
227                (a, 100.0 + 1.2 * f64::from(t) + (f64::from(t) * 0.5).sin())
228            })
229            .collect();
230        let batch = DistanceSsd::new(15).unwrap().batch(&pairs);
231        let mut d = DistanceSsd::new(15).unwrap();
232        let streamed: Vec<_> = pairs.iter().map(|p| d.update(*p)).collect();
233        assert_eq!(batch, streamed);
234    }
235}