glowbarn 2.0.0

Multi-Sensor Anomaly Detection System - High Performance Native Application
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
428
// Copyright (c) 2026 bad-antics
// Licensed under the MIT License. See LICENSE file in the project root.
// https://github.com/bad-antics/glowbarn-rs

//! Statistical analysis and hypothesis testing

use serde::{Deserialize, Serialize};

/// Statistical summary
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StatisticalSummary {
    pub count: usize,
    pub mean: f64,
    pub median: f64,
    pub mode: Option<f64>,
    pub std_dev: f64,
    pub variance: f64,
    pub min: f64,
    pub max: f64,
    pub range: f64,
    pub q1: f64,
    pub q3: f64,
    pub iqr: f64,
    pub skewness: f64,
    pub kurtosis: f64,
    pub coefficient_of_variation: f64,
}

/// Statistical analyzer
pub struct StatisticalAnalyzer;

impl StatisticalAnalyzer {
    pub fn new() -> Self {
        Self
    }
    
    pub fn summarize(&self, data: &[f64]) -> StatisticalSummary {
        if data.is_empty() {
            return StatisticalSummary::default();
        }
        
        let count = data.len();
        let mean = data.iter().sum::<f64>() / count as f64;
        
        let mut sorted = data.to_vec();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
        
        let median = if count % 2 == 0 {
            (sorted[count / 2 - 1] + sorted[count / 2]) / 2.0
        } else {
            sorted[count / 2]
        };
        
        let min = sorted[0];
        let max = sorted[count - 1];
        let range = max - min;
        
        let q1 = self.percentile(&sorted, 25.0);
        let q3 = self.percentile(&sorted, 75.0);
        let iqr = q3 - q1;
        
        let variance = if count > 1 {
            data.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / (count - 1) as f64
        } else {
            0.0
        };
        let std_dev = variance.sqrt();
        
        let (skewness, kurtosis) = if std_dev > 1e-10 && count > 3 {
            let n = count as f64;
            let skew = data.iter()
                .map(|&x| ((x - mean) / std_dev).powi(3))
                .sum::<f64>() * n / ((n - 1.0) * (n - 2.0));
            
            let kurt = data.iter()
                .map(|&x| ((x - mean) / std_dev).powi(4))
                .sum::<f64>() * n * (n + 1.0) / ((n - 1.0) * (n - 2.0) * (n - 3.0))
                - 3.0 * (n - 1.0).powi(2) / ((n - 2.0) * (n - 3.0));
            
            (skew, kurt)
        } else {
            (0.0, 0.0)
        };
        
        let coefficient_of_variation = if mean.abs() > 1e-10 {
            std_dev / mean.abs()
        } else {
            0.0
        };
        
        let mode = self.calculate_mode(&sorted);
        
        StatisticalSummary {
            count,
            mean,
            median,
            mode,
            std_dev,
            variance,
            min,
            max,
            range,
            q1,
            q3,
            iqr,
            skewness,
            kurtosis,
            coefficient_of_variation,
        }
    }
    
    fn percentile(&self, sorted: &[f64], p: f64) -> f64 {
        if sorted.is_empty() {
            return 0.0;
        }
        let k = (p / 100.0 * (sorted.len() - 1) as f64);
        let f = k.floor() as usize;
        let c = k.ceil() as usize;
        
        if f == c || c >= sorted.len() {
            sorted[f.min(sorted.len() - 1)]
        } else {
            sorted[f] + (sorted[c] - sorted[f]) * (k - f as f64)
        }
    }
    
    fn calculate_mode(&self, sorted: &[f64]) -> Option<f64> {
        if sorted.is_empty() {
            return None;
        }
        
        // Bin the data and find most common bin
        let n_bins = (sorted.len() as f64).sqrt() as usize;
        if n_bins < 3 {
            return None;
        }
        
        let min = sorted[0];
        let max = sorted[sorted.len() - 1];
        let bin_width = (max - min) / n_bins as f64;
        
        if bin_width < 1e-10 {
            return Some(min);
        }
        
        let mut bins = vec![0usize; n_bins];
        for &x in sorted {
            let bin = ((x - min) / bin_width) as usize;
            let bin = bin.min(n_bins - 1);
            bins[bin] += 1;
        }
        
        let (max_bin, _) = bins.iter().enumerate().max_by_key(|(_, &c)| c)?;
        Some(min + (max_bin as f64 + 0.5) * bin_width)
    }
    
