trustformers-debug 0.1.1

Advanced debugging tools for TrustformeRS models
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
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
//! Advanced analytics for model behavior analysis.
//!
//! This module provides sophisticated analytical capabilities including
//! clustering analysis, temporal dynamics monitoring, representation
//! stability assessment, and multi-dimensional data visualization.

use anyhow::Result;
use std::collections::{HashMap, VecDeque};

use super::types::{
    ActivationHeatmap, ClusteringResults, DriftInfo, HiddenStateAnalysis, LayerActivationStats,
    ModelPerformanceMetrics, RepresentationStability, TemporalDynamics,
};

/// Advanced analytics engine for model behavior analysis.
#[derive(Debug)]
#[allow(dead_code)]
pub struct AdvancedAnalytics {
    /// Analytics configuration
    config: AnalyticsConfig,
    /// Historical hidden state data
    hidden_states_history: VecDeque<HiddenStateData>,
    /// Performance correlation data
    performance_correlations: HashMap<String, CorrelationData>,
    /// Temporal analysis cache
    #[allow(dead_code)]
    temporal_analysis_cache: TemporalAnalysisCache,
    /// Clustering analysis results
    clustering_results_cache: HashMap<String, ClusteringResults>,
}

/// Configuration for advanced analytics.
#[derive(Debug, Clone)]
pub struct AnalyticsConfig {
    /// Maximum number of historical samples to retain
    pub max_history_samples: usize,
    /// Minimum samples required for clustering analysis
    pub min_clustering_samples: usize,
    /// Number of clusters for k-means analysis
    pub default_num_clusters: usize,
    /// Window size for temporal analysis
    pub temporal_analysis_window: usize,
    /// Drift detection sensitivity
    pub drift_detection_sensitivity: f64,
    /// Correlation analysis threshold
    pub correlation_threshold: f64,
    /// Enable advanced visualizations
    pub enable_visualizations: bool,
}

/// Hidden state data for analysis.
#[derive(Debug, Clone)]
pub struct HiddenStateData {
    /// Layer name
    pub layer_name: String,
    /// Hidden state vectors
    pub hidden_states: Vec<Vec<f64>>,
    /// Corresponding labels or metadata
    pub labels: Option<Vec<String>>,
    /// Timestamp of collection
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// Training step when collected
    pub training_step: usize,
}

/// Correlation analysis data.
#[derive(Debug, Clone)]
pub struct CorrelationData {
    /// Metric name
    pub metric_name: String,
    /// Historical values
    pub values: VecDeque<f64>,
    /// Correlations with other metrics
    pub correlations: HashMap<String, f64>,
    /// Last update timestamp
    pub last_updated: chrono::DateTime<chrono::Utc>,
}

/// Temporal analysis cache for performance optimization.
#[derive(Debug, Clone)]
pub struct TemporalAnalysisCache {
    /// Cached drift detection results
    pub drift_results: HashMap<String, DriftInfo>,
    /// Cached temporal consistency scores
    pub consistency_scores: HashMap<String, f64>,
    /// Cached stability windows
    pub stability_windows: HashMap<String, Vec<(usize, usize)>>,
    /// Last analysis timestamp
    pub last_analysis: chrono::DateTime<chrono::Utc>,
}

/// Advanced clustering analysis parameters.
#[derive(Debug, Clone)]
pub struct ClusteringParameters {
    /// Number of clusters
    pub num_clusters: usize,
    /// Maximum iterations for k-means
    pub max_iterations: usize,
    /// Convergence tolerance
    pub tolerance: f64,
    /// Random seed for reproducibility
    pub random_seed: Option<u64>,
    /// Distance metric to use
    pub distance_metric: DistanceMetric,
}

/// Distance metrics for clustering.
#[derive(Debug, Clone)]
pub enum DistanceMetric {
    /// Euclidean distance
    Euclidean,
    /// Manhattan distance
    Manhattan,
    /// Cosine similarity
    Cosine,
    /// Minkowski distance with parameter p
    Minkowski { p: f64 },
}

/// Dimensionality reduction parameters.
#[derive(Debug, Clone)]
pub struct DimensionalityReductionParams {
    /// Target dimensions
    pub target_dimensions: usize,
    /// Reduction method
    pub method: ReductionMethod,
    /// Preserve variance ratio
    pub preserve_variance_ratio: f64,
}

/// Dimensionality reduction methods.
#[derive(Debug, Clone)]
pub enum ReductionMethod {
    /// Principal Component Analysis
    PCA,
    /// t-SNE
    TSNE { perplexity: f64 },
    /// UMAP
    UMAP { n_neighbors: usize, min_dist: f64 },
}

/// Visualization generation parameters.
#[derive(Debug, Clone)]
pub struct VisualizationParams {
    /// Output dimensions (width, height)
    pub dimensions: (usize, usize),
    /// Color scheme
    pub color_scheme: ColorScheme,
    /// Include annotations
    pub include_annotations: bool,
    /// Export format
    pub export_format: ExportFormat,
}

/// Color schemes for visualizations.
#[derive(Debug, Clone)]
pub enum ColorScheme {
    /// Viridis color scale
    Viridis,
    /// Plasma color scale
    Plasma,
    /// Inferno color scale
    Inferno,
    /// Custom color map
    Custom(Vec<(f64, f64, f64)>),
}

