scirs2-metrics 0.3.0

Machine Learning evaluation metrics module for SciRS2 (scirs2-metrics)
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
//! Financial modeling and quantitative finance domain metrics
//!
//! This module provides specialized metrics for financial applications including:
//! - Risk management and Value at Risk (VaR) calculations
//! - Portfolio optimization and performance evaluation
//! - Credit risk modeling and default prediction
//! - Market risk assessment and stress testing
//! - Algorithmic trading strategy evaluation
//! - Fraud detection and anti-money laundering
//! - Regulatory compliance and Basel III metrics
//! - ESG (Environmental, Social, Governance) scoring

#![allow(clippy::too_many_arguments)]
#![allow(dead_code)]

use super::{DomainEvaluationResult, DomainMetrics};
use crate::error::{MetricsError, Result};
use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2};
use scirs2_core::numeric::Float;
use std::collections::HashMap;

/// Comprehensive financial metrics suite
#[derive(Debug)]
pub struct FinancialSuite {
    /// Risk management metrics
    pub risk_metrics: RiskManagementMetrics,
    /// Portfolio performance metrics
    pub portfolio_metrics: PortfolioMetrics,
    /// Credit risk metrics
    pub credit_metrics: CreditRiskMetrics,
    /// Market risk metrics
    pub market_risk_metrics: MarketRiskMetrics,
    /// Trading strategy metrics
    pub trading_metrics: TradingStrategyMetrics,
    /// Fraud detection metrics
    pub fraud_metrics: FraudDetectionMetrics,
    /// Regulatory compliance metrics
    pub regulatory_metrics: RegulatoryMetrics,
    /// ESG scoring metrics
    pub esg_metrics: ESGMetrics,
}

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

impl FinancialSuite {
    /// Create new financial metrics suite
    pub fn new() -> Self {
        Self {
            risk_metrics: RiskManagementMetrics::new(),
            portfolio_metrics: PortfolioMetrics::new(),
            credit_metrics: CreditRiskMetrics::new(),
            market_risk_metrics: MarketRiskMetrics::new(),
            trading_metrics: TradingStrategyMetrics::new(),
            fraud_metrics: FraudDetectionMetrics::new(),
            regulatory_metrics: RegulatoryMetrics::new(),
            esg_metrics: ESGMetrics::new(),
        }
    }

    /// Get risk management metrics
    pub fn risk_management(&self) -> &RiskManagementMetrics {
        &self.risk_metrics
    }

    /// Get portfolio metrics
    pub fn portfolio(&self) -> &PortfolioMetrics {
        &self.portfolio_metrics
    }

    /// Get credit risk metrics
    pub fn credit_risk(&self) -> &CreditRiskMetrics {
        &self.credit_metrics
    }

    /// Get market risk metrics
    pub fn market_risk(&self) -> &MarketRiskMetrics {
        &self.market_risk_metrics
    }

    /// Get trading strategy metrics
    pub fn trading_strategy(&self) -> &TradingStrategyMetrics {
        &self.trading_metrics
    }

    /// Get fraud detection metrics
    pub fn fraud_detection(&self) -> &FraudDetectionMetrics {
        &self.fraud_metrics
    }

    /// Get regulatory compliance metrics
    pub fn regulatory(&self) -> &RegulatoryMetrics {
        &self.regulatory_metrics
    }

    /// Get ESG metrics
    pub fn esg(&self) -> &ESGMetrics {
        &self.esg_metrics
    }
}

impl DomainMetrics for FinancialSuite {
    type Result = DomainEvaluationResult;

