rs-stats 2.0.3

Statistics library in rust
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! # Distribution Fitting
//!
//! High-level API for automatic distribution detection and fitting.
//!
//! Given a dataset, this module:
//! 1. **Detects** whether the data is discrete or continuous (`detect_data_type`)
//! 2. **Fits** all applicable distribution candidates using MLE or MOM
//! 3. **Ranks** them by AIC (lower = better fit, penalised for complexity)
//! 4. **Validates** with a Kolmogorov-Smirnov goodness-of-fit test
//!
//! ## Key functions
//!
//! | Function | Description |
//! |----------|-------------|
//! | `auto_fit(data)` | Auto-detect type + return single best fit |
//! | `fit_all(data)` | All 10 continuous distributions, ranked by AIC |
//! | `fit_best(data)` | Best continuous distribution (lowest AIC) |
//! | `fit_all_discrete(data)` | All 4 discrete distributions, ranked by AIC |
//! | `fit_best_discrete(data)` | Best discrete distribution |
//! | `detect_data_type(data)` | `DataKind::Discrete` or `DataKind::Continuous` |
//! | `ks_test(data, cdf)` | Two-sided KS test for continuous distributions |
//! | `ks_test_discrete(data, cdf)` | KS test for discrete distributions |
//!
//! ## Medical example — identifying the best distribution for drug half-life
//!
//! ```rust
//! use rs_stats::distributions::fitting::{fit_all, auto_fit};
//!
//! // Drug half-life (hours) measured in 20 patients — typically log-normal in PK studies
//! let half_lives = vec![
//!     4.2, 6.1, 3.8, 9.5, 5.3, 7.4, 4.9, 11.2, 3.5, 6.8,
//!     8.1, 4.4, 5.7, 7.0, 3.9, 10.3, 5.1,  6.5, 4.7,  8.6,
//! ];
//!
//! // One-call: auto-detect + return single best (lowest AIC)
//! let best = auto_fit(&half_lives).unwrap();
//! println!("Best fit: {} (AIC={:.2}, KS p={:.3})", best.name, best.aic, best.ks_p_value);
//!
//! // Full ranking for model comparison and reporting
//! println!("{:<15} {:>8} {:>8} {:>10}", "Distribution", "AIC", "BIC", "KS p-value");
//! for r in fit_all(&half_lives).unwrap() {
//!     println!("{:<15} {:>8.2} {:>8.2} {:>10.4}", r.name, r.aic, r.bic, r.ks_p_value);
//! }
//! ```
//!
//! ## Medical example — discrete event counts (adverse reactions)
//!
//! ```rust
//! use rs_stats::distributions::fitting::fit_all_discrete;
//!
//! // Adverse drug reaction counts per patient over 6 months
//! let adr_counts = vec![0.0, 1.0, 0.0, 2.0, 0.0, 1.0, 3.0, 0.0, 1.0, 0.0,
//!                       2.0, 1.0, 0.0, 0.0, 1.0, 4.0, 0.0, 2.0, 1.0, 0.0];
//!
//! for r in fit_all_discrete(&adr_counts).unwrap() {
//!     println!("{:<20} AIC={:.2}  KS p={:.3}", r.name, r.aic, r.ks_p_value);
//! }
//! // Poisson usually wins when variance ≈ mean; NegativeBinomial wins if overdispersed
//! ```

use crate::distributions::{
    beta::Beta,
    binomial_distribution::Binomial,
    chi_squared::ChiSquared,
    f_distribution::FDistribution,
    gamma_distribution::Gamma,
    geometric::Geometric,
    lognormal::LogNormal,
    negative_binomial::NegativeBinomial,
    normal_distribution::Normal,
    poisson_distribution::Poisson,
    student_t::StudentT,
    traits::{DiscreteDistribution, Distribution},
    uniform_distribution::Uniform,
    weibull::Weibull,
};
use crate::error::{StatsError, StatsResult};

// ── Data kind detection ────────────────────────────────────────────────────────

/// Whether a dataset looks discrete or continuous.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DataKind {
    /// All values are non-negative integers (whole numbers ≥ 0).
    Discrete,
    /// Contains non-integer or negative values — treated as continuous.
    Continuous,
}

