nexus-stats-detection 2.0.0

Advanced change detection and signal analysis for nexus-stats
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Checking var_product <= 0.0 is intentional: zero variance means
// correlation is undefined. This is exact, not approximate.
#![allow(clippy::float_cmp)]

// Online Cross-Correlation — Two-Stream, Multi-Lag
//
// Cross-correlation between stream A and stream B at lags 0..lag-1.
// "Does A at time t-k correlate with B at time t?"
//
// Maintains a circular buffer for stream A's history, per-stream
// Welford accumulators, and per-lag cross-moment accumulators.
//
// r_AB(k) = C_AB(k) / sqrt(Var(A) * Var(B))

extern crate alloc;
use alloc::boxed::Box;
use alloc::vec;

/// Online cross-correlation between two streams at multiple lags.
///
/// Tracks the Pearson correlation between stream A (lagged by 0..lag-1
/// steps) and stream B at the current time. Uses Welford-style
/// running accumulators for numerical stability.
///
/// "Does A at time t-k predict B at time t?"
///
/// # Use Cases
/// - Lead/lag detection between two price series
/// - Identifying which signal predicts another
/// - Measuring coupling strength between two metrics
///
/// # Complexity
/// - O(lag) per update, heap-allocated buffers.
///
/// # Examples
///
/// ```
/// use nexus_stats_detection::signal::CrossCorrelationF64;
///
/// // B = A shifted by 3 steps
/// let mut cc = CrossCorrelationF64::builder().lag(10).build().unwrap();
/// let a: Vec<f64> = (0..500).map(|i| (i as f64).sin()).collect();
/// for i in 0..500 {
///     let b = if i >= 3 { a[i - 3] } else { 0.0 };
///     cc.update(a[i], b).unwrap();
/// }
/// // Peak correlation should be near lag 3
/// if let Some(peak) = cc.peak_lag() {
///     assert!((peak as i32 - 3).unsigned_abs() <= 2);
/// }
/// ```
#[derive(Debug, Clone)]
pub struct CrossCorrelationF64 {
    buffer_a: Box<[f64]>,
    cross_m: Box<[f64]>,
    lag: usize,
    head: usize,
    count: u64,
    mean_a: f64,
    mean_b: f64,
    m2_a: f64,
    m2_b: f64,
}

/// Builder for [`CrossCorrelationF64`].
#[derive(Debug, Clone)]
pub struct CrossCorrelationF64Builder {
    lag: Option<usize>,
}

impl CrossCorrelationF64 {
    /// Creates a builder.
    #[inline]
    #[must_use]
    pub fn builder() -> CrossCorrelationF64Builder {
        CrossCorrelationF64Builder { lag: None }
    }

    /// Feeds paired observations from both streams.
    ///
    /// # Errors
    ///
    /// Returns `DataError::NotANumber` if either value is NaN, or
    /// `DataError::Infinite` if either value is infinite.
    #[inline]
    pub fn update(&mut self, a: f64, b: f64) -> Result<(), nexus_stats_core::DataError> {
        check_finite!(a);
        check_finite!(b);
        self.count += 1;
        let n = self.count as f64;
        let lag = self.lag;

        // Capture old means for Welford co-moment
        let da_old = a - self.mean_a;
        let db_old = b - self.mean_b;

        // Welford mean + variance updates
        self.mean_a += da_old / n;
        let da_new = a - self.mean_a;
        self.m2_a += da_old * da_new;

        self.mean_b += db_old / n;
        let db_new = b - self.mean_b;
        self.m2_b += db_old * db_new;

        // Lag 0: exact Welford co-moment (old_delta_a * new_residual_b)
        self.cross_m[0] += da_old * db_new;

        // Lags 1..lag-1: use buffered A values (approximate,
        // error is O(1/n) per step — fine for streaming)
        if self.count > 1 {
            let filled = (self.count - 1).min(lag as u64) as usize;
            for k in 1..filled.min(lag) {
                let idx = (self.head + lag - k) % lag;
                let a_lagged = self.buffer_a[idx];
                self.cross_m[k] += (a_lagged - self.mean_a) * db_new;
            }
        }

        // Store A in circular buffer
        self.buffer_a[self.head] = a;
        self.head = (self.head + 1) % lag;
        Ok(())
    }