    fn domain_name(&self) -> &'static str {
        "Financial Modeling & Quantitative Finance"
    }

    fn available_metrics(&self) -> Vec<&'static str> {
        vec![
            "value_at_risk",
            "conditional_value_at_risk",
            "expected_shortfall",
            "maximum_drawdown",
            "sharpe_ratio",
            "sortino_ratio",
            "calmar_ratio",
            "information_ratio",
            "treynor_ratio",
            "jensen_alpha",
            "beta",
            "tracking_error",
            "upside_capture",
            "downside_capture",
            "probability_of_default",
            "loss_given_default",
            "exposure_at_default",
            "expected_credit_loss",
            "credit_var",
            "discriminatory_power",
            "population_stability_index",
            "kolmogorov_smirnov",
            "gini_coefficient",
            "area_under_curve",
            "concordance_ratio",
            "divergence_ratio",
            "total_return",
            "excess_return",
            "annualized_return",
            "volatility",
            "skewness",
            "kurtosis",
            "hit_ratio",
            "profit_factor",
            "recovery_factor",
            "ulcer_index",
            "sterling_ratio",
            "burke_ratio",
            "fraud_detection_rate",
            "false_positive_rate",
            "alert_precision",
            "investigation_efficiency",
            "regulatory_capital_ratio",
            "leverage_ratio",
            "liquidity_coverage_ratio",
            "net_stable_funding_ratio",
            "esg_score",
            "carbon_intensity",
            "social_impact_score",
            "governance_score",
        ]
    }

    fn metric_descriptions(&self) -> HashMap<&'static str, &'static str> {
        let mut descriptions = HashMap::new();
        descriptions.insert(
            "value_at_risk",
            "Maximum potential loss over a given time period at a specific confidence level",
        );
        descriptions.insert(
            "conditional_value_at_risk",
            "Expected loss in the worst-case scenarios beyond VaR",
        );
        descriptions.insert(
            "sharpe_ratio",
            "Risk-adjusted return measure (excess return per unit of volatility)",
        );
        descriptions.insert(
            "sortino_ratio",
            "Modified Sharpe ratio that only considers downside volatility",
        );
        descriptions.insert(
            "maximum_drawdown",
            "Largest peak-to-trough decline in portfolio value",
        );
        descriptions.insert(
            "probability_of_default",
            "Likelihood that a borrower will default within a given time period",
        );
        descriptions.insert(
            "expected_credit_loss",
            "Expected monetary loss from credit defaults",
        );
        descriptions.insert(
            "discriminatory_power",
            "Ability of a model to distinguish between good and bad credit risks",
        );
        descriptions.insert(
            "gini_coefficient",
            "Measure of inequality or discrimination power in credit models",
        );
        descriptions.insert(
            "hit_ratio",
            "Percentage of profitable trades in a trading strategy",
        );
        descriptions.insert("profit_factor", "Ratio of gross profit to gross loss");
        descriptions.insert(
            "fraud_detection_rate",
            "Percentage of fraudulent transactions correctly identified",
        );
        descriptions.insert(
            "regulatory_capital_ratio",
            "Ratio of bank's capital to risk-weighted assets",
        );
        descriptions.insert(
            "esg_score",
            "Environmental, Social, and Governance performance score",
        );
        descriptions
    }
}

/// Risk management and VaR calculation metrics
#[derive(Debug, Clone)]
pub struct RiskManagementMetrics {
    /// Historical VaR calculations
    pub var_results: HashMap<String, f64>,
    /// Stress test results
    pub stress_test_results: HashMap<String, f64>,
    /// Risk decomposition
    pub risk_decomposition: HashMap<String, f64>,
}

impl RiskManagementMetrics {
    pub fn new() -> Self {
        Self {
            var_results: HashMap::new(),
            stress_test_results: HashMap::new(),
            risk_decomposition: HashMap::new(),
        }
    }

    /// Calculate Value at Risk using historical simulation
    pub fn historical_var<F>(
        &self,
        returns: &Array1<F>,
        confidencelevel: F,
        holding_period: usize,
    ) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + PartialOrd + Clone + std::iter::Sum,
    {
        if returns.is_empty() {
            return Err(MetricsError::InvalidInput(
                "Returns array is empty".to_string(),
            ));
        }

        // Sort returns in ascending order
        let mut sorted_returns: Vec<F> = returns.iter().cloned().collect();
        sorted_returns.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        // Calculate percentile index
        let alpha = F::one() - confidencelevel;
        let index = (alpha * F::from(sorted_returns.len()).expect("Operation failed")).floor();
        let var_index = index.to_usize().unwrap_or(0);

        if var_index < sorted_returns.len() {
            let daily_var = -sorted_returns[var_index]; // VaR is positive for losses
                                                        // Scale for holding _period (assuming sqrt of time scaling)
            let holding_period_factor = F::from(holding_period)
                .expect("Failed to convert to float")
                .sqrt();
            Ok(daily_var * holding_period_factor)
        } else {
            Ok(F::zero())
        }
    }

