quantrs2-ml 0.1.3

Quantum Machine Learning module for QuantRS2
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
//! Performance metrics and evaluation tools for time series forecasting

use super::config::AnomalyType;
use crate::error::{MLError, Result};
use scirs2_core::ndarray::{Array1, Array2};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Comprehensive forecast metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForecastMetrics {
    /// Mean Absolute Error
    pub mae: f64,

    /// Mean Squared Error
    pub mse: f64,

    /// Root Mean Squared Error
    pub rmse: f64,

    /// Mean Absolute Percentage Error
    pub mape: f64,

    /// Symmetric MAPE
    pub smape: f64,

    /// Directional accuracy
    pub directional_accuracy: f64,

    /// Quantum fidelity of predictions
    pub quantum_fidelity: f64,

    /// Coverage of prediction intervals
    pub coverage: f64,

    /// Additional custom metrics
    pub custom_metrics: HashMap<String, f64>,
}

/// Forecast result with predictions and metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForecastResult {
    /// Point predictions
    pub predictions: Array2<f64>,

    /// Lower prediction interval
    pub lower_bound: Array2<f64>,

    /// Upper prediction interval
    pub upper_bound: Array2<f64>,

    /// Detected anomalies
    pub anomalies: Vec<AnomalyPoint>,

    /// Confidence scores for each prediction
    pub confidence_scores: Array1<f64>,

    /// Quantum uncertainty measure
    pub quantum_uncertainty: f64,
}

/// Anomaly point in forecasts
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalyPoint {
    /// Time index
    pub timestamp: usize,

    /// Anomalous value
    pub value: f64,

    /// Anomaly score
    pub anomaly_score: f64,

    /// Type of anomaly
    pub anomaly_type: AnomalyType,
}

/// Training history tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrainingHistory {
    /// Loss values per epoch
    pub losses: Vec<f64>,

    /// Validation losses per epoch
    pub val_losses: Vec<f64>,

    /// Metrics per epoch
    pub metrics: Vec<HashMap<String, f64>>,

    /// Best model parameters
    pub best_params: Option<Array1<f64>>,

    /// Total training time in seconds
    pub training_time: f64,

    /// Learning curves
    pub learning_curves: LearningCurves,
}

/// Learning curves for training analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LearningCurves {
    /// Training accuracy over epochs
    pub training_accuracy: Vec<f64>,

    /// Validation accuracy over epochs
    pub validation_accuracy: Vec<f64>,

    /// Learning rate schedule
    pub learning_rates: Vec<f64>,

    /// Quantum coherence measures
    pub quantum_coherence: Vec<f64>,
}

/// Model evaluation suite
#[derive(Debug, Clone)]
pub struct ModelEvaluator {
    /// Evaluation metrics to compute
    metrics: Vec<MetricType>,

    /// Cross-validation configuration
    cv_config: CrossValidationConfig,

    /// Statistical test configuration
    statistical_tests: StatisticalTestConfig,
}

/// Types of evaluation metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MetricType {
    MAE,
    MSE,
    RMSE,
    MAPE,
    SMAPE,
    DirectionalAccuracy,
    QuantumFidelity,
    Coverage,
    MSIS, // Mean Scaled Interval Score
    CRPS, // Continuous Ranked Probability Score
    Custom(String),
}

/// Cross-validation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrossValidationConfig {
    /// Number of folds
    pub n_folds: usize,

    /// Validation strategy
    pub strategy: ValidationStrategy,

    /// Time series specific settings
    pub time_series_split: TimeSeriesSplitConfig,
}

/// Validation strategies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ValidationStrategy {
    KFold,
    TimeSeriesSplit,
    WalkForward,
    BlockingTimeSeriesSplit,
    QuantumBootstrap,
}

/// Time series splitting configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeSeriesSplitConfig {
    /// Test size as fraction of total data
    pub test_size: f64,

    /// Minimum training size
    pub min_train_size: Option<usize>,

    /// Gap between train and test
    pub gap: usize,

    /// Enable expanding window
    pub expanding_window: bool,
}

/// Statistical test configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalTestConfig {
    /// Significance level
    pub alpha: f64,

    /// Tests to perform
    pub tests: Vec<StatisticalTest>,

    /// Multiple comparison correction
    pub correction: MultipleComparisonCorrection,
}

/// Statistical tests for model comparison
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StatisticalTest {
    DieboldMariano,
    WilcoxonSignedRank,
    PairedTTest,
    McNemar,
    QuantumSignificanceTest,
}

/// Multiple comparison corrections
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MultipleComparisonCorrection {
    None,
    Bonferroni,
    BenjaminiHochberg,
    Holm,
    QuantumCorrection,
}

