optirs-nas 0.3.2

OptiRS Neural Architecture Search and hyperparameter optimization
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
//! Statistical analysis for evaluation results
//!
//! Provides statistical tests, analysis methods, and confidence intervals.

use scirs2_core::numeric::Float;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt::Debug;

use super::benchmark::TestResult;
use super::types::*;
use crate::error::{OptimError, Result};
use crate::numeric::{count_as, scalar_as};
use crate::EvaluationMetric;

/// Statistical analyzer for evaluation results
///
/// `analysis_methods` and `multiple_comparison` were configuration the analyzer
/// stored and never consulted: it always produced descriptive statistics and
/// never corrected a p-value, whatever they said. Both now drive behaviour —
/// see [`StatisticalAnalyzer::analyze`] and
/// [`StatisticalAnalyzer::adjusted_p_values`] — and the recorded
/// [`StatisticalTest`]s they operate on are readable through
/// [`StatisticalAnalyzer::tests`].
#[derive(Debug)]
pub struct StatisticalAnalyzer<T: Float + Debug + Send + Sync + 'static> {
    /// Statistical tests
    statistical_tests: Vec<StatisticalTest<T>>,

    /// Analyses [`StatisticalAnalyzer::analyze`] performs, in order.
    analysis_methods: Vec<AnalysisMethod>,

    /// Significance thresholds
    significance_thresholds: HashMap<String, T>,

    /// Family-wise / false-discovery correction applied by
    /// [`StatisticalAnalyzer::adjusted_p_values`].
    multiple_comparison: MultipleComparisonCorrection,
}

/// Result of running the analyzer's enabled analyses over one sample.
#[derive(Debug, Clone)]
pub struct AnalysisReport<T: Float + Debug + Send + Sync + 'static> {
    /// Descriptive statistics, present when
    /// [`AnalysisMethod::DescriptiveStatistics`] is enabled.
    pub descriptive: Option<DescriptiveStats<T>>,

    /// Lag-1 autocorrelation of the sample, present when
    /// [`AnalysisMethod::CorrelationAnalysis`] is enabled and the sample has at
    /// least two points and non-zero variance. `None` for a constant or
    /// too-short sample: an autocorrelation is undefined there, and reporting
    /// `0` would claim "no correlation" where the truth is "no answer".
    pub lag1_autocorrelation: Option<T>,
}

/// Statistical test
#[derive(Debug)]
pub struct StatisticalTest<T: Float + Debug + Send + Sync + 'static> {
    /// Test name
    pub name: String,

    /// Test type
    pub test_type: StatisticalTestType,

    /// Test statistic
    pub test_statistic: T,

    /// P-value
    pub p_value: T,

    /// Effect size
    pub effect_size: T,

    /// Confidence interval
    pub confidence_interval: (T, T),
}

impl<T: Float + Debug + Default + std::iter::Sum + Send + Sync> StatisticalAnalyzer<T> {
    pub(crate) fn new() -> Self {
        Self {
            statistical_tests: Vec::new(),
            analysis_methods: vec![
                AnalysisMethod::DescriptiveStatistics,
                AnalysisMethod::CorrelationAnalysis,
            ],
            significance_thresholds: {
                let mut thresholds = HashMap::new();
                // Unreachable for `f32`/`f64` (see `crate::numeric`). If a
                // caller's `Float` type did reject 0.05, a threshold of zero
                // admits nothing — the conservative direction. A fallback of one
                // would declare every test significant, which is precisely the
                // silent-wrong-number failure `crate::numeric` exists to avoid.
                thresholds.insert(
                    "alpha".to_string(),
                    scalar_as::<T>(0.05, "default significance level")
                        .unwrap_or_else(|_| T::zero()),
                );
                thresholds
            },
            multiple_comparison: MultipleComparisonCorrection::BenjaminiHochberg,
        }
    }

    /// Analyses this analyzer performs, in order.
    pub fn analysis_methods(&self) -> &[AnalysisMethod] {
        &self.analysis_methods
    }

