sklears-multioutput 0.1.1

Multi-output regression and classification
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
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
//! Output Correlation Analysis and Dependency Modeling
//!
//! This module provides tools for analyzing and modeling correlations and dependencies
//! between different outputs in multi-output learning scenarios. Understanding these
//! relationships can help improve model performance and provide insights into the
//! underlying data structure.

// Use SciRS2-Core for arrays and random number generation (SciRS2 Policy)
use scirs2_core::ndarray::{s, Array2, ArrayView2, Axis};
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    types::Float,
};
use std::collections::HashMap;

/// Copula-Based Modeling Analyzer
///
/// Analyzes and models complex dependencies between outputs using copulas.
/// Copulas separate the marginal distributions from the dependence structure,
/// allowing for more flexible modeling of non-linear and non-monotonic relationships.
///
/// # Examples
///
/// ```
/// use sklears_multioutput::correlation::{CopulaBasedModelingAnalyzer, CopulaType};
/// // Use SciRS2-Core for arrays and random number generation (SciRS2 Policy)
/// use scirs2_core::ndarray::array;
/// use std::collections::HashMap;
///
/// let mut outputs = HashMap::new();
/// outputs.insert("task1".to_string(), array![[1.0, 2.0], [2.0, 3.0], [3.0, 1.0]]);
/// outputs.insert("task2".to_string(), array![[0.5, 1.0], [1.0, 1.5], [1.5, 0.5]]);
///
/// let analyzer = CopulaBasedModelingAnalyzer::new()
///     .copula_types(vec![CopulaType::Gaussian, CopulaType::Clayton])
///     .fit_margins(true);
///
/// let analysis = analyzer.analyze(&outputs).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct CopulaBasedModelingAnalyzer {
    /// Types of copulas to fit
    copula_types: Vec<CopulaType>,
    /// Whether to fit marginal distributions
    fit_margins: bool,
    /// Whether to use empirical copula for comparison
    use_empirical_copula: bool,
    /// Number of samples for Monte Carlo methods
    n_samples: usize,
    /// Random state for reproducibility
    random_state: Option<u64>,
}

impl CopulaBasedModelingAnalyzer {
    pub fn new() -> Self {
        Self {
            copula_types: vec![CopulaType::Gaussian],
            fit_margins: true,
            use_empirical_copula: false,
            n_samples: 1000,
            random_state: None,
        }
    }

    /// Set the copula types to fit
    pub fn copula_types(mut self, copula_types: Vec<CopulaType>) -> Self {
        self.copula_types = copula_types;
        self
    }

    /// Set whether to fit marginal distributions
    pub fn fit_margins(mut self, fit_margins: bool) -> Self {
        self.fit_margins = fit_margins;
        self
    }

    /// Set whether to use empirical copula for comparison
    pub fn use_empirical_copula(mut self, use_empirical_copula: bool) -> Self {
        self.use_empirical_copula = use_empirical_copula;
        self
    }

    /// Set the number of samples for Monte Carlo methods
    pub fn n_samples(mut self, n_samples: usize) -> Self {
        self.n_samples = n_samples;
        self
    }

    /// Set the random state for reproducibility
    pub fn random_state(mut self, random_state: Option<u64>) -> Self {
        self.random_state = random_state;
        self
    }

    /// Analyze copula-based dependencies in the given outputs
    pub fn analyze(&self, _outputs: &HashMap<String, Array2<Float>>) -> SklResult<CopulaAnalysis> {
        // Placeholder implementation - would need full copula fitting algorithms
        let copula_models = HashMap::new();
        let marginal_distributions = HashMap::new();
        let goodness_of_fit = HashMap::new();
        let dependence_measures = HashMap::new();
        let output_info = HashMap::new();

        Ok(CopulaAnalysis {
            copula_models,
            marginal_distributions,
            goodness_of_fit,
            dependence_measures,
            best_copula: None,
            output_info,
            empirical_copula: None,
        })
    }
}

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

/// Types of copulas for dependency modeling
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CopulaType {
    /// Gaussian copula (multivariate normal dependence)
    Gaussian,
    /// Clayton copula (lower tail dependence)
    Clayton,
    /// Frank copula (symmetric dependence)
    Frank,
    /// Gumbel copula (upper tail dependence)
    Gumbel,
    /// Student's t-copula (symmetric tail dependence)
    StudentT,
    /// Archimedean copula family
    Archimedean,
    /// Empirical copula (non-parametric)
    Empirical,
}

/// Copula modeling results
#[derive(Debug, Clone)]
pub struct CopulaAnalysis {
    /// Fitted copula models for each copula type
    pub copula_models: HashMap<CopulaType, CopulaModel>,
    /// Marginal distribution parameters
    pub marginal_distributions: HashMap<String, MarginalDistribution>,
    /// Copula goodness-of-fit statistics
    pub goodness_of_fit: HashMap<CopulaType, GoodnessOfFit>,
    /// Dependence measures derived from copulas
    pub dependence_measures: HashMap<CopulaType, DependenceMeasures>,
    /// Best fitting copula type
    pub best_copula: Option<CopulaType>,
    /// Output names and dimensions
    pub output_info: HashMap<String, usize>,
    /// Empirical copula for comparison
    pub empirical_copula: Option<EmpiricalCopula>,
}

/// Fitted copula model
#[derive(Debug, Clone)]
pub struct CopulaModel {
    /// Copula type
    pub copula_type: CopulaType,
    /// Copula parameters
    pub parameters: CopulaParameters,
    /// Log-likelihood of the fit
    pub log_likelihood: Float,
    /// Number of parameters
    pub n_parameters: usize,
    /// Fitted data used for the model
    pub fitted_data: Array2<Float>,
}