/// Export formats for visualizations.
#[derive(Debug, Clone)]
pub enum ExportFormat {
    /// PNG image
    PNG,
    /// SVG vector
    SVG,
    /// JSON data
    JSON,
    /// CSV data
    CSV,
}

/// Statistical analysis results.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct StatisticalAnalysis {
    /// Mean values
    pub means: Vec<f64>,
    /// Standard deviations
    pub std_devs: Vec<f64>,
    /// Correlation matrix
    pub correlation_matrix: Vec<Vec<f64>>,
    /// Principal components
    pub principal_components: Vec<Vec<f64>>,
    /// Explained variance ratios
    pub explained_variance_ratios: Vec<f64>,
    /// Statistical significance tests
    pub significance_tests: Vec<SignificanceTest>,
}

/// Statistical significance test result.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SignificanceTest {
    /// Test name
    pub test_name: String,
    /// Test statistic
    pub statistic: f64,
    /// P-value
    pub p_value: f64,
    /// Degrees of freedom
    pub degrees_of_freedom: Option<usize>,
    /// Confidence interval
    pub confidence_interval: Option<(f64, f64)>,
}

/// Anomaly detection results.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AnomalyDetectionResults {
    /// Detected anomalies
    pub anomalies: Vec<Anomaly>,
    /// Anomaly scores for all data points
    pub anomaly_scores: Vec<f64>,
    /// Detection threshold used
    pub threshold: f64,
    /// Detection method
    pub method: AnomalyDetectionMethod,
}

/// Individual anomaly information.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Anomaly {
    /// Index in dataset
    pub index: usize,
    /// Anomaly score
    pub score: f64,
    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// Additional context
    pub context: HashMap<String, String>,
}

/// Anomaly detection methods.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum AnomalyDetectionMethod {
    /// Isolation Forest
    IsolationForest { n_trees: usize },
    /// Local Outlier Factor
    LocalOutlierFactor { n_neighbors: usize },
    /// One-Class SVM
    OneClassSVM { nu: f64 },
    /// Statistical threshold
    StatisticalThreshold { n_std: f64 },
}

impl Default for AnalyticsConfig {
    fn default() -> Self {
        Self {
            max_history_samples: 10000,
            min_clustering_samples: 50,
            default_num_clusters: 8,
            temporal_analysis_window: 100,
            drift_detection_sensitivity: 0.05,
            correlation_threshold: 0.7,
            enable_visualizations: true,
        }
    }
}

impl Default for ClusteringParameters {
    fn default() -> Self {
        Self {
            num_clusters: 8,
            max_iterations: 100,
            tolerance: 1e-4,
            random_seed: Some(42),
            distance_metric: DistanceMetric::Euclidean,
        }
    }
}

impl AdvancedAnalytics {
    /// Create a new advanced analytics engine.
    pub fn new() -> Self {
        Self {
            config: AnalyticsConfig::default(),
            hidden_states_history: VecDeque::new(),
            performance_correlations: HashMap::new(),
            temporal_analysis_cache: TemporalAnalysisCache::new(),
            clustering_results_cache: HashMap::new(),
        }
    }

    /// Create analytics engine with custom configuration.
    pub fn with_config(config: AnalyticsConfig) -> Self {
        Self {
            config,
            hidden_states_history: VecDeque::new(),
            performance_correlations: HashMap::new(),
            temporal_analysis_cache: TemporalAnalysisCache::new(),
            clustering_results_cache: HashMap::new(),
        }
    }

    /// Record hidden state data for analysis.
    pub fn record_hidden_states(&mut self, hidden_states: HiddenStateData) {
        self.hidden_states_history.push_back(hidden_states);

        while self.hidden_states_history.len() > self.config.max_history_samples {
            self.hidden_states_history.pop_front();
        }
    }

    /// Record performance metrics for correlation analysis.
    pub fn record_performance_metrics(&mut self, metrics: &ModelPerformanceMetrics) {
        self.update_correlation_data("loss", metrics.loss);
        self.update_correlation_data("throughput", metrics.throughput_samples_per_sec);
        self.update_correlation_data("memory_usage", metrics.memory_usage_mb);

        if let Some(accuracy) = metrics.accuracy {
            self.update_correlation_data("accuracy", accuracy);
        }

        if let Some(gpu_util) = metrics.gpu_utilization {
            self.update_correlation_data("gpu_utilization", gpu_util);
        }
    }

    /// Perform comprehensive hidden state analysis.
    pub fn analyze_hidden_states(&self, layer_name: &str) -> Result<HiddenStateAnalysis> {
        let layer_data: Vec<_> = self
            .hidden_states_history
            .iter()
            .filter(|data| data.layer_name == layer_name)
            .collect();

        if layer_data.is_empty() {
            return Err(anyhow::anyhow!(
                "No hidden state data available for layer: {}",
                layer_name
            ));
        }

        // Extract all hidden states for this layer
        let all_states: Vec<Vec<f64>> =
            layer_data.iter().flat_map(|data| data.hidden_states.iter()).cloned().collect();

        if all_states.is_empty() {
            return Err(anyhow::anyhow!(
                "No hidden states found for layer: {}",
                layer_name
            ));
        }

        let dimensionality = all_states[0].len();

        // Perform clustering analysis
        let clustering_results = self.perform_clustering_analysis(&all_states)?;

        // Analyze temporal dynamics
        let temporal_dynamics = self.analyze_temporal_dynamics(&layer_data)?;

        // Assess representation stability
        let representation_stability = self.assess_representation_stability(&all_states)?;

        // Calculate information content
        let information_content = self.calculate_information_content(&all_states)?;

        Ok(HiddenStateAnalysis {
            dimensionality,
            information_content,
            clustering_results,
            temporal_dynamics,
            representation_stability,
        })
    }