    /// Cross-correlation at the given lag, or `None` if not primed.
    ///
    /// Returns the Pearson correlation between A(t-lag) and B(t).
    /// Values in \[-1, 1\]. Returns `None` if either stream has zero
    /// variance.
    #[cfg(any(feature = "std", feature = "libm"))]
    #[inline]
    #[must_use]
    pub fn correlation(&self, lag: usize) -> Option<f64> {
        if lag >= self.lag {
            return None;
        }
        if self.count < (lag as u64 + 2) {
            return None;
        }
        let var_product = self.m2_a * self.m2_b;
        if var_product <= 0.0 {
            return None;
        }
        // Lag 0: cross_m and m2_a/m2_b have the same number of
        // contributing samples — no scaling needed.
        // Lags > 0: cross_m[lag] has (count - lag) pairs while
        // m2 has (count - 1) samples. Scale to normalize.
        let scale = if lag == 0 {
            1.0
        } else {
            let n_pairs = (self.count - lag as u64) as f64;
            let n_samples = (self.count - 1) as f64;
            n_samples / n_pairs
        };
        let denom = nexus_stats_core::math::sqrt(var_product);
        Some(self.cross_m[lag] * scale / denom)
    }

    /// The lag (0..max_lag) with the strongest absolute correlation,
    /// or `None` if not primed.
    #[cfg(any(feature = "std", feature = "libm"))]
    #[inline]
    #[must_use]
    pub fn peak_lag(&self) -> Option<usize> {
        if self.count < 2 {
            return None;
        }
        let var_product = self.m2_a * self.m2_b;
        if var_product <= 0.0 {
            return None;
        }

        let mut best_lag = 0;
        let mut best_abs = 0.0;
        let max_lag = (self.count - 1).min(self.lag as u64) as usize;
        let n_samples = (self.count - 1) as f64;

        for k in 0..max_lag {
            let normalized = if k == 0 {
                self.cross_m[k]
            } else {
                let n_pairs = (self.count - k as u64) as f64;
                self.cross_m[k] * n_samples / n_pairs
            };
            let abs_cm = normalized.abs();
            if abs_cm > best_abs {
                best_abs = abs_cm;
                best_lag = k;
            }
        }

        Some(best_lag)
    }

    /// Raw cross-covariance at the given lag, or `None` if not primed.
    #[inline]
    #[must_use]
    pub fn covariance(&self, lag: usize) -> Option<f64> {
        if lag >= self.lag {
            return None;
        }
        if self.count < (lag as u64 + 2) {
            return None;
        }
        let n_pairs = (self.count - lag as u64) as f64;
        Some(self.cross_m[lag] / n_pairs)
    }

    /// The configured maximum lag.
    #[inline]
    #[must_use]
    pub fn lag(&self) -> usize {
        self.lag
    }

    /// Number of paired observations processed.
    #[inline]
    #[must_use]
    pub fn count(&self) -> u64 {
        self.count
    }

    /// Whether enough data has been collected for all lags (> lag).
    #[inline]
    #[must_use]
    pub fn is_primed(&self) -> bool {
        self.count > self.lag as u64
    }

    /// Resets to empty state. Configuration and buffer allocations preserved.
    #[inline]
    pub fn reset(&mut self) {
        self.buffer_a.fill(0.0);
        self.cross_m.fill(0.0);
        self.head = 0;
        self.count = 0;
        self.mean_a = 0.0;
        self.mean_b = 0.0;
        self.m2_a = 0.0;
        self.m2_b = 0.0;
    }
}

impl CrossCorrelationF64Builder {
    /// Sets the maximum lag (required, >= 1).
    ///
    /// The tracker computes cross-correlation at lags 0..lag-1.
    #[inline]
    #[must_use]
    pub fn lag(mut self, lag: usize) -> Self {
        self.lag = Some(lag);
        self
    }

