optirs-core 0.3.1

OptiRS core optimization algorithms and utilities
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
// Drift detection and adaptation for streaming data
//
// This module provides comprehensive drift detection capabilities including
// statistical methods, distribution-based approaches, model-based detection,
// and ensemble methods for identifying concept drift in streaming data.

use super::config::*;
use super::optimizer::{Adaptation, AdaptationPriority, AdaptationType, StreamingDataPoint};

use scirs2_core::numeric::Float;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};

/// Enhanced drift detector with multiple detection methods
pub struct EnhancedDriftDetector<A: Float + Send + Sync> {
    /// Configuration for drift detection
    config: DriftConfig,
    /// Current detection method
    detection_method: DriftDetectionMethod,
    /// Statistical test implementations
    statistical_tests: HashMap<StatisticalMethod, Box<dyn StatisticalTest<A>>>,
    /// Distribution comparison methods
    distribution_methods: HashMap<DistributionMethod, Box<dyn DistributionComparator<A>>>,
    /// Model-based detectors
    model_detectors: HashMap<ModelType, Box<dyn ModelBasedDetector<A>>>,
    /// Ensemble voting strategy
    ensemble_strategy: Option<VotingStrategy>,
    /// Detection history
    detection_history: VecDeque<DriftEvent<A>>,
    /// False positive tracker
    false_positive_tracker: FalsePositiveTracker<A>,
    /// Reference window for comparison
    reference_window: VecDeque<StreamingDataPoint<A>>,
    /// Current drift state
    drift_state: DriftState,
    /// Last detection timestamp
    last_detection: Option<Instant>,
    /// Sensitivity adjustment factor
    sensitivity_factor: A,
}

/// Drift event information
#[derive(Debug, Clone)]
pub struct DriftEvent<A: Float + Send + Sync> {
    /// Event timestamp
    pub timestamp: Instant,
    /// Drift severity level
    pub severity: DriftSeverity,
    /// Detection confidence
    pub confidence: A,
    /// Detection method that triggered
    pub detection_method: String,
    /// Statistical significance
    pub p_value: Option<A>,
    /// Drift magnitude estimate
    pub magnitude: A,
    /// Affected features (if applicable)
    pub affected_features: Vec<usize>,
}

/// Drift severity levels
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum DriftSeverity {
    /// Minor drift that may not require immediate action
    Minor,
    /// Moderate drift requiring attention
    Moderate,
    /// Major drift requiring significant adaptation
    Major,
    /// Critical drift requiring immediate response
    Critical,
}

/// Current drift detection state
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DriftState {
    /// Normal operation, no drift detected
    Stable,
    /// Warning level - potential drift detected
    Warning,
    /// Drift confirmed
    Drift,
    /// Recovering from drift
    Recovery,
}

/// False positive tracking for drift detection
pub struct FalsePositiveTracker<A: Float + Send + Sync> {
    /// Recent false positive events
    false_positives: VecDeque<Instant>,
    /// True positive events
    true_positives: VecDeque<Instant>,
    /// Current false positive rate
    current_fp_rate: A,
    /// Target false positive rate
    target_fp_rate: A,
}

/// Trait for statistical drift detection tests
pub trait StatisticalTest<A: Float + Send + Sync>: Send + Sync {
    /// Performs the statistical test for drift
    fn test_for_drift(
        &mut self,
        reference: &[A],
        current: &[A],
    ) -> Result<DriftTestResult<A>, String>;

    /// Updates test parameters based on historical performance
    fn update_parameters(&mut self, performance_feedback: A) -> Result<(), String>;

    /// Resets the test state
    fn reset(&mut self);
}

/// Result of a drift detection test
#[derive(Debug, Clone)]
pub struct DriftTestResult<A: Float + Send + Sync> {
    /// Whether drift was detected
    pub drift_detected: bool,
    /// Statistical significance (p-value)
    pub p_value: A,
    /// Test statistic value
    pub test_statistic: A,
    /// Confidence in the result
    pub confidence: A,
    /// Additional test-specific metadata
    pub metadata: HashMap<String, A>,
}