    /// Calculate Conditional Value at Risk (Expected Shortfall)
    pub fn conditional_var<F>(&self, returns: &Array1<F>, confidencelevel: F) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + PartialOrd + Clone + std::iter::Sum,
    {
        if returns.is_empty() {
            return Err(MetricsError::InvalidInput(
                "Returns array is empty".to_string(),
            ));
        }

        let mut sorted_returns: Vec<F> = returns.iter().cloned().collect();
        sorted_returns.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let alpha = F::one() - confidencelevel;
        let cutoff_index = (alpha * F::from(sorted_returns.len()).expect("Operation failed"))
            .floor()
            .to_usize()
            .unwrap_or(0);

        if cutoff_index == 0 {
            return Ok(F::zero());
        }

        // Calculate mean of worst returns (tail)
        let tail_sum: F = sorted_returns[..cutoff_index].iter().cloned().sum();
        let cvar = -tail_sum / F::from(cutoff_index).expect("Failed to convert to float"); // CVaR is positive for losses

        Ok(cvar)
    }

    /// Calculate Maximum Drawdown
    pub fn maximum_drawdown<F>(&self, prices: &Array1<F>) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + PartialOrd,
    {
        if prices.len() < 2 {
            return Ok(F::zero());
        }

        let mut peak = prices[0];
        let mut max_dd = F::zero();

        for &price in prices.iter().skip(1) {
            if price > peak {
                peak = price;
            }

            let drawdown = (peak - price) / peak;
            if drawdown > max_dd {
                max_dd = drawdown;
            }
        }

        Ok(max_dd)
    }

    /// Calculate portfolio beta
    pub fn beta<F>(&self, portfolioreturns: &Array1<F>, marketreturns: &Array1<F>) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + std::iter::Sum,
    {
        if portfolioreturns.len() != marketreturns.len() {
            return Err(MetricsError::InvalidInput(
                "Portfolio and market _returns must have same length".to_string(),
            ));
        }

        let portfolio_mean = portfolioreturns.iter().cloned().sum::<F>()
            / F::from(portfolioreturns.len()).expect("Operation failed");
        let market_mean = marketreturns.iter().cloned().sum::<F>()
            / F::from(marketreturns.len()).expect("Operation failed");

        let covariance = portfolioreturns
            .iter()
            .zip(marketreturns.iter())
            .map(|(&p, &m)| (p - portfolio_mean) * (m - market_mean))
            .sum::<F>()
            / F::from(portfolioreturns.len() - 1).expect("Operation failed");

        let market_variance = marketreturns
            .iter()
            .map(|&m| (m - market_mean) * (m - market_mean))
            .sum::<F>()
            / F::from(marketreturns.len() - 1).expect("Operation failed");

        if market_variance > F::zero() {
            Ok(covariance / market_variance)
        } else {
            Ok(F::zero())
        }
    }
}

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

/// Portfolio performance evaluation metrics
#[derive(Debug, Clone)]
pub struct PortfolioMetrics {
    /// Performance attribution results
    pub attribution_results: HashMap<String, f64>,
    /// Risk-adjusted performance metrics
    pub risk_adjusted_metrics: HashMap<String, f64>,
}

impl PortfolioMetrics {
    pub fn new() -> Self {
        Self {
            attribution_results: HashMap::new(),
            risk_adjusted_metrics: HashMap::new(),
        }
    }

    /// Calculate Sharpe Ratio
    pub fn sharpe_ratio<F>(&self, portfolioreturns: &Array1<F>, risk_freerate: F) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + std::iter::Sum,
    {
        if portfolioreturns.is_empty() {
            return Ok(F::zero());
        }

        let mean_return = portfolioreturns.iter().cloned().sum::<F>()
            / F::from(portfolioreturns.len()).expect("Operation failed");
        let excess_return = mean_return - risk_freerate;

        let variance = portfolioreturns
            .iter()
            .map(|&r| (r - mean_return) * (r - mean_return))
            .sum::<F>()
            / F::from(portfolioreturns.len() - 1).expect("Operation failed");

        let volatility = variance.sqrt();

        if volatility > F::zero() {
            Ok(excess_return / volatility)
        } else {
            Ok(F::zero())
        }
    }