    /// Builds the cross-correlation tracker.
    ///
    /// # Errors
    /// Returns `ConfigError` if lag is missing or < 1.
    #[inline]
    pub fn build(self) -> Result<CrossCorrelationF64, nexus_stats_core::ConfigError> {
        let lag = self
            .lag
            .ok_or(nexus_stats_core::ConfigError::Missing("lag"))?;
        if lag < 1 {
            return Err(nexus_stats_core::ConfigError::Invalid("lag must be >= 1"));
        }
        Ok(CrossCorrelationF64 {
            buffer_a: vec![0.0; lag].into_boxed_slice(),
            cross_m: vec![0.0; lag].into_boxed_slice(),
            lag,
            head: 0,
            count: 0,
            mean_a: 0.0,
            mean_b: 0.0,
            m2_a: 0.0,
            m2_b: 0.0,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn identical_streams_correlation_one() {
        let mut cc = CrossCorrelationF64::builder().lag(1).build().unwrap();
        for i in 0..1000u64 {
            let x = i as f64;
            cc.update(x, x).unwrap();
        }
        let r = cc.correlation(0).unwrap();
        assert!(
            (r - 1.0).abs() < 1e-6,
            "identical streams should correlate at 1.0, got {r}"
        );
    }

    #[test]
    fn opposite_streams_correlation_negative() {
        let mut cc = CrossCorrelationF64::builder().lag(1).build().unwrap();
        for i in 0..1000u64 {
            let x = i as f64;
            cc.update(x, -x).unwrap();
        }
        let r = cc.correlation(0).unwrap();
        assert!(
            (r - (-1.0)).abs() < 1e-6,
            "opposite streams should correlate at -1.0, got {r}"
        );
    }

    #[test]
    fn shifted_signal_peak_lag() {
        let mut cc = CrossCorrelationF64::builder().lag(10).build().unwrap();
        let shift = 3;
        let a: Vec<f64> = (0..1000).map(|i| ((i as f64) * 0.1).sin()).collect();
        for i in 0..1000 {
            let b = if i >= shift { a[i - shift] } else { 0.0 };
            cc.update(a[i], b).unwrap();
        }
        let peak = cc.peak_lag().unwrap();
        assert!(
            (peak as i32 - shift as i32).unsigned_abs() <= 1,
            "peak lag should be near {shift}, got {peak}"
        );
    }

    #[test]
    fn lag0_matches_covariance_type() {
        let mut cc = CrossCorrelationF64::builder().lag(1).build().unwrap();
        let mut cov = nexus_stats_core::statistics::CovarianceF64::new();

        for i in 0..500u64 {
            let x = i as f64;
            let y = x * 2.0 + 1.0;
            cc.update(x, y).unwrap();
            let _ = cov.update(x, y);
        }

        let r_cc = cc.correlation(0).unwrap();
        let r_cov = cov.correlation().unwrap();
        assert!(
            (r_cc - r_cov).abs() < 0.01,
            "lag-0 cross-correlation ({r_cc}) should match covariance correlation ({r_cov})"
        );
    }

    #[test]
    fn not_primed_until_enough_samples() {
        let mut cc = CrossCorrelationF64::builder().lag(5).build().unwrap();
        for i in 0..5 {
            cc.update(i as f64, i as f64).unwrap();
            assert!(!cc.is_primed());
        }
        cc.update(5.0, 5.0).unwrap();
        assert!(cc.is_primed());
    }

    #[test]
    fn lag_out_of_range_returns_none() {
        let mut cc = CrossCorrelationF64::builder().lag(5).build().unwrap();
        for i in 0..20 {
            cc.update(i as f64, i as f64).unwrap();
        }
        assert!(cc.correlation(5).is_none()); // lag=5, max valid lag index is 4
        assert!(cc.covariance(5).is_none());
    }

    #[test]
    fn zero_variance_returns_none() {
        let mut cc = CrossCorrelationF64::builder().lag(1).build().unwrap();
        for _ in 0..100 {
            cc.update(42.0, 42.0).unwrap();
        }
        assert!(cc.correlation(0).is_none());
    }

    #[test]
    fn reset_clears_state() {
        let mut cc = CrossCorrelationF64::builder().lag(3).build().unwrap();
        for i in 0..100 {
            cc.update(i as f64, (i * 2) as f64).unwrap();
        }
        cc.reset();
        assert_eq!(cc.count(), 0);
        assert!(!cc.is_primed());
    }

    #[test]
    fn lag_accessor() {
        let cc = CrossCorrelationF64::builder().lag(7).build().unwrap();
        assert_eq!(cc.lag(), 7);
    }

    #[test]
    fn rejects_nan_and_inf() {
        let mut cc = CrossCorrelationF64::builder().lag(1).build().unwrap();
        assert_eq!(
            cc.update(f64::NAN, 1.0),
            Err(nexus_stats_core::DataError::NotANumber)
        );
        assert_eq!(
            cc.update(1.0, f64::INFINITY),
            Err(nexus_stats_core::DataError::Infinite)
        );
        assert_eq!(cc.count(), 0);
    }

    #[test]
    fn builder_requires_lag() {
        let result = CrossCorrelationF64::builder().build();
        assert!(matches!(
            result,
            Err(nexus_stats_core::ConfigError::Missing("lag"))
        ));
    }

    #[test]
    fn builder_rejects_zero_lag() {
        let result = CrossCorrelationF64::builder().lag(0).build();
        assert!(result.is_err());
    }
}