/// Trait for distribution-based drift detection
pub trait DistributionComparator<A: Float + Send + Sync>: Send + Sync {
    /// Compares two distributions for drift
    fn compare_distributions(
        &self,
        reference: &[A],
        current: &[A],
    ) -> Result<DistributionComparison<A>, String>;

    /// Gets the threshold for drift detection
    fn get_threshold(&self) -> A;

    /// Updates threshold based on performance
    fn update_threshold(&mut self, new_threshold: A);
}

/// Result of distribution comparison
#[derive(Debug, Clone)]
pub struct DistributionComparison<A: Float + Send + Sync> {
    /// Distance/divergence measure
    pub distance: A,
    /// Threshold for drift detection
    pub threshold: A,
    /// Whether drift was detected
    pub drift_detected: bool,
    /// Comparison confidence
    pub confidence: A,
}

/// Trait for model-based drift detection
pub trait ModelBasedDetector<A: Float + Send + Sync>: Send + Sync {
    /// Updates the model with new data
    fn update_model(&mut self, data: &[StreamingDataPoint<A>]) -> Result<(), String>;

    /// Detects drift based on model performance
    fn detect_drift(
        &mut self,
        data: &[StreamingDataPoint<A>],
    ) -> Result<ModelDriftResult<A>, String>;

    /// Resets the model
    fn reset_model(&mut self) -> Result<(), String>;
}

/// Result of model-based drift detection
#[derive(Debug, Clone)]
pub struct ModelDriftResult<A: Float + Send + Sync> {
    /// Whether drift was detected
    pub drift_detected: bool,
    /// Model performance degradation
    pub performance_degradation: A,
    /// Drift confidence
    pub confidence: A,
    /// Feature importance changes
    pub feature_importance_changes: Vec<A>,
}

impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum + 'static> EnhancedDriftDetector<A> {
    /// Creates a new enhanced drift detector
    pub fn new(config: &StreamingConfig) -> Result<Self, String> {
        let drift_config = config.drift_config.clone();

        let mut statistical_tests: HashMap<StatisticalMethod, Box<dyn StatisticalTest<A>>> =
            HashMap::new();
        let mut distribution_methods: HashMap<
            DistributionMethod,
            Box<dyn DistributionComparator<A>>,
        > = HashMap::new();
        let mut model_detectors: HashMap<ModelType, Box<dyn ModelBasedDetector<A>>> =
            HashMap::new();

        // Initialize statistical tests
        statistical_tests.insert(
            StatisticalMethod::ADWIN,
            Box::new(ADWINTest::new(drift_config.sensitivity)?),
        );
        statistical_tests.insert(
            StatisticalMethod::DDM,
            Box::new(DDMTest::new(drift_config.sensitivity)?),
        );
        statistical_tests.insert(
            StatisticalMethod::PageHinkley,
            Box::new(PageHinkleyTest::new(drift_config.sensitivity)?),
        );

        // Initialize distribution methods
        distribution_methods.insert(
            DistributionMethod::KLDivergence,
            Box::new(KLDivergenceComparator::new(drift_config.sensitivity)?),
        );
        distribution_methods.insert(
            DistributionMethod::JSDivergence,
            Box::new(JSDivergenceComparator::new(drift_config.sensitivity)?),
        );

        // Initialize model detectors
        model_detectors.insert(ModelType::Linear, Box::new(LinearModelDetector::new()?));

        let ensemble_strategy = match &drift_config.detection_method {
            DriftDetectionMethod::Ensemble {
                voting_strategy, ..
            } => Some(voting_strategy.clone()),
            _ => None,
        };

        let false_positive_tracker = FalsePositiveTracker::new();

        Ok(Self {
            config: drift_config.clone(),
            detection_method: drift_config.detection_method,
            statistical_tests,
            distribution_methods,
            model_detectors,
            ensemble_strategy,
            detection_history: VecDeque::with_capacity(1000),
            false_positive_tracker,
            reference_window: VecDeque::with_capacity(drift_config.window_size),
            drift_state: DriftState::Stable,
            last_detection: None,
            sensitivity_factor: A::one(),
        })
    }

    /// Detects drift in the given batch of data
    pub fn detect_drift(&mut self, batch: &[StreamingDataPoint<A>]) -> Result<bool, String> {
        if !self.config.enable_detection || batch.len() < self.config.min_samples {
            return Ok(false);
        }

        // Update reference window
        self.update_reference_window(batch)?;

        // Check if we have enough data for comparison
        if self.reference_window.len() < self.config.window_size / 2 {
            return Ok(false);
        }

        // Extract features for comparison
        let current_features = self.extract_features(batch)?;
        let reference_features = self.extract_reference_features()?;

        // Perform drift detection based on configured method
        let detection_method = self.detection_method.clone();
        let drift_result = match detection_method {
            DriftDetectionMethod::Statistical(method) => {
                self.detect_statistical_drift(&method, &reference_features, &current_features)?
            }
            DriftDetectionMethod::Distribution(method) => {
                self.detect_distribution_drift(&method, &reference_features, &current_features)?
            }
            DriftDetectionMethod::ModelBased(model_type) => {
                self.detect_model_drift(&model_type, batch)?
            }
            DriftDetectionMethod::Ensemble {
                methods,
                voting_strategy,
            } => self.detect_ensemble_drift(
                &methods,
                &voting_strategy,
                &reference_features,
                &current_features,
                batch,
            )?,
        };

        // Update drift state and history
        if drift_result.drift_detected {
            self.handle_drift_detection(drift_result)?;
            Ok(true)
        } else {
            self.update_drift_state(false);
            Ok(false)
        }
    }

    /// Updates the reference window with new data
    fn update_reference_window(&mut self, batch: &[StreamingDataPoint<A>]) -> Result<(), String> {
        for data_point in batch {
            if self.reference_window.len() >= self.config.window_size {
                self.reference_window.pop_front();
            }
            self.reference_window.push_back(data_point.clone());
        }
        Ok(())
    }

    /// Extracts features from a batch of data points
    fn extract_features(&self, batch: &[StreamingDataPoint<A>]) -> Result<Vec<A>, String> {
        let mut features = Vec::new();

        for data_point in batch {
            features.extend(data_point.features.iter().cloned());
        }

        Ok(features)
    }

    /// Extracts reference features from the reference window
    fn extract_reference_features(&self) -> Result<Vec<A>, String> {
        let reference_data: Vec<_> = self
            .reference_window
            .iter()
            .take(self.reference_window.len() / 2)
            .collect();

        let mut features = Vec::new();
        for data_point in reference_data {
            features.extend(data_point.features.iter().cloned());
        }

        Ok(features)
    }

    /// Performs statistical drift detection
    fn detect_statistical_drift(
        &mut self,
        method: &StatisticalMethod,
        reference: &[A],
        current: &[A],
    ) -> Result<DriftTestResult<A>, String> {
        if let Some(test) = self.statistical_tests.get_mut(method) {
            let mut result = test.test_for_drift(reference, current)?;

            // Apply sensitivity factor
            result.confidence = result.confidence * self.sensitivity_factor;
            result.drift_detected = result.p_value
                < A::from(self.config.significance_level).expect("unwrap failed")
                    * self.sensitivity_factor;

            Ok(result)
        } else {
            Err(format!("Statistical method {:?} not implemented", method))
        }
    }

    /// Performs distribution-based drift detection
    fn detect_distribution_drift(
        &mut self,
        method: &DistributionMethod,
        reference: &[A],
        current: &[A],
    ) -> Result<DriftTestResult<A>, String> {
        if let Some(comparator) = self.distribution_methods.get(method) {
            let comparison = comparator.compare_distributions(reference, current)?;

            let result = DriftTestResult {
                drift_detected: comparison.drift_detected,
                p_value: A::one() - comparison.confidence, // Convert confidence to p-value like measure
                test_statistic: comparison.distance,
                confidence: comparison.confidence * self.sensitivity_factor,
                metadata: HashMap::new(),
            };

            Ok(result)
        } else {
            Err(format!("Distribution method {:?} not implemented", method))
        }
    }

    /// Performs model-based drift detection
    fn detect_model_drift(
        &mut self,
        model_type: &ModelType,
        batch: &[StreamingDataPoint<A>],
    ) -> Result<DriftTestResult<A>, String> {
        if let Some(detector) = self.model_detectors.get_mut(model_type) {
            let model_result = detector.detect_drift(batch)?;

            let result = DriftTestResult {
                drift_detected: model_result.drift_detected,
                p_value: A::one() - model_result.confidence,
                test_statistic: model_result.performance_degradation,
                confidence: model_result.confidence * self.sensitivity_factor,
                metadata: HashMap::new(),
            };

            Ok(result)
        } else {
            Err(format!("Model type {:?} not implemented", model_type))
        }
    }

    /// Performs ensemble drift detection
    fn detect_ensemble_drift(
        &mut self,
        methods: &[DriftDetectionMethod],
        voting_strategy: &VotingStrategy,
        reference: &[A],
        current: &[A],
        batch: &[StreamingDataPoint<A>],
    ) -> Result<DriftTestResult<A>, String> {
        let mut results = Vec::new();

        // Collect results from all methods
        for method in methods {
            let result = match method {
                DriftDetectionMethod::Statistical(stat_method) => {
                    self.detect_statistical_drift(stat_method, reference, current)?
                }
                DriftDetectionMethod::Distribution(dist_method) => {
                    self.detect_distribution_drift(dist_method, reference, current)?
                }
                DriftDetectionMethod::ModelBased(model_type) => {
                    self.detect_model_drift(model_type, batch)?
                }
                DriftDetectionMethod::Ensemble { .. } => {
                    // Avoid recursive ensemble calls
                    continue;
                }
            };
            results.push(result);
        }

        // Apply voting strategy
        let ensemble_result = self.apply_voting_strategy(voting_strategy, &results)?;
        Ok(ensemble_result)
    }

    /// Applies the ensemble voting strategy
    fn apply_voting_strategy(
        &self,
        strategy: &VotingStrategy,
        results: &[DriftTestResult<A>],
    ) -> Result<DriftTestResult<A>, String> {
        if results.is_empty() {
            return Err("No results to vote on".to_string());
        }

        let drift_detected = match strategy {
            VotingStrategy::Majority => {
                let positive_votes = results.iter().filter(|r| r.drift_detected).count();
                positive_votes > results.len() / 2
            }
            VotingStrategy::Weighted { weights } => {
                if weights.len() != results.len() {
                    return Err("Number of weights doesn't match number of results".to_string());
                }

                let weighted_score: f64 = results
                    .iter()
                    .zip(weights.iter())
                    .map(|(result, &weight)| weight * if result.drift_detected { 1.0 } else { 0.0 })
                    .sum();

                let total_weight: f64 = weights.iter().sum();
                weighted_score / total_weight > 0.5
            }
            VotingStrategy::Unanimous => results.iter().all(|r| r.drift_detected),
            VotingStrategy::Threshold { min_votes } => {
                let positive_votes = results.iter().filter(|r| r.drift_detected).count();
                positive_votes >= *min_votes
            }
        };

        // Aggregate confidence and p-values
        let avg_confidence = results.iter().map(|r| r.confidence).sum::<A>()
            / A::from(results.len()).expect("unwrap failed");

        let avg_p_value = results.iter().map(|r| r.p_value).sum::<A>()
            / A::from(results.len()).expect("unwrap failed");

        let avg_test_statistic = results.iter().map(|r| r.test_statistic).sum::<A>()
            / A::from(results.len()).expect("unwrap failed");

        Ok(DriftTestResult {
            drift_detected,
            p_value: avg_p_value,
            test_statistic: avg_test_statistic,
            confidence: avg_confidence,
            metadata: HashMap::new(),
        })
    }

    /// Handles drift detection event
    fn handle_drift_detection(&mut self, result: DriftTestResult<A>) -> Result<(), String> {
        let severity = self.classify_drift_severity(&result);

        let drift_event = DriftEvent {
            timestamp: Instant::now(),
            severity: severity.clone(),
            confidence: result.confidence,
            detection_method: format!("{:?}", self.detection_method),
            p_value: Some(result.p_value),
            magnitude: result.test_statistic,
            affected_features: Vec::new(), // Could be computed based on feature-wise analysis
        };

        // Store in history
        if self.detection_history.len() >= 1000 {
            self.detection_history.pop_front();
        }
        self.detection_history.push_back(drift_event);

        // Update drift state
        self.update_drift_state(true);
        self.last_detection = Some(Instant::now());

        // Update false positive tracker if enabled
        if self.config.enable_false_positive_tracking {
            self.false_positive_tracker.record_detection(true)?;
        }

        Ok(())
    }

    /// Classifies drift severity based on test results
    fn classify_drift_severity(&self, result: &DriftTestResult<A>) -> DriftSeverity {
        let confidence = result.confidence.to_f64().unwrap_or(0.0);
        let p_value = result.p_value.to_f64().unwrap_or(1.0);

        if p_value < 0.001 && confidence > 0.95 {
            DriftSeverity::Critical
        } else if p_value < 0.01 && confidence > 0.9 {
            DriftSeverity::Major
        } else if p_value < 0.05 && confidence > 0.8 {
            DriftSeverity::Moderate
        } else {
            DriftSeverity::Minor
        }
    }

    /// Updates the current drift state
    fn update_drift_state(&mut self, drift_detected: bool) {
        self.drift_state = match (&self.drift_state, drift_detected) {
            (DriftState::Stable, true) => DriftState::Warning,
            (DriftState::Warning, true) => DriftState::Drift,
            (DriftState::Drift, false) => DriftState::Recovery,
            (DriftState::Recovery, false) => DriftState::Stable,
            (state, _) => state.clone(),
        };
    }

    /// Computes adaptation for drift sensitivity
    pub fn compute_sensitivity_adaptation(&mut self) -> Result<Option<Adaptation<A>>, String> {
        // Check if sensitivity should be adjusted based on false positive rate
        if self.config.enable_false_positive_tracking {
            let current_fp_rate = self.false_positive_tracker.current_fp_rate;
            let target_fp_rate = A::from(0.05).expect("unwrap failed"); // 5% target false positive rate

            if (current_fp_rate - target_fp_rate).abs() > A::from(0.02).expect("unwrap failed") {
                let adjustment = if current_fp_rate > target_fp_rate {
                    // Too many false positives, decrease sensitivity
                    -A::from(0.1).expect("unwrap failed")
                } else {
                    // Too few detections (potentially missing true positives), increase sensitivity
                    A::from(0.1).expect("unwrap failed")
                };

                let adaptation = Adaptation {
                    adaptation_type: AdaptationType::DriftSensitivity,
                    magnitude: adjustment,
                    target_component: "drift_detector".to_string(),
                    parameters: HashMap::new(),
                    priority: AdaptationPriority::Normal,
                    timestamp: Instant::now(),
                };

                return Ok(Some(adaptation));
            }
        }

        Ok(None)
    }

    /// Applies sensitivity adaptation
    pub fn apply_sensitivity_adaptation(
        &mut self,
        adaptation: &Adaptation<A>,
    ) -> Result<(), String> {
        if adaptation.adaptation_type == AdaptationType::DriftSensitivity {
            self.sensitivity_factor = (self.sensitivity_factor + adaptation.magnitude)
                .max(A::from(0.1).expect("unwrap failed"))
                .min(A::from(2.0).expect("unwrap failed"));
        }
        Ok(())
    }

    /// Checks if drift is currently detected
    pub fn is_drift_detected(&self) -> bool {
        matches!(self.drift_state, DriftState::Drift | DriftState::Warning)
    }

    /// Gets the current drift state
    pub fn get_drift_state(&self) -> &DriftState {
        &self.drift_state
    }

    /// Gets recent drift events
    pub fn get_recent_drift_events(&self, count: usize) -> Vec<&DriftEvent<A>> {
        self.detection_history.iter().rev().take(count).collect()
    }

    /// Resets the drift detector
    pub fn reset(&mut self) -> Result<(), String> {
        self.detection_history.clear();
        self.reference_window.clear();
        self.drift_state = DriftState::Stable;
        self.last_detection = None;
        self.sensitivity_factor = A::one();

        // Reset all detection methods
        for test in self.statistical_tests.values_mut() {
            test.reset();
        }

        for detector in self.model_detectors.values_mut() {
            detector.reset_model()?;
        }

        Ok(())
    }

    /// Gets diagnostic information
    pub fn get_diagnostics(&self) -> DriftDiagnostics {
        DriftDiagnostics {
            current_state: self.drift_state.clone(),
            detection_count: self.detection_history.len(),
            false_positive_rate: self
                .false_positive_tracker
                .current_fp_rate
                .to_f64()
                .unwrap_or(0.0),
            sensitivity_factor: self.sensitivity_factor.to_f64().unwrap_or(1.0),
            last_detection_time: self.last_detection,
            reference_window_size: self.reference_window.len(),
        }
    }
}