/// Benchmark suite for model comparison
#[derive(Debug, Clone)]
pub struct BenchmarkSuite {
    /// Benchmark datasets
    datasets: Vec<BenchmarkDataset>,

    /// Models to compare
    models: Vec<String>,

    /// Evaluation metrics
    metrics: Vec<MetricType>,

    /// Results storage
    results: BenchmarkResults,
}

/// Benchmark dataset metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkDataset {
    /// Dataset name
    pub name: String,

    /// Dataset description
    pub description: String,

    /// Data characteristics
    pub characteristics: DataCharacteristics,

    /// Source reference
    pub source: String,
}

/// Data characteristics for benchmarking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataCharacteristics {
    /// Number of time series
    pub n_series: usize,

    /// Length of time series
    pub series_length: usize,

    /// Frequency of observations
    pub frequency: String,

    /// Seasonality periods
    pub seasonality: Vec<usize>,

    /// Trend characteristics
    pub trend_type: TrendType,

    /// Noise level
    pub noise_level: f64,
}

/// Types of trends in data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TrendType {
    None,
    Linear,
    Exponential,
    Polynomial,
    Cyclic,
    Random,
    QuantumSuperposition,
}

/// Benchmark results storage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkResults {
    /// Results by model and dataset
    pub results: HashMap<String, HashMap<String, ModelPerformance>>,

    /// Statistical comparisons
    pub statistical_comparisons: Vec<StatisticalComparison>,

    /// Rankings
    pub rankings: HashMap<String, Vec<String>>,

    /// Summary statistics
    pub summary: BenchmarkSummary,
}

/// Model performance on a dataset
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelPerformance {
    /// Metric values
    pub metrics: HashMap<String, f64>,

    /// Confidence intervals
    pub confidence_intervals: HashMap<String, (f64, f64)>,

    /// Execution time
    pub execution_time: f64,

    /// Memory usage
    pub memory_usage: f64,

    /// Quantum enhancement factor
    pub quantum_enhancement: f64,
}

/// Statistical comparison between models
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalComparison {
    /// Model names being compared
    pub models: (String, String),

    /// Test type
    pub test_type: StatisticalTest,

    /// Test statistic
    pub statistic: f64,

    /// p-value
    pub p_value: f64,

    /// Effect size
    pub effect_size: f64,

    /// Significant difference
    pub is_significant: bool,
}

/// Benchmark summary statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkSummary {
    /// Best performing model overall
    pub best_model: String,

    /// Average performance by model
    pub average_performance: HashMap<String, f64>,

    /// Win rates by model
    pub win_rates: HashMap<String, f64>,

    /// Quantum advantage metrics
    pub quantum_advantage: HashMap<String, f64>,
}

impl ForecastMetrics {
    /// Create new forecast metrics
    pub fn new() -> Self {
        Self {
            mae: 0.0,
            mse: 0.0,
            rmse: 0.0,
            mape: 0.0,
            smape: 0.0,
            directional_accuracy: 0.0,
            quantum_fidelity: 0.0,
            coverage: 0.0,
            custom_metrics: HashMap::new(),
        }
    }

    /// Calculate all metrics from predictions and actuals
    pub fn calculate_metrics(
        &mut self,
        predictions: &Array2<f64>,
        actuals: &Array2<f64>,
    ) -> Result<()> {
        if predictions.shape() != actuals.shape() {
            return Err(MLError::DimensionMismatch(
                "Predictions and actuals must have the same shape".to_string(),
            ));
        }

        let n = predictions.len() as f64;

        // Reset metrics
        self.mae = 0.0;
        self.mse = 0.0;
        self.mape = 0.0;
        self.smape = 0.0;

        // Calculate basic metrics
        for (pred, actual) in predictions.iter().zip(actuals.iter()) {
            let error = pred - actual;
            let abs_error = error.abs();

            self.mae += abs_error;
            self.mse += error * error;

            // MAPE calculation (avoid division by zero)
            if actual.abs() > 1e-10 {
                self.mape += (abs_error / actual.abs()) * 100.0;
            }

            // SMAPE calculation
            let denominator = (pred.abs() + actual.abs()) / 2.0;
            if denominator > 1e-10 {
                self.smape += (abs_error / denominator) * 100.0;
            }
        }

        // Normalize by number of observations
        self.mae /= n;
        self.mse /= n;
        self.mape /= n;
        self.smape /= n;

        // Calculate RMSE
        self.rmse = self.mse.sqrt();

        // Calculate directional accuracy
        self.directional_accuracy = self.calculate_directional_accuracy(predictions, actuals)?;

        // Calculate quantum fidelity (simplified)
        self.quantum_fidelity = self.calculate_quantum_fidelity(predictions, actuals)?;

        // Coverage would be calculated if prediction intervals are available
        self.coverage = 0.95; // Placeholder

        Ok(())
    }

