git-perf 0.20.0

Track, plot, and statistically validate simple measurements using git-notes for storage
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
use std::fmt::Display;

use average::{self, concatenate, Estimate, Mean, Variance};
use itertools::Itertools;

use readable::num::*;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReductionFunc {
    Min,
    Max,
    Median,
    Mean,
}

impl Display for ReductionFunc {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            ReductionFunc::Min => "min",
            ReductionFunc::Max => "max",
            ReductionFunc::Median => "median",
            ReductionFunc::Mean => "mean",
        };
        write!(f, "{}", s)
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DispersionMethod {
    StandardDeviation,
    MedianAbsoluteDeviation,
}

// Conversion from CLI types to stats types
impl From<git_perf_cli_types::ReductionFunc> for ReductionFunc {
    fn from(func: git_perf_cli_types::ReductionFunc) -> Self {
        match func {
            git_perf_cli_types::ReductionFunc::Min => ReductionFunc::Min,
            git_perf_cli_types::ReductionFunc::Max => ReductionFunc::Max,
            git_perf_cli_types::ReductionFunc::Median => ReductionFunc::Median,
            git_perf_cli_types::ReductionFunc::Mean => ReductionFunc::Mean,
        }
    }
}

impl From<git_perf_cli_types::DispersionMethod> for DispersionMethod {
    fn from(method: git_perf_cli_types::DispersionMethod) -> Self {
        match method {
            git_perf_cli_types::DispersionMethod::StandardDeviation => {
                DispersionMethod::StandardDeviation
            }
            git_perf_cli_types::DispersionMethod::MedianAbsoluteDeviation => {
                DispersionMethod::MedianAbsoluteDeviation
            }
        }
    }
}

pub trait VecAggregation {
    fn median(&mut self) -> Option<f64>;
}

concatenate!(AggStats, [Mean, mean], [Variance, sample_variance]);

pub fn aggregate_measurements<'a>(measurements: impl Iterator<Item = &'a f64>) -> Stats {
    let measurements_vec: Vec<f64> = measurements.cloned().collect();
    let s: AggStats = measurements_vec.iter().collect();
    Stats {
        mean: s.mean(),
        stddev: s.sample_variance().sqrt(),
        mad: calculate_mad(&measurements_vec),
        len: s.mean.len() as usize,
    }
}

#[must_use]
pub fn calculate_mad(measurements: &[f64]) -> f64 {
    if measurements.is_empty() {
        return 0.0;
    }

    // Calculate median without modifying original data
    let mut measurements_copy = measurements.to_vec();
    let median = measurements_copy.median().unwrap();

    // Calculate absolute deviations
    let mut abs_deviations: Vec<f64> = measurements.iter().map(|&x| (x - median).abs()).collect();

    // Calculate median of absolute deviations
    abs_deviations.median().unwrap()
}

#[derive(Debug)]
pub struct Stats {
    pub mean: f64,
    pub stddev: f64,
    pub mad: f64,
    pub len: usize,
}

impl Display for Stats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let stddev_str = if self.stddev.is_nan() {
            "N/A".to_string()
        } else {
            format!("{}", Float::from(self.stddev))
        };
        let mad_str = if self.mad.is_nan() {
            "N/A".to_string()
        } else {
            format!("{}", Float::from(self.mad))
        };
        write!(
            f,
            "μ: {} σ: {} MAD: {} n: {}",
            Float::from(self.mean),
            stddev_str,
            mad_str,
            Unsigned::from(self.len),
        )
    }
}

impl Stats {
    #[must_use]
    pub fn z_score(&self, other: &Stats) -> f64 {
        self.z_score_with_method(other, DispersionMethod::StandardDeviation)
    }

    #[must_use]
    pub fn z_score_with_method(&self, other: &Stats, method: DispersionMethod) -> f64 {
        assert!(self.len == 1);
        assert!(other.len >= 1);

        let dispersion = match method {
            DispersionMethod::StandardDeviation => other.stddev,
            DispersionMethod::MedianAbsoluteDeviation => other.mad,
        };

        // Division by zero is an expected case here: For measurements with no variance
        (self.mean - other.mean).abs() / dispersion
    }