impl<A: Float + Send + Sync + Send + Sync> FalsePositiveTracker<A> {
    fn new() -> Self {
        Self {
            false_positives: VecDeque::new(),
            true_positives: VecDeque::new(),
            current_fp_rate: A::zero(),
            target_fp_rate: A::from(0.05).expect("unwrap failed"),
        }
    }

    fn record_detection(&mut self, is_true_positive: bool) -> Result<(), String> {
        let now = Instant::now();

        if is_true_positive {
            self.true_positives.push_back(now);
        } else {
            self.false_positives.push_back(now);
        }

        // Keep only recent events (last hour)
        let cutoff = now - Duration::from_secs(3600);
        self.false_positives.retain(|&time| time > cutoff);
        self.true_positives.retain(|&time| time > cutoff);

        // Update false positive rate
        let total_detections = self.false_positives.len() + self.true_positives.len();
        if total_detections > 0 {
            self.current_fp_rate = A::from(self.false_positives.len()).expect("unwrap failed")
                / A::from(total_detections).expect("unwrap failed");
        }

        Ok(())
    }
}

/// Diagnostic information for drift detection
#[derive(Debug, Clone)]
pub struct DriftDiagnostics {
    pub current_state: DriftState,
    pub detection_count: usize,
    pub false_positive_rate: f64,
    pub sensitivity_factor: f64,
    pub last_detection_time: Option<Instant>,
    pub reference_window_size: usize,
}