    /// Calculate directional accuracy
    fn calculate_directional_accuracy(
        &self,
        predictions: &Array2<f64>,
        actuals: &Array2<f64>,
    ) -> Result<f64> {
        if predictions.nrows() < 2 {
            return Ok(0.0);
        }

        let mut correct_directions = 0;
        let mut total_directions = 0;

        for i in 1..predictions.nrows() {
            let pred_change = predictions[[i, 0]] - predictions[[i - 1, 0]];
            let actual_change = actuals[[i, 0]] - actuals[[i - 1, 0]];

            if pred_change * actual_change > 0.0 {
                correct_directions += 1;
            }
            total_directions += 1;
        }

        Ok(if total_directions > 0 {
            correct_directions as f64 / total_directions as f64
        } else {
            0.0
        })
    }

    /// Calculate quantum fidelity measure
    fn calculate_quantum_fidelity(
        &self,
        predictions: &Array2<f64>,
        actuals: &Array2<f64>,
    ) -> Result<f64> {
        // Simplified quantum fidelity based on normalized correlation
        let pred_flat: Vec<f64> = predictions.iter().cloned().collect();
        let actual_flat: Vec<f64> = actuals.iter().cloned().collect();

        let pred_mean = pred_flat.iter().sum::<f64>() / pred_flat.len() as f64;
        let actual_mean = actual_flat.iter().sum::<f64>() / actual_flat.len() as f64;

        let mut numerator = 0.0;
        let mut pred_sq_sum = 0.0;
        let mut actual_sq_sum = 0.0;

        for (pred, actual) in pred_flat.iter().zip(actual_flat.iter()) {
            let pred_dev = pred - pred_mean;
            let actual_dev = actual - actual_mean;

            numerator += pred_dev * actual_dev;
            pred_sq_sum += pred_dev * pred_dev;
            actual_sq_sum += actual_dev * actual_dev;
        }

        let denominator = (pred_sq_sum * actual_sq_sum).sqrt();
        let correlation = if denominator > 1e-10 {
            numerator / denominator
        } else {
            0.0
        };

        // Convert correlation to fidelity-like measure
        Ok((correlation + 1.0) / 2.0)
    }

    /// Add custom metric
    pub fn add_custom_metric(&mut self, name: String, value: f64) {
        self.custom_metrics.insert(name, value);
    }

    /// Get summary of all metrics
    pub fn get_summary(&self) -> HashMap<String, f64> {
        let mut summary = HashMap::new();

        summary.insert("MAE".to_string(), self.mae);
        summary.insert("MSE".to_string(), self.mse);
        summary.insert("RMSE".to_string(), self.rmse);
        summary.insert("MAPE".to_string(), self.mape);
        summary.insert("SMAPE".to_string(), self.smape);
        summary.insert("DirectionalAccuracy".to_string(), self.directional_accuracy);
        summary.insert("QuantumFidelity".to_string(), self.quantum_fidelity);
        summary.insert("Coverage".to_string(), self.coverage);

        // Add custom metrics
        for (name, value) in &self.custom_metrics {
            summary.insert(name.clone(), *value);
        }

        summary
    }
}

impl TrainingHistory {
    /// Create new training history
    pub fn new() -> Self {
        Self {
            losses: Vec::new(),
            val_losses: Vec::new(),
            metrics: Vec::new(),
            best_params: None,
            training_time: 0.0,
            learning_curves: LearningCurves::new(),
        }
    }

    /// Add metrics for an epoch
    pub fn add_epoch_metrics(&mut self, metrics: HashMap<String, f64>, loss: f64, val_loss: f64) {
        self.losses.push(loss);
        self.val_losses.push(val_loss);
        self.metrics.push(metrics);

        // Update learning curves
        if let Some(accuracy) = self.metrics.last().and_then(|m| m.get("accuracy")) {
            self.learning_curves.training_accuracy.push(*accuracy);
        }

        if let Some(val_accuracy) = self.metrics.last().and_then(|m| m.get("val_accuracy")) {
            self.learning_curves.validation_accuracy.push(*val_accuracy);
        }
    }