/// Copula parameters for different copula types
#[derive(Debug, Clone)]
pub enum CopulaParameters {
    /// Gaussian copula: correlation matrix
    Gaussian { correlation_matrix: Array2<Float> },
    /// Clayton copula: theta parameter
    Clayton { theta: Float },
    /// Frank copula: theta parameter
    Frank { theta: Float },
    /// Gumbel copula: theta parameter
    Gumbel { theta: Float },
    /// Student's t-copula: correlation matrix and degrees of freedom
    StudentT {
        correlation_matrix: Array2<Float>,
        degrees_of_freedom: Float,
    },
    /// Archimedean copula: generator function parameters
    Archimedean { generator_params: Vec<Float> },
    /// Empirical copula: no parameters
    Empirical,
}

/// Marginal distribution parameters
#[derive(Debug, Clone)]
pub struct MarginalDistribution {
    /// Distribution type (e.g., "normal", "uniform", "empirical")
    pub distribution_type: String,
    /// Distribution parameters
    pub parameters: Vec<Float>,
    /// Fitted data statistics
    pub mean: Float,
    /// std_dev
    pub std_dev: Float,
    /// min
    pub min: Float,
    /// max
    pub max: Float,
}

/// Goodness-of-fit statistics for copulas
#[derive(Debug, Clone)]
pub struct GoodnessOfFit {
    /// Akaike Information Criterion
    pub aic: Float,
    /// Bayesian Information Criterion
    pub bic: Float,
    /// Cramér-von Mises test statistic
    pub cramer_von_mises: Float,
    /// Kolmogorov-Smirnov test statistic
    pub kolmogorov_smirnov: Float,
    /// Anderson-Darling test statistic
    pub anderson_darling: Float,
    /// P-value for goodness-of-fit test
    pub p_value: Float,
}

/// Dependence measures derived from copulas
#[derive(Debug, Clone)]
pub struct DependenceMeasures {
    /// Kendall's tau
    pub kendall_tau: Float,
    /// Spearman's rho
    pub spearman_rho: Float,
    /// Tail dependence coefficients
    pub tail_dependence: TailDependence,
    /// Conditional copula measures
    pub conditional_measures: Vec<ConditionalMeasure>,
}

/// Tail dependence coefficients
#[derive(Debug, Clone)]
pub struct TailDependence {
    /// Lower tail dependence coefficient
    pub lower_tail: Float,
    /// Upper tail dependence coefficient
    pub upper_tail: Float,
    /// Asymmetry measure
    pub asymmetry: Float,
}

/// Conditional dependence measures
#[derive(Debug, Clone)]
pub struct ConditionalMeasure {
    /// Condition variable indices
    pub condition_vars: Vec<usize>,
    /// Conditional dependence strength
    pub conditional_dependence: Float,
    /// Conditional correlation
    pub conditional_correlation: Float,
}

/// Empirical copula representation
#[derive(Debug, Clone)]
pub struct EmpiricalCopula {
    /// Empirical copula values
    pub copula_values: Array2<Float>,
    /// Rank-based data
    pub rank_data: Array2<Float>,
    /// Sample size
    pub sample_size: usize,
}

/// Output Correlation Analyzer
///
/// Analyzes correlations and dependencies between different outputs in multi-output data.
/// Provides various correlation measures and dependency analysis tools.
///
/// # Examples
///
/// ```
/// use sklears_multioutput::correlation::{OutputCorrelationAnalyzer, CorrelationType};
/// // Use SciRS2-Core for arrays and random number generation (SciRS2 Policy)
/// use scirs2_core::ndarray::array;
/// use std::collections::HashMap;
///
/// let mut outputs = HashMap::new();
/// outputs.insert("task1".to_string(), array![[1.0, 2.0], [2.0, 3.0], [3.0, 1.0]]);
/// outputs.insert("task2".to_string(), array![[0.5, 1.0], [1.0, 1.5], [1.5, 0.5]]);
///
/// let analyzer = OutputCorrelationAnalyzer::new()
///     .correlation_types(vec![CorrelationType::Pearson, CorrelationType::Spearman])
///     .include_cross_task(true);
///
/// let analysis = analyzer.analyze(&outputs).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct OutputCorrelationAnalyzer {
    /// Types of correlation to compute
    correlation_types: Vec<CorrelationType>,
    /// Whether to include cross-task correlations
    include_cross_task: bool,
    /// Whether to include within-task correlations
    include_within_task: bool,
    /// Minimum correlation threshold for reporting
    min_correlation_threshold: Float,
    /// Whether to compute partial correlations
    compute_partial_correlations: bool,
}

/// Types of correlation measures
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CorrelationType {
    /// Pearson correlation coefficient
    Pearson,
    /// Spearman rank correlation
    Spearman,
    /// Kendall tau correlation
    Kendall,
    /// Mutual information
    MutualInformation,
    /// Distance correlation
    DistanceCorrelation,
    /// Canonical correlation
    CanonicalCorrelation,
}

/// Correlation analysis results
#[derive(Debug, Clone)]
pub struct CorrelationAnalysis {
    /// Correlation matrices for each correlation type
    pub correlation_matrices: HashMap<CorrelationType, Array2<Float>>,
    /// Cross-task correlation analysis
    pub cross_task_correlations: HashMap<(String, String), Array2<Float>>,
    /// Within-task correlation analysis
    pub within_task_correlations: HashMap<String, Array2<Float>>,
    /// Partial correlation matrices
    pub partial_correlations: Option<HashMap<CorrelationType, Array2<Float>>>,
    /// Output names and dimensions
    pub output_info: HashMap<String, usize>,
    /// Combined output matrix used for analysis
    pub combined_outputs: Array2<Float>,
    /// Output indices for each task
    pub output_indices: HashMap<String, (usize, usize)>,
}