    #[must_use]
    pub fn is_significant(&self, other: &Stats, sigma: f64, method: DispersionMethod) -> bool {
        let z_score = self.z_score_with_method(other, method);
        z_score > sigma
    }
}

/// A wrapper around Stats that includes an optional unit for the mean value.
/// When displayed, only the mean (μ) will have the unit suffix.
/// Sigma (σ) and MAD remain unitless as they are dispersion measures.
pub struct StatsWithUnit<'a> {
    pub stats: &'a Stats,
    pub unit: Option<&'a str>,
}

impl<'a> Display for StatsWithUnit<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use crate::units::{format_measurement, parse_value_with_unit, Measurement};

        match self.unit {
            Some(u) => {
                // Try to parse and format the mean value with auto-scaling
                let mean_measurement = parse_value_with_unit(self.stats.mean, u);
                let mean_display = match &mean_measurement {
                    Ok(measurement) if !matches!(measurement, Measurement::Count(_)) => {
                        format_measurement(measurement.clone())
                    }
                    _ => format!("{} {}", Float::from(self.stats.mean), u),
                };

                // Try to parse and format stddev with auto-scaling
                let stddev_measurement = parse_value_with_unit(self.stats.stddev, u);
                let stddev_display = match &stddev_measurement {
                    Ok(measurement) if !matches!(measurement, Measurement::Count(_)) => {
                        format_measurement(measurement.clone())
                    }
                    _ if self.stats.stddev.is_nan() => "N/A".to_string(),
                    _ => format!("{}", Float::from(self.stats.stddev)),
                };

                // Try to parse and format MAD with auto-scaling
                let mad_measurement = parse_value_with_unit(self.stats.mad, u);
                let mad_display = match &mad_measurement {
                    Ok(measurement) if !matches!(measurement, Measurement::Count(_)) => {
                        format_measurement(measurement.clone())
                    }
                    _ if self.stats.mad.is_nan() => "N/A".to_string(),
                    _ => format!("{}", Float::from(self.stats.mad)),
                };

                write!(
                    f,
                    "μ: {} σ: {} MAD: {} n: {}",
                    mean_display,
                    stddev_display,
                    mad_display,
                    Unsigned::from(self.stats.len)
                )
            }
            None => write!(f, "{}", self.stats),
        }
    }
}

impl VecAggregation for Vec<f64> {
    fn median(&mut self) -> Option<f64> {
        self.sort_by(f64::total_cmp);
        match self.len() {
            0 => None,
            even if even % 2 == 0 => {
                let left = self[even / 2 - 1];
                let right = self[even / 2];
                Some((left + right) / 2.0)
            }
            odd => Some(self[odd / 2]),
        }
    }
}

pub trait NumericReductionFunc: Iterator<Item = f64> {
    fn aggregate_by(&mut self, fun: ReductionFunc) -> Option<Self::Item> {
        match fun {
            ReductionFunc::Min => self.reduce(f64::min),
            ReductionFunc::Max => self.reduce(f64::max),
            ReductionFunc::Median => self.collect_vec().median(),
            ReductionFunc::Mean => {
                let stats: AggStats = self.collect();
                if stats.mean.is_empty() {
                    None
                } else {
                    Some(stats.mean())
                }
            }
        }
    }
}

impl<T> NumericReductionFunc for T where T: Iterator<Item = f64> {}

#[cfg(test)]
mod test {
    use average::assert_almost_eq;

    use super::*;

    #[test]
    fn no_floating_error() {
        let measurements = (0..100).map(|_| 0.1).collect_vec();
        let stats = aggregate_measurements(measurements.iter());
        assert_eq!(stats.mean, 0.1);
        assert_eq!(stats.len, 100);
        let naive_mean = (0..100).map(|_| 0.1).sum::<f64>() / 100.0;
        assert_ne!(naive_mean, 0.1);
    }