    /// Get best epoch based on validation loss
    pub fn get_best_epoch(&self) -> Option<usize> {
        if self.val_losses.is_empty() {
            return None;
        }

        self.val_losses
            .iter()
            .enumerate()
            .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(idx, _)| idx)
    }

    /// Check if training is converged
    pub fn is_converged(&self, patience: usize, min_delta: f64) -> bool {
        if self.val_losses.len() < patience {
            return false;
        }

        let recent_losses = &self.val_losses[self.val_losses.len() - patience..];
        let min_recent = recent_losses.iter().fold(f64::INFINITY, |a, &b| a.min(b));

        // Check if there's been improvement recently
        recent_losses
            .iter()
            .any(|&loss| min_recent - loss > min_delta)
    }
}

impl LearningCurves {
    /// Create new learning curves
    pub fn new() -> Self {
        Self {
            training_accuracy: Vec::new(),
            validation_accuracy: Vec::new(),
            learning_rates: Vec::new(),
            quantum_coherence: Vec::new(),
        }
    }

    /// Add learning rate
    pub fn add_learning_rate(&mut self, lr: f64) {
        self.learning_rates.push(lr);
    }

    /// Add quantum coherence measure
    pub fn add_quantum_coherence(&mut self, coherence: f64) {
        self.quantum_coherence.push(coherence);
    }
}

impl ModelEvaluator {
    /// Create new model evaluator
    pub fn new() -> Self {
        Self {
            metrics: vec![
                MetricType::MAE,
                MetricType::MSE,
                MetricType::RMSE,
                MetricType::MAPE,
                MetricType::DirectionalAccuracy,
            ],
            cv_config: CrossValidationConfig::default(),
            statistical_tests: StatisticalTestConfig::default(),
        }
    }

    /// Evaluate model performance
    pub fn evaluate(
        &self,
        predictions: &Array2<f64>,
        actuals: &Array2<f64>,
    ) -> Result<HashMap<String, f64>> {
        let mut results = HashMap::new();

        for metric_type in &self.metrics {
            let value = self.calculate_metric(metric_type, predictions, actuals)?;
            let metric_name = format!("{:?}", metric_type);
            results.insert(metric_name, value);
        }

        Ok(results)
    }

    /// Calculate specific metric
    fn calculate_metric(
        &self,
        metric_type: &MetricType,
        predictions: &Array2<f64>,
        actuals: &Array2<f64>,
    ) -> Result<f64> {
        match metric_type {
            MetricType::MAE => self.calculate_mae(predictions, actuals),
            MetricType::MSE => self.calculate_mse(predictions, actuals),
            MetricType::RMSE => {
                let mse = self.calculate_mse(predictions, actuals)?;
                Ok(mse.sqrt())
            }
            MetricType::MAPE => self.calculate_mape(predictions, actuals),
            MetricType::SMAPE => self.calculate_smape(predictions, actuals),
            MetricType::DirectionalAccuracy => {
                self.calculate_directional_accuracy(predictions, actuals)
            }
            MetricType::QuantumFidelity => self.calculate_quantum_fidelity(predictions, actuals),
            _ => Ok(0.0), // Placeholder for other metrics
        }
    }

    /// Calculate Mean Absolute Error
    fn calculate_mae(&self, predictions: &Array2<f64>, actuals: &Array2<f64>) -> Result<f64> {
        if predictions.shape() != actuals.shape() {
            return Err(MLError::DimensionMismatch(
                "Predictions and actuals must have same shape".to_string(),
            ));
        }

        let mae = predictions
            .iter()
            .zip(actuals.iter())
            .map(|(p, a)| (p - a).abs())
            .sum::<f64>()
            / predictions.len() as f64;

        Ok(mae)
    }

    /// Calculate Mean Squared Error
    fn calculate_mse(&self, predictions: &Array2<f64>, actuals: &Array2<f64>) -> Result<f64> {
        if predictions.shape() != actuals.shape() {
            return Err(MLError::DimensionMismatch(
                "Predictions and actuals must have same shape".to_string(),
            ));
        }

        let mse = predictions
            .iter()
            .zip(actuals.iter())
            .map(|(p, a)| (p - a).powi(2))
            .sum::<f64>()
            / predictions.len() as f64;

        Ok(mse)
    }

    /// Calculate Mean Absolute Percentage Error
    fn calculate_mape(&self, predictions: &Array2<f64>, actuals: &Array2<f64>) -> Result<f64> {
        if predictions.shape() != actuals.shape() {
            return Err(MLError::DimensionMismatch(
                "Predictions and actuals must have same shape".to_string(),
            ));
        }

        let mut mape_sum = 0.0;
        let mut count = 0;

        for (pred, actual) in predictions.iter().zip(actuals.iter()) {
            if actual.abs() > 1e-10 {
                mape_sum += ((pred - actual) / actual).abs() * 100.0;
                count += 1;
            }
        }

        Ok(if count > 0 {
            mape_sum / count as f64
        } else {
            0.0
        })
    }