    /// Calculate Sortino Ratio (downside risk-adjusted return)
    pub fn sortino_ratio<F>(&self, portfolioreturns: &Array1<F>, targetreturn: F) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + std::iter::Sum,
    {
        if portfolioreturns.is_empty() {
            return Ok(F::zero());
        }

        let mean_return = portfolioreturns.iter().cloned().sum::<F>()
            / F::from(portfolioreturns.len()).expect("Operation failed");
        let excess_return = mean_return - targetreturn;

        // Calculate downside deviation
        let downside_variance = portfolioreturns
            .iter()
            .map(|&r| {
                let downside_diff = targetreturn - r;
                if downside_diff > F::zero() {
                    downside_diff * downside_diff
                } else {
                    F::zero()
                }
            })
            .sum::<F>()
            / F::from(portfolioreturns.len() - 1).expect("Operation failed");

        let downside_deviation = downside_variance.sqrt();

        if downside_deviation > F::zero() {
            Ok(excess_return / downside_deviation)
        } else {
            Ok(F::zero())
        }
    }

    /// Calculate Information Ratio
    pub fn information_ratio<F>(
        &self,
        portfolioreturns: &Array1<F>,
        benchmark_returns: &Array1<F>,
    ) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + std::iter::Sum,
    {
        if portfolioreturns.len() != benchmark_returns.len() {
            return Err(MetricsError::InvalidInput(
                "Portfolio and benchmark _returns must have same length".to_string(),
            ));
        }

        // Calculate active _returns
        let active_returns: Vec<F> = portfolioreturns
            .iter()
            .zip(benchmark_returns.iter())
            .map(|(&p, &b)| p - b)
            .collect();

        let mean_active_return = active_returns.iter().cloned().sum::<F>()
            / F::from(active_returns.len()).expect("Operation failed");

        // Calculate tracking error (standard deviation of active returns)
        let tracking_error = {
            let variance = active_returns
                .iter()
                .map(|&ar| (ar - mean_active_return) * (ar - mean_active_return))
                .sum::<F>()
                / F::from(active_returns.len() - 1).expect("Operation failed");
            variance.sqrt()
        };

        if tracking_error > F::zero() {
            Ok(mean_active_return / tracking_error)
        } else {
            Ok(F::zero())
        }
    }

    /// Calculate Calmar Ratio
    pub fn calmar_ratio<F>(&self, portfolioreturns: &Array1<F>, prices: &Array1<F>) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + PartialOrd + std::iter::Sum,
    {
        let annualized_return = self.annualized_return(portfolioreturns)?;

        let risk_mgmt = RiskManagementMetrics::new();
        let max_drawdown = risk_mgmt.maximum_drawdown(prices)?;

        if max_drawdown > F::zero() {
            Ok(annualized_return / max_drawdown)
        } else {
            Ok(F::zero())
        }
    }

    /// Calculate annualized return
    fn annualized_return<F>(&self, returns: &Array1<F>) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + std::iter::Sum,
    {
        if returns.is_empty() {
            return Ok(F::zero());
        }

        let mean_return =
            returns.iter().cloned().sum::<F>() / F::from(returns.len()).expect("Operation failed");
        // Assuming daily returns, multiply by 252 trading days
        Ok(mean_return * F::from(252).expect("Failed to convert constant to float"))
    }
}

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

/// Credit risk modeling metrics
#[derive(Debug, Clone)]
pub struct CreditRiskMetrics {
    /// Model discrimination metrics
    pub discrimination_metrics: HashMap<String, f64>,
    /// Model calibration metrics
    pub calibration_metrics: HashMap<String, f64>,
    /// Model stability metrics
    pub stability_metrics: HashMap<String, f64>,
}

impl CreditRiskMetrics {
    pub fn new() -> Self {
        Self {
            discrimination_metrics: HashMap::new(),
            calibration_metrics: HashMap::new(),
            stability_metrics: HashMap::new(),
        }
    }