    /// Perform clustering analysis on hidden states.
    pub fn perform_clustering_analysis(&self, data: &[Vec<f64>]) -> Result<ClusteringResults> {
        if data.len() < self.config.min_clustering_samples {
            return Err(anyhow::anyhow!("Insufficient data for clustering analysis"));
        }

        let params = ClusteringParameters::default();
        let num_clusters = params.num_clusters.min(data.len() / 2);

        // Simple k-means clustering implementation
        let mut cluster_centers = self.initialize_cluster_centers(data, num_clusters)?;
        let mut cluster_assignments = vec![0; data.len()];
        #[allow(dead_code)]
        #[allow(unused_assignments)]
        let mut _converged = false;

        for _iteration in 0..params.max_iterations {
            // Assign points to nearest cluster
            let mut new_assignments = vec![0; data.len()];
            for (i, point) in data.iter().enumerate() {
                let mut best_distance = f64::INFINITY;
                let mut best_cluster = 0;

                for (j, center) in cluster_centers.iter().enumerate() {
                    let distance =
                        self.calculate_distance(point, center, &params.distance_metric)?;
                    if distance < best_distance {
                        best_distance = distance;
                        best_cluster = j;
                    }
                }
                new_assignments[i] = best_cluster;
            }

            // Check for convergence
            if new_assignments == cluster_assignments {
                _converged = true;
                break;
            }
            cluster_assignments = new_assignments;

            // Update cluster centers
            cluster_centers =
                self.update_cluster_centers(data, &cluster_assignments, num_clusters)?;
        }

        // Calculate silhouette score
        let silhouette_score =
            self.calculate_silhouette_score(data, &cluster_assignments, &cluster_centers)?;

        // Calculate inertia
        let inertia = self.calculate_inertia(data, &cluster_assignments, &cluster_centers)?;

        Ok(ClusteringResults {
            num_clusters,
            cluster_centers,
            cluster_assignments,
            silhouette_score,
            inertia,
        })
    }

    /// Analyze temporal dynamics of hidden states.
    pub fn analyze_temporal_dynamics(
        &self,
        layer_data: &[&HiddenStateData],
    ) -> Result<TemporalDynamics> {
        if layer_data.len() < 2 {
            return Err(anyhow::anyhow!("Insufficient temporal data"));
        }

        // Calculate temporal consistency
        let temporal_consistency = self.calculate_temporal_consistency(layer_data)?;

        // Calculate change rate
        let change_rate = self.calculate_change_rate(layer_data)?;

        // Identify stability windows
        let stability_windows = self.identify_stability_windows(layer_data)?;

        // Detect distribution drift
        let drift_detection = self.detect_distribution_drift(layer_data)?;

        Ok(TemporalDynamics {
            temporal_consistency,
            change_rate,
            stability_windows,
            drift_detection,
        })
    }

    /// Assess representation stability.
    pub fn assess_representation_stability(
        &self,
        hidden_states: &[Vec<f64>],
    ) -> Result<RepresentationStability> {
        if hidden_states.is_empty() {
            return Err(anyhow::anyhow!("No hidden states provided"));
        }

        // Calculate overall stability score
        let stability_score = self.calculate_stability_score(hidden_states)?;

        // Calculate variance across batches (simulated)
        let variance_across_batches = self.calculate_batch_variance(hidden_states)?;

        // Calculate consistency measure
        let consistency_measure = self.calculate_consistency_measure(hidden_states)?;

        // Assess robustness to noise (simulated)
        let robustness_to_noise = self.assess_noise_robustness(hidden_states)?;

        Ok(RepresentationStability {
            stability_score,
            variance_across_batches,
            consistency_measure,
            robustness_to_noise,
        })
    }

    /// Generate activation heatmap for visualization.
    pub fn generate_activation_heatmap(
        &self,
        layer_stats: &[LayerActivationStats],
    ) -> Result<ActivationHeatmap> {
        if layer_stats.is_empty() {
            return Err(anyhow::anyhow!("No layer statistics provided"));
        }

        // Create heatmap data based on layer statistics
        let mut data = Vec::new();
        let mut min_val = f64::INFINITY;
        let mut max_val = f64::NEG_INFINITY;

        for stats in layer_stats {
            let row = vec![
                stats.mean_activation,
                stats.std_activation,
                stats.min_activation,
                stats.max_activation,
                stats.dead_neurons_ratio,
                stats.saturated_neurons_ratio,
                stats.sparsity,
            ];

            for &val in &row {
                min_val = min_val.min(val);
                max_val = max_val.max(val);
            }

            data.push(row);
        }

        let dimensions = (data.len(), data.first().map_or(0, |row| row.len()));

        Ok(ActivationHeatmap {
            data,
            dimensions,
            value_range: (min_val, max_val),
            interpretation: "Activation statistics heatmap showing layer behavior patterns"
                .to_string(),
        })
    }

