nexus-stats-core 3.0.1

Core types and utilities shared across nexus-stats subcrates
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
426
427
// We intentionally avoid mul_add/FMA in the moment formulas — the update
// order (M4 before M3 before M2) is critical and FMA rewriting could
// change numerical behavior. Additionally, moments must compile on
// no_std without libm.
#![allow(clippy::suboptimal_flops, clippy::float_cmp)]

/// Online skewness and kurtosis via Pébay's higher-moment extension
/// of Welford's algorithm (Pébay, 2008).
///
/// Numerically stable single-pass computation of mean, variance,
/// skewness, and excess kurtosis. Supports merging partial results
/// via Pébay's parallel aggregation formulas.
///
/// # Use Cases
/// - Distribution shape monitoring (is latency becoming skewed?)
/// - Fat-tail detection (kurtosis spike → regime change)
/// - Quality control (symmetric vs asymmetric error distributions)
///
/// Computes population (not sample-corrected) skewness and kurtosis.
/// For streaming use cases with n > 100, population and sample estimators
/// are indistinguishable. For small-sample inference (n < 30), use a
/// batch estimator with Bessel's correction instead.
///
/// # Complexity
/// - O(1) per update, 40 bytes state, zero allocation.
///
/// # Examples
///
/// ```
/// use nexus_stats_core::statistics::MomentsF64;
///
/// let mut m = MomentsF64::new();
/// for i in 1..=1000u64 { m.update(i as f64).unwrap(); }
/// // Uniform distribution: skewness ≈ 0, kurtosis ≈ -1.2
/// let skew = m.skewness().unwrap();
/// assert!(skew.abs() < 0.1);
/// ```
#[derive(Debug, Clone)]
pub struct MomentsF64 {
    count: u64,
    mean: f64,
    m2: f64,
    m3: f64,
    m4: f64,
}

impl MomentsF64 {
    /// Creates a new empty accumulator.
    #[inline]
    #[must_use]
    pub const fn new() -> Self {
        Self {
            count: 0,
            mean: 0.0,
            m2: 0.0,
            m3: 0.0,
            m4: 0.0,
        }
    }

    /// Feeds a sample.
    ///
    /// # Errors
    ///
    /// Returns `DataError::NotANumber` if the sample is NaN, or
    /// `DataError::Infinite` if the sample is infinite.
    #[inline]
    pub fn update(&mut self, sample: f64) -> Result<(), crate::DataError> {
        check_finite!(sample);
        self.count += 1;
        let n = self.count as f64;
        let delta = sample - self.mean;
        let delta_n = delta / n;
        let delta_n2 = delta_n * delta_n;
        let term1 = delta * delta_n * (n - 1.0);

        // M4 before M3 before M2 — each uses previous iteration's lower moments
        self.m4 += term1 * delta_n2 * (n * n - 3.0 * n + 3.0) + 6.0 * delta_n2 * self.m2
            - 4.0 * delta_n * self.m3;
        self.m3 += term1 * delta_n * (n - 2.0) - 3.0 * delta_n * self.m2;
        self.m2 += term1;
        self.mean += delta_n;
        Ok(())
    }

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

    /// Running mean, or `None` if empty.
    #[inline]
    #[must_use]
    pub fn mean(&self) -> Option<f64> {
        if self.count == 0 {
            None
        } else {
            Some(self.mean)
        }
    }

    /// Sample variance (N-1 denominator), or `None` if < 2 samples.
    #[inline]
    #[must_use]
    pub fn variance(&self) -> Option<f64> {
        if self.count < 2 {
            None
        } else {
            Some(self.m2 / (self.count - 1) as f64)
        }
    }

    /// Population variance (N denominator), or `None` if empty.
    #[inline]
    #[must_use]
    pub fn population_variance(&self) -> Option<f64> {
        if self.count == 0 {
            None
        } else {
            Some(self.m2 / self.count as f64)
        }
    }

    /// Sample standard deviation, or `None` if < 2 samples.
    #[cfg(any(feature = "std", feature = "libm"))]
    #[inline]
    #[must_use]
    pub fn std_dev(&self) -> Option<f64> {
        self.variance().map(crate::math::sqrt)
    }