    /// Calculate Gini coefficient for credit model discrimination
    pub fn gini_coefficient<F>(
        &self,
        predicted_scores: &Array1<F>,
        actual_defaults: &Array1<bool>,
    ) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + PartialOrd,
    {
        if predicted_scores.len() != actual_defaults.len() {
            return Err(MetricsError::InvalidInput(
                "Predicted _scores and actual _defaults must have same length".to_string(),
            ));
        }

        // Create (score, default) pairs and sort by score descending
        let mut pairs: Vec<(F, bool)> = predicted_scores
            .iter()
            .zip(actual_defaults.iter())
            .map(|(&score, &default)| (score, default))
            .collect();
        pairs.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));

        let total_defaults = actual_defaults.iter().filter(|&&x| x).count();
        let total_non_defaults = actual_defaults.len() - total_defaults;

        if total_defaults == 0 || total_non_defaults == 0 {
            return Ok(F::zero());
        }

        let mut cumulative_defaults = 0;
        let mut _cumulative_non_defaults = 0;
        let mut auc = F::zero();

        for (_, is_default) in pairs {
            if is_default {
                cumulative_defaults += 1;
            } else {
                _cumulative_non_defaults += 1;
                auc = auc + F::from(cumulative_defaults).expect("Failed to convert to float");
            }
        }

        let auc_normalized = auc
            / (F::from(total_defaults).expect("Failed to convert to float")
                * F::from(total_non_defaults).expect("Failed to convert to float"));
        let gini =
            F::from(2.0).expect("Failed to convert constant to float") * auc_normalized - F::one();

        Ok(gini)
    }

    /// Calculate Kolmogorov-Smirnov statistic
    pub fn kolmogorov_smirnov<F>(
        &self,
        predicted_scores: &Array1<F>,
        actual_defaults: &Array1<bool>,
    ) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + PartialOrd,
    {
        if predicted_scores.len() != actual_defaults.len() {
            return Err(MetricsError::InvalidInput(
                "Predicted _scores and actual _defaults must have same length".to_string(),
            ));
        }

        // Create (score, default) pairs and sort by score
        let mut pairs: Vec<(F, bool)> = predicted_scores
            .iter()
            .zip(actual_defaults.iter())
            .map(|(&score, &default)| (score, default))
            .collect();
        pairs.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

        let total_defaults = actual_defaults.iter().filter(|&&x| x).count();
        let total_non_defaults = actual_defaults.len() - total_defaults;

        if total_defaults == 0 || total_non_defaults == 0 {
            return Ok(F::zero());
        }

        let mut cumulative_defaults = 0;
        let mut cumulative_non_defaults = 0;
        let mut max_ks = F::zero();

        for (_, is_default) in pairs {
            if is_default {
                cumulative_defaults += 1;
            } else {
                cumulative_non_defaults += 1;
            }

            let default_rate = F::from(cumulative_defaults).expect("Failed to convert to float")
                / F::from(total_defaults).expect("Failed to convert to float");
            let non_default_rate = F::from(cumulative_non_defaults)
                .expect("Failed to convert to float")
                / F::from(total_non_defaults).expect("Failed to convert to float");

            let ks_stat = (default_rate - non_default_rate).abs();
            if ks_stat > max_ks {
                max_ks = ks_stat;
            }
        }

        Ok(max_ks)
    }

    /// Calculate Population Stability Index (PSI)
    pub fn population_stability_index<F>(
        &self,
        baseline_scores: &Array1<F>,
        current_scores: &Array1<F>,
        num_buckets: usize,
    ) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + PartialOrd + Clone + std::iter::Sum,
    {
        if num_buckets == 0 {
            return Err(MetricsError::InvalidInput(
                "Number of _buckets must be greater than 0".to_string(),
            ));
        }

        // Create score _buckets based on baseline distribution
        let mut baseline_sorted: Vec<F> = baseline_scores.iter().cloned().collect();
        baseline_sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let bucket_size = baseline_sorted.len() / num_buckets;
        let mut bucket_boundaries = Vec::new();

        for i in 1..num_buckets {
            let idx = i * bucket_size;
            if idx < baseline_sorted.len() {
                bucket_boundaries.push(baseline_sorted[idx]);
            }
        }

        // Count observations in each bucket for baseline and current
        let baseline_counts =
            self.count_observations_in_buckets(baseline_scores, &bucket_boundaries);
        let current_counts = self.count_observations_in_buckets(current_scores, &bucket_boundaries);

        // Calculate PSI
        let mut psi = F::zero();
        for i in 0..num_buckets {
            let baseline_pct = F::from(baseline_counts[i]).expect("Failed to convert to float")
                / F::from(baseline_scores.len()).expect("Operation failed");
            let current_pct = F::from(current_counts[i]).expect("Failed to convert to float")
                / F::from(current_scores.len()).expect("Operation failed");

            if baseline_pct > F::zero() && current_pct > F::zero() {
                let ratio = current_pct / baseline_pct;
                psi = psi + (current_pct - baseline_pct) * ratio.ln();
            }
        }

        Ok(psi)
    }

    /// Helper method to count observations in buckets
    fn count_observations_in_buckets<F>(&self, scores: &Array1<F>, boundaries: &[F]) -> Vec<usize>
    where
        F: Float + PartialOrd,
    {
        let num_buckets = boundaries.len() + 1;
        let mut counts = vec![0; num_buckets];

        for &score in scores.iter() {
            let mut bucket = 0;
            for (i, &boundary) in boundaries.iter().enumerate() {
                if score >= boundary {
                    bucket = i + 1;
                } else {
                    break;
                }
            }
            if bucket < counts.len() {
                counts[bucket] += 1;
            }
        }

        counts
    }
}

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