    /// Perform anomaly detection on performance metrics.
    pub fn detect_performance_anomalies(&self) -> Result<AnomalyDetectionResults> {
        // Extract performance data
        let mut all_values = Vec::new();
        for correlation_data in self.performance_correlations.values() {
            all_values.extend(correlation_data.values.iter().cloned());
        }

        if all_values.is_empty() {
            return Err(anyhow::anyhow!("No performance data available"));
        }

        // Use statistical threshold method for anomaly detection
        let method = AnomalyDetectionMethod::StatisticalThreshold { n_std: 2.0 };

        let mean = all_values.iter().sum::<f64>() / all_values.len() as f64;
        let variance =
            all_values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / all_values.len() as f64;
        let std_dev = variance.sqrt();

        let threshold = mean + 2.0 * std_dev;

        let mut anomalies = Vec::new();
        let mut anomaly_scores = Vec::new();

        for (i, &value) in all_values.iter().enumerate() {
            let score = (value - mean).abs() / std_dev;
            anomaly_scores.push(score);

            if value > threshold {
                anomalies.push(Anomaly {
                    index: i,
                    score,
                    timestamp: chrono::Utc::now(),
                    context: HashMap::new(),
                });
            }
        }

        Ok(AnomalyDetectionResults {
            anomalies,
            anomaly_scores,
            threshold,
            method,
        })
    }

    /// Calculate correlation matrix for all metrics.
    pub fn calculate_correlation_matrix(&self) -> Result<Vec<Vec<f64>>> {
        let metric_names: Vec<_> = self.performance_correlations.keys().cloned().collect();
        let n_metrics = metric_names.len();

        if n_metrics == 0 {
            return Err(anyhow::anyhow!(
                "No metrics available for correlation analysis"
            ));
        }

        let mut correlation_matrix = vec![vec![0.0; n_metrics]; n_metrics];

        for (i, metric1) in metric_names.iter().enumerate() {
            for (j, metric2) in metric_names.iter().enumerate() {
                if i == j {
                    correlation_matrix[i][j] = 1.0;
                } else {
                    let correlation = self.calculate_correlation(metric1, metric2)?;
                    correlation_matrix[i][j] = correlation;
                }
            }
        }

        Ok(correlation_matrix)
    }

    /// Perform statistical analysis on collected data.
    pub fn perform_statistical_analysis(&self) -> Result<StatisticalAnalysis> {
        if self.performance_correlations.is_empty() {
            return Err(anyhow::anyhow!(
                "No data available for statistical analysis"
            ));
        }

        // Calculate means and standard deviations
        let mut means = Vec::new();
        let mut std_devs = Vec::new();

        for correlation_data in self.performance_correlations.values() {
            let values: Vec<f64> = correlation_data.values.iter().cloned().collect();
            if !values.is_empty() {
                let mean = values.iter().sum::<f64>() / values.len() as f64;
                let variance =
                    values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / values.len() as f64;
                let std_dev = variance.sqrt();

                means.push(mean);
                std_devs.push(std_dev);
            }
        }

        // Calculate correlation matrix
        let correlation_matrix = self.calculate_correlation_matrix()?;

        // Placeholder for principal components and explained variance
        let principal_components = vec![vec![1.0; means.len()]; means.len()];
        let explained_variance_ratios = vec![1.0 / means.len() as f64; means.len()];

        // Placeholder for significance tests
        let significance_tests = vec![SignificanceTest {
            test_name: "Sample t-test".to_string(),
            statistic: 1.0,
            p_value: 0.05,
            degrees_of_freedom: Some(means.len() - 1),
            confidence_interval: Some((0.0, 1.0)),
        }];

        Ok(StatisticalAnalysis {
            means,
            std_devs,
            correlation_matrix,
            principal_components,
            explained_variance_ratios,
            significance_tests,
        })
    }

    /// Generate comprehensive analytics report.
    pub fn generate_analytics_report(&self) -> Result<AnalyticsReport> {
        let correlation_matrix = self.calculate_correlation_matrix().unwrap_or_default();
        let statistical_analysis = self.perform_statistical_analysis().unwrap_or_default();
        let anomaly_detection = self.detect_performance_anomalies().unwrap_or_default();

        // Analyze each layer if data is available
        let mut layer_analyses = HashMap::new();
        let unique_layers: std::collections::HashSet<String> =
            self.hidden_states_history.iter().map(|data| data.layer_name.clone()).collect();

        for layer_name in unique_layers {
            if let Ok(analysis) = self.analyze_hidden_states(&layer_name) {
                layer_analyses.insert(layer_name, analysis);
            }
        }

        Ok(AnalyticsReport {
            correlation_matrix,
            statistical_analysis,
            layer_analyses,
            anomaly_detection,
            temporal_summary: self.generate_temporal_summary(),
            recommendations: self.generate_analytics_recommendations(),
        })
    }

    // Helper methods

    /// Update correlation data for a metric.
    fn update_correlation_data(&mut self, metric_name: &str, value: f64) {
        let correlation_data = self
            .performance_correlations
            .entry(metric_name.to_string())
            .or_insert_with(|| CorrelationData {
                metric_name: metric_name.to_string(),
                values: VecDeque::new(),
                correlations: HashMap::new(),
                last_updated: chrono::Utc::now(),
            });

        correlation_data.values.push_back(value);
        correlation_data.last_updated = chrono::Utc::now();

        // Limit history size
        while correlation_data.values.len() > self.config.max_history_samples {
            correlation_data.values.pop_front();
        }
    }