    /// Choose which analyses [`StatisticalAnalyzer::analyze`] performs.
    ///
    /// Only the methods this analyzer actually implements are accepted. Enabling
    /// one it cannot perform is reported as [`OptimError::NotImplemented`] rather
    /// than accepted and silently skipped, which would let a caller believe a
    /// regression or cluster analysis had run.
    pub fn set_analysis_methods(&mut self, methods: Vec<AnalysisMethod>) -> Result<()> {
        for method in &methods {
            match method {
                AnalysisMethod::DescriptiveStatistics | AnalysisMethod::CorrelationAnalysis => {}
                other => {
                    return Err(OptimError::NotImplemented(format!(
                        "StatisticalAnalyzer implements DescriptiveStatistics and \
                         CorrelationAnalysis; {:?} has no implementation here",
                        other
                    )))
                }
            }
        }
        self.analysis_methods = methods;
        Ok(())
    }

    /// Run every enabled analysis over `values`.
    pub fn analyze(&self, values: &[T]) -> Result<AnalysisReport<T>> {
        let mut report = AnalysisReport {
            descriptive: None,
            lag1_autocorrelation: None,
        };
        for method in &self.analysis_methods {
            match method {
                AnalysisMethod::DescriptiveStatistics => {
                    report.descriptive = Some(self.compute_descriptive_stats(values)?);
                }
                AnalysisMethod::CorrelationAnalysis => {
                    report.lag1_autocorrelation = self.lag1_autocorrelation(values)?;
                }
                other => {
                    return Err(OptimError::NotImplemented(format!(
                        "StatisticalAnalyzer has no implementation for {:?}",
                        other
                    )))
                }
            }
        }
        Ok(report)
    }

    /// Lag-1 autocorrelation of `values`, or `None` when it is undefined.
    fn lag1_autocorrelation(&self, values: &[T]) -> Result<Option<T>> {
        if values.len() < 2 {
            return Ok(None);
        }
        let count: T = count_as(values.len(), "sample size")?;
        let mean = values.iter().cloned().sum::<T>() / count;

        let mut denominator = T::zero();
        for &value in values {
            let centered = value - mean;
            denominator = denominator + centered * centered;
        }
        if denominator <= T::zero() {
            // A constant series has no autocorrelation to report.
            return Ok(None);
        }

        let mut numerator = T::zero();
        for window in values.windows(2) {
            numerator = numerator + (window[0] - mean) * (window[1] - mean);
        }
        Ok(Some(numerator / denominator))
    }

    /// Recorded statistical tests, in the order they were added.
    pub fn tests(&self) -> &[StatisticalTest<T>] {
        &self.statistical_tests
    }

    /// Multiple-comparison correction applied by
    /// [`StatisticalAnalyzer::adjusted_p_values`].
    pub fn multiple_comparison(&self) -> &MultipleComparisonCorrection {
        &self.multiple_comparison
    }

    /// Choose the multiple-comparison correction.
    pub fn set_multiple_comparison(&mut self, correction: MultipleComparisonCorrection) {
        self.multiple_comparison = correction;
    }