/// Infer whether `data` is discrete (all non-negative integers) or continuous.
///
/// # Examples
/// ```
/// use rs_stats::distributions::fitting::{detect_data_type, DataKind};
///
/// assert_eq!(detect_data_type(&[0.0, 1.0, 2.0, 3.0]), DataKind::Discrete);
/// assert_eq!(detect_data_type(&[0.5, 1.5, 2.3]), DataKind::Continuous);
/// ```
pub fn detect_data_type(data: &[f64]) -> DataKind {
    if data
        .iter()
        .all(|&x| x >= 0.0 && x.fract() == 0.0 && x.is_finite())
    {
        DataKind::Discrete
    } else {
        DataKind::Continuous
    }
}

// ── Kolmogorov-Smirnov test ────────────────────────────────────────────────────

/// Result of a Kolmogorov-Smirnov goodness-of-fit test.
#[derive(Debug, Clone, Copy)]
pub struct KsResult {
    /// KS statistic D (maximum absolute deviation between empirical and theoretical CDF).
    pub statistic: f64,
    /// Approximate two-sided p-value.
    pub p_value: f64,
}

/// Two-sided Kolmogorov-Smirnov test of `data` against `cdf`.
///
/// Uses the Kolmogorov distribution for the p-value approximation.
pub fn ks_test(data: &[f64], cdf: impl Fn(f64) -> f64) -> KsResult {
    let n = data.len();
    if n == 0 {
        return KsResult {
            statistic: 0.0,
            p_value: 1.0,
        };
    }
    let mut sorted = data.to_vec();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let nf = n as f64;
    let mut d = 0.0_f64;
    for (i, &x) in sorted.iter().enumerate() {
        let f = cdf(x);
        let upper = (i + 1) as f64 / nf;
        let lower = i as f64 / nf;
        d = d.max((upper - f).abs()).max((f - lower).abs());
    }

    let p_value = kolmogorov_p(((nf).sqrt() + 0.12 + 0.11 / nf.sqrt()) * d);

    KsResult {
        statistic: d,
        p_value,
    }
}

/// KS test for discrete distributions (uses PMF-based CDF on integer grid).
pub fn ks_test_discrete(data: &[f64], cdf: impl Fn(u64) -> f64) -> KsResult {
    let n = data.len();
    if n == 0 {
        return KsResult {
            statistic: 0.0,
            p_value: 1.0,
        };
    }
    let mut sorted = data.to_vec();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let nf = n as f64;
    let mut d = 0.0_f64;
    for (i, &x) in sorted.iter().enumerate() {
        let k = x.round() as u64;
        let f = cdf(k);
        let upper = (i + 1) as f64 / nf;
        let lower = i as f64 / nf;
        d = d.max((upper - f).abs()).max((f - lower).abs());
    }

    let p_value = kolmogorov_p(((nf).sqrt() + 0.12 + 0.11 / nf.sqrt()) * d);

    KsResult {
        statistic: d,
        p_value,
    }
}

/// Approximate p-value of the Kolmogorov distribution at `x`.
fn kolmogorov_p(x: f64) -> f64 {
    if x <= 0.0 {
        return 1.0;
    }
    // P(K > x) = 2 Σ_{j=1}^∞ (−1)^{j+1} exp(−2j²x²)
    let mut sum = 0.0_f64;
    for j in 1_u32..=100 {
        let term = (-(2.0 * (j as f64).powi(2) * x * x)).exp();
        if j % 2 == 1 {
            sum += term;
        } else {
            sum -= term;
        }
        if term < 1e-15 {
            break;
        }
    }
    (2.0 * sum).clamp(0.0, 1.0)
}

// ── Fit result ─────────────────────────────────────────────────────────────────

/// Summary of a distribution fit.
#[derive(Debug, Clone)]
pub struct FitResult {
    /// Distribution name (e.g. `"Normal"`, `"Gamma"`).
    pub name: String,
    /// Akaike Information Criterion (lower = better).
    pub aic: f64,
    /// Bayesian Information Criterion (lower = better).
    pub bic: f64,
    /// KS test statistic D.
    pub ks_statistic: f64,
    /// KS test p-value (higher = better fit).
    pub ks_p_value: f64,
}

// ── Continuous fitting ─────────────────────────────────────────────────────────