    /// Initialize cluster centers using k-means++ algorithm.
    fn initialize_cluster_centers(
        &self,
        data: &[Vec<f64>],
        num_clusters: usize,
    ) -> Result<Vec<Vec<f64>>> {
        if data.is_empty() || num_clusters == 0 {
            return Err(anyhow::anyhow!("Invalid input for cluster initialization"));
        }

        let mut centers = Vec::new();
        let _dimensions = data[0].len();

        // Choose first center randomly (simplified - just use first point)
        centers.push(data[0].clone());

        // Choose remaining centers using k-means++ logic (simplified)
        for _ in 1..num_clusters {
            if centers.len() >= data.len() {
                break;
            }

            let mut best_distance = 0.0;
            let mut best_point = data[0].clone();

            for point in data {
                let mut min_distance = f64::INFINITY;
                for center in &centers {
                    let distance =
                        self.calculate_distance(point, center, &DistanceMetric::Euclidean)?;
                    min_distance = min_distance.min(distance);
                }

                if min_distance > best_distance {
                    best_distance = min_distance;
                    best_point = point.clone();
                }
            }

            centers.push(best_point);
        }

        Ok(centers)
    }

    /// Calculate distance between two points.
    fn calculate_distance(
        &self,
        point1: &[f64],
        point2: &[f64],
        metric: &DistanceMetric,
    ) -> Result<f64> {
        if point1.len() != point2.len() {
            return Err(anyhow::anyhow!("Points must have same dimensionality"));
        }

        match metric {
            DistanceMetric::Euclidean => {
                let sum_squared =
                    point1.iter().zip(point2.iter()).map(|(a, b)| (a - b).powi(2)).sum::<f64>();
                Ok(sum_squared.sqrt())
            },
            DistanceMetric::Manhattan => {
                let sum_abs =
                    point1.iter().zip(point2.iter()).map(|(a, b)| (a - b).abs()).sum::<f64>();
                Ok(sum_abs)
            },
            DistanceMetric::Cosine => {
                let dot_product = point1.iter().zip(point2.iter()).map(|(a, b)| a * b).sum::<f64>();
                let norm1 = point1.iter().map(|x| x.powi(2)).sum::<f64>().sqrt();
                let norm2 = point2.iter().map(|x| x.powi(2)).sum::<f64>().sqrt();

                if norm1 == 0.0 || norm2 == 0.0 {
                    Ok(1.0)
                } else {
                    Ok(1.0 - (dot_product / (norm1 * norm2)))
                }
            },
            DistanceMetric::Minkowski { p } => {
                let sum_powered = point1
                    .iter()
                    .zip(point2.iter())
                    .map(|(a, b)| (a - b).abs().powf(*p))
                    .sum::<f64>();
                Ok(sum_powered.powf(1.0 / p))
            },
        }
    }

    /// Update cluster centers based on current assignments.
    fn update_cluster_centers(
        &self,
        data: &[Vec<f64>],
        assignments: &[usize],
        num_clusters: usize,
    ) -> Result<Vec<Vec<f64>>> {
        let dimensions = data[0].len();
        let mut new_centers = vec![vec![0.0; dimensions]; num_clusters];
        let mut cluster_counts = vec![0; num_clusters];

        // Sum points in each cluster
        for (point, &cluster_id) in data.iter().zip(assignments.iter()) {
            if cluster_id < num_clusters {
                for (i, &value) in point.iter().enumerate() {
                    new_centers[cluster_id][i] += value;
                }
                cluster_counts[cluster_id] += 1;
            }
        }

        // Average to get new centers
        for (cluster_id, count) in cluster_counts.iter().enumerate() {
            if *count > 0 {
                for value in &mut new_centers[cluster_id] {
                    *value /= *count as f64;
                }
            }
        }

        Ok(new_centers)
    }

    /// Calculate silhouette score for clustering quality.
    fn calculate_silhouette_score(
        &self,
        data: &[Vec<f64>],
        assignments: &[usize],
        centers: &[Vec<f64>],
    ) -> Result<f64> {
        if data.is_empty() {
            return Ok(0.0);
        }

        let mut total_score = 0.0;
        let mut valid_points = 0;

        for (i, point) in data.iter().enumerate() {
            let cluster_id = assignments[i];

            // Calculate average distance to points in same cluster (a)
            let mut same_cluster_distances = Vec::new();
            for (j, other_point) in data.iter().enumerate() {
                if i != j && assignments[j] == cluster_id {
                    let distance =
                        self.calculate_distance(point, other_point, &DistanceMetric::Euclidean)?;
                    same_cluster_distances.push(distance);
                }
            }

            let a = if same_cluster_distances.is_empty() {
                0.0
            } else {
                same_cluster_distances.iter().sum::<f64>() / same_cluster_distances.len() as f64
            };

            // Calculate minimum average distance to points in other clusters (b)
            let mut min_other_cluster_distance = f64::INFINITY;
            for (other_cluster_id, _) in centers.iter().enumerate() {
                if other_cluster_id != cluster_id {
                    let mut other_cluster_distances = Vec::new();
                    for (j, other_point) in data.iter().enumerate() {
                        if assignments[j] == other_cluster_id {
                            let distance = self.calculate_distance(
                                point,
                                other_point,
                                &DistanceMetric::Euclidean,
                            )?;
                            other_cluster_distances.push(distance);
                        }
                    }

                    if !other_cluster_distances.is_empty() {
                        let avg_distance = other_cluster_distances.iter().sum::<f64>()
                            / other_cluster_distances.len() as f64;
                        min_other_cluster_distance = min_other_cluster_distance.min(avg_distance);
                    }
                }
            }

            let b = min_other_cluster_distance;

            if a < b {
                total_score += (b - a) / b;
            } else if a > b {
                total_score += (b - a) / a;
            }
            // If a == b, silhouette score is 0 (no contribution)

            valid_points += 1;
        }

        Ok(if valid_points > 0 { total_score / valid_points as f64 } else { 0.0 })
    }