    /// Welch's t-test for comparing two samples
    pub fn welch_t_test(&self, sample1: &[f64], sample2: &[f64]) -> TTestResult {
        let n1 = sample1.len() as f64;
        let n2 = sample2.len() as f64;
        
        if n1 < 2.0 || n2 < 2.0 {
            return TTestResult {
                t_statistic: 0.0,
                p_value: 1.0,
                degrees_of_freedom: 0.0,
                significant: false,
            };
        }
        
        let mean1 = sample1.iter().sum::<f64>() / n1;
        let mean2 = sample2.iter().sum::<f64>() / n2;
        
        let var1 = sample1.iter().map(|&x| (x - mean1).powi(2)).sum::<f64>() / (n1 - 1.0);
        let var2 = sample2.iter().map(|&x| (x - mean2).powi(2)).sum::<f64>() / (n2 - 1.0);
        
        let se = (var1 / n1 + var2 / n2).sqrt();
        
        if se < 1e-10 {
            return TTestResult {
                t_statistic: 0.0,
                p_value: 1.0,
                degrees_of_freedom: n1 + n2 - 2.0,
                significant: false,
            };
        }
        
        let t = (mean1 - mean2) / se;
        
        // Welch-Satterthwaite degrees of freedom
        let df = (var1 / n1 + var2 / n2).powi(2) / (
            (var1 / n1).powi(2) / (n1 - 1.0) + (var2 / n2).powi(2) / (n2 - 1.0)
        );
        
        // Approximate p-value using Student's t distribution
        let p_value = self.t_distribution_p_value(t.abs(), df);
        
        TTestResult {
            t_statistic: t,
            p_value,
            degrees_of_freedom: df,
            significant: p_value < 0.05,
        }
    }
    
    fn t_distribution_p_value(&self, t: f64, df: f64) -> f64 {
        // Approximation using normal distribution for large df
        if df > 30.0 {
            return 2.0 * self.normal_cdf(-t.abs());
        }
        
        // Beta function approximation for small df
        let x = df / (df + t * t);
        let p = 0.5 * self.regularized_beta(x, df / 2.0, 0.5);
        2.0 * p
    }
    
    fn normal_cdf(&self, x: f64) -> f64 {
        0.5 * (1.0 + self.erf(x / std::f64::consts::SQRT_2))
    }
    
    fn erf(&self, x: f64) -> f64 {
        // Approximation
        let a1 = 0.254829592;
        let a2 = -0.284496736;
        let a3 = 1.421413741;
        let a4 = -1.453152027;
        let a5 = 1.061405429;
        let p = 0.3275911;
        
        let sign = if x < 0.0 { -1.0 } else { 1.0 };
        let x = x.abs();
        let t = 1.0 / (1.0 + p * x);
        let y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * (-x * x).exp();
        
        sign * y
    }
    
    fn regularized_beta(&self, x: f64, a: f64, b: f64) -> f64 {
        // Simplified approximation
        if x <= 0.0 { return 0.0; }
        if x >= 1.0 { return 1.0; }
        
        // Use continued fraction for better accuracy
        let mut result = x.powf(a) * (1.0 - x).powf(b) / (a * self.beta(a, b));
        
        let mut sum = 1.0;
        let mut term = 1.0;
        for n in 1..100 {
            term *= (a + b + n as f64 - 1.0) * x / (a + n as f64);
            sum += term;
            if term.abs() < 1e-10 {
                break;
            }
        }
        
        result * sum
    }
    
    fn beta(&self, a: f64, b: f64) -> f64 {
        (self.gamma_ln(a) + self.gamma_ln(b) - self.gamma_ln(a + b)).exp()
    }
    
    fn gamma_ln(&self, x: f64) -> f64 {
        // Lanczos approximation
        let g = 7.0;
        let c = [
            0.99999999999980993,
            676.5203681218851,
            -1259.1392167224028,
            771.32342877765313,
            -176.61502916214059,
            12.507343278686905,
            -0.13857109526572012,
            9.9843695780195716e-6,
            1.5056327351493116e-7,
        ];
        
        let x = x - 1.0;
        let mut sum = c[0];
        for i in 1..9 {
            sum += c[i] / (x + i as f64);
        }
        
        let t = x + g + 0.5;
        0.5 * (2.0 * std::f64::consts::PI).ln() + (x + 0.5) * t.ln() - t + sum.ln()
    }
    
