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
//! Streaming d×d covariance matrix with exponential weighting.
use alloc::vec;
use alloc::vec::Vec;
/// Streaming d×d covariance matrix with exponential weighting.
///
/// Tracks means and an EW covariance matrix from observation vectors.
/// All updates are O(d²) per sample. Memory: O(d²) for the matrix.
///
/// # Use Cases
///
/// - Natural gradient optimizer (Fisher information approximation)
/// - Portfolio risk decomposition
/// - Feature correlation monitoring
#[derive(Debug, Clone)]
pub struct OnlineCovarianceF64 {
cov: Vec<f64>,
means: Vec<f64>,
dim: usize,
alpha: f64,
count: u64,
}
/// Builder for [`OnlineCovarianceF64`].
#[derive(Debug, Clone)]
pub struct OnlineCovarianceF64Builder {
dim: Option<usize>,
alpha: Option<f64>,
}
impl OnlineCovarianceF64 {
/// Creates a builder.
#[inline]
#[must_use]
pub fn builder() -> OnlineCovarianceF64Builder {
OnlineCovarianceF64Builder {
dim: None,
alpha: None,
}
}
/// Feed an observation vector.
///
/// # Panics
///
/// Panics if `observation.len() != dim`.
///
/// # Errors
///
/// Returns `DataError::NotANumber` or `DataError::Infinite` if any
/// element is not finite.
pub fn update(&mut self, observation: &[f64]) -> Result<(), crate::DataError> {
assert_eq!(
observation.len(),
self.dim,
"observation dimension ({}) != configured dim ({})",
observation.len(),
self.dim,
);
for &v in observation {
check_finite!(v);
}
self.count += 1;
let d = self.dim;
if self.count == 1 {
self.means.copy_from_slice(observation);
// Covariance stays at zero — need 2+ samples.
return Ok(());
}
// Compute all deltas from current (pre-update) means BEFORE modifying
// anything. This ensures the covariance update uses consistent deltas —
// mixing old and new means would introduce bias in cov[i,i].
let alpha = self.alpha;
let one_minus_alpha = 1.0 - alpha;
// Temporarily repurpose means as deltas: means[i] = obs[i] - old_mean[i].
// Then update covariance, then restore means to new_mean.
// This avoids any allocation.
for i in 0..d {
self.means[i] = observation[i] - self.means[i]; // means[i] is now delta_i
}
// Update covariance using the stable EW recurrence.
// The (1 - alpha) factor on the increment matches the scalar EwmaVar
// recurrence: cov = (1-α) * cov + α * (1-α) * δ_i * δ_j.
// Without it, covariance would be biased upward by ~1/(1-α).
let alpha_times_one_minus = alpha * one_minus_alpha;
for i in 0..d {
for j in i..d {
let idx = i * d + j;
self.cov[idx] = one_minus_alpha * self.cov[idx]
+ alpha_times_one_minus * self.means[i] * self.means[j];
if i != j {
self.cov[j * d + i] = self.cov[idx];
}
}
}
// Restore means: new_mean = old_mean + alpha * delta = (obs - delta) + alpha * delta
// = obs - delta * (1 - alpha)
for i in 0..d {
self.means[i] = (-self.means[i]).mul_add(one_minus_alpha, observation[i]);
}
Ok(())
}
/// Covariance between dimensions `i` and `j`.
///
/// Returns `None` if fewer than 2 observations have been fed
/// (covariance is undefined with fewer than 2 data points).
#[inline]
#[must_use]
pub fn covariance(&self, i: usize, j: usize) -> Option<f64> {
if !self.is_primed() {
return None;
}
debug_assert!(i < self.dim && j < self.dim);
Some(self.cov[i * self.dim + j])
}
/// Pearson correlation between dimensions `i` and `j`.
///
/// Returns `None` if not primed or if either variance is zero.
#[cfg(any(feature = "std", feature = "libm"))]
#[inline]
#[must_use]
pub fn correlation(&self, i: usize, j: usize) -> Option<f64> {
let var_i = self.variance(i)?;
let var_j = self.variance(j)?;
if var_i < f64::EPSILON || var_j < f64::EPSILON {
return None;
}
Some(self.covariance(i, j)? / (crate::math::sqrt(var_i) * crate::math::sqrt(var_j)))
}
/// Variance of dimension `i`.
///
/// Returns `None` if not primed.
#[inline]
#[must_use]
pub fn variance(&self, i: usize) -> Option<f64> {
self.covariance(i, i)
}
/// Mean of dimension `i`.
///
/// Returns `None` if no observations have been fed.
#[inline]
#[must_use]
pub fn mean(&self, i: usize) -> Option<f64> {
if self.count == 0 {
return None;
}
Some(self.means[i])
}
/// The full covariance matrix as a flat row-major slice.
///
/// Returns zeroes if not primed — check [`is_primed()`](Self::is_primed)
/// before interpreting values.
#[inline]
#[must_use]
pub fn as_matrix(&self) -> &[f64] {
&self.cov
}
/// Dimensionality.
#[inline]
#[must_use]
pub fn dim(&self) -> usize {
self.dim
}
/// Whether at least 2 observations have been fed.
#[inline]
#[must_use]
pub fn is_primed(&self) -> bool {
self.count >= 2
}
/// Number of observations processed.
#[inline]
#[must_use]
pub fn count(&self) -> u64 {
self.count
}
/// Reset to initial state.
pub fn reset(&mut self) {
self.cov.iter_mut().for_each(|v| *v = 0.0);
self.means.iter_mut().for_each(|v| *v = 0.0);
self.count = 0;
}
}
impl OnlineCovarianceF64Builder {
/// Number of dimensions (required).
#[inline]
#[must_use]
pub fn dim(mut self, d: usize) -> Self {
self.dim = Some(d);
self
}
/// Halflife for exponential weighting (required).
#[cfg(any(feature = "std", feature = "libm"))]
#[inline]
#[must_use]
pub fn halflife(mut self, h: f64) -> Self {
let alpha = 1.0 - crate::math::exp(-core::f64::consts::LN_2 / h);
self.alpha = Some(alpha);
self
}
/// Direct smoothing factor.
#[inline]
#[must_use]
pub fn alpha(mut self, alpha: f64) -> Self {
self.alpha = Some(alpha);
self
}
/// Build.
///
/// # Errors
///
/// Returns `ConfigError` if dim or alpha are missing/invalid.
pub fn build(self) -> Result<OnlineCovarianceF64, crate::ConfigError> {
let dim = self.dim.ok_or(crate::ConfigError::Missing("dim"))?;
if dim == 0 {
return Err(crate::ConfigError::Invalid("dim must be > 0"));
}
let alpha = self
.alpha
.ok_or(crate::ConfigError::Missing("halflife or alpha"))?;
if !(alpha > 0.0 && alpha < 1.0) {
return Err(crate::ConfigError::Invalid("alpha must be in (0, 1)"));
}
Ok(OnlineCovarianceF64 {
cov: vec![0.0; dim * dim],
means: vec![0.0; dim],
dim,
alpha,
count: 0,
})
}
}
#[cfg(test)]
#[allow(clippy::float_cmp)]
mod tests {
use super::*;
fn basic_cov(dim: usize) -> OnlineCovarianceF64 {
OnlineCovarianceF64::builder()
.dim(dim)
.halflife(50.0)
.build()
.unwrap()
}
#[test]
fn uncorrelated_2d() {
let mut cov = basic_cov(2);
// Feed independent signals
for i in 0..200 {
let x = (i % 10) as f64;
let y = ((i * 7) % 13) as f64;
cov.update(&[x, y]).unwrap();
}
// Variances should be positive
assert!(cov.variance(0).unwrap() > 0.0);
assert!(cov.variance(1).unwrap() > 0.0);
// Correlation should be near zero (not perfectly, small sample)
let corr = cov.correlation(0, 1).unwrap().abs();
assert!(corr < 0.5, "expected low correlation, got {corr}");
}
#[test]
fn perfectly_correlated_2d() {
let mut cov = basic_cov(2);
for i in 0..200 {
let x = i as f64;
cov.update(&[x, x.mul_add(2.0, 1.0)]).unwrap();
}
let corr = cov.correlation(0, 1).unwrap();
assert!(corr > 0.95, "expected high correlation, got {corr}");
}
#[test]
fn symmetry() {
let mut cov = basic_cov(3);
for i in 0..100 {
let v = [i as f64, (i * 2) as f64, (i * 3) as f64];
cov.update(&v).unwrap();
}
for i in 0..3 {
for j in 0..3 {
assert!(
(cov.covariance(i, j).unwrap() - cov.covariance(j, i).unwrap()).abs() < 1e-10,
"cov({i},{j}) != cov({j},{i})"
);
}
}
}
#[test]
#[should_panic(expected = "observation dimension")]
fn wrong_dimension_panics() {
let mut cov = basic_cov(3);
let _ = cov.update(&[1.0, 2.0]);
}
#[test]
fn priming() {
let mut cov = basic_cov(2);
assert!(!cov.is_primed());
assert!(cov.covariance(0, 1).is_none());
assert!(cov.correlation(0, 1).is_none());
assert!(cov.variance(0).is_none());
assert!(cov.mean(0).is_none());
cov.update(&[1.0, 2.0]).unwrap();
assert!(!cov.is_primed());
assert!(cov.covariance(0, 1).is_none());
assert!(cov.mean(0).is_some()); // mean available after 1 sample
cov.update(&[3.0, 4.0]).unwrap();
assert!(cov.is_primed());
assert!(cov.covariance(0, 1).is_some());
}
#[test]
fn reset_clears() {
let mut cov = basic_cov(2);
for i in 0..50 {
cov.update(&[i as f64, i as f64]).unwrap();
}
cov.reset();
assert_eq!(cov.count(), 0);
assert!(!cov.is_primed());
}
#[test]
fn invalid_config() {
assert!(OnlineCovarianceF64::builder().alpha(0.1).build().is_err()); // missing dim
assert!(OnlineCovarianceF64::builder().dim(2).build().is_err()); // missing alpha
assert!(
OnlineCovarianceF64::builder()
.dim(0)
.alpha(0.1)
.build()
.is_err()
); // dim = 0
}
}