    /// Calculate inertia (sum of squared distances to centroids).
    fn calculate_inertia(
        &self,
        data: &[Vec<f64>],
        assignments: &[usize],
        centers: &[Vec<f64>],
    ) -> Result<f64> {
        let mut inertia = 0.0;

        for (point, &cluster_id) in data.iter().zip(assignments.iter()) {
            if cluster_id < centers.len() {
                let distance = self.calculate_distance(
                    point,
                    &centers[cluster_id],
                    &DistanceMetric::Euclidean,
                )?;
                inertia += distance.powi(2);
            }
        }

        Ok(inertia)
    }

    /// Calculate information content of hidden states.
    fn calculate_information_content(&self, hidden_states: &[Vec<f64>]) -> Result<f64> {
        if hidden_states.is_empty() {
            return Ok(0.0);
        }

        let dimensions = hidden_states[0].len();
        let mut total_variance = 0.0;

        for dim in 0..dimensions {
            let values: Vec<f64> = hidden_states.iter().map(|state| state[dim]).collect();
            if values.len() > 1 {
                let mean = values.iter().sum::<f64>() / values.len() as f64;
                let variance = values.iter().map(|x| (x - mean).powi(2)).sum::<f64>()
                    / (values.len() - 1) as f64;
                total_variance += variance;
            }
        }

        // Information content as normalized variance
        Ok(total_variance / dimensions as f64)
    }

    /// Calculate temporal consistency of hidden states.
    fn calculate_temporal_consistency(&self, layer_data: &[&HiddenStateData]) -> Result<f64> {
        if layer_data.len() < 2 {
            return Ok(1.0);
        }

        let mut consistency_scores = Vec::new();

        for i in 1..layer_data.len() {
            let prev_states = &layer_data[i - 1].hidden_states;
            let curr_states = &layer_data[i].hidden_states;

            if !prev_states.is_empty() && !curr_states.is_empty() {
                // Simple consistency measure based on mean state similarity
                let prev_mean = self.calculate_mean_state(prev_states);
                let curr_mean = self.calculate_mean_state(curr_states);

                if prev_mean.len() == curr_mean.len() {
                    let distance = self.calculate_distance(
                        &prev_mean,
                        &curr_mean,
                        &DistanceMetric::Euclidean,
                    )?;
                    consistency_scores.push(1.0 / (1.0 + distance));
                }
            }
        }

        Ok(if consistency_scores.is_empty() {
            1.0
        } else {
            consistency_scores.iter().sum::<f64>() / consistency_scores.len() as f64
        })
    }

    /// Calculate mean state from a collection of states.
    fn calculate_mean_state(&self, states: &[Vec<f64>]) -> Vec<f64> {
        if states.is_empty() {
            return Vec::new();
        }

        let dimensions = states[0].len();
        let mut mean_state = vec![0.0; dimensions];

        for state in states {
            for (i, &value) in state.iter().enumerate() {
                if i < dimensions {
                    mean_state[i] += value;
                }
            }
        }

        for value in &mut mean_state {
            *value /= states.len() as f64;
        }

        mean_state
    }

    /// Calculate change rate between temporal samples.
    fn calculate_change_rate(&self, layer_data: &[&HiddenStateData]) -> Result<f64> {
        if layer_data.len() < 2 {
            return Ok(0.0);
        }

        let mut total_change = 0.0;
        let mut valid_comparisons = 0;

        for i in 1..layer_data.len() {
            let prev_mean = self.calculate_mean_state(&layer_data[i - 1].hidden_states);
            let curr_mean = self.calculate_mean_state(&layer_data[i].hidden_states);

            if !prev_mean.is_empty() && !curr_mean.is_empty() && prev_mean.len() == curr_mean.len()
            {
                let change =
                    self.calculate_distance(&prev_mean, &curr_mean, &DistanceMetric::Euclidean)?;
                total_change += change;
                valid_comparisons += 1;
            }
        }

        Ok(if valid_comparisons > 0 {
            total_change / valid_comparisons as f64
        } else {
            0.0
        })
    }