// Simplified implementations of detection methods
// In practice, these would be more sophisticated

struct ADWINTest<A: Float + Send + Sync> {
    sensitivity: A,
    window: VecDeque<A>,
}

impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> ADWINTest<A> {
    fn new(sensitivity: f64) -> Result<Self, String> {
        Ok(Self {
            sensitivity: A::from(sensitivity).expect("unwrap failed"),
            window: VecDeque::new(),
        })
    }
}

impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> StatisticalTest<A>
    for ADWINTest<A>
{
    fn test_for_drift(
        &mut self,
        reference: &[A],
        current: &[A],
    ) -> Result<DriftTestResult<A>, String> {
        // Simplified ADWIN implementation
        let ref_mean =
            reference.iter().cloned().sum::<A>() / A::from(reference.len()).expect("unwrap failed");
        let cur_mean =
            current.iter().cloned().sum::<A>() / A::from(current.len()).expect("unwrap failed");

        let difference = (ref_mean - cur_mean).abs();
        let threshold = self.sensitivity;

        let drift_detected = difference > threshold;

        Ok(DriftTestResult {
            drift_detected,
            p_value: if drift_detected {
                A::from(0.01).expect("unwrap failed")
            } else {
                A::from(0.5).expect("unwrap failed")
            },
            test_statistic: difference,
            confidence: if drift_detected {
                A::from(0.9).expect("unwrap failed")
            } else {
                A::from(0.1).expect("unwrap failed")
            },
            metadata: HashMap::new(),
        })
    }

    fn update_parameters(&mut self, _performance_feedback: A) -> Result<(), String> {
        Ok(())
    }

    fn reset(&mut self) {
        self.window.clear();
    }
}