    /// Calculate Symmetric Mean Absolute Percentage Error
    fn calculate_smape(&self, predictions: &Array2<f64>, actuals: &Array2<f64>) -> Result<f64> {
        if predictions.shape() != actuals.shape() {
            return Err(MLError::DimensionMismatch(
                "Predictions and actuals must have same shape".to_string(),
            ));
        }

        let smape = predictions
            .iter()
            .zip(actuals.iter())
            .map(|(pred, actual)| {
                let numerator = (pred - actual).abs();
                let denominator = (pred.abs() + actual.abs()) / 2.0;
                if denominator > 1e-10 {
                    numerator / denominator * 100.0
                } else {
                    0.0
                }
            })
            .sum::<f64>()
            / predictions.len() as f64;

        Ok(smape)
    }

    /// Calculate directional accuracy
    fn calculate_directional_accuracy(
        &self,
        predictions: &Array2<f64>,
        actuals: &Array2<f64>,
    ) -> Result<f64> {
        if predictions.nrows() < 2 {
            return Ok(0.0);
        }

        let mut correct = 0;
        let mut total = 0;

        for i in 1..predictions.nrows() {
            let pred_change = predictions[[i, 0]] - predictions[[i - 1, 0]];
            let actual_change = actuals[[i, 0]] - actuals[[i - 1, 0]];

            if pred_change * actual_change > 0.0 {
                correct += 1;
            }
            total += 1;
        }

        Ok(correct as f64 / total as f64)
    }

    /// Calculate quantum fidelity
    fn calculate_quantum_fidelity(
        &self,
        predictions: &Array2<f64>,
        actuals: &Array2<f64>,
    ) -> Result<f64> {
        // Simplified quantum fidelity calculation
        let mae = self.calculate_mae(predictions, actuals)?;
        let max_possible_error = actuals.iter().map(|x| x.abs()).fold(0.0, f64::max);

        let fidelity = if max_possible_error > 1e-10 {
            1.0 - (mae / max_possible_error).min(1.0)
        } else {
            1.0
        };

        Ok(fidelity)
    }
}

impl Default for CrossValidationConfig {
    fn default() -> Self {
        Self {
            n_folds: 5,
            strategy: ValidationStrategy::TimeSeriesSplit,
            time_series_split: TimeSeriesSplitConfig::default(),
        }
    }
}

impl Default for TimeSeriesSplitConfig {
    fn default() -> Self {
        Self {
            test_size: 0.2,
            min_train_size: None,
            gap: 0,
            expanding_window: false,
        }
    }
}

impl Default for StatisticalTestConfig {
    fn default() -> Self {
        Self {
            alpha: 0.05,
            tests: vec![StatisticalTest::DieboldMariano],
            correction: MultipleComparisonCorrection::BenjaminiHochberg,
        }
    }
}

/// Utility functions for metrics calculation

/// Calculate prediction interval coverage
pub fn calculate_coverage(
    actuals: &Array2<f64>,
    lower_bounds: &Array2<f64>,
    upper_bounds: &Array2<f64>,
) -> Result<f64> {
    if actuals.shape() != lower_bounds.shape() || actuals.shape() != upper_bounds.shape() {
        return Err(MLError::DimensionMismatch(
            "All arrays must have the same shape".to_string(),
        ));
    }

    let mut covered = 0;
    let total = actuals.len();

    for ((actual, lower), upper) in actuals
        .iter()
        .zip(lower_bounds.iter())
        .zip(upper_bounds.iter())
    {
        if actual >= lower && actual <= upper {
            covered += 1;
        }
    }

    Ok(covered as f64 / total as f64)
}

/// Calculate Mean Scaled Interval Score (MSIS)
pub fn calculate_msis(
    actuals: &Array2<f64>,
    predictions: &Array2<f64>,
    lower_bounds: &Array2<f64>,
    upper_bounds: &Array2<f64>,
    alpha: f64,
    seasonal_period: Option<usize>,
) -> Result<f64> {
    // Simplified MSIS calculation
    let coverage = calculate_coverage(actuals, lower_bounds, upper_bounds)?;
    let interval_width: f64 = upper_bounds
        .iter()
        .zip(lower_bounds.iter())
        .map(|(u, l)| u - l)
        .sum::<f64>()
        / upper_bounds.len() as f64;

    // Simplified scaling factor
    let scaling_factor = if let Some(period) = seasonal_period {
        period as f64
    } else {
        1.0
    };

    Ok(interval_width / scaling_factor * (1.0 - coverage + alpha))
}