    /// Identify stability windows in temporal data.
    fn identify_stability_windows(
        &self,
        layer_data: &[&HiddenStateData],
    ) -> Result<Vec<(usize, usize)>> {
        if layer_data.len() < 3 {
            return Ok(Vec::new());
        }

        let mut stability_windows = Vec::new();
        let mut window_start = 0;
        let stability_threshold = 0.1; // Configurable threshold

        for i in 1..layer_data.len() {
            let prev_mean = self.calculate_mean_state(&layer_data[i - 1].hidden_states);
            let curr_mean = self.calculate_mean_state(&layer_data[i].hidden_states);

            if !prev_mean.is_empty() && !curr_mean.is_empty() && prev_mean.len() == curr_mean.len()
            {
                let change = self
                    .calculate_distance(&prev_mean, &curr_mean, &DistanceMetric::Euclidean)
                    .unwrap_or(f64::INFINITY);

                if change > stability_threshold {
                    // End of stability window
                    if i - window_start > 2 {
                        stability_windows.push((window_start, i - 1));
                    }
                    window_start = i;
                }
            }
        }

        // Handle final window
        if layer_data.len() - window_start > 2 {
            stability_windows.push((window_start, layer_data.len() - 1));
        }

        Ok(stability_windows)
    }

    /// Detect distribution drift in temporal data.
    fn detect_distribution_drift(&self, layer_data: &[&HiddenStateData]) -> Result<DriftInfo> {
        if layer_data.len() < self.config.temporal_analysis_window {
            return Ok(DriftInfo {
                drift_detected: false,
                drift_magnitude: 0.0,
                drift_direction: "unknown".to_string(),
                onset_step: None,
            });
        }

        let window_size = self.config.temporal_analysis_window;
        let mid_point = layer_data.len() / 2;

        // Compare early and late windows
        let early_data = &layer_data[0..window_size.min(mid_point)];
        let late_data = &layer_data[mid_point.max(layer_data.len() - window_size)..];

        let early_mean = self.calculate_aggregated_mean(early_data);
        let late_mean = self.calculate_aggregated_mean(late_data);

        if early_mean.len() == late_mean.len() && !early_mean.is_empty() {
            let drift_magnitude =
                self.calculate_distance(&early_mean, &late_mean, &DistanceMetric::Euclidean)?;
            let drift_detected = drift_magnitude > self.config.drift_detection_sensitivity;

            Ok(DriftInfo {
                drift_detected,
                drift_magnitude,
                drift_direction: if drift_detected {
                    "forward".to_string()
                } else {
                    "stable".to_string()
                },
                onset_step: if drift_detected { Some(mid_point) } else { None },
            })
        } else {
            Ok(DriftInfo {
                drift_detected: false,
                drift_magnitude: 0.0,
                drift_direction: "unknown".to_string(),
                onset_step: None,
            })
        }
    }

    /// Calculate aggregated mean across multiple data samples.
    fn calculate_aggregated_mean(&self, layer_data: &[&HiddenStateData]) -> Vec<f64> {
        let all_states: Vec<Vec<f64>> =
            layer_data.iter().flat_map(|data| data.hidden_states.iter()).cloned().collect();

        self.calculate_mean_state(&all_states)
    }

    /// Calculate stability score for representation.
    fn calculate_stability_score(&self, hidden_states: &[Vec<f64>]) -> Result<f64> {
        if hidden_states.len() < 2 {
            return Ok(1.0);
        }

        let mut stability_scores = Vec::new();
        let window_size = (hidden_states.len() / 10).max(2);

        for i in window_size..hidden_states.len() {
            let current_window = &hidden_states[i - window_size..i];
            let mean_current = self.calculate_mean_state(current_window);

            if i >= 2 * window_size {
                let prev_window = &hidden_states[i - 2 * window_size..i - window_size];
                let mean_prev = self.calculate_mean_state(prev_window);

                if mean_current.len() == mean_prev.len() && !mean_current.is_empty() {
                    let distance = self.calculate_distance(
                        &mean_current,
                        &mean_prev,
                        &DistanceMetric::Euclidean,
                    )?;
                    stability_scores.push(1.0 / (1.0 + distance));
                }
            }
        }

        Ok(if stability_scores.is_empty() {
            1.0
        } else {
            stability_scores.iter().sum::<f64>() / stability_scores.len() as f64
        })
    }

    /// Calculate variance across batches (simulated).
    fn calculate_batch_variance(&self, hidden_states: &[Vec<f64>]) -> Result<f64> {
        if hidden_states.is_empty() {
            return Ok(0.0);
        }

        let dimensions = hidden_states[0].len();
        let mut total_variance = 0.0;

        for dim in 0..dimensions {
            let values: Vec<f64> = hidden_states.iter().map(|state| state[dim]).collect();
            if values.len() > 1 {
                let mean = values.iter().sum::<f64>() / values.len() as f64;
                let variance = values.iter().map(|x| (x - mean).powi(2)).sum::<f64>()
                    / (values.len() - 1) as f64;
                total_variance += variance;
            }
        }

        Ok(total_variance / dimensions as f64)
    }

    /// Calculate consistency measure for representation.
    fn calculate_consistency_measure(&self, hidden_states: &[Vec<f64>]) -> Result<f64> {
        if hidden_states.len() < 2 {
            return Ok(1.0);
        }

        // Calculate pairwise similarities and return average
        let mut similarities = Vec::new();
        let sample_size = hidden_states.len().min(100); // Limit for performance

        for i in 0..sample_size {
            for j in (i + 1)..sample_size {
                let distance = self.calculate_distance(
                    &hidden_states[i],
                    &hidden_states[j],
                    &DistanceMetric::Cosine,
                )?;
                similarities.push(1.0 - distance); // Convert distance to similarity
            }
        }

        Ok(if similarities.is_empty() {
            1.0
        } else {
            similarities.iter().sum::<f64>() / similarities.len() as f64
        })
    }