/// Market risk assessment metrics
#[derive(Debug, Clone)]
pub struct MarketRiskMetrics;

impl MarketRiskMetrics {
    pub fn new() -> Self {
        Self
    }

    /// Calculate correlation matrix between multiple assets
    pub fn correlation_matrix<F>(&self, returnsmatrix: &Array2<F>) -> Result<Array2<F>>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + std::iter::Sum,
    {
        let (n_periods, n_assets) = returnsmatrix.dim();
        let mut correlation_matrix = Array2::zeros((n_assets, n_assets));

        // Calculate means for each asset
        let means: Vec<F> = (0..n_assets)
            .map(|i| {
                returnsmatrix.column(i).iter().cloned().sum::<F>()
                    / F::from(n_periods).expect("Failed to convert to float")
            })
            .collect();

        // Calculate correlation coefficients
        for i in 0..n_assets {
            for j in 0..n_assets {
                if i == j {
                    correlation_matrix[[i, j]] = F::one();
                } else {
                    let correlation = self.calculate_correlation(
                        &returnsmatrix.column(i),
                        &returnsmatrix.column(j),
                        means[i],
                        means[j],
                    )?;
                    correlation_matrix[[i, j]] = correlation;
                }
            }
        }

        Ok(correlation_matrix)
    }

    /// Helper method to calculate correlation between two return series
    fn calculate_correlation<F>(
        &self,
        returns1: &ArrayView1<F>,
        returns2: &ArrayView1<F>,
        mean1: F,
        mean2: F,
    ) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + std::iter::Sum,
    {
        let n = F::from(returns1.len()).expect("Operation failed");

        let covariance = returns1
            .iter()
            .zip(returns2.iter())
            .map(|(&r1, &r2)| (r1 - mean1) * (r2 - mean2))
            .sum::<F>()
            / (n - F::one());

        let var1 = returns1
            .iter()
            .map(|&r| (r - mean1) * (r - mean1))
            .sum::<F>()
            / (n - F::one());

        let var2 = returns2
            .iter()
            .map(|&r| (r - mean2) * (r - mean2))
            .sum::<F>()
            / (n - F::one());

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

        if std_product > F::zero() {
            Ok(covariance / std_product)
        } else {
            Ok(F::zero())
        }
    }
}

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

/// Trading strategy evaluation metrics
#[derive(Debug, Clone)]
pub struct TradingStrategyMetrics;

impl TradingStrategyMetrics {
    pub fn new() -> Self {
        Self
    }

    /// Calculate hit ratio (percentage of profitable trades)
    pub fn hit_ratio<F>(&self, tradereturns: &Array1<F>) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + PartialOrd,
    {
        if tradereturns.is_empty() {
            return Ok(F::zero());
        }

        let profitable_trades = tradereturns.iter().filter(|&&ret| ret > F::zero()).count();

        Ok(
            F::from(profitable_trades).expect("Failed to convert to float")
                / F::from(tradereturns.len()).expect("Operation failed"),
        )
    }