    #[test]
    fn single_measurement() {
        let measurements = [1.0];
        let stats = aggregate_measurements(measurements.iter());
        assert_eq!(stats.len, 1);
        assert_eq!(stats.mean, 1.0);
        // The average crate 0.16+ returns NaN for sample variance when n <= 1,
        // which is mathematically correct (sample variance undefined for n < 2).
        assert!(
            stats.stddev.is_nan(),
            "stddev should be NaN for single measurement"
        );
    }

    #[test]
    fn no_measurement() {
        let measurements = [];
        let stats = aggregate_measurements(measurements.iter());
        assert_eq!(stats.len, 0);
        // The average crate 0.16+ returns NaN for mean when n == 0.
        assert!(
            stats.mean.is_nan(),
            "mean should be NaN for empty measurements"
        );
        assert!(
            stats.stddev.is_nan(),
            "stddev should be NaN for empty measurements"
        );
    }

    #[test]
    fn z_score_with_zero_stddev() {
        let tail = Stats {
            mean: 30.0,
            stddev: 0.0,
            mad: 0.0,
            len: 40,
        };

        let head_normal = Stats {
            mean: 30.0,
            stddev: 0.0,
            mad: 0.0,
            len: 1,
        };

        let head_low = Stats {
            mean: 20.0,
            stddev: 0.0,
            mad: 0.0,
            len: 1,
        };

        let z_normal = head_normal.z_score(&tail);
        assert!(z_normal.is_nan());

        let z_low = head_low.z_score(&tail);
        assert!(z_low.is_infinite());
    }

    #[test]
    fn verify_stats() {
        let empty_vec = [];
        assert_eq!(None, empty_vec.into_iter().aggregate_by(ReductionFunc::Min));
        assert_eq!(None, empty_vec.into_iter().aggregate_by(ReductionFunc::Max));
        assert_eq!(
            None,
            empty_vec.into_iter().aggregate_by(ReductionFunc::Median)
        );
        assert_eq!(
            None,
            empty_vec.into_iter().aggregate_by(ReductionFunc::Mean)
        );

        let single_el_vec = [3.0];
        assert_eq!(
            Some(3.0),
            single_el_vec.into_iter().aggregate_by(ReductionFunc::Min)
        );
        assert_eq!(
            Some(3.0),
            single_el_vec.into_iter().aggregate_by(ReductionFunc::Max)
        );
        assert_eq!(
            Some(3.0),
            single_el_vec
                .into_iter()
                .aggregate_by(ReductionFunc::Median)
        );
        assert_eq!(
            Some(3.0),
            single_el_vec.into_iter().aggregate_by(ReductionFunc::Mean)
        );

        let two_el_vec = [3.0, 1.0];
        assert_eq!(
            Some(1.0),
            two_el_vec.into_iter().aggregate_by(ReductionFunc::Min)
        );
        assert_eq!(
            Some(3.0),
            two_el_vec.into_iter().aggregate_by(ReductionFunc::Max)
        );
        assert_eq!(
            Some(2.0),
            two_el_vec.into_iter().aggregate_by(ReductionFunc::Median)
        );
        assert_eq!(
            Some(2.0),
            two_el_vec.into_iter().aggregate_by(ReductionFunc::Mean)
        );

        let three_el_vec = [2.0, 6.0, 1.0];
        assert_eq!(
            Some(1.0),
            three_el_vec.into_iter().aggregate_by(ReductionFunc::Min)
        );
        assert_eq!(
            Some(6.0),
            three_el_vec.into_iter().aggregate_by(ReductionFunc::Max)
        );
        assert_eq!(
            Some(2.0),
            three_el_vec.into_iter().aggregate_by(ReductionFunc::Median)
        );
        assert_eq!(
            Some(3.0),
            three_el_vec.into_iter().aggregate_by(ReductionFunc::Mean)
        );
    }