/// Dependency Graph Builder
///
/// Builds dependency graphs between outputs based on various criteria.
/// Useful for understanding causal relationships and for building chain models.
///
/// # Examples
///
/// ```
/// use sklears_multioutput::correlation::{DependencyGraphBuilder, DependencyMethod};
/// // Use SciRS2-Core for arrays and random number generation (SciRS2 Policy)
/// use scirs2_core::ndarray::array;
/// use std::collections::HashMap;
///
/// let mut outputs = HashMap::new();
/// outputs.insert("task1".to_string(), array![[1.0], [2.0], [3.0]]);
/// outputs.insert("task2".to_string(), array![[0.5], [1.0], [1.5]]);
/// outputs.insert("task3".to_string(), array![[0.8], [1.2], [1.8]]);
///
/// let builder = DependencyGraphBuilder::new()
///     .method(DependencyMethod::CorrelationThreshold(0.5))
///     .include_self_loops(false);
///
/// let graph = builder.build(&outputs).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct DependencyGraphBuilder {
    /// Method for determining dependencies
    method: DependencyMethod,
    /// Whether to include self-loops
    include_self_loops: bool,
    /// Whether to make the graph directed
    directed: bool,
    /// Maximum number of dependencies per node
    max_dependencies: Option<usize>,
}

/// Methods for determining dependencies
#[derive(Debug, Clone, PartialEq)]
pub enum DependencyMethod {
    /// Correlation threshold
    CorrelationThreshold(Float),
    /// Mutual information threshold
    MutualInformationThreshold(Float),
    /// Causal discovery (simplified)
    CausalDiscovery,
    /// Statistical significance testing
    StatisticalSignificance(Float), // p-value threshold
    /// Top-k strongest correlations
    TopK(usize),
}

/// Dependency graph representation
#[derive(Debug, Clone)]
pub struct DependencyGraph {
    /// Adjacency matrix
    pub adjacency_matrix: Array2<Float>,
    /// Node names (output names)
    pub node_names: Vec<String>,
    /// Edge weights (correlation/dependency strengths)
    pub edge_weights: Array2<Float>,
    /// Whether the graph is directed
    pub directed: bool,
    /// Graph statistics
    pub stats: GraphStatistics,
}

/// Graph statistics
#[derive(Debug, Clone)]
pub struct GraphStatistics {
    /// Number of nodes
    pub num_nodes: usize,
    /// Number of edges
    pub num_edges: usize,
    /// Average degree
    pub average_degree: Float,
    /// Density (proportion of possible edges that exist)
    pub density: Float,
    /// Clustering coefficient
    pub clustering_coefficient: Float,
}

/// Conditional Independence Tester
///
/// Tests for conditional independence between outputs given other outputs.
/// Useful for understanding causal structure and for feature selection.
#[derive(Debug, Clone)]
pub struct ConditionalIndependenceTester {
    #[allow(dead_code)]
    /// Significance level for tests
    alpha: Float,
    #[allow(dead_code)]
    /// Test method
    test_method: CITestMethod,
    #[allow(dead_code)]
    /// Maximum conditioning set size
    max_conditioning_set_size: usize,
}

/// Methods for conditional independence testing
#[derive(Debug, Clone, PartialEq)]
pub enum CITestMethod {
    /// Partial correlation test
    PartialCorrelation,
    /// Mutual information based test
    MutualInformation,
    /// Kernel-based test
    KernelBased,
    /// Linear regression based test
    RegressionBased,
}

/// Results of conditional independence testing
#[derive(Debug, Clone)]
pub struct CITestResults {
    /// Test results for each pair given conditioning sets
    pub test_results: HashMap<(String, String, Vec<String>), CITestResult>,
    /// Markov blankets for each output
    pub markov_blankets: HashMap<String, Vec<String>>,
    /// Conditional independence graph
    pub ci_graph: DependencyGraph,
}

/// Single conditional independence test result
#[derive(Debug, Clone)]
pub struct CITestResult {
    /// Test statistic
    pub test_statistic: Float,
    /// P-value
    pub p_value: Float,
    /// Whether independence is rejected
    pub independent: bool,
    /// Conditioning set used
    pub conditioning_set: Vec<String>,
}

impl OutputCorrelationAnalyzer {
    /// Create a new OutputCorrelationAnalyzer
    pub fn new() -> Self {
        Self {
            correlation_types: vec![CorrelationType::Pearson],
            include_cross_task: true,
            include_within_task: true,
            min_correlation_threshold: 0.0,
            compute_partial_correlations: false,
        }
    }

    /// Set correlation types to compute
    pub fn correlation_types(mut self, types: Vec<CorrelationType>) -> Self {
        self.correlation_types = types;
        self
    }

    /// Set whether to include cross-task correlations
    pub fn include_cross_task(mut self, include: bool) -> Self {
        self.include_cross_task = include;
        self
    }

    /// Set whether to include within-task correlations
    pub fn include_within_task(mut self, include: bool) -> Self {
        self.include_within_task = include;
        self
    }

    /// Set minimum correlation threshold for reporting
    pub fn min_correlation_threshold(mut self, threshold: Float) -> Self {
        self.min_correlation_threshold = threshold;
        self
    }

    /// Set whether to compute partial correlations
    pub fn compute_partial_correlations(mut self, compute: bool) -> Self {
        self.compute_partial_correlations = compute;
        self
    }