    /// Calculate profit factor (gross profit / gross loss)
    pub fn profit_factor<F>(&self, tradereturns: &Array1<F>) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + PartialOrd + std::iter::Sum,
    {
        let gross_profit = tradereturns
            .iter()
            .filter(|&&ret| ret > F::zero())
            .cloned()
            .sum::<F>();

        let gross_loss = tradereturns
            .iter()
            .filter(|&&ret| ret < F::zero())
            .cloned()
            .sum::<F>()
            .abs();

        if gross_loss > F::zero() {
            Ok(gross_profit / gross_loss)
        } else if gross_profit > F::zero() {
            Ok(F::infinity())
        } else {
            Ok(F::zero())
        }
    }

    /// Calculate recovery factor
    pub fn recovery_factor<F>(&self, tradereturns: &Array1<F>, prices: &Array1<F>) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive + PartialOrd + std::iter::Sum,
    {
        let total_return = tradereturns.iter().cloned().sum::<F>();

        let risk_mgmt = RiskManagementMetrics::new();
        let max_drawdown = risk_mgmt.maximum_drawdown(prices)?;

        if max_drawdown > F::zero() {
            Ok(total_return / max_drawdown)
        } else {
            Ok(F::zero())
        }
    }
}

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

/// Fraud detection metrics
#[derive(Debug, Clone)]
pub struct FraudDetectionMetrics;

impl FraudDetectionMetrics {
    pub fn new() -> Self {
        Self
    }

    /// Calculate fraud detection rate
    pub fn fraud_detection_rate(
        &self,
        true_fraud: &Array1<bool>,
        predicted_fraud: &Array1<bool>,
    ) -> Result<f64> {
        if true_fraud.len() != predicted_fraud.len() {
            return Err(MetricsError::InvalidInput(
                "True and predicted arrays must have same length".to_string(),
            ));
        }

        let true_positives = true_fraud
            .iter()
            .zip(predicted_fraud.iter())
            .filter(|(&actual, &predicted)| actual && predicted)
            .count();

        let total_fraud = true_fraud.iter().filter(|&&x| x).count();

        if total_fraud > 0 {
            Ok(true_positives as f64 / total_fraud as f64)
        } else {
            Ok(0.0)
        }
    }

    /// Calculate false positive rate
    pub fn false_positive_rate(
        &self,
        true_fraud: &Array1<bool>,
        predicted_fraud: &Array1<bool>,
    ) -> Result<f64> {
        if true_fraud.len() != predicted_fraud.len() {
            return Err(MetricsError::InvalidInput(
                "True and predicted arrays must have same length".to_string(),
            ));
        }

        let false_positives = true_fraud
            .iter()
            .zip(predicted_fraud.iter())
            .filter(|(&actual, &predicted)| !actual && predicted)
            .count();

        let total_legitimate = true_fraud.iter().filter(|&&x| !x).count();

        if total_legitimate > 0 {
            Ok(false_positives as f64 / total_legitimate as f64)
        } else {
            Ok(0.0)
        }
    }
}

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

/// Regulatory compliance metrics (Basel III, etc.)
#[derive(Debug, Clone)]
pub struct RegulatoryMetrics;

impl RegulatoryMetrics {
    pub fn new() -> Self {
        Self
    }

    /// Calculate regulatory capital ratio
    pub fn capital_ratio<F>(&self, tier1_capital: F, risk_weightedassets: F) -> Result<F>
    where
        F: Float,
    {
        if risk_weightedassets > F::zero() {
            Ok(tier1_capital / risk_weightedassets)
        } else {
            Err(MetricsError::InvalidInput(
                "Risk-weighted _assets must be greater than zero".to_string(),
            ))
        }
    }

    /// Calculate leverage ratio
    pub fn leverage_ratio<F>(&self, tier1_capital: F, totalexposure: F) -> Result<F>
    where
        F: Float,
    {
        if totalexposure > F::zero() {
            Ok(tier1_capital / totalexposure)
        } else {
            Err(MetricsError::InvalidInput(
                "Total _exposure must be greater than zero".to_string(),
            ))
        }
    }

    /// Calculate Liquidity Coverage Ratio (LCR)
    pub fn liquidity_coverage_ratio<F>(
        &self,
        high_quality_liquid_assets: F,
        net_cash_outflows: F,
    ) -> Result<F>
    where
        F: Float,
    {
        if net_cash_outflows > F::zero() {
            Ok(high_quality_liquid_assets / net_cash_outflows)
        } else {
            Err(MetricsError::InvalidInput(
                "Net cash _outflows must be greater than zero".to_string(),
            ))
        }
    }
}

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