    /// Assess robustness to noise (simulated).
    fn assess_noise_robustness(&self, hidden_states: &[Vec<f64>]) -> Result<f64> {
        // Simplified robustness assessment based on state variance
        self.calculate_batch_variance(hidden_states).map(|variance| {
            // High variance might indicate low robustness to noise
            1.0 / (1.0 + variance)
        })
    }

    /// Calculate correlation between two metrics.
    fn calculate_correlation(&self, metric1: &str, metric2: &str) -> Result<f64> {
        let data1 = self
            .performance_correlations
            .get(metric1)
            .ok_or_else(|| anyhow::anyhow!("Metric {} not found", metric1))?;

        let data2 = self
            .performance_correlations
            .get(metric2)
            .ok_or_else(|| anyhow::anyhow!("Metric {} not found", metric2))?;

        let values1: Vec<f64> = data1.values.iter().cloned().collect();
        let values2: Vec<f64> = data2.values.iter().cloned().collect();

        if values1.len() != values2.len() || values1.is_empty() {
            return Ok(0.0);
        }

        let mean1 = values1.iter().sum::<f64>() / values1.len() as f64;
        let mean2 = values2.iter().sum::<f64>() / values2.len() as f64;

        let numerator: f64 = values1
            .iter()
            .zip(values2.iter())
            .map(|(x1, x2)| (x1 - mean1) * (x2 - mean2))
            .sum();

        let var1: f64 = values1.iter().map(|x| (x - mean1).powi(2)).sum();
        let var2: f64 = values2.iter().map(|x| (x - mean2).powi(2)).sum();

        let denominator = (var1 * var2).sqrt();

        Ok(if denominator == 0.0 { 0.0 } else { numerator / denominator })
    }

    /// Generate temporal summary.
    fn generate_temporal_summary(&self) -> String {
        format!(
            "Temporal analysis: {} hidden state samples collected across {} layers. \
            Average stability observed with {} correlation metrics tracked.",
            self.hidden_states_history.len(),
            self.hidden_states_history
                .iter()
                .map(|data| &data.layer_name)
                .collect::<std::collections::HashSet<_>>()
                .len(),
            self.performance_correlations.len()
        )
    }

    /// Generate analytics recommendations.
    fn generate_analytics_recommendations(&self) -> Vec<String> {
        let mut recommendations = Vec::new();

        if self.performance_correlations.len() < 3 {
            recommendations.push(
                "Collect more performance metrics for comprehensive correlation analysis"
                    .to_string(),
            );
        }

        if self.hidden_states_history.len() < 50 {
            recommendations
                .push("Increase hidden state sampling for better temporal analysis".to_string());
        }

        recommendations
            .push("Consider implementing automated anomaly detection alerts".to_string());
        recommendations.push("Enable advanced visualization for better insights".to_string());

        recommendations
    }
}

/// Comprehensive analytics report.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AnalyticsReport {
    /// Correlation matrix between metrics
    pub correlation_matrix: Vec<Vec<f64>>,
    /// Statistical analysis results
    pub statistical_analysis: StatisticalAnalysis,
    /// Layer-specific analyses
    pub layer_analyses: HashMap<String, HiddenStateAnalysis>,
    /// Anomaly detection results
    pub anomaly_detection: AnomalyDetectionResults,
    /// Temporal analysis summary
    pub temporal_summary: String,
    /// Analytics recommendations
    pub recommendations: Vec<String>,
}

impl TemporalAnalysisCache {
    /// Create a new temporal analysis cache.
    fn new() -> Self {
        Self {
            drift_results: HashMap::new(),
            consistency_scores: HashMap::new(),
            stability_windows: HashMap::new(),
            last_analysis: chrono::Utc::now(),
        }
    }
}

impl Default for AnomalyDetectionResults {
    fn default() -> Self {
        Self {
            anomalies: Vec::new(),
            anomaly_scores: Vec::new(),
            threshold: 0.0,
            method: AnomalyDetectionMethod::StatisticalThreshold { n_std: 2.0 },
        }
    }
}

impl Default for AdvancedAnalytics {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_advanced_analytics_creation() {
        let analytics = AdvancedAnalytics::new();
        assert_eq!(analytics.hidden_states_history.len(), 0);
        assert_eq!(analytics.performance_correlations.len(), 0);
    }

    #[test]
    fn test_distance_calculation() {
        let analytics = AdvancedAnalytics::new();
        let point1 = vec![1.0, 2.0, 3.0];
        let point2 = vec![4.0, 5.0, 6.0];

        let distance = analytics
            .calculate_distance(&point1, &point2, &DistanceMetric::Euclidean)
            .expect("operation failed in test");
        assert!(distance > 0.0);
    }

    #[test]
    fn test_clustering_parameters() {
        let params = ClusteringParameters::default();
        assert_eq!(params.num_clusters, 8);
        assert_eq!(params.max_iterations, 100);
    }

    #[test]
    fn test_correlation_calculation() {
        let mut analytics = AdvancedAnalytics::new();

        // Add some test data
        analytics.update_correlation_data("metric1", 1.0);
        analytics.update_correlation_data("metric1", 2.0);
        analytics.update_correlation_data("metric2", 3.0);
        analytics.update_correlation_data("metric2", 4.0);

        assert_eq!(analytics.performance_correlations.len(), 2);
    }
}