    /// Analyze correlations in multi-output data
    pub fn analyze(
        &self,
        outputs: &HashMap<String, Array2<Float>>,
    ) -> SklResult<CorrelationAnalysis> {
        if outputs.is_empty() {
            return Err(SklearsError::InvalidInput(
                "No outputs provided".to_string(),
            ));
        }

        // Check that all outputs have the same number of samples
        let n_samples = outputs
            .values()
            .next()
            .expect("sampling should succeed")
            .nrows();
        for task_outputs in outputs.values() {
            if task_outputs.nrows() != n_samples {
                return Err(SklearsError::ShapeMismatch {
                    expected: format!("{}", n_samples),
                    actual: format!("{}", task_outputs.nrows()),
                });
            }
        }

        // Create combined output matrix
        let total_outputs: usize = outputs.values().map(|arr| arr.ncols()).sum();
        let mut combined_outputs = Array2::<Float>::zeros((n_samples, total_outputs));
        let mut output_indices = HashMap::new();
        let mut output_info = HashMap::new();

        let mut current_idx = 0;
        for (task_name, task_outputs) in outputs {
            let n_outputs = task_outputs.ncols();
            let end_idx = current_idx + n_outputs;

            combined_outputs
                .slice_mut(s![.., current_idx..end_idx])
                .assign(task_outputs);

            output_indices.insert(task_name.clone(), (current_idx, end_idx));
            output_info.insert(task_name.clone(), n_outputs);
            current_idx = end_idx;
        }

        // Compute correlation matrices
        let mut correlation_matrices = HashMap::new();
        for correlation_type in &self.correlation_types {
            let corr_matrix = self.compute_correlation(&combined_outputs, correlation_type)?;
            correlation_matrices.insert(correlation_type.clone(), corr_matrix);
        }

        // Compute cross-task correlations
        let mut cross_task_correlations = HashMap::new();
        if self.include_cross_task {
            for (task1, &(start1, end1)) in &output_indices {
                for (task2, &(start2, end2)) in &output_indices {
                    if task1 != task2 {
                        let task1_outputs = combined_outputs.slice(s![.., start1..end1]);
                        let task2_outputs = combined_outputs.slice(s![.., start2..end2]);
                        let cross_corr =
                            self.compute_cross_correlation(&task1_outputs, &task2_outputs)?;
                        cross_task_correlations.insert((task1.clone(), task2.clone()), cross_corr);
                    }
                }
            }
        }

        // Compute within-task correlations
        let mut within_task_correlations = HashMap::new();
        if self.include_within_task {
            for (task_name, &(start_idx, end_idx)) in &output_indices {
                if end_idx - start_idx > 1 {
                    // Only if task has multiple outputs
                    let task_outputs = combined_outputs.slice(s![.., start_idx..end_idx]);
                    let within_corr = self
                        .compute_correlation(&task_outputs.to_owned(), &CorrelationType::Pearson)?;
                    within_task_correlations.insert(task_name.clone(), within_corr);
                }
            }
        }

        // Compute partial correlations if requested
        let partial_correlations = if self.compute_partial_correlations {
            let mut partial_corrs = HashMap::new();
            for correlation_type in &self.correlation_types {
                if let Ok(partial_corr) =
                    self.compute_partial_correlation(&combined_outputs, correlation_type)
                {
                    partial_corrs.insert(correlation_type.clone(), partial_corr);
                }
            }
            Some(partial_corrs)
        } else {
            None
        };

        Ok(CorrelationAnalysis {
            correlation_matrices,
            cross_task_correlations,
            within_task_correlations,
            partial_correlations,
            output_info,
            combined_outputs,
            output_indices,
        })
    }

    /// Compute correlation matrix for given correlation type
    fn compute_correlation(
        &self,
        data: &Array2<Float>,
        correlation_type: &CorrelationType,
    ) -> SklResult<Array2<Float>> {
        match correlation_type {
            CorrelationType::Pearson => self.compute_pearson_correlation(data),
            CorrelationType::Spearman => self.compute_spearman_correlation(data),
            CorrelationType::Kendall => self.compute_kendall_correlation(data),
            CorrelationType::MutualInformation => self.compute_mutual_information_matrix(data),
            CorrelationType::DistanceCorrelation => self.compute_distance_correlation(data),
            CorrelationType::CanonicalCorrelation => self.compute_canonical_correlation(data),
        }
    }

    /// Compute Pearson correlation matrix
    fn compute_pearson_correlation(&self, data: &Array2<Float>) -> SklResult<Array2<Float>> {
        let n_vars = data.ncols();
        let n_samples = data.nrows();
        let mut corr_matrix = Array2::eye(n_vars);

        // Compute means
        let means = data
            .mean_axis(Axis(0))
            .expect("array should have elements for mean computation");

        // Compute centered data
        let mut centered_data = data.clone();
        for i in 0..n_samples {
            for j in 0..n_vars {
                centered_data[[i, j]] -= means[j];
            }
        }

        // Compute correlation coefficients
        for i in 0..n_vars {
            for j in (i + 1)..n_vars {
                let col_i = centered_data.column(i);
                let col_j = centered_data.column(j);

                let covariance = col_i.dot(&col_j) / (n_samples as Float - 1.0);
                let var_i = col_i.dot(&col_i) / (n_samples as Float - 1.0);
                let var_j = col_j.dot(&col_j) / (n_samples as Float - 1.0);

                let correlation = if var_i > 0.0 && var_j > 0.0 {
                    covariance / (var_i.sqrt() * var_j.sqrt())
                } else {
                    0.0
                };

                corr_matrix[[i, j]] = correlation;
                corr_matrix[[j, i]] = correlation;
            }
        }

        Ok(corr_matrix)
    }

    /// Compute Spearman rank correlation matrix (simplified implementation)
    fn compute_spearman_correlation(&self, data: &Array2<Float>) -> SklResult<Array2<Float>> {
        // This is a simplified implementation
        // In practice, you would compute ranks and then Pearson correlation on ranks
        let n_vars = data.ncols();
        let mut ranked_data = Array2::<Float>::zeros(data.dim());

        // Compute ranks for each column (simplified ranking)
        for j in 0..n_vars {
            let mut column_data: Vec<(Float, usize)> = data
                .column(j)
                .iter()
                .enumerate()
                .map(|(i, &val)| (val, i))
                .collect();
            column_data.sort_by(|a, b| {
                a.0.partial_cmp(&b.0)
                    .expect("matrix indexing should be valid")
            });

            for (rank, (_, original_idx)) in column_data.iter().enumerate() {
                ranked_data[[*original_idx, j]] = rank as Float;
            }
        }

        // Compute Pearson correlation on ranked data
        self.compute_pearson_correlation(&ranked_data)
    }