    /// Population skewness (Fisher's definition), or `None` if < 3
    /// samples or variance is zero.
    ///
    /// Positive = right-skewed (tail extends right).
    /// Negative = left-skewed (tail extends left).
    /// Zero = symmetric.
    #[cfg(any(feature = "std", feature = "libm"))]
    #[inline]
    #[must_use]
    pub fn skewness(&self) -> Option<f64> {
        if self.count < 3 {
            return None;
        }
        if self.m2 == 0.0 {
            return None;
        }
        let n = self.count as f64;
        Some(crate::math::sqrt(n) * self.m3 / (self.m2 * crate::math::sqrt(self.m2)))
    }

    /// Population excess kurtosis, or `None` if < 4 samples or
    /// variance is zero.
    ///
    /// Normal distribution = 0. Positive = heavy tails (leptokurtic).
    /// Negative = light tails (platykurtic). This is the most common
    /// convention (numpy, scipy, most finance).
    #[inline]
    #[must_use]
    pub fn excess_kurtosis(&self) -> Option<f64> {
        if self.count < 4 {
            return None;
        }
        if self.m2 == 0.0 {
            return None;
        }
        let n = self.count as f64;
        Some(n * self.m4 / (self.m2 * self.m2) - 3.0)
    }

    /// Population kurtosis (non-excess), or `None` if < 4 samples or
    /// variance is zero.
    ///
    /// Normal distribution = 3. This is `excess_kurtosis() + 3`.
    #[inline]
    #[must_use]
    pub fn kurtosis(&self) -> Option<f64> {
        self.excess_kurtosis().map(|k| k + 3.0)
    }

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

    /// Merges another accumulator into this one (Pébay's parallel algorithm).
    ///
    /// After merging, `self` contains the statistics of the combined
    /// dataset. The other accumulator is unchanged.
    #[inline]
    #[allow(clippy::suspicious_operation_groupings)]
    pub fn merge(&mut self, other: &Self) {
        if other.count == 0 {
            return;
        }
        if self.count == 0 {
            *self = other.clone();
            return;
        }

        let n_a = self.count as f64;
        let n_b = other.count as f64;
        let n = n_a + n_b;
        let delta = other.mean - self.mean;
        let delta2 = delta * delta;
        let delta3 = delta2 * delta;
        let delta4 = delta2 * delta2;

        let new_m4 = self.m4
            + other.m4
            + delta4 * n_a * n_b * (n_a * n_a - n_a * n_b + n_b * n_b) / (n * n * n)
            + 6.0 * delta2 * (n_a * n_a * other.m2 + n_b * n_b * self.m2) / (n * n)
            + 4.0 * delta * (n_a * other.m3 - n_b * self.m3) / n;

        let new_m3 = self.m3
            + other.m3
            + delta3 * n_a * n_b * (n_a - n_b) / (n * n)
            + 3.0 * delta * (n_a * other.m2 - n_b * self.m2) / n;

        let new_m2 = self.m2 + other.m2 + delta2 * n_a * n_b / n;

        self.mean += delta * n_b / n;
        self.count += other.count;
        self.m2 = new_m2;
        self.m3 = new_m3;
        self.m4 = new_m4;
    }

    /// Resets to empty state.
    #[inline]
    pub fn reset(&mut self) {
        *self = Self::new();
    }
}

impl Default for MomentsF64 {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn uniform_1_to_100() {
        let mut m = MomentsF64::new();
        for i in 1..=100u64 {
            m.update(i as f64).unwrap();
        }

        assert_eq!(m.count(), 100);

        let mean = m.mean().unwrap();
        assert!((mean - 50.5).abs() < 1e-10, "mean = {mean}");

        let pop_var = m.population_variance().unwrap();
        assert!((pop_var - 833.25).abs() < 1e-6, "pop variance = {pop_var}");

        let var = m.variance().unwrap();
        assert!((var - 841.6667).abs() < 0.01, "variance = {var}");
    }

    #[test]
    fn uniform_skewness_near_zero() {
        let mut m = MomentsF64::new();
        for i in 1..=10000u64 {
            m.update(i as f64).unwrap();
        }
        let skew = m.skewness().unwrap();
        assert!(skew.abs() < 0.01, "skewness = {skew}, expected ≈ 0");
    }

    #[test]
    fn uniform_kurtosis() {
        let mut m = MomentsF64::new();
        for i in 1..=10000u64 {
            m.update(i as f64).unwrap();
        }
        let kurt = m.excess_kurtosis().unwrap();
        assert!(
            (kurt - (-1.2)).abs() < 0.01,
            "kurtosis = {kurt}, expected ≈ -1.2"
        );
    }