    /// Mann-Whitney U test (non-parametric)
    pub fn mann_whitney_test(&self, sample1: &[f64], sample2: &[f64]) -> UTestResult {
        let n1 = sample1.len();
        let n2 = sample2.len();
        
        if n1 < 3 || n2 < 3 {
            return UTestResult {
                u_statistic: 0.0,
                p_value: 1.0,
                significant: false,
            };
        }
        
        // Combine and rank
        let mut combined: Vec<(f64, usize)> = sample1.iter()
            .map(|&x| (x, 0usize))
            .chain(sample2.iter().map(|&x| (x, 1usize)))
            .collect();
        combined.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
        
        // Assign ranks
        let mut ranks = vec![0.0; combined.len()];
        let mut i = 0;
        while i < combined.len() {
            let mut j = i;
            while j < combined.len() && (combined[j].0 - combined[i].0).abs() < 1e-10 {
                j += 1;
            }
            let avg_rank = (i + j + 1) as f64 / 2.0 + 0.5;
            for k in i..j {
                ranks[k] = avg_rank;
            }
            i = j;
        }
        
        // Sum ranks for sample 1
        let r1: f64 = combined.iter()
            .zip(ranks.iter())
            .filter(|((_, group), _)| *group == 0)
            .map(|(_, &r)| r)
            .sum();
        
        let u1 = r1 - (n1 * (n1 + 1)) as f64 / 2.0;
        let u2 = (n1 * n2) as f64 - u1;
        let u = u1.min(u2);
        
        // Normal approximation for p-value
        let mean_u = (n1 * n2) as f64 / 2.0;
        let std_u = ((n1 * n2 * (n1 + n2 + 1)) as f64 / 12.0).sqrt();
        
        let z = if std_u > 1e-10 { (u - mean_u) / std_u } else { 0.0 };
        let p_value = 2.0 * self.normal_cdf(-z.abs());
        
        UTestResult {
            u_statistic: u,
            p_value,
            significant: p_value < 0.05,
        }
    }
    
    /// Kolmogorov-Smirnov test for distribution comparison
    pub fn ks_test(&self, sample: &[f64], theoretical_cdf: impl Fn(f64) -> f64) -> KSTestResult {
        if sample.is_empty() {
            return KSTestResult {
                d_statistic: 0.0,
                p_value: 1.0,
                significant: false,
            };
        }
        
        let n = sample.len();
        let mut sorted = sample.to_vec();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
        
        let mut d_max: f64 = 0.0;
        
        for (i, &x) in sorted.iter().enumerate() {
            let f_empirical = (i + 1) as f64 / n as f64;
            let f_empirical_prev = i as f64 / n as f64;
            let f_theoretical = theoretical_cdf(x);
            
            d_max = d_max
                .max((f_empirical - f_theoretical).abs())
                .max((f_empirical_prev - f_theoretical).abs());
        }
        
        // Kolmogorov distribution approximation for p-value
        let sqrt_n = (n as f64).sqrt();
        let p_value = self.kolmogorov_p_value(d_max * sqrt_n);
        
        KSTestResult {
            d_statistic: d_max,
            p_value,
            significant: p_value < 0.05,
        }
    }
    
    fn kolmogorov_p_value(&self, z: f64) -> f64 {
        if z < 0.27 {
            return 1.0;
        }
        if z > 3.5 {
            return 0.0;
        }
        
        // Approximation
        let mut sum = 0.0;
        for k in 1..100 {
            let term = (-2.0 * (k as f64).powi(2) * z * z).exp();
            sum += if k % 2 == 1 { term } else { -term };
            if term.abs() < 1e-10 {
                break;
            }
        }
        
        2.0 * sum
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TTestResult {
    pub t_statistic: f64,
    pub p_value: f64,
    pub degrees_of_freedom: f64,
    pub significant: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UTestResult {
    pub u_statistic: f64,
    pub p_value: f64,
    pub significant: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KSTestResult {
    pub d_statistic: f64,
    pub p_value: f64,
    pub significant: bool,
}