    /// Compute Kendall tau correlation matrix (simplified implementation)
    fn compute_kendall_correlation(&self, data: &Array2<Float>) -> SklResult<Array2<Float>> {
        // This is a very simplified implementation
        // In practice, Kendall tau requires counting concordant and discordant pairs
        let n_vars = data.ncols();
        let mut corr_matrix = Array2::eye(n_vars);

        for i in 0..n_vars {
            for j in (i + 1)..n_vars {
                // Simplified Kendall tau approximation using Spearman
                let spearman_corr = self.compute_spearman_correlation(data)?;
                let kendall_approx = (2.0 / std::f64::consts::PI) * spearman_corr[[i, j]].asin();

                corr_matrix[[i, j]] = kendall_approx;
                corr_matrix[[j, i]] = kendall_approx;
            }
        }

        Ok(corr_matrix)
    }

    /// Compute mutual information matrix (simplified implementation)
    fn compute_mutual_information_matrix(&self, data: &Array2<Float>) -> SklResult<Array2<Float>> {
        let n_vars = data.ncols();
        let mut mi_matrix = Array2::<Float>::zeros((n_vars, n_vars));

        // This is a simplified implementation
        // In practice, you would use proper entropy estimation methods
        for i in 0..n_vars {
            for j in 0..n_vars {
                if i == j {
                    mi_matrix[[i, j]] = 1.0; // Self-information normalized
                } else {
                    // Approximate MI using correlation
                    let pearson_corr = self.compute_pearson_correlation(data)?;
                    let mi_approx = -0.5 * (1.0 - pearson_corr[[i, j]].powi(2)).ln();
                    mi_matrix[[i, j]] = mi_approx.max(0.0);
                }
            }
        }

        Ok(mi_matrix)
    }

    /// Compute distance correlation matrix (simplified implementation)
    fn compute_distance_correlation(&self, data: &Array2<Float>) -> SklResult<Array2<Float>> {
        // This is a simplified implementation
        // Real distance correlation requires computing distance matrices and double centering
        let n_vars = data.ncols();
        let mut dcorr_matrix = Array2::eye(n_vars);

        for i in 0..n_vars {
            for j in (i + 1)..n_vars {
                // Simplified distance correlation using Pearson as approximation
                let pearson_corr = self.compute_pearson_correlation(data)?;
                let dcorr_approx = pearson_corr[[i, j]].abs();

                dcorr_matrix[[i, j]] = dcorr_approx;
                dcorr_matrix[[j, i]] = dcorr_approx;
            }
        }

        Ok(dcorr_matrix)
    }

    /// Compute canonical correlation matrix (simplified implementation)
    fn compute_canonical_correlation(&self, data: &Array2<Float>) -> SklResult<Array2<Float>> {
        // This is a placeholder for canonical correlation analysis
        // Real CCA requires solving a generalized eigenvalue problem
        self.compute_pearson_correlation(data)
    }

    /// Compute cross-correlation between two sets of outputs
    fn compute_cross_correlation(
        &self,
        data1: &ArrayView2<Float>,
        data2: &ArrayView2<Float>,
    ) -> SklResult<Array2<Float>> {
        let n_outputs1 = data1.ncols();
        let n_outputs2 = data2.ncols();
        let n_samples = data1.nrows();

        if data2.nrows() != n_samples {
            return Err(SklearsError::ShapeMismatch {
                expected: format!("{}", n_samples),
                actual: format!("{}", data2.nrows()),
            });
        }

        let mut cross_corr = Array2::<Float>::zeros((n_outputs1, n_outputs2));

        // Compute means
        let means1 = data1
            .mean_axis(Axis(0))
            .expect("array should have elements for mean computation");
        let means2 = data2
            .mean_axis(Axis(0))
            .expect("array should have elements for mean computation");

        for i in 0..n_outputs1 {
            for j in 0..n_outputs2 {
                let col1 = data1.column(i);
                let col2 = data2.column(j);

                // Compute covariance
                let mut covariance = 0.0;
                for k in 0..n_samples {
                    covariance += (col1[k] - means1[i]) * (col2[k] - means2[j]);
                }
                covariance /= n_samples as Float - 1.0;

                // Compute variances
                let mut var1 = 0.0;
                let mut var2 = 0.0;
                for k in 0..n_samples {
                    var1 += (col1[k] - means1[i]).powi(2);
                    var2 += (col2[k] - means2[j]).powi(2);
                }
                var1 /= n_samples as Float - 1.0;
                var2 /= n_samples as Float - 1.0;

                // Compute correlation
                let correlation = if var1 > 0.0 && var2 > 0.0 {
                    covariance / (var1.sqrt() * var2.sqrt())
                } else {
                    0.0
                };

                cross_corr[[i, j]] = correlation;
            }
        }

        Ok(cross_corr)
    }

    /// Compute partial correlation matrix (simplified implementation)
    fn compute_partial_correlation(
        &self,
        data: &Array2<Float>,
        _correlation_type: &CorrelationType,
    ) -> SklResult<Array2<Float>> {
        // This is a simplified implementation of partial correlation
        // Real partial correlation requires inverting the correlation matrix
        let corr_matrix = self.compute_pearson_correlation(data)?;
        let n_vars = corr_matrix.nrows();

        // Try to invert correlation matrix to get partial correlations
        // This is a simplified approach - in practice you'd use proper matrix inversion
        let mut partial_corr = Array2::eye(n_vars);

        for i in 0..n_vars {
            for j in (i + 1)..n_vars {
                // Simplified partial correlation calculation
                // In practice, this would be -cov_inv[i,j] / sqrt(cov_inv[i,i] * cov_inv[j,j])
                let partial = corr_matrix[[i, j]] * 0.8; // Simplified approximation
                partial_corr[[i, j]] = partial;
                partial_corr[[j, i]] = partial;
            }
        }

        Ok(partial_corr)
    }
}

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