    #[test]
    fn test_calculate_mad() {
        // Test empty array
        assert_eq!(calculate_mad(&[]), 0.0);

        // Test single value
        assert_eq!(calculate_mad(&[5.0]), 0.0);

        // Test two values
        assert_eq!(calculate_mad(&[1.0, 3.0]), 1.0);

        // Test three values
        assert_eq!(calculate_mad(&[1.0, 2.0, 3.0]), 1.0);

        // Test with outliers
        let data = [1.0, 2.0, 3.0, 100.0];
        let mad = calculate_mad(&data);
        assert_almost_eq!(mad, 1.0, 0.001);
        // assert!(mad > 0.0);
        // assert!(mad < 50.0); // Should be robust to outliers

        // Test with known MAD value
        let data = [1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0];
        let mad = calculate_mad(&data);
        assert_almost_eq!(mad, 1.0, 0.001);
    }

    #[test]
    fn test_mad_in_aggregate_measurements() {
        let measurements = [1.0, 2.0, 3.0, 4.0, 5.0];
        let stats = aggregate_measurements(measurements.iter());

        assert_eq!(stats.len, 5);
        assert_eq!(stats.mean, 3.0);
        assert!(stats.mad > 0.0);
        assert!(stats.stddev > 0.0);

        // MAD should be less than stddev for normal distributions
        assert!(stats.mad < stats.stddev);
    }

    #[test]
    fn test_z_score_with_mad() {
        let tail = Stats {
            mean: 30.0,
            stddev: 5.0,
            mad: 3.0,
            len: 40,
        };

        let head = Stats {
            mean: 35.0,
            stddev: 0.0,
            mad: 0.0,
            len: 1,
        };

        let z_score_stddev = head.z_score_with_method(&tail, DispersionMethod::StandardDeviation);
        let z_score_mad =
            head.z_score_with_method(&tail, DispersionMethod::MedianAbsoluteDeviation);

        assert_eq!(z_score_stddev, 1.0); // (35-30)/5 = 1.0
        assert_eq!(z_score_mad, 5.0 / 3.0); // (35-30)/3 ≈ 1.67

        // MAD z-score should be different from stddev z-score
        assert_ne!(z_score_stddev, z_score_mad);
    }

    #[test]
    fn test_backward_compatibility() {
        // Test that existing z_score method still works
        let tail = Stats {
            mean: 30.0,
            stddev: 5.0,
            mad: 3.0,
            len: 40,
        };

        let head = Stats {
            mean: 35.0,
            stddev: 0.0,
            mad: 0.0,
            len: 1,
        };

        let z_score_old = head.z_score(&tail);
        let z_score_new = head.z_score_with_method(&tail, DispersionMethod::StandardDeviation);

        assert_eq!(z_score_old, z_score_new);
    }

    #[test]
    fn test_display_with_mad() {
        let stats = Stats {
            mean: 10.0,
            stddev: 2.0,
            mad: 1.5,
            len: 5,
        };

        let display = format!("{}", stats);
        assert!(display.contains("μ: 10"));
        assert!(display.contains("σ: 2"));
        assert!(display.contains("MAD: 1.5"));
        assert!(display.contains("n: 5"));
    }