    #[test]
    fn empty() {
        let m = MomentsF64::new();
        assert_eq!(m.count(), 0);
        assert!(m.mean().is_none());
        assert!(m.variance().is_none());
        assert!(m.skewness().is_none());
        assert!(m.excess_kurtosis().is_none());
        assert!(!m.is_primed());
    }

    #[test]
    fn single_sample() {
        let mut m = MomentsF64::new();
        m.update(42.0).unwrap();
        assert_eq!(m.count(), 1);
        assert_eq!(m.mean(), Some(42.0));
        assert!(m.variance().is_none());
        assert!(m.skewness().is_none());
        assert!(m.excess_kurtosis().is_none());
    }

    #[test]
    fn priming_thresholds() {
        let mut m = MomentsF64::new();
        m.update(1.0).unwrap();
        assert!(m.mean().is_some());
        m.update(2.0).unwrap();
        assert!(m.variance().is_some());
        m.update(3.0).unwrap();
        assert!(m.skewness().is_some());
        m.update(4.0).unwrap();
        assert!(m.excess_kurtosis().is_some());
        assert!(m.is_primed());
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn constant_input() {
        let mut m = MomentsF64::new();
        for _ in 0..100 {
            m.update(42.0).unwrap();
        }
        assert_eq!(m.mean(), Some(42.0));
        assert_eq!(m.variance(), Some(0.0));
        assert!(m.skewness().is_none());
        assert!(m.excess_kurtosis().is_none());
    }

    #[test]
    fn right_skewed_distribution() {
        let mut m = MomentsF64::new();
        for _ in 0..900 {
            m.update(1.0).unwrap();
        }
        for _ in 0..100 {
            m.update(10.0).unwrap();
        }
        let skew = m.skewness().unwrap();
        assert!(skew > 0.0, "right-skewed should be positive, got {skew}");
    }

    #[test]
    fn reset_clears_state() {
        let mut m = MomentsF64::new();
        for i in 0..100 {
            m.update(i as f64).unwrap();
        }
        m.reset();
        assert_eq!(m.count(), 0);
        assert!(m.mean().is_none());
        assert!(m.excess_kurtosis().is_none());
    }

    #[test]
    fn merge_empty_into_empty() {
        let mut a = MomentsF64::new();
        let b = MomentsF64::new();
        a.merge(&b);
        assert_eq!(a.count(), 0);
    }

    #[test]
    fn merge_into_empty() {
        let mut a = MomentsF64::new();
        let mut b = MomentsF64::new();
        for i in 1..=50u64 {
            b.update(i as f64).unwrap();
        }
        a.merge(&b);
        assert_eq!(a.count(), 50);
        assert!((a.mean().unwrap() - 25.5).abs() < 1e-10);
    }

    #[test]
    fn merge_matches_single_pass() {
        let data: Vec<f64> = (1..=200).map(|i| i as f64).collect();

        let mut single = MomentsF64::new();
        for &x in &data {
            single.update(x).unwrap();
        }

        let mut first = MomentsF64::new();
        let mut second = MomentsF64::new();
        for &x in &data[..80] {
            first.update(x).unwrap();
        }
        for &x in &data[80..] {
            second.update(x).unwrap();
        }
        first.merge(&second);

        assert_eq!(first.count(), single.count());
        assert!((first.mean().unwrap() - single.mean().unwrap()).abs() < 1e-10);
        assert!((first.variance().unwrap() - single.variance().unwrap()).abs() < 1e-6);
        assert!((first.skewness().unwrap() - single.skewness().unwrap()).abs() < 1e-6);
        assert!((first.kurtosis().unwrap() - single.kurtosis().unwrap()).abs() < 1e-4);
    }

    #[test]
    fn default_is_empty() {
        let m = MomentsF64::default();
        assert_eq!(m.count(), 0);
    }

    #[test]
    fn rejects_nan_and_inf() {
        let mut m = MomentsF64::new();
        assert_eq!(m.update(f64::NAN), Err(crate::DataError::NotANumber));
        assert_eq!(m.update(f64::INFINITY), Err(crate::DataError::Infinite));
        assert_eq!(m.update(f64::NEG_INFINITY), Err(crate::DataError::Infinite));
        assert_eq!(m.count(), 0);
    }
}