    /// p-values of the recorded tests after the configured multiple-comparison
    /// correction, in the order the tests were added.
    ///
    /// Every correction is closed-form and clamped to `[0, 1]`:
    ///
    /// * `None` — the raw p-values.
    /// * `Bonferroni` — `m * p`.
    /// * `Sidak` — `1 - (1 - p)^m`.
    /// * `HolmBonferroni` — step-down `(m - i) * p`, made monotone.
    /// * `BenjaminiHochberg` — step-up `m * p / (i + 1)`, made monotone.
    /// * `BenjaminiYekutieli` — Benjamini-Hochberg scaled by the harmonic number
    ///   `sum_{k=1..m} 1/k`, which is the dependence-robust variant.
    pub fn adjusted_p_values(&self) -> Result<Vec<T>> {
        let m = self.statistical_tests.len();
        if m == 0 {
            return Ok(Vec::new());
        }
        let raw: Vec<T> = self
            .statistical_tests
            .iter()
            .map(|test| test.p_value)
            .collect();
        let count: T = count_as(m, "number of statistical tests")?;

        let adjusted = match self.multiple_comparison {
            MultipleComparisonCorrection::None => raw,
            MultipleComparisonCorrection::Bonferroni => {
                raw.into_iter().map(|p| (p * count).min(T::one())).collect()
            }
            MultipleComparisonCorrection::Sidak => raw
                .into_iter()
                .map(|p| {
                    let complement = (T::one() - p).max(T::zero());
                    (T::one() - complement.powf(count)).min(T::one())
                })
                .collect(),
            MultipleComparisonCorrection::HolmBonferroni => Self::step_down(&raw, |rank, p| {
                let factor: T = count_as((m - rank).max(1), "Holm rank factor")?;
                Ok((p * factor).min(T::one()))
            })?,
            MultipleComparisonCorrection::BenjaminiHochberg => {
                Self::step_up(&raw, count, T::one())?
            }
            MultipleComparisonCorrection::BenjaminiYekutieli => {
                let mut harmonic = T::zero();
                for k in 1..=m {
                    let denominator: T = count_as(k, "harmonic-series term")?;
                    harmonic = harmonic + T::one() / denominator;
                }
                Self::step_up(&raw, count, harmonic)?
            }
        };
        Ok(adjusted)
    }

    /// Indices of the tests whose adjusted p-value is at or below `alpha`.
    pub fn significant_tests(&self, alpha: T) -> Result<Vec<usize>> {
        let adjusted = self.adjusted_p_values()?;
        Ok(adjusted
            .into_iter()
            .enumerate()
            .filter(|(_, p)| *p <= alpha)
            .map(|(index, _)| index)
            .collect())
    }

    /// Shared machinery for the step-down (Holm) family: sort ascending, scale by
    /// rank, then enforce monotonicity by carrying the running maximum forward.
    fn step_down<F>(raw: &[T], scale: F) -> Result<Vec<T>>
    where
        F: Fn(usize, T) -> Result<T>,
    {
        let mut order: Vec<usize> = (0..raw.len()).collect();
        order.sort_by(|&a, &b| raw[a].partial_cmp(&raw[b]).unwrap_or(Ordering::Equal));

        let mut adjusted = vec![T::zero(); raw.len()];
        let mut running = T::zero();
        for (rank, &index) in order.iter().enumerate() {
            let scaled = scale(rank, raw[index])?;
            running = running.max(scaled);
            adjusted[index] = running.min(T::one());
        }
        Ok(adjusted)
    }

    /// Shared machinery for the step-up (Benjamini) family: sort descending,
    /// scale by `m * factor / rank`, then enforce monotonicity by carrying the
    /// running minimum backwards.
    fn step_up(raw: &[T], count: T, factor: T) -> Result<Vec<T>> {
        let mut order: Vec<usize> = (0..raw.len()).collect();
        order.sort_by(|&a, &b| raw[b].partial_cmp(&raw[a]).unwrap_or(Ordering::Equal));

        let mut adjusted = vec![T::zero(); raw.len()];
        let mut running = T::one();
        for (position, &index) in order.iter().enumerate() {
            let rank = raw.len() - position;
            let rank_value: T = count_as(rank, "Benjamini rank")?;
            let scaled = raw[index] * count * factor / rank_value;
            running = running.min(scaled);
            adjusted[index] = running.min(T::one()).max(T::zero());
        }
        Ok(adjusted)
    }