struct DDMTest<A: Float + Send + Sync> {
    sensitivity: A,
    error_rate: A,
    std_dev: A,
}

impl<A: Float + Default + Send + Sync + std::iter::Sum> DDMTest<A> {
    fn new(sensitivity: f64) -> Result<Self, String> {
        Ok(Self {
            sensitivity: A::from(sensitivity).expect("unwrap failed"),
            error_rate: A::zero(),
            std_dev: A::zero(),
        })
    }
}

impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> StatisticalTest<A> for DDMTest<A> {
    fn test_for_drift(
        &mut self,
        reference: &[A],
        current: &[A],
    ) -> Result<DriftTestResult<A>, String> {
        // Simplified DDM implementation
        let ref_mean =
            reference.iter().cloned().sum::<A>() / A::from(reference.len()).expect("unwrap failed");
        let cur_mean =
            current.iter().cloned().sum::<A>() / A::from(current.len()).expect("unwrap failed");

        let difference = (ref_mean - cur_mean).abs();
        let drift_detected = difference > self.sensitivity;

        Ok(DriftTestResult {
            drift_detected,
            p_value: if drift_detected {
                A::from(0.02).expect("unwrap failed")
            } else {
                A::from(0.6).expect("unwrap failed")
            },
            test_statistic: difference,
            confidence: if drift_detected {
                A::from(0.85).expect("unwrap failed")
            } else {
                A::from(0.15).expect("unwrap failed")
            },
            metadata: HashMap::new(),
        })
    }

    fn update_parameters(&mut self, _performance_feedback: A) -> Result<(), String> {
        Ok(())
    }

    fn reset(&mut self) {
        self.error_rate = A::zero();
        self.std_dev = A::zero();
    }
}