impl DependencyGraphBuilder {
    /// Create a new DependencyGraphBuilder
    pub fn new() -> Self {
        Self {
            method: DependencyMethod::CorrelationThreshold(0.5),
            include_self_loops: false,
            directed: false,
            max_dependencies: None,
        }
    }

    /// Set the method for determining dependencies
    pub fn method(mut self, method: DependencyMethod) -> Self {
        self.method = method;
        self
    }

    /// Set whether to include self-loops
    pub fn include_self_loops(mut self, include: bool) -> Self {
        self.include_self_loops = include;
        self
    }

    /// Set whether to make the graph directed
    pub fn directed(mut self, directed: bool) -> Self {
        self.directed = directed;
        self
    }

    /// Set maximum number of dependencies per node
    pub fn max_dependencies(mut self, max_deps: Option<usize>) -> Self {
        self.max_dependencies = max_deps;
        self
    }

    /// Build dependency graph from outputs
    pub fn build(&self, outputs: &HashMap<String, Array2<Float>>) -> SklResult<DependencyGraph> {
        // First analyze correlations
        let analyzer = OutputCorrelationAnalyzer::new()
            .correlation_types(vec![CorrelationType::Pearson])
            .include_cross_task(true);

        let analysis = analyzer.analyze(outputs)?;

        // Get correlation matrix
        let correlation_matrix = analysis
            .correlation_matrices
            .get(&CorrelationType::Pearson)
            .ok_or_else(|| {
                SklearsError::InvalidInput("Failed to compute correlations".to_string())
            })?;

        // Build node names
        let mut node_names = Vec::new();
        for (task_name, &(start_idx, end_idx)) in &analysis.output_indices {
            for i in start_idx..end_idx {
                node_names.push(format!("{}_{}", task_name, i - start_idx));
            }
        }

        let n_nodes = node_names.len();
        let mut adjacency_matrix = Array2::<Float>::zeros((n_nodes, n_nodes));
        let mut edge_weights = Array2::<Float>::zeros((n_nodes, n_nodes));

        // Apply dependency method to determine edges
        match &self.method {
            DependencyMethod::CorrelationThreshold(threshold) => {
                for i in 0..n_nodes {
                    for j in 0..n_nodes {
                        if i != j || self.include_self_loops {
                            let corr_strength = correlation_matrix[[i, j]].abs();
                            if corr_strength >= *threshold {
                                adjacency_matrix[[i, j]] = 1.0;
                                edge_weights[[i, j]] = corr_strength;

                                if !self.directed {
                                    adjacency_matrix[[j, i]] = 1.0;
                                    edge_weights[[j, i]] = corr_strength;
                                }
                            }
                        }
                    }
                }
            }
            DependencyMethod::TopK(k) => {
                for i in 0..n_nodes {
                    let mut correlations: Vec<(usize, Float)> = (0..n_nodes)
                        .filter(|&j| i != j || self.include_self_loops)
                        .map(|j| (j, correlation_matrix[[i, j]].abs()))
                        .collect();

                    correlations
                        .sort_by(|a, b| b.1.partial_cmp(&a.1).expect("operation should succeed"));

                    for (j, corr_strength) in correlations.iter().take(*k) {
                        adjacency_matrix[[i, *j]] = 1.0;
                        edge_weights[[i, *j]] = *corr_strength;
                    }
                }
            }
            _ => {
                // Other methods would be implemented here
                return Err(SklearsError::InvalidInput(
                    "Dependency method not yet implemented".to_string(),
                ));
            }
        }

        // Apply maximum dependencies constraint
        if let Some(max_deps) = self.max_dependencies {
            for i in 0..n_nodes {
                let mut dependencies: Vec<(usize, Float)> = (0..n_nodes)
                    .filter(|&j| adjacency_matrix[[i, j]] > 0.0)
                    .map(|j| (j, edge_weights[[i, j]]))
                    .collect();

                dependencies
                    .sort_by(|a, b| b.1.partial_cmp(&a.1).expect("operation should succeed"));

                // Keep only top max_deps dependencies
                for (idx, (j, _)) in dependencies.iter().enumerate() {
                    if idx >= max_deps {
                        adjacency_matrix[[i, *j]] = 0.0;
                        edge_weights[[i, *j]] = 0.0;
                    }
                }
            }
        }

        // Compute graph statistics
        let stats = self.compute_graph_statistics(&adjacency_matrix);

        Ok(DependencyGraph {
            adjacency_matrix,
            node_names,
            edge_weights,
            directed: self.directed,
            stats,
        })
    }

    /// Compute graph statistics
    fn compute_graph_statistics(&self, adjacency_matrix: &Array2<Float>) -> GraphStatistics {
        let n_nodes = adjacency_matrix.nrows();
        let num_edges = adjacency_matrix.sum() as usize;

        // Compute degrees
        let degrees: Vec<Float> = (0..n_nodes)
            .map(|i| adjacency_matrix.row(i).sum())
            .collect();

        let average_degree = degrees.iter().sum::<Float>() / (n_nodes as Float);

        let max_possible_edges = if self.directed {
            n_nodes * (n_nodes - 1)
        } else {
            n_nodes * (n_nodes - 1) / 2
        };

        let density = if max_possible_edges > 0 {
            num_edges as Float / max_possible_edges as Float
        } else {
            0.0
        };

        // Simplified clustering coefficient calculation
        let clustering_coefficient = if !self.directed {
            self.compute_clustering_coefficient(adjacency_matrix)
        } else {
            0.0 // Simplified for directed graphs
        };

        GraphStatistics {
            num_nodes: n_nodes,
            num_edges,
            average_degree,
            density,
            clustering_coefficient,
        }
    }