    pub(crate) fn compute_confidence_intervals(
        &self,
        results: &[TestResult<T>],
    ) -> Result<HashMap<EvaluationMetric, (T, T)>> {
        let mut intervals = HashMap::new();

        if !results.is_empty() {
            let scores: Vec<T> = results.iter().map(|r| r.score).collect();
            let count: T = count_as(scores.len(), "confidence-interval sample size")?;
            let mean = scores.iter().cloned().sum::<T>() / count;
            let std_dev = if scores.len() > 1 {
                let degrees_of_freedom: T =
                    count_as(scores.len() - 1, "confidence-interval degrees of freedom")?;
                let variance =
                    scores.iter().map(|&s| (s - mean) * (s - mean)).sum::<T>() / degrees_of_freedom;
                variance.sqrt()
            } else {
                T::zero()
            };

            // 95% confidence interval (normal approximation).
            let z: T = scalar_as(1.96, "95% normal quantile")?;
            let margin = std_dev * z / count.sqrt();
            intervals.insert(
                EvaluationMetric::FinalPerformance,
                (mean - margin, mean + margin),
            );
        }

        Ok(intervals)
    }

    /// Add a statistical test
    pub fn add_test(&mut self, test: StatisticalTest<T>) {
        self.statistical_tests.push(test);
    }

    /// Get significance threshold
    pub fn get_threshold(&self, name: &str) -> Option<T> {
        self.significance_thresholds.get(name).copied()
    }

    /// Set significance threshold
    pub fn set_threshold(&mut self, name: String, value: T) {
        self.significance_thresholds.insert(name, value);
    }

    /// Compute descriptive statistics.
    ///
    /// An empty sample yields an all-zero summary with `count == 0`, which the
    /// caller can distinguish from a genuine all-zero sample. Fallible because the
    /// sample size has to reach the element type to be a divisor; there is no
    /// fallback divisor that would not silently report a different statistic.
    pub fn compute_descriptive_stats(&self, values: &[T]) -> Result<DescriptiveStats<T>> {
        if values.is_empty() {
            return Ok(DescriptiveStats {
                mean: T::zero(),
                median: T::zero(),
                std_dev: T::zero(),
                min: T::zero(),
                max: T::zero(),
                count: 0,
            });
        }

        let count = values.len();
        let count_t: T = count_as(count, "descriptive sample size")?;
        let mean = sum_of(values) / count_t;

        let variance = values.iter().map(|&v| (v - mean) * (v - mean)).sum::<T>() / count_t;
        let std_dev = variance.sqrt();

        let mut sorted = values.to_vec();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let min = sorted.first().copied().unwrap_or(T::zero());
        let max = sorted.last().copied().unwrap_or(T::zero());
        let median = if count.is_multiple_of(2) {
            (sorted[count / 2 - 1] + sorted[count / 2])
                / scirs2_core::numeric::NumCast::from(2.0).unwrap_or_else(|| T::one())
        } else {
            sorted[count / 2]
        };

        Ok(DescriptiveStats {
            mean,
            median,
            std_dev,
            min,
            max,
            count,
        })
    }
}

/// Descriptive statistics result
#[derive(Debug, Clone)]
pub struct DescriptiveStats<T: Float + Debug + Send + Sync + 'static> {
    /// Mean value
    pub mean: T,
    /// Median value
    pub median: T,
    /// Standard deviation
    pub std_dev: T,
    /// Minimum value
    pub min: T,
    /// Maximum value
    pub max: T,
    /// Count
    pub count: usize,
}