struct PageHinkleyTest<A: Float + Send + Sync> {
    sensitivity: A,
    cumulative_sum: A,
}

impl<A: Float + Default + Send + Sync + std::iter::Sum> PageHinkleyTest<A> {
    fn new(sensitivity: f64) -> Result<Self, String> {
        Ok(Self {
            sensitivity: A::from(sensitivity).expect("unwrap failed"),
            cumulative_sum: A::zero(),
        })
    }
}

impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> StatisticalTest<A>
    for PageHinkleyTest<A>
{
    fn test_for_drift(
        &mut self,
        reference: &[A],
        current: &[A],
    ) -> Result<DriftTestResult<A>, String> {
        // Simplified Page-Hinkley test
        let ref_mean =
            reference.iter().cloned().sum::<A>() / A::from(reference.len()).expect("unwrap failed");
        let cur_mean =
            current.iter().cloned().sum::<A>() / A::from(current.len()).expect("unwrap failed");

        let difference = cur_mean - ref_mean;
        self.cumulative_sum = self.cumulative_sum + difference;

        let drift_detected = self.cumulative_sum.abs() > self.sensitivity;

        Ok(DriftTestResult {
            drift_detected,
            p_value: if drift_detected {
                A::from(0.015).expect("unwrap failed")
            } else {
                A::from(0.7).expect("unwrap failed")
            },
            test_statistic: self.cumulative_sum,
            confidence: if drift_detected {
                A::from(0.88).expect("unwrap failed")
            } else {
                A::from(0.12).expect("unwrap failed")
            },
            metadata: HashMap::new(),
        })
    }

    fn update_parameters(&mut self, _performance_feedback: A) -> Result<(), String> {
        Ok(())
    }

    fn reset(&mut self) {
        self.cumulative_sum = A::zero();
    }
}

struct KLDivergenceComparator<A: Float + Send + Sync> {
    threshold: A,
}

impl<A: Float + Send + Sync + Send + Sync> KLDivergenceComparator<A> {
    fn new(sensitivity: f64) -> Result<Self, String> {
        Ok(Self {
            threshold: A::from(sensitivity).expect("unwrap failed"),
        })
    }
}

impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> DistributionComparator<A>
    for KLDivergenceComparator<A>
{
    fn compare_distributions(
        &self,
        reference: &[A],
        current: &[A],
    ) -> Result<DistributionComparison<A>, String> {
        // Simplified KL divergence calculation
        let ref_mean =
            reference.iter().cloned().sum::<A>() / A::from(reference.len()).expect("unwrap failed");
        let cur_mean =
            current.iter().cloned().sum::<A>() / A::from(current.len()).expect("unwrap failed");

        let distance = (ref_mean - cur_mean).abs();
        let drift_detected = distance > self.threshold;

        Ok(DistributionComparison {
            distance,
            threshold: self.threshold,
            drift_detected,
            confidence: if drift_detected {
                A::from(0.8).expect("unwrap failed")
            } else {
                A::from(0.2).expect("unwrap failed")
            },
        })
    }

    fn get_threshold(&self) -> A {
        self.threshold
    }

    fn update_threshold(&mut self, new_threshold: A) {
        self.threshold = new_threshold;
    }
}

struct JSDivergenceComparator<A: Float + Send + Sync> {
    threshold: A,
}

impl<A: Float + Send + Sync + Send + Sync> JSDivergenceComparator<A> {
    fn new(sensitivity: f64) -> Result<Self, String> {
        Ok(Self {
            threshold: A::from(sensitivity).expect("unwrap failed"),
        })
    }
}

impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> DistributionComparator<A>
    for JSDivergenceComparator<A>
{
    fn compare_distributions(
        &self,
        reference: &[A],
        current: &[A],
    ) -> Result<DistributionComparison<A>, String> {
        // Simplified JS divergence calculation
        let ref_mean =
            reference.iter().cloned().sum::<A>() / A::from(reference.len()).expect("unwrap failed");
        let cur_mean =
            current.iter().cloned().sum::<A>() / A::from(current.len()).expect("unwrap failed");

        let distance = (ref_mean - cur_mean).abs() * A::from(0.5).expect("unwrap failed"); // Simplified
        let drift_detected = distance > self.threshold;

        Ok(DistributionComparison {
            distance,
            threshold: self.threshold,
            drift_detected,
            confidence: if drift_detected {
                A::from(0.75).expect("unwrap failed")
            } else {
                A::from(0.25).expect("unwrap failed")
            },
        })
    }

    fn get_threshold(&self) -> A {
        self.threshold
    }

    fn update_threshold(&mut self, new_threshold: A) {
        self.threshold = new_threshold;
    }
}

struct LinearModelDetector<A: Float + Send + Sync> {
    model_performance: A,
    baseline_performance: A,
}

impl<A: Float + Default + Send + Sync + Send + Sync> LinearModelDetector<A> {
    fn new() -> Result<Self, String> {
        Ok(Self {
            model_performance: A::zero(),
            baseline_performance: A::zero(),
        })
    }
}

impl<A: Float + Default + Clone + Send + Sync + std::iter::Sum> ModelBasedDetector<A>
    for LinearModelDetector<A>
{
    fn update_model(&mut self, _data: &[StreamingDataPoint<A>]) -> Result<(), String> {
        // Simplified model update
        Ok(())
    }

    fn detect_drift(
        &mut self,
        _data: &[StreamingDataPoint<A>],
    ) -> Result<ModelDriftResult<A>, String> {
        // Simplified drift detection based on performance degradation
        let performance_degradation = self.baseline_performance - self.model_performance;
        let drift_detected = performance_degradation > A::from(0.1).expect("unwrap failed");

        Ok(ModelDriftResult {
            drift_detected,
            performance_degradation,
            confidence: if drift_detected {
                A::from(0.7).expect("unwrap failed")
            } else {
                A::from(0.3).expect("unwrap failed")
            },
            feature_importance_changes: Vec::new(),
        })
    }

    fn reset_model(&mut self) -> Result<(), String> {
        self.model_performance = A::zero();
        self.baseline_performance = A::zero();
        Ok(())
    }
}