/// ESG (Environmental, Social, Governance) scoring metrics
#[derive(Debug, Clone)]
pub struct ESGMetrics;

impl ESGMetrics {
    pub fn new() -> Self {
        Self
    }

    /// Calculate composite ESG score
    pub fn composite_esg_score<F>(
        &self,
        environmental_score: F,
        social_score: F,
        governance_score: F,
        weights: Option<(F, F, F)>,
    ) -> Result<F>
    where
        F: Float + scirs2_core::numeric::FromPrimitive,
    {
        let (env_weight, social_weight, gov_weight) = weights.unwrap_or((
            F::from(1.0).expect("Failed to convert constant to float")
                / F::from(3.0).expect("Failed to convert constant to float"),
            F::from(1.0).expect("Failed to convert constant to float")
                / F::from(3.0).expect("Failed to convert constant to float"),
            F::from(1.0).expect("Failed to convert constant to float")
                / F::from(3.0).expect("Failed to convert constant to float"),
        ));

        Ok(environmental_score * env_weight
            + social_score * social_weight
            + governance_score * gov_weight)
    }

    /// Calculate carbon intensity metric
    pub fn carbon_intensity<F>(&self, carbonemissions: F, revenue: F) -> Result<F>
    where
        F: Float,
    {
        if revenue > F::zero() {
            Ok(carbonemissions / revenue)
        } else {
            Err(MetricsError::InvalidInput(
                "Revenue must be greater than zero".to_string(),
            ))
        }
    }
}

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

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

    #[test]
    fn test_financial_suite_creation() {
        let suite = FinancialSuite::new();
        assert_eq!(
            suite.domain_name(),
            "Financial Modeling & Quantitative Finance"
        );

        let metrics = suite.available_metrics();
        assert!(metrics.contains(&"value_at_risk"));
        assert!(metrics.contains(&"sharpe_ratio"));
        assert!(metrics.contains(&"gini_coefficient"));
    }

    #[test]
    fn test_sharpe_ratio() {
        let portfolio = PortfolioMetrics::new();
        let returns = array![0.01, 0.02, -0.01, 0.03, 0.0];
        let risk_freerate = 0.005;

        let sharpe = portfolio
            .sharpe_ratio(&returns, risk_freerate)
            .expect("Operation failed");
        assert!(sharpe.is_finite());
    }

    #[test]
    fn test_historical_var() {
        let risk_mgmt = RiskManagementMetrics::new();
        let returns = array![-0.05, -0.02, 0.01, 0.03, -0.01, 0.02, -0.03];
        let confidencelevel = 0.95;

        let var = risk_mgmt
            .historical_var(&returns, confidencelevel, 1)
            .expect("Operation failed");
        assert!(var >= 0.0);
    }

    #[test]
    fn test_gini_coefficient() {
        let credit = CreditRiskMetrics::new();
        let scores = array![0.8, 0.6, 0.9, 0.3, 0.7];
        let defaults = array![true, false, true, false, false];

        let gini = credit
            .gini_coefficient(&scores, &defaults)
            .expect("Operation failed");
        assert!((-1.0..=1.0).contains(&gini));
    }

    #[test]
    fn test_hit_ratio() {
        let trading = TradingStrategyMetrics::new();
        let tradereturns = array![0.02, -0.01, 0.03, -0.005, 0.01];

        let hit_ratio = trading.hit_ratio(&tradereturns).expect("Operation failed");
        assert!((0.0..=1.0).contains(&hit_ratio));
        assert_eq!(hit_ratio, 0.6); // 3 out of 5 profitable trades
    }

    #[test]
    fn test_capital_ratio() {
        let regulatory = RegulatoryMetrics::new();
        let tier1_capital = 100.0;
        let risk_weightedassets = 800.0;

        let ratio = regulatory
            .capital_ratio(tier1_capital, risk_weightedassets)
            .expect("Operation failed");
        assert_eq!(ratio, 0.125); // 12.5%
    }

    #[test]
    fn test_composite_esg_score() {
        let esg = ESGMetrics::new();
        let environmental = 0.8;
        let social = 0.7;
        let governance = 0.9;

        let composite = esg
            .composite_esg_score(environmental, social, governance, None)
            .expect("Operation failed");
        assert!((0.0..=1.0).contains(&composite));
    }
}