    #[test]
    fn test_stats_with_unit() {
        let stats = Stats {
            mean: 1_234.5,
            stddev: 123.4,
            mad: 98.7,
            len: 10,
        };

        // Test with unit - values should be auto-scaled (1234.5ms → 1.23s)
        let with_unit = StatsWithUnit {
            stats: &stats,
            unit: Some("ms"),
        };
        let formatted = format!("{}", with_unit);

        // Mean should be auto-scaled from 1234.5ms to ~1.23s
        assert!(
            formatted.contains("μ: 1.23s") || formatted.contains("μ: 1.2s"),
            "Mean should be auto-scaled to seconds: {}",
            formatted
        );
        // Stddev should be auto-scaled from 123.4ms to ~123ms or 123.4ms
        assert!(
            formatted.contains("σ: 123") && formatted.contains("ms"),
            "Stddev should be auto-scaled: {}",
            formatted
        );
        // MAD should be auto-scaled from 98.7ms to ~98ms or 98.7ms
        assert!(
            formatted.contains("MAD: 98") && formatted.contains("ms"),
            "MAD should be auto-scaled: {}",
            formatted
        );
        assert!(
            formatted.contains("n: 10"),
            "Count should be present: {}",
            formatted
        );

        // Test without unit (should match Display trait)
        let without_unit = StatsWithUnit {
            stats: &stats,
            unit: None,
        };
        let formatted_without = format!("{}", without_unit);
        let display_format = format!("{}", stats);
        assert_eq!(
            formatted_without, display_format,
            "StatsWithUnit with None should match Stats Display"
        );

        // Test with large values - should be auto-scaled (1234567.89ns → 1.23ms)
        let large_stats = Stats {
            mean: 1_234_567.89, // nanoseconds
            stddev: 123_456.78,
            mad: 12_345.67,
            len: 1000,
        };

        let large_with_unit = StatsWithUnit {
            stats: &large_stats,
            unit: Some("ns"),
        };
        let large_formatted = format!("{}", large_with_unit);

        // Mean should be auto-scaled from nanoseconds to milliseconds
        assert!(
            large_formatted.contains("μ: 1.23ms") || large_formatted.contains("μ: 1.2ms"),
            "Large mean should be auto-scaled to ms: {}",
            large_formatted
        );
        // Stddev should be auto-scaled appropriately
        assert!(
            large_formatted.contains("σ:")
                && (large_formatted.contains("ms") || large_formatted.contains("μs")),
            "Large stddev should be auto-scaled: {}",
            large_formatted
        );
        // MAD should be auto-scaled appropriately
        assert!(
            large_formatted.contains("MAD:")
                && (large_formatted.contains("ms") || large_formatted.contains("μs")),
            "Large MAD should be auto-scaled: {}",
            large_formatted
        );
        assert!(
            large_formatted.contains("n: 1,000") || large_formatted.contains("n: 1000"),
            "Large count should be present: {}",
            large_formatted
        );
    }

    #[test]
    fn test_stats_with_unit_various_values() {
        // Test various edge cases and value types

        // Small decimal values - should remain in ms (no auto-scaling needed)
        let small_stats = Stats {
            mean: 42.5,
            stddev: 2.0,
            mad: 1.5,
            len: 5,
        };
        let formatted = format!(
            "{}",
            StatsWithUnit {
                stats: &small_stats,
                unit: Some("ms")
            }
        );
        assert!(
            formatted.contains("42.5ms") || formatted.contains("42ms"),
            "Small decimal with unit: {}",
            formatted
        );

        // Zero value - should be formatted as 0ns or 0ms
        let zero_stats = Stats {
            mean: 0.0,
            stddev: 0.0,
            mad: 0.0,
            len: 1,
        };
        let formatted = format!(
            "{}",
            StatsWithUnit {
                stats: &zero_stats,
                unit: Some("ms")
            }
        );
        assert!(
            formatted.contains("0") && formatted.contains("ns"),
            "Zero value with unit: {}",
            formatted
        );

        // Value with more precision - "seconds" is unknown unit, falls back to Count
        let precise_stats = Stats {
            mean: 3.21, // Arbitrary value to avoid clippy::approx_constant warning
            stddev: 0.5,
            mad: 0.3,
            len: 10,
        };
        let formatted = format!(
            "{}",
            StatsWithUnit {
                stats: &precise_stats,
                unit: Some("seconds")
            }
        );
        assert!(
            formatted.contains("3.21") && formatted.contains("seconds"),
            "Precise value with unknown unit (fallback): {}",
            formatted
        );

        // Large round number - should be auto-scaled if using "B" unit
        let million_stats = Stats {
            mean: 1_000_000.0,
            stddev: 50_000.0,
            mad: 30_000.0,
            len: 100,
        };
        let formatted = format!(
            "{}",
            StatsWithUnit {
                stats: &million_stats,
                unit: Some("B")
            }
        );
        // 1,000,000 B = 1 MB
        assert!(
            formatted.contains("1MB") || formatted.contains("1.0MB"),
            "Million bytes should be auto-scaled to MB: {}",
            formatted
        );

        // Different unit types - unknown unit falls back to Count format
        let temp_stats = Stats {
            mean: 98.6,
            stddev: 1.2,
            mad: 0.8,
            len: 20,
        };
        let formatted = format!(
            "{}",
            StatsWithUnit {
                stats: &temp_stats,
                unit: Some("°F")
            }
        );
        assert!(
            formatted.contains("98.6") && formatted.contains("°F"),
            "Temperature unit (unknown, fallback): {}",
            formatted
        );

        // Without unit - no unit should appear anywhere
        let no_unit = format!(
            "{}",
            StatsWithUnit {
                stats: &small_stats,
                unit: None
            }
        );
        assert!(
            !no_unit.contains(" ms"),
            "Should have no units: {}",
            no_unit
        );
        assert!(
            !no_unit.contains(" bytes"),
            "Should have no units: {}",
            no_unit
        );
    }