/// Fit all continuous distributions to `data` and return ranked results (by AIC).
///
/// Distributions that fail to fit (e.g. Beta when data are not in (0,1)) are silently skipped.
pub fn fit_all(data: &[f64]) -> StatsResult<Vec<FitResult>> {
    if data.is_empty() {
        return Err(StatsError::InvalidInput {
            message: "fit_all: data must not be empty".to_string(),
        });
    }

    let mut results: Vec<FitResult> = Vec::new();

    macro_rules! try_fit {
        ($dist_type:ty, $fit_expr:expr) => {
            if let Ok(dist) = $fit_expr {
                if let (Ok(aic), Ok(bic)) = (dist.aic(data), dist.bic(data)) {
                    if aic.is_finite() && bic.is_finite() {
                        let ks = ks_test(data, |x| dist.cdf(x).unwrap_or(0.0));
                        results.push(FitResult {
                            name: dist.name().to_string(),
                            aic,
                            bic,
                            ks_statistic: ks.statistic,
                            ks_p_value: ks.p_value,
                        });
                    }
                }
            }
        };
    }

    try_fit!(Normal, Normal::fit(data));
    try_fit!(
        Exponential,
        crate::distributions::exponential_distribution::Exponential::fit(data)
    );
    try_fit!(Uniform, Uniform::fit(data));
    try_fit!(Gamma, Gamma::fit(data));
    try_fit!(LogNormal, LogNormal::fit(data));
    try_fit!(Weibull, Weibull::fit(data));
    try_fit!(Beta, Beta::fit(data));
    try_fit!(StudentT, StudentT::fit(data));
    try_fit!(FDistribution, FDistribution::fit(data));
    try_fit!(ChiSquared, ChiSquared::fit(data));

    if results.is_empty() {
        return Err(StatsError::InvalidInput {
            message: "fit_all: no distribution could be fitted to the data".to_string(),
        });
    }

    results.sort_by(|a, b| {
        a.aic
            .partial_cmp(&b.aic)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    Ok(results)
}

/// Fit all continuous distributions and return the best one (lowest AIC).
pub fn fit_best(data: &[f64]) -> StatsResult<FitResult> {
    let mut all = fit_all(data)?;
    Ok(all.remove(0))
}

// ── Verbose fitting (with diagnostics) ────────────────────────────────────────

/// A distribution candidate that failed to fit, with a human-readable reason.
///
/// Returned alongside successful fits by [`fit_all_verbose`] and [`fit_all_discrete_verbose`].
#[derive(Debug, Clone)]
pub struct SkippedFit {
    /// Distribution name (e.g. `"Beta"`).
    pub name: &'static str,
    /// Why this distribution was not included (e.g. `"fit failed: data must be in (0,1)"`).
    pub reason: String,
}

/// Like [`fit_all`] but also reports which distributions were skipped and why.
///
/// # Examples
/// ```
/// use rs_stats::distributions::fitting::fit_all_verbose;
///
/// // Data outside (0,1): Beta will be skipped
/// let data = vec![2.1, 3.5, 1.8, 4.2, 2.9];
/// let (fitted, skipped) = fit_all_verbose(&data).unwrap();
/// println!("{} distributions fitted, {} skipped", fitted.len(), skipped.len());
/// for s in &skipped {
///     println!("  Skipped {}: {}", s.name, s.reason);
/// }
/// ```
pub fn fit_all_verbose(data: &[f64]) -> StatsResult<(Vec<FitResult>, Vec<SkippedFit>)> {
    if data.is_empty() {
        return Err(StatsError::InvalidInput {
            message: "fit_all_verbose: data must not be empty".to_string(),
        });
    }

    let mut results: Vec<FitResult> = Vec::new();
    let mut skipped: Vec<SkippedFit> = Vec::new();

    macro_rules! try_fit_v {
        ($name:literal, $fit_expr:expr) => {
            match $fit_expr {
                Err(e) => skipped.push(SkippedFit {
                    name: $name,
                    reason: format!("fit failed: {e}"),
                }),
                Ok(dist) => match (dist.aic(data), dist.bic(data)) {
                    (Ok(aic), Ok(bic)) if aic.is_finite() && bic.is_finite() => {
                        let ks = ks_test(data, |x| dist.cdf(x).unwrap_or(0.0));
                        results.push(FitResult {
                            name: dist.name().to_string(),
                            aic,
                            bic,
                            ks_statistic: ks.statistic,
                            ks_p_value: ks.p_value,
                        });
                    }
                    _ => skipped.push(SkippedFit {
                        name: $name,
                        reason: "non-finite AIC/BIC (log-likelihood diverged)".to_string(),
                    }),
                },
            }
        };
    }

    try_fit_v!("Normal", Normal::fit(data));
    try_fit_v!(
        "Exponential",
        crate::distributions::exponential_distribution::Exponential::fit(data)
    );
    try_fit_v!("Uniform", Uniform::fit(data));
    try_fit_v!("Gamma", Gamma::fit(data));
    try_fit_v!("LogNormal", LogNormal::fit(data));
    try_fit_v!("Weibull", Weibull::fit(data));
    try_fit_v!("Beta", Beta::fit(data));
    try_fit_v!("StudentT", StudentT::fit(data));
    try_fit_v!("FDistribution", FDistribution::fit(data));
    try_fit_v!("ChiSquared", ChiSquared::fit(data));

    if results.is_empty() {
        return Err(StatsError::InvalidInput {
            message: "fit_all_verbose: no distribution could be fitted to the data".to_string(),
        });
    }

    results.sort_by(|a, b| {
        a.aic
            .partial_cmp(&b.aic)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    Ok((results, skipped))
}

/// Like [`fit_all_discrete`] but also reports which distributions were skipped and why.
pub fn fit_all_discrete_verbose(data: &[f64]) -> StatsResult<(Vec<FitResult>, Vec<SkippedFit>)> {
    if data.is_empty() {
        return Err(StatsError::InvalidInput {
            message: "fit_all_discrete_verbose: data must not be empty".to_string(),
        });
    }

    let int_data: Vec<u64> = data.iter().map(|&x| x.round() as u64).collect();
    let mut results: Vec<FitResult> = Vec::new();
    let mut skipped: Vec<SkippedFit> = Vec::new();

    macro_rules! try_fit_disc_v {
        ($name:literal, $fit_expr:expr) => {
            match $fit_expr {
                Err(e) => skipped.push(SkippedFit {
                    name: $name,
                    reason: format!("fit failed: {e}"),
                }),
                Ok(dist) => match (dist.aic(&int_data), dist.bic(&int_data)) {
                    (Ok(aic), Ok(bic)) if aic.is_finite() && bic.is_finite() => {
                        let ks = ks_test_discrete(data, |k| dist.cdf(k).unwrap_or(0.0));
                        results.push(FitResult {
                            name: dist.name().to_string(),
                            aic,
                            bic,
                            ks_statistic: ks.statistic,
                            ks_p_value: ks.p_value,
                        });
                    }
                    _ => skipped.push(SkippedFit {
                        name: $name,
                        reason: "non-finite AIC/BIC (log-likelihood diverged)".to_string(),
                    }),
                },
            }
        };
    }

    try_fit_disc_v!("Poisson", Poisson::fit(data));
    try_fit_disc_v!("Geometric", Geometric::fit(data));
    try_fit_disc_v!("NegativeBinomial", NegativeBinomial::fit(data));
    try_fit_disc_v!("Binomial", Binomial::fit(data));

    if results.is_empty() {
        return Err(StatsError::InvalidInput {
            message: "fit_all_discrete_verbose: no distribution could be fitted".to_string(),
        });
    }

    results.sort_by(|a, b| {
        a.aic
            .partial_cmp(&b.aic)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    Ok((results, skipped))
}

// ── Discrete fitting ───────────────────────────────────────────────────────────

/// Fit all discrete distributions to integer `data` (passed as f64) and return ranked results.
///
/// Skips distributions that cannot be fitted.
pub fn fit_all_discrete(data: &[f64]) -> StatsResult<Vec<FitResult>> {
    if data.is_empty() {
        return Err(StatsError::InvalidInput {
            message: "fit_all_discrete: data must not be empty".to_string(),
        });
    }

    // Convert to u64 for discrete distributions
    let int_data: Vec<u64> = data.iter().map(|&x| x.round() as u64).collect();

    let mut results: Vec<FitResult> = Vec::new();

    macro_rules! try_fit_disc {
        ($fit_expr:expr) => {
            if let Ok(dist) = $fit_expr {
                if let (Ok(aic), Ok(bic)) = (dist.aic(&int_data), dist.bic(&int_data)) {
                    if aic.is_finite() && bic.is_finite() {
                        let ks = ks_test_discrete(data, |k| dist.cdf(k).unwrap_or(0.0));
                        results.push(FitResult {
                            name: dist.name().to_string(),
                            aic,
                            bic,
                            ks_statistic: ks.statistic,
                            ks_p_value: ks.p_value,
                        });
                    }
                }
            }
        };
    }

    try_fit_disc!(Poisson::fit(data));
    try_fit_disc!(Geometric::fit(data));
    try_fit_disc!(NegativeBinomial::fit(data));
    try_fit_disc!(Binomial::fit(data));

    if results.is_empty() {
        return Err(StatsError::InvalidInput {
            message: "fit_all_discrete: no distribution could be fitted to the data".to_string(),
        });
    }

    results.sort_by(|a, b| {
        a.aic
            .partial_cmp(&b.aic)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    Ok(results)
}

/// Fit discrete distributions and return the best (lowest AIC).
pub fn fit_best_discrete(data: &[f64]) -> StatsResult<FitResult> {
    let mut all = fit_all_discrete(data)?;
    Ok(all.remove(0))
}

// ── Auto-detect and fit ────────────────────────────────────────────────────────

/// Automatically detect whether data is discrete or continuous, then fit all applicable
/// distributions and return the best match (lowest AIC).
///
/// # Examples
/// ```
/// use rs_stats::distributions::fitting::auto_fit;
///
/// let data = vec![1.2, 2.3, 1.8, 2.9, 1.5];
/// let best = auto_fit(&data).unwrap();
/// println!("Best fit: {}", best.name);
/// ```
pub fn auto_fit(data: &[f64]) -> StatsResult<FitResult> {
    match detect_data_type(data) {
        DataKind::Discrete => fit_best_discrete(data),
        DataKind::Continuous => fit_best(data),
    }
}

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

    #[test]
    fn test_detect_data_type_discrete() {
        assert_eq!(detect_data_type(&[0.0, 1.0, 2.0, 3.0]), DataKind::Discrete);
        assert_eq!(detect_data_type(&[0.0, 0.0, 1.0]), DataKind::Discrete);
    }

    #[test]
    fn test_detect_data_type_continuous() {
        assert_eq!(detect_data_type(&[0.5, 1.5, 2.3]), DataKind::Continuous);
        assert_eq!(detect_data_type(&[-1.0, 0.0, 1.0]), DataKind::Continuous);
        assert_eq!(detect_data_type(&[1.0, 2.5, 3.0]), DataKind::Continuous);
    }

    #[test]
    fn test_ks_test_uniform() {
        // Data from Uniform(0,1) should give large p-value against U(0,1) CDF
        let data: Vec<f64> = (0..20).map(|i| i as f64 / 20.0).collect();
        let ks = ks_test(&data, |x| x.clamp(0.0, 1.0));
        assert!(ks.statistic < 0.15);
    }

    #[test]
    fn test_fit_all_returns_results() {
        let data: Vec<f64> = (0..50).map(|i| (i as f64) * 0.1 + 0.5).collect();
        let results = fit_all(&data).unwrap();
        assert!(!results.is_empty());
        // Results sorted by AIC (ascending)
        for i in 1..results.len() {
            assert!(results[i].aic >= results[i - 1].aic);
        }
    }

    #[test]
    fn test_fit_best_normal_data() {
        // Data generated from N(5, 1)
        let data = vec![
            4.1, 5.2, 5.8, 4.7, 5.3, 4.9, 6.1, 4.5, 5.5, 5.0, 4.8, 5.1, 4.3, 5.7, 4.6, 5.4, 4.2,
            5.9, 5.2, 4.4,
        ];
        let best = fit_best(&data).unwrap();
        // Normal should win (or be competitive)
        assert!(best.aic.is_finite());
    }

    #[test]
    fn test_fit_all_discrete() {
        let data = vec![0.0, 1.0, 2.0, 3.0, 1.0, 0.0, 2.0, 1.0, 0.0, 4.0];
        let results = fit_all_discrete(&data).unwrap();
        assert!(!results.is_empty());
    }

    #[test]
    fn test_auto_fit_continuous() {
        let data = vec![1.5, 2.3, 1.8, 2.1, 2.7, 1.9, 2.4, 2.0];
        let best = auto_fit(&data).unwrap();
        assert!(!best.name.is_empty());
    }

    #[test]
    fn test_auto_fit_discrete() {
        let data = vec![0.0, 1.0, 2.0, 1.0, 0.0, 3.0, 1.0, 2.0];
        let best = auto_fit(&data).unwrap();
        assert!(!best.name.is_empty());
    }

    #[test]
    fn test_fit_all_empty_data() {
        assert!(fit_all(&[]).is_err());
    }
}