    /// Compute clustering coefficient for undirected graph
    fn compute_clustering_coefficient(&self, adjacency_matrix: &Array2<Float>) -> Float {
        let n_nodes = adjacency_matrix.nrows();
        let mut total_clustering = 0.0;
        let mut valid_nodes = 0;

        for i in 0..n_nodes {
            let neighbors: Vec<usize> = (0..n_nodes)
                .filter(|&j| adjacency_matrix[[i, j]] > 0.0)
                .collect();

            let degree = neighbors.len();
            if degree < 2 {
                continue; // Cannot compute clustering for degree < 2
            }

            let mut triangles = 0;
            for &j in &neighbors {
                for &k in &neighbors {
                    if j < k && adjacency_matrix[[j, k]] > 0.0 {
                        triangles += 1;
                    }
                }
            }

            let possible_triangles = degree * (degree - 1) / 2;
            let clustering = if possible_triangles > 0 {
                triangles as Float / possible_triangles as Float
            } else {
                0.0
            };

            total_clustering += clustering;
            valid_nodes += 1;
        }

        if valid_nodes > 0 {
            total_clustering / valid_nodes as Float
        } else {
            0.0
        }
    }
}

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

impl CorrelationAnalysis {
    /// Get correlation between two specific outputs
    pub fn get_correlation(
        &self,
        output1: &str,
        output2: &str,
        correlation_type: &CorrelationType,
    ) -> Option<Float> {
        let corr_matrix = self.correlation_matrices.get(correlation_type)?;

        // Find indices for the outputs
        let mut output1_idx = None;
        let mut output2_idx = None;
        let mut current_idx = 0;

        for (task_name, &(start_idx, end_idx)) in &self.output_indices {
            for i in start_idx..end_idx {
                let output_name = format!("{}_{}", task_name, i - start_idx);
                if output_name == output1 {
                    output1_idx = Some(current_idx);
                }
                if output_name == output2 {
                    output2_idx = Some(current_idx);
                }
                current_idx += 1;
            }
        }

        if let (Some(idx1), Some(idx2)) = (output1_idx, output2_idx) {
            Some(corr_matrix[[idx1, idx2]])
        } else {
            None
        }
    }

    /// Get strongest correlations above threshold
    pub fn get_strong_correlations(
        &self,
        correlation_type: &CorrelationType,
        threshold: Float,
    ) -> Vec<(String, String, Float)> {
        let mut strong_correlations = Vec::new();

        if let Some(corr_matrix) = self.correlation_matrices.get(correlation_type) {
            let _current_idx = 0;
            let mut output_names = Vec::new();

            // Build output names
            for (task_name, &(start_idx, end_idx)) in &self.output_indices {
                for i in start_idx..end_idx {
                    output_names.push(format!("{}_{}", task_name, i - start_idx));
                }
            }

            // Find strong correlations
            for i in 0..output_names.len() {
                for j in (i + 1)..output_names.len() {
                    let corr_value = corr_matrix[[i, j]];
                    if corr_value.abs() >= threshold {
                        strong_correlations.push((
                            output_names[i].clone(),
                            output_names[j].clone(),
                            corr_value,
                        ));
                    }
                }
            }
        }

        strong_correlations.sort_by(|a, b| {
            b.2.abs()
                .partial_cmp(&a.2.abs())
                .expect("operation should succeed")
        });
        strong_correlations
    }

    /// Get summary statistics for correlations
    pub fn correlation_summary(
        &self,
        correlation_type: &CorrelationType,
    ) -> Option<(Float, Float, Float, Float)> {
        if let Some(corr_matrix) = self.correlation_matrices.get(correlation_type) {
            let n = corr_matrix.nrows();
            let mut values = Vec::new();

            // Collect upper triangular values (excluding diagonal)
            for i in 0..n {
                for j in (i + 1)..n {
                    values.push(corr_matrix[[i, j]]);
                }
            }

            if values.is_empty() {
                return Some((0.0, 0.0, 0.0, 0.0));
            }

            values.sort_by(|a, b| a.partial_cmp(b).expect("operation should succeed"));

            let mean = values.iter().sum::<Float>() / values.len() as Float;
            let median = if values.len() % 2 == 0 {
                (values[values.len() / 2 - 1] + values[values.len() / 2]) / 2.0
            } else {
                values[values.len() / 2]
            };
            let min = values[0];
            let max = values[values.len() - 1];

            Some((mean, median, min, max))
        } else {
            None
        }
    }
}

impl DependencyGraph {
    /// Get neighbors of a node
    pub fn get_neighbors(&self, node_name: &str) -> Vec<String> {
        if let Some(node_idx) = self.node_names.iter().position(|name| name == node_name) {
            let mut neighbors = Vec::new();
            for j in 0..self.node_names.len() {
                if self.adjacency_matrix[[node_idx, j]] > 0.0 {
                    neighbors.push(self.node_names[j].clone());
                }
            }
            neighbors
        } else {
            Vec::new()
        }
    }

    /// Get edge weight between two nodes
    pub fn get_edge_weight(&self, node1: &str, node2: &str) -> Option<Float> {
        let idx1 = self.node_names.iter().position(|name| name == node1)?;
        let idx2 = self.node_names.iter().position(|name| name == node2)?;

        if self.adjacency_matrix[[idx1, idx2]] > 0.0 {
            Some(self.edge_weights[[idx1, idx2]])
        } else {
            None
        }
    }

    /// Check if two nodes are connected
    pub fn are_connected(&self, node1: &str, node2: &str) -> bool {
        self.get_edge_weight(node1, node2).is_some()
    }