    #[test]
    fn test_thousands_separator_with_unknown_unit() {
        // Test that thousands separators are maintained for unknown units
        // This uses the readable crate's Float formatter which adds separators
        let large_stats = Stats {
            mean: 12_345.67,
            stddev: 1_234.56,
            mad: 567.89,
            len: 100,
        };

        let formatted = format!(
            "{}",
            StatsWithUnit {
                stats: &large_stats,
                unit: Some("widgets") // Unknown unit
            }
        );

        // The Float formatter from readable crate should add thousands separators
        assert!(
            formatted.contains("12,345") || formatted.contains("12_345"),
            "Mean should have thousands separators for unknown unit, got: {}",
            formatted
        );

        assert!(
            formatted.contains("widgets"),
            "Unknown unit should be preserved, got: {}",
            formatted
        );

        // Verify stddev also has separators
        assert!(
            formatted.contains("1,234") || formatted.contains("1_234"),
            "Stddev should have thousands separators, got: {}",
            formatted
        );
    }

    #[test]
    fn test_is_significant_boundary() {
        // COVERS MUTATION: z_score > sigma vs >=
        let tail = Stats {
            mean: 10.0,
            stddev: 2.0,
            mad: 1.5,
            len: 5,
        };

        let head = Stats {
            mean: 12.0, // z_score = (12-10)/2 = 1.0
            stddev: 0.0,
            mad: 0.0,
            len: 1,
        };

        // Test boundary: z_score = 1.0, sigma = 1.0
        // Should NOT be significant (z_score is not > sigma)
        assert!(!head.is_significant(&tail, 1.0, DispersionMethod::StandardDeviation));

        // Test just above boundary: z_score = 1.0, sigma = 0.9
        // Should be significant (z_score > sigma)
        assert!(head.is_significant(&tail, 0.9, DispersionMethod::StandardDeviation));

        // Test just below boundary: z_score = 1.0, sigma = 1.1
        // Should NOT be significant (z_score is not > sigma)
        assert!(!head.is_significant(&tail, 1.1, DispersionMethod::StandardDeviation));

        // Test with MAD
        let head_mad = Stats {
            mean: 11.5, // z_score = (11.5-10)/1.5 = 1.0
            stddev: 0.0,
            mad: 0.0,
            len: 1,
        };

        // Test boundary with MAD: z_score = 1.0, sigma = 1.0
        assert!(!head_mad.is_significant(&tail, 1.0, DispersionMethod::MedianAbsoluteDeviation));
        assert!(head_mad.is_significant(&tail, 0.9, DispersionMethod::MedianAbsoluteDeviation));
        assert!(!head_mad.is_significant(&tail, 1.1, DispersionMethod::MedianAbsoluteDeviation));
    }
}