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
//! Streaming statistics computation using online algorithms.
//!
//! This module provides algorithms for computing statistics incrementally
//! without loading all data into memory, enabling extraction from large datasets.
use std::collections::HashMap;
/// Streaming statistics accumulator for numeric data.
///
/// Uses Welford's online algorithm for numerically stable computation
/// of mean and variance in a single pass.
#[derive(Debug, Clone)]
pub struct StreamingNumericStats {
/// Number of values seen.
count: u64,
/// Running mean.
mean: f64,
/// Running M2 (sum of squares of differences from the mean).
m2: f64,
/// Minimum value seen.
min: f64,
/// Maximum value seen.
max: f64,
/// Sum of values (for total computation).
sum: f64,
/// Count of zero values.
zero_count: u64,
/// Count of negative values.
negative_count: u64,
/// Benford first digit counts.
benford_counts: [u64; 9],
/// Reservoir sample for percentile estimation.
reservoir: Vec<f64>,
/// Maximum reservoir size.
reservoir_capacity: usize,
/// Total items seen (for reservoir sampling).
total_seen: u64,
}
impl StreamingNumericStats {
/// Create a new streaming stats accumulator.
pub fn new(reservoir_capacity: usize) -> Self {
Self {
count: 0,
mean: 0.0,
m2: 0.0,
min: f64::INFINITY,
max: f64::NEG_INFINITY,
sum: 0.0,
zero_count: 0,
negative_count: 0,
benford_counts: [0; 9],
reservoir: Vec::with_capacity(reservoir_capacity),
reservoir_capacity,
total_seen: 0,
}
}
/// Add a value to the accumulator.
pub fn add(&mut self, value: f64) {
self.count += 1;
self.total_seen += 1;
self.sum += value;
// Update min/max
if value < self.min {
self.min = value;
}
if value > self.max {
self.max = value;
}
// Welford's algorithm for online mean and variance
let delta = value - self.mean;
self.mean += delta / self.count as f64;
let delta2 = value - self.mean;
self.m2 += delta * delta2;
// Count zeros and negatives
if value == 0.0 {
self.zero_count += 1;
}
if value < 0.0 {
self.negative_count += 1;
}
// Benford's law first digit
let abs_value = value.abs();
if abs_value > 0.0 {
let s = format!("{abs_value:.15}");
for c in s.chars() {
if c.is_ascii_digit() && c != '0' {
if let Some(digit) = c.to_digit(10) {
if (1..=9).contains(&digit) {
self.benford_counts[(digit - 1) as usize] += 1;
}
}
break;
}
}
}
// Reservoir sampling for percentiles
if self.reservoir.len() < self.reservoir_capacity {
self.reservoir.push(value);
} else {
// Random replacement with probability k/n
let j = rand::random::<u64>() % self.total_seen;
if j < self.reservoir_capacity as u64 {
self.reservoir[j as usize] = value;
}
}
}
/// Add a batch of values.
pub fn add_batch(&mut self, values: &[f64]) {
for &value in values {
self.add(value);
}
}
/// Get the count.
pub fn count(&self) -> u64 {
self.count
}
/// Get the mean.
pub fn mean(&self) -> f64 {
self.mean
}
/// Get the variance.
pub fn variance(&self) -> f64 {
if self.count < 2 {
0.0
} else {
self.m2 / self.count as f64
}
}
/// Get the standard deviation.
pub fn std_dev(&self) -> f64 {
self.variance().sqrt()
}
/// Get the minimum value.
pub fn min(&self) -> f64 {
if self.min.is_infinite() {
0.0
} else {
self.min
}
}
/// Get the maximum value.
pub fn max(&self) -> f64 {
if self.max.is_infinite() {
0.0
} else {
self.max
}
}
/// Get the zero rate.
pub fn zero_rate(&self) -> f64 {
if self.count == 0 {
0.0
} else {
self.zero_count as f64 / self.count as f64
}
}
/// Get the negative rate.
pub fn negative_rate(&self) -> f64 {
if self.count == 0 {
0.0
} else {
self.negative_count as f64 / self.count as f64
}
}
/// Get the Benford first digit distribution.
pub fn benford_distribution(&self) -> [f64; 9] {
let total: u64 = self.benford_counts.iter().sum();
if total == 0 {
return [0.0; 9];
}
let mut dist = [0.0; 9];
for i in 0..9 {
dist[i] = self.benford_counts[i] as f64 / total as f64;
}
dist
}
/// Estimate percentiles from the reservoir sample.
pub fn percentiles(&self) -> crate::models::Percentiles {
let mut sorted = self.reservoir.clone();
sorted.sort_by(f64::total_cmp);
fn percentile(sorted: &[f64], p: f64) -> f64 {
if sorted.is_empty() {
return 0.0;
}
let idx = (p / 100.0 * (sorted.len() - 1) as f64).round() as usize;
sorted[idx.min(sorted.len() - 1)]
}
crate::models::Percentiles {
p1: percentile(&sorted, 1.0),
p5: percentile(&sorted, 5.0),
p10: percentile(&sorted, 10.0),
p25: percentile(&sorted, 25.0),
p50: percentile(&sorted, 50.0),
p75: percentile(&sorted, 75.0),
p90: percentile(&sorted, 90.0),
p95: percentile(&sorted, 95.0),
p99: percentile(&sorted, 99.0),
}
}
/// Percentiles of `ln(|x|)` over `|x| >= 1`, from the reservoir sample.
///
/// Sign-agnostic and tail-robust: for a heavy-tailed, possibly-signed amount
/// column the signed mean/min/max are DP-noised/winsorized into uselessness for a
/// log-normal fit (a balanced column has median 0), but these log-magnitude
/// quantiles let a synthesizer recover `lognormal_mu = p50`,
/// `lognormal_sigma = (p75 - p25) / 1.349`. `None` if too few qualifying samples.
pub fn log_magnitude_percentiles(&self) -> Option<crate::models::Percentiles> {
let mut sorted: Vec<f64> = self
.reservoir
.iter()
.filter(|v| v.abs() >= 1.0)
.map(|v| v.abs().ln())
.collect();
if sorted.len() < 20 {
return None;
}
sorted.sort_by(f64::total_cmp);
fn percentile(sorted: &[f64], p: f64) -> f64 {
let idx = (p / 100.0 * (sorted.len() - 1) as f64).round() as usize;
sorted[idx.min(sorted.len() - 1)]
}
Some(crate::models::Percentiles {
p1: percentile(&sorted, 1.0),
p5: percentile(&sorted, 5.0),
p10: percentile(&sorted, 10.0),
p25: percentile(&sorted, 25.0),
p50: percentile(&sorted, 50.0),
p75: percentile(&sorted, 75.0),
p90: percentile(&sorted, 90.0),
p95: percentile(&sorted, 95.0),
p99: percentile(&sorted, 99.0),
})
}
}
/// Streaming statistics accumulator for categorical data.
#[derive(Debug, Clone)]
pub struct StreamingCategoricalStats {
/// Total count.
count: u64,
/// Frequency counts (capped at max_categories for memory).
frequencies: HashMap<String, u64>,
/// Maximum number of categories to track.
max_categories: usize,
/// Count of values in categories not tracked.
other_count: u64,
}
impl StreamingCategoricalStats {
/// Create a new streaming categorical stats accumulator.
pub fn new(max_categories: usize) -> Self {
Self {
count: 0,
frequencies: HashMap::new(),
max_categories,
other_count: 0,
}
}
/// Add a value to the accumulator.
pub fn add(&mut self, value: String) {
if value.is_empty() {
return;
}
self.count += 1;
if let Some(count) = self.frequencies.get_mut(&value) {
*count += 1;
} else if self.frequencies.len() < self.max_categories {
self.frequencies.insert(value, 1);
} else {
// Check if this value should replace a less frequent one
// (This implements a lossy counting approach)
self.other_count += 1;
// Periodically prune low-frequency items
if self.other_count > self.max_categories as u64 {
self.prune_low_frequency();
}
}
}
/// Prune low-frequency items to make room for new ones.
fn prune_low_frequency(&mut self) {
let threshold = self.other_count / self.max_categories as u64;
self.frequencies.retain(|_, &mut count| count > threshold);
self.other_count = 0;
}
/// Get the count.
pub fn count(&self) -> u64 {
self.count
}
/// Get the cardinality (number of unique categories tracked).
pub fn cardinality(&self) -> u64 {
self.frequencies.len() as u64
}
/// Get the top values by frequency.
pub fn top_values(&self, limit: usize) -> Vec<(String, u64)> {
let mut values: Vec<_> = self.frequencies.iter().collect();
values.sort_by(|a, b| b.1.cmp(a.1));
values
.into_iter()
.take(limit)
.map(|(k, v)| (k.clone(), *v))
.collect()
}
/// Compute entropy.
pub fn entropy(&self) -> f64 {
let total = self.count as f64;
if total == 0.0 {
return 0.0;
}
let mut entropy = 0.0;
for &count in self.frequencies.values() {
if count > 0 {
let p = count as f64 / total;
entropy -= p * p.ln();
}
}
entropy
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_streaming_numeric_stats() {
let mut stats = StreamingNumericStats::new(1000);
// Add some values
for i in 1..=100 {
stats.add(i as f64);
}
assert_eq!(stats.count(), 100);
assert!((stats.mean() - 50.5).abs() < 0.001);
assert_eq!(stats.min(), 1.0);
assert_eq!(stats.max(), 100.0);
}
#[test]
fn test_streaming_categorical_stats() {
let mut stats = StreamingCategoricalStats::new(100);
// Add some values
for _ in 0..50 {
stats.add("A".to_string());
}
for _ in 0..30 {
stats.add("B".to_string());
}
for _ in 0..20 {
stats.add("C".to_string());
}
assert_eq!(stats.count(), 100);
assert_eq!(stats.cardinality(), 3);
let top = stats.top_values(3);
assert_eq!(top[0].0, "A");
assert_eq!(top[0].1, 50);
}
}