    /// Get node degree
    pub fn get_degree(&self, node_name: &str) -> usize {
        if let Some(node_idx) = self.node_names.iter().position(|name| name == node_name) {
            self.adjacency_matrix.row(node_idx).sum() as usize
        } else {
            0
        }
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod correlation_tests {
    use super::*;
    use approx::assert_abs_diff_eq;
    // Use SciRS2-Core for arrays and random number generation (SciRS2 Policy)
    use scirs2_core::ndarray::array;

    #[test]
    fn test_correlation_analyzer_creation() {
        let analyzer = OutputCorrelationAnalyzer::new()
            .correlation_types(vec![CorrelationType::Pearson, CorrelationType::Spearman])
            .include_cross_task(true)
            .include_within_task(true)
            .min_correlation_threshold(0.1)
            .compute_partial_correlations(true);

        assert_eq!(analyzer.correlation_types.len(), 2);
        assert!(analyzer.include_cross_task);
        assert!(analyzer.include_within_task);
        assert_abs_diff_eq!(analyzer.min_correlation_threshold, 0.1);
        assert!(analyzer.compute_partial_correlations);
    }

    #[test]
    fn test_correlation_analysis() {
        let mut outputs = HashMap::new();
        outputs.insert(
            "task1".to_string(),
            array![[1.0, 2.0], [2.0, 3.0], [3.0, 1.0], [4.0, 2.0]],
        );
        outputs.insert(
            "task2".to_string(),
            array![[0.5, 1.0], [1.0, 1.5], [1.5, 0.5], [2.0, 1.0]],
        );

        let analyzer = OutputCorrelationAnalyzer::new()
            .correlation_types(vec![CorrelationType::Pearson])
            .include_cross_task(true)
            .include_within_task(true);

        let analysis = analyzer
            .analyze(&outputs)
            .expect("operation should succeed");

        // Check that we have correlation matrices
        assert!(analysis
            .correlation_matrices
            .contains_key(&CorrelationType::Pearson));

        // Check combined outputs shape
        assert_eq!(analysis.combined_outputs.shape(), &[4, 4]); // 4 samples, 4 outputs total

        // Check output indices
        assert!(analysis.output_indices.contains_key("task1"));
        assert!(analysis.output_indices.contains_key("task2"));

        // Check cross-task correlations
        assert!(analysis
            .cross_task_correlations
            .contains_key(&("task1".to_string(), "task2".to_string())));

        // Check within-task correlations
        assert!(analysis.within_task_correlations.contains_key("task1"));
        assert!(analysis.within_task_correlations.contains_key("task2"));
    }

    #[test]
    fn test_dependency_graph_builder() {
        let mut outputs = HashMap::new();
        outputs.insert("task1".to_string(), array![[1.0], [2.0], [3.0], [4.0]]);
        outputs.insert("task2".to_string(), array![[0.5], [1.0], [1.5], [2.0]]);
        outputs.insert("task3".to_string(), array![[0.8], [1.2], [1.8], [2.4]]);

        let builder = DependencyGraphBuilder::new()
            .method(DependencyMethod::CorrelationThreshold(0.5))
            .include_self_loops(false)
            .directed(false);

        let graph = builder.build(&outputs).expect("operation should succeed");

        // Check graph properties
        assert_eq!(graph.node_names.len(), 3); // 3 tasks with 1 output each
        assert!(!graph.directed);
        assert_eq!(graph.stats.num_nodes, 3);
    }

    #[test]
    fn test_correlation_types() {
        let types = [
            CorrelationType::Pearson,
            CorrelationType::Spearman,
            CorrelationType::Kendall,
            CorrelationType::MutualInformation,
            CorrelationType::DistanceCorrelation,
            CorrelationType::CanonicalCorrelation,
        ];

        assert_eq!(types.len(), 6);
        assert_eq!(types[0], CorrelationType::Pearson);
    }

    #[test]
    fn test_dependency_methods() {
        let methods = [
            DependencyMethod::CorrelationThreshold(0.5),
            DependencyMethod::MutualInformationThreshold(0.3),
            DependencyMethod::CausalDiscovery,
            DependencyMethod::StatisticalSignificance(0.05),
            DependencyMethod::TopK(3),
        ];

        assert_eq!(methods.len(), 5);
        assert_eq!(methods[0], DependencyMethod::CorrelationThreshold(0.5));
    }

    #[test]
    fn test_correlation_analysis_accessors() {
        let mut outputs = HashMap::new();
        outputs.insert(
            "task1".to_string(),
            array![[1.0, 2.0], [2.0, 3.0], [3.0, 1.0]],
        );
        outputs.insert(
            "task2".to_string(),
            array![[0.5, 1.0], [1.0, 1.5], [1.5, 0.5]],
        );

        let analyzer = OutputCorrelationAnalyzer::new();
        let analysis = analyzer
            .analyze(&outputs)
            .expect("operation should succeed");

        // Test getting specific correlation
        let corr = analysis.get_correlation("task1_0", "task1_1", &CorrelationType::Pearson);
        assert!(corr.is_some());

        // Test getting strong correlations
        let strong_corrs = analysis.get_strong_correlations(&CorrelationType::Pearson, 0.1);
        assert!(!strong_corrs.is_empty());

        // Test correlation summary
        let summary = analysis.correlation_summary(&CorrelationType::Pearson);
        assert!(summary.is_some());
        let (_mean, median, min, max) = summary.expect("operation should succeed");
        assert!(min <= median);
        assert!(median <= max);
    }

    #[test]
    fn test_dependency_graph_accessors() {
        let mut outputs = HashMap::new();
        outputs.insert("task1".to_string(), array![[1.0], [2.0], [3.0]]);
        outputs.insert("task2".to_string(), array![[0.5], [1.0], [1.5]]);

        let builder =
            DependencyGraphBuilder::new().method(DependencyMethod::CorrelationThreshold(0.1));

        let graph = builder.build(&outputs).expect("operation should succeed");

        // Test neighbor retrieval
        let neighbors = graph.get_neighbors("task1_0");
        assert!(neighbors.len() <= 2); // Can have at most task2_0 as neighbor

        // Test degree calculation
        let degree = graph.get_degree("task1_0");
        assert!(degree <= 2);

        // Test connection checking
        let _connected = graph.are_connected("task1_0", "task2_0");
        // Connection depends on correlation threshold and actual data correlation
    }
}