/// Sum of a sample, factored out so the descriptive path reads the values once.
fn sum_of<T: Float + std::iter::Sum>(values: &[T]) -> T {
    values.iter().cloned().sum()
}

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

    fn test_with_p(name: &str, p_value: f64) -> StatisticalTest<f64> {
        StatisticalTest {
            name: name.to_string(),
            test_type: StatisticalTestType::TTest,
            test_statistic: 0.0,
            p_value,
            effect_size: 0.0,
            confidence_interval: (0.0, 0.0),
        }
    }

    fn analyzer_with(ps: &[f64]) -> StatisticalAnalyzer<f64> {
        let mut analyzer = StatisticalAnalyzer::<f64>::new();
        for (index, &p) in ps.iter().enumerate() {
            analyzer.add_test(test_with_p(&format!("t{index}"), p));
        }
        analyzer
    }

    #[test]
    fn no_correction_returns_the_raw_p_values() {
        let mut analyzer = analyzer_with(&[0.01, 0.04, 0.20]);
        analyzer.set_multiple_comparison(MultipleComparisonCorrection::None);
        let adjusted = analyzer.adjusted_p_values().expect("adjustment");
        assert_eq!(adjusted, vec![0.01, 0.04, 0.20]);
    }

    #[test]
    fn bonferroni_and_sidak_multiply_by_the_family_size() {
        let mut analyzer = analyzer_with(&[0.01, 0.04, 0.20]);

        analyzer.set_multiple_comparison(MultipleComparisonCorrection::Bonferroni);
        let bonferroni = analyzer.adjusted_p_values().expect("adjustment");
        assert!((bonferroni[0] - 0.03).abs() < 1e-12);
        assert!((bonferroni[1] - 0.12).abs() < 1e-12);
        // Clamped at one rather than reporting a probability above 1.
        assert!((bonferroni[2] - 0.6).abs() < 1e-12);

        analyzer.set_multiple_comparison(MultipleComparisonCorrection::Sidak);
        let sidak = analyzer.adjusted_p_values().expect("adjustment");
        for (s, b) in sidak.iter().zip(bonferroni.iter()) {
            // Sidak is uniformly less conservative than Bonferroni.
            assert!(*s <= *b + 1e-12, "sidak {s} exceeded bonferroni {b}");
            assert!((0.0..=1.0).contains(s));
        }
    }

    #[test]
    fn holm_is_monotone_and_less_conservative_than_bonferroni() {
        let mut analyzer = analyzer_with(&[0.20, 0.01, 0.04]);
        analyzer.set_multiple_comparison(MultipleComparisonCorrection::Bonferroni);
        let bonferroni = analyzer.adjusted_p_values().expect("adjustment");

        analyzer.set_multiple_comparison(MultipleComparisonCorrection::HolmBonferroni);
        let holm = analyzer.adjusted_p_values().expect("adjustment");

        // Smallest raw p (0.01, index 1) is scaled by 3, the next by 2, the last
        // by 1 — then made monotone.
        assert!((holm[1] - 0.03).abs() < 1e-12);
        assert!((holm[2] - 0.08).abs() < 1e-12);
        assert!((holm[0] - 0.20).abs() < 1e-12);
        for (h, b) in holm.iter().zip(bonferroni.iter()) {
            assert!(*h <= *b + 1e-12);
        }
    }

    #[test]
    fn benjamini_hochberg_is_monotone_and_yekutieli_is_stricter() {
        let mut analyzer = analyzer_with(&[0.001, 0.008, 0.039, 0.041, 0.042]);

        analyzer.set_multiple_comparison(MultipleComparisonCorrection::BenjaminiHochberg);
        let bh = analyzer.adjusted_p_values().expect("adjustment");
        // Ordering is preserved: a smaller raw p never gets a larger adjusted p.
        for window in bh.windows(2) {
            assert!(
                window[0] <= window[1] + 1e-12,
                "BH broke monotonicity: {bh:?}"
            );
        }
        // 0.001 * 5 / 1 = 0.005.
        assert!((bh[0] - 0.005).abs() < 1e-12);

        analyzer.set_multiple_comparison(MultipleComparisonCorrection::BenjaminiYekutieli);
        let by = analyzer.adjusted_p_values().expect("adjustment");
        for (y, h) in by.iter().zip(bh.iter()) {
            assert!(*y >= *h - 1e-12, "Yekutieli must not be looser than BH");
            assert!((0.0..=1.0).contains(y));
        }
    }

    #[test]
    fn significance_uses_the_adjusted_values_not_the_raw_ones() {
        let mut analyzer = analyzer_with(&[0.02, 0.03, 0.04]);
        analyzer.set_multiple_comparison(MultipleComparisonCorrection::None);
        assert_eq!(
            analyzer.significant_tests(0.05).expect("significance"),
            vec![0, 1, 2]
        );

        // Under Bonferroni every adjusted value is above 0.05, so nothing survives.
        analyzer.set_multiple_comparison(MultipleComparisonCorrection::Bonferroni);
        assert!(analyzer
            .significant_tests(0.05)
            .expect("significance")
            .is_empty());

        assert_eq!(analyzer.tests().len(), 3);
        assert_eq!(
            analyzer.multiple_comparison(),
            &MultipleComparisonCorrection::Bonferroni
        );
    }

    #[test]
    fn an_empty_family_has_no_adjusted_values() {
        let analyzer = StatisticalAnalyzer::<f64>::new();
        assert!(analyzer.adjusted_p_values().expect("adjustment").is_empty());
        assert!(analyzer
            .significant_tests(0.05)
            .expect("significance")
            .is_empty());
    }

    #[test]
    fn analyze_runs_exactly_the_enabled_methods() {
        let mut analyzer = StatisticalAnalyzer::<f64>::new();
        let series = [1.0, 2.0, 3.0, 4.0, 5.0];

        let report = analyzer.analyze(&series).expect("analysis");
        let descriptive = report.descriptive.expect("descriptive statistics enabled");
        assert_eq!(descriptive.count, 5);
        assert!((descriptive.mean - 3.0).abs() < 1e-12);
        let autocorrelation = report
            .lag1_autocorrelation
            .expect("correlation analysis enabled");
        // Standard (biased) lag-1 estimator on 1..=5: numerator 4, denominator 10.
        assert!(
            (autocorrelation - 0.4).abs() < 1e-12,
            "got {autocorrelation}"
        );

        // An alternating series is negatively autocorrelated, so the sign is real
        // signal and not an artefact of the ramp.
        let alternating = [1.0, -1.0, 1.0, -1.0, 1.0, -1.0];
        let flipped = analyzer
            .analyze(&alternating)
            .expect("analysis")
            .lag1_autocorrelation
            .expect("correlation analysis enabled");
        assert!(flipped < 0.0, "got {flipped}");

        analyzer
            .set_analysis_methods(vec![AnalysisMethod::DescriptiveStatistics])
            .expect("descriptive statistics are implemented");
        let report = analyzer.analyze(&series).expect("analysis");
        assert!(report.descriptive.is_some());
        assert!(
            report.lag1_autocorrelation.is_none(),
            "a disabled analysis must not run"
        );
    }

    #[test]
    fn an_unimplemented_analysis_is_rejected_not_silently_skipped() {
        let mut analyzer = StatisticalAnalyzer::<f64>::new();
        let err = analyzer
            .set_analysis_methods(vec![AnalysisMethod::PrincipalComponentAnalysis])
            .expect_err("PCA has no implementation here");
        assert!(matches!(err, OptimError::NotImplemented(_)));
        // The rejected configuration must not have been stored.
        assert_eq!(analyzer.analysis_methods().len(), 2);
    }

    #[test]
    fn autocorrelation_is_undefined_for_short_or_constant_samples() {
        let analyzer = StatisticalAnalyzer::<f64>::new();
        assert!(analyzer
            .analyze(&[])
            .expect("analysis")
            .lag1_autocorrelation
            .is_none());
        assert!(analyzer
            .analyze(&[1.0])
            .expect("analysis")
            .lag1_autocorrelation
            .is_none());
        assert!(
            analyzer
                .analyze(&[2.0, 2.0, 2.0])
                .expect("analysis")
                .lag1_autocorrelation
                .is_none(),
            "a constant series has no autocorrelation to report"
        );
    }

    #[test]
    fn confidence_intervals_survive_degenerate_samples() {
        let analyzer = StatisticalAnalyzer::<f64>::new();
        // No results at all: no interval, and no panic from an empty divisor.
        assert!(analyzer
            .compute_confidence_intervals(&[])
            .expect("intervals")
            .is_empty());
    }
}