scirs2-metrics 0.3.2

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
//! Threshold analysis for binary classification
//!
//! This module provides tools for analyzing binary classification performance
//! across different thresholds and finding optimal thresholds based on various
//! metrics and strategies.

use scirs2_core::ndarray::{ArrayBase, Data, Dimension, Ix1};
use std::collections::HashMap;
use std::hash::{Hash, Hasher};

use crate::classification::curves::roc_curve;
use crate::error::{MetricsError, Result};

/// Metrics calculated at a specific threshold
#[derive(Debug, Clone)]
pub struct ThresholdMetrics {
    /// Classification threshold value
    pub threshold: f64,
    /// True positive rate (sensitivity, recall)
    pub tpr: f64,
    /// False positive rate (1 - specificity)
    pub fpr: f64,
    /// Precision (positive predictive value)
    pub precision: f64,
    /// F1 score (harmonic mean of precision and recall)
    pub f1_score: f64,
    /// Accuracy (correct predictions / total predictions)
    pub accuracy: f64,
    /// Specificity (true negative rate)
    pub specificity: f64,
    /// Negative predictive value
    pub npv: f64,
    /// Matthews correlation coefficient
    pub mcc: f64,
    /// Cohen's kappa coefficient
    pub kappa: f64,
    /// Youden's J statistic (sensitivity + specificity - 1)
    pub youdens_j: f64,
    /// Balanced accuracy
    pub balanced_accuracy: f64,
    /// Count of true positives
    pub tp: usize,
    /// Count of false positives
    pub fp: usize,
    /// Count of true negatives
    pub tn: usize,
    /// Count of false negatives
    pub fn_: usize,
}

/// Strategies for finding optimal threshold
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OptimalThresholdStrategy {
    /// Maximize F1 score
    MaxF1,
    /// Maximize Youden's J statistic (sensitivity + specificity - 1)
    YoudensJ,
    /// Maximize accuracy
    MaxAccuracy,
    /// Maximize Matthews correlation coefficient
    MaxMCC,
    /// Maximize Cohen's kappa
    MaxKappa,
    /// Balance sensitivity and specificity
    BalancedSensSpec,
    /// Balance precision and recall
    BalancedPrecRecall,
    /// Minimize distance to perfect classifier (0,1) in ROC space
    MinDistanceToOptimal,
    /// Use a specific threshold value
    Manual(f64),
}

impl Hash for OptimalThresholdStrategy {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            OptimalThresholdStrategy::MaxF1 => 0.hash(state),
            OptimalThresholdStrategy::YoudensJ => 1.hash(state),
            OptimalThresholdStrategy::MaxAccuracy => 2.hash(state),
            OptimalThresholdStrategy::MaxMCC => 3.hash(state),
            OptimalThresholdStrategy::MaxKappa => 4.hash(state),
            OptimalThresholdStrategy::BalancedSensSpec => 5.hash(state),
            OptimalThresholdStrategy::BalancedPrecRecall => 6.hash(state),
            OptimalThresholdStrategy::MinDistanceToOptimal => 7.hash(state),
            OptimalThresholdStrategy::Manual(val) => {
                8.hash(state);
                val.to_bits().hash(state);
            }
        }
    }
}

impl Eq for OptimalThresholdStrategy {}

/// Analyzer for binary classification thresholds
///
/// This struct provides tools for analyzing binary classification performance
/// across different thresholds and finding optimal thresholds based on various
/// metrics and strategies.
#[derive(Debug)]
pub struct ThresholdAnalyzer {
    /// True positive rates
    tpr: Vec<f64>,
    /// False positive rates
    fpr: Vec<f64>,
    /// Thresholds
    thresholds: Vec<f64>,
    /// Raw true labels
    y_true: Vec<f64>,
    /// Raw score predictions
    y_score: Vec<f64>,
    /// Metrics at each threshold
    metrics: Option<Vec<ThresholdMetrics>>,
    /// Optimal thresholds for each strategy
    optimal_thresholds: HashMap<OptimalThresholdStrategy, usize>,
}

impl ThresholdAnalyzer {
    /// Create a new threshold analyzer from true labels and scores
    ///
    /// # Arguments
    ///
    /// * `y_true` - Binary true labels (0 or 1)
    /// * `y_score` - Predicted scores (probabilities)
    ///
    /// # Returns
    ///
    /// * `Result<ThresholdAnalyzer>` - The analyzer or error
    pub fn new<D1, D2, S1, S2>(
        y_true: &ArrayBase<S1, D1>,
        y_score: &ArrayBase<S2, D2>,
    ) -> Result<Self>
    where
        S1: Data,
        S2: Data,
        D1: Dimension,
        D2: Dimension,
        S1::Elem: Clone + Into<f64> + PartialEq,
        S2::Elem: Clone + Into<f64> + PartialOrd,
    {
        // Compute ROC curve
        let (fpr, tpr, thresholds) = roc_curve(y_true, y_score)?;

        // Convert arrays to vectors for storage
        let fpr = fpr.to_vec();
        let tpr = tpr.to_vec();
        let thresholds = thresholds.to_vec();

        // Store original data
        let y_true = y_true
            .iter()
            .map(|x| x.clone().into())
            .collect::<Vec<f64>>();
        let y_score = y_score
            .iter()
            .map(|x| x.clone().into())
            .collect::<Vec<f64>>();

        Ok(Self {
            tpr,
            fpr,
            thresholds,
            y_true,
            y_score,
            metrics: None,
            optimal_thresholds: HashMap::new(),
        })
    }

    /// Create a new threshold analyzer from pre-computed ROC curve
    ///
    /// # Arguments
    ///
    /// * `fpr` - False positive rates
    /// * `tpr` - True positive rates
    /// * `thresholds` - Thresholds
    /// * `y_true` - Binary true labels (0 or 1)
    /// * `y_score` - Predicted scores (probabilities)
    ///
    /// # Returns
    ///
    /// * `Result<ThresholdAnalyzer>` - The analyzer or error
    pub fn from_roc_curve<D1, D2, S1, S2, S3, S4, S5, D3, D4, D5>(
        fpr: &ArrayBase<S1, D1>,
        tpr: &ArrayBase<S2, D2>,
        thresholds: &ArrayBase<S3, D3>,
        y_true: &ArrayBase<S4, D4>,
        y_score: &ArrayBase<S5, D5>,
    ) -> Result<Self>
    where
        S1: Data<Elem = f64>,
        S2: Data<Elem = f64>,
        S3: Data<Elem = f64>,
        S4: Data,
        S5: Data,
        D1: Dimension,
        D2: Dimension,
        D3: Dimension,
        D4: Dimension,
        D5: Dimension,
        S4::Elem: Clone + Into<f64>,
        S5::Elem: Clone + Into<f64>,
    {
        // Convert arrays to vectors for storage
        let fpr = fpr.iter().cloned().collect::<Vec<f64>>();
        let tpr = tpr.iter().cloned().collect::<Vec<f64>>();
        let thresholds = thresholds.iter().cloned().collect::<Vec<f64>>();

        // Store original data
        let y_true = y_true
            .iter()
            .map(|x| x.clone().into())
            .collect::<Vec<f64>>();
        let y_score = y_score
            .iter()
            .map(|x| x.clone().into())
            .collect::<Vec<f64>>();

        // Ensure proper shape
        if fpr.len() != tpr.len() || fpr.len() != thresholds.len() {
            return Err(MetricsError::ShapeMismatch {
                shape1: format!("fpr: {}", fpr.len()),
                shape2: format!("tpr: {}, thresholds: {}", tpr.len(), thresholds.len()),
            });
        }

        Ok(Self {
            tpr,
            fpr,
            thresholds,
            y_true,
            y_score,
            metrics: None,
            optimal_thresholds: HashMap::new(),
        })
    }

    /// Calculate metrics at all thresholds
    ///
    /// # Returns
    ///
    /// * `Result<&[ThresholdMetrics]>` - Metrics at all thresholds
    pub fn calculate_metrics(&mut self) -> Result<&[ThresholdMetrics]> {
        // If metrics are already calculated, return them
        if let Some(ref metrics) = self.metrics {
            return Ok(metrics);
        }

        // Calculate metrics for each threshold
        let mut metrics = Vec::with_capacity(self.thresholds.len());

        for &threshold in self.thresholds.iter() {
            // Count TP, FP, TN, FN
            let mut tp = 0;
            let mut fp = 0;
            let mut tn = 0;
            let mut fn_ = 0;

            for (&true_val, &score) in self.y_true.iter().zip(&self.y_score) {
                let pred = if score >= threshold { 1.0 } else { 0.0 };

                match (true_val, pred) {
                    (1.0, 1.0) => tp += 1,
                    (0.0, 1.0) => fp += 1,
                    (0.0, 0.0) => tn += 1,
                    (1.0, 0.0) => fn_ += 1,
                    _ => {
                        return Err(MetricsError::InvalidArgument(format!(
                            "Invalid true value: {true_val}"
                        )));
                    }
                }
            }

            // Calculate metrics
            let tpr = if tp + fn_ > 0 {
                tp as f64 / (tp + fn_) as f64
            } else {
                0.0
            };
            let fpr = if fp + tn > 0 {
                fp as f64 / (fp + tn) as f64
            } else {
                0.0
            };
            let precision = if tp + fp > 0 {
                tp as f64 / (tp + fp) as f64
            } else {
                0.0
            };
            let f1_score = if precision + tpr > 0.0 {
                2.0 * precision * tpr / (precision + tpr)
            } else {
                0.0
            };
            let accuracy = (tp + tn) as f64 / (tp + fp + tn + fn_) as f64;
            let specificity = if tn + fp > 0 {
                tn as f64 / (tn + fp) as f64
            } else {
                0.0
            };
            let npv = if tn + fn_ > 0 {
                tn as f64 / (tn + fn_) as f64
            } else {
                0.0
            };
            let youdens_j = tpr + specificity - 1.0;
            let balanced_accuracy = (tpr + specificity) / 2.0;

            // Matthews correlation coefficient
            let mcc_numerator = (tp * tn) as f64 - (fp * fn_) as f64;
            let mcc_denominator = ((tp + fp) * (tp + fn_) * (tn + fp) * (tn + fn_)) as f64;
            let mcc = if mcc_denominator > 0.0 {
                mcc_numerator / mcc_denominator.sqrt()
            } else {
                0.0
            };

            // Cohen's kappa
            let p_o = accuracy;
            let p_e = (((tp + fp) as f64 / (tp + fp + tn + fn_) as f64)
                * ((tp + fn_) as f64 / (tp + fp + tn + fn_) as f64))
                + (((tn + fn_) as f64 / (tp + fp + tn + fn_) as f64)
                    * ((tn + fp) as f64 / (tp + fp + tn + fn_) as f64));
            let kappa = if p_e < 1.0 {
                (p_o - p_e) / (1.0 - p_e)
            } else {
                0.0
            };

            metrics.push(ThresholdMetrics {
                threshold,
                tpr,
                fpr,
                precision,
                f1_score,
                accuracy,
                specificity,
                npv,
                mcc,
                kappa,
                youdens_j,
                balanced_accuracy,
                tp,
                fp,
                tn,
                fn_,
            });
        }

        self.metrics = Some(metrics);
        Ok(self.metrics.as_ref().expect("Operation failed"))
    }

    /// Find optimal threshold based on a given strategy
    ///
    /// # Arguments
    ///
    /// * `strategy` - Strategy for finding optimal threshold
    ///
    /// # Returns
    ///
    /// * `Result<(f64, ThresholdMetrics)>` - Optimal threshold and its metrics
    pub fn find_optimal_threshold(
        &mut self,
        strategy: OptimalThresholdStrategy,
    ) -> Result<(f64, ThresholdMetrics)> {
        // Check if optimal threshold for this strategy is already calculated first
        if let Some(&idx) = self.optimal_thresholds.get(&strategy) {
            self.calculate_metrics()?;
            let threshold = self.thresholds[idx];
            let metrics = self.metrics.as_ref().expect("Operation failed");
            return Ok((threshold, metrics[idx].clone()));
        }

        // Calculate metrics for finding optimal
        self.calculate_metrics()?;
        let metrics = self.metrics.as_ref().expect("Operation failed");

        // Find optimal threshold based on strategy
        let optimal_idx = match strategy {
            OptimalThresholdStrategy::MaxF1 => metrics
                .iter()
                .enumerate()
                .max_by(|(_, a), (_, b)| {
                    a.f1_score
                        .partial_cmp(&b.f1_score)
                        .expect("Operation failed")
                })
                .map(|(idx, _)| idx)
                .unwrap_or(0),
            OptimalThresholdStrategy::YoudensJ => metrics
                .iter()
                .enumerate()
                .max_by(|(_, a), (_, b)| {
                    a.youdens_j
                        .partial_cmp(&b.youdens_j)
                        .expect("Operation failed")
                })
                .map(|(idx, _)| idx)
                .unwrap_or(0),
            OptimalThresholdStrategy::MaxAccuracy => metrics
                .iter()
                .enumerate()
                .max_by(|(_, a), (_, b)| {
                    a.accuracy
                        .partial_cmp(&b.accuracy)
                        .expect("Operation failed")
                })
                .map(|(idx, _)| idx)
                .unwrap_or(0),
            OptimalThresholdStrategy::MaxMCC => metrics
                .iter()
                .enumerate()
                .max_by(|(_, a), (_, b)| a.mcc.partial_cmp(&b.mcc).expect("Operation failed"))
                .map(|(idx, _)| idx)
                .unwrap_or(0),
            OptimalThresholdStrategy::MaxKappa => metrics
                .iter()
                .enumerate()
                .max_by(|(_, a), (_, b)| a.kappa.partial_cmp(&b.kappa).expect("Operation failed"))
                .map(|(idx, _)| idx)
                .unwrap_or(0),
            OptimalThresholdStrategy::BalancedSensSpec => metrics
                .iter()
                .enumerate()
                .min_by(|(_, a), (_, b)| {
                    let a_diff = (a.tpr - a.specificity).abs();
                    let b_diff = (b.tpr - b.specificity).abs();
                    a_diff.partial_cmp(&b_diff).expect("Operation failed")
                })
                .map(|(idx, _)| idx)
                .unwrap_or(0),
            OptimalThresholdStrategy::BalancedPrecRecall => metrics
                .iter()
                .enumerate()
                .min_by(|(_, a), (_, b)| {
                    let a_diff = (a.precision - a.tpr).abs();
                    let b_diff = (b.precision - b.tpr).abs();
                    a_diff.partial_cmp(&b_diff).expect("Operation failed")
                })
                .map(|(idx, _)| idx)
                .unwrap_or(0),
            OptimalThresholdStrategy::MinDistanceToOptimal => metrics
                .iter()
                .enumerate()
                .min_by(|(_, a), (_, b)| {
                    let a_dist = (a.fpr.powi(2) + (1.0 - a.tpr).powi(2)).sqrt();
                    let b_dist = (b.fpr.powi(2) + (1.0 - b.tpr).powi(2)).sqrt();
                    a_dist.partial_cmp(&b_dist).expect("Operation failed")
                })
                .map(|(idx, _)| idx)
                .unwrap_or(0),
            OptimalThresholdStrategy::Manual(threshold) => {
                // Find the closest threshold
                metrics
                    .iter()
                    .enumerate()
                    .min_by(|(_, a), (_, b)| {
                        let a_diff = (a.threshold - threshold).abs();
                        let b_diff = (b.threshold - threshold).abs();
                        a_diff.partial_cmp(&b_diff).expect("Operation failed")
                    })
                    .map(|(idx, _)| idx)
                    .unwrap_or(0)
            }
        };

        // Store the threshold and metrics before borrowing conflicts
        let threshold = self.thresholds[optimal_idx];
        let metric = metrics[optimal_idx].clone();

        // Store optimal threshold for this strategy
        self.optimal_thresholds.insert(strategy, optimal_idx);

        Ok((threshold, metric))
    }

    /// Get metrics at a specific threshold
    ///
    /// # Arguments
    ///
    /// * `threshold` - Threshold value
    ///
    /// # Returns
    ///
    /// * `Result<ThresholdMetrics>` - Metrics at the threshold
    pub fn get_metrics_at_threshold(&mut self, threshold: f64) -> Result<ThresholdMetrics> {
        // Calculate metrics if not already calculated
        self.calculate_metrics()?;

        // Find the closest threshold index
        let idx = self
            .thresholds
            .iter()
            .enumerate()
            .min_by(|(_, &a), (_, &b)| {
                let a_diff = (a - threshold).abs();
                let b_diff = (b - threshold).abs();
                a_diff.partial_cmp(&b_diff).expect("Operation failed")
            })
            .map(|(idx, _)| idx)
            .unwrap_or(0);

        // Get metrics - we know it's calculated at this point
        let metrics = self.metrics.as_ref().expect("Operation failed");
        Ok(metrics[idx].clone())
    }

    /// Get all threshold metrics
    ///
    /// # Returns
    ///
    /// * `Result<&[ThresholdMetrics]>` - All threshold metrics
    pub fn get_all_metrics(&mut self) -> Result<&[ThresholdMetrics]> {
        self.calculate_metrics()
    }

    /// Get thresholds
    ///
    /// # Returns
    ///
    /// * `&[f64]` - Thresholds
    pub fn get_thresholds(&self) -> &[f64] {
        &self.thresholds
    }

    /// Get false positive rates
    ///
    /// # Returns
    ///
    /// * `&[f64]` - False positive rates
    pub fn get_fpr(&self) -> &[f64] {
        &self.fpr
    }

    /// Get true positive rates
    ///
    /// # Returns
    ///
    /// * `&[f64]` - True positive rates
    pub fn get_tpr(&self) -> &[f64] {
        &self.tpr
    }

    /// Convert metrics to a specific column data structure
    ///
    /// # Arguments
    ///
    /// * `metric_name` - Name of the metric to extract
    ///
    /// # Returns
    ///
    /// * `Result<Vec<f64>>` - Values of the specified metric
    pub fn get_metric_values(&mut self, metricname: &str) -> Result<Vec<f64>> {
        let metrics = self.calculate_metrics()?;

        let values = match metricname {
            "threshold" => metrics.iter().map(|m| m.threshold).collect(),
            "tpr" | "recall" | "sensitivity" => metrics.iter().map(|m| m.tpr).collect(),
            "fpr" => metrics.iter().map(|m| m.fpr).collect(),
            "precision" => metrics.iter().map(|m| m.precision).collect(),
            "f1_score" | "f1" => metrics.iter().map(|m| m.f1_score).collect(),
            "accuracy" => metrics.iter().map(|m| m.accuracy).collect(),
            "specificity" => metrics.iter().map(|m| m.specificity).collect(),
            "npv" => metrics.iter().map(|m| m.npv).collect(),
            "mcc" => metrics.iter().map(|m| m.mcc).collect(),
            "kappa" => metrics.iter().map(|m| m.kappa).collect(),
            "youdens_j" | "j" => metrics.iter().map(|m| m.youdens_j).collect(),
            "balanced_accuracy" => metrics.iter().map(|m| m.balanced_accuracy).collect(),
            _ => {
                return Err(MetricsError::InvalidArgument(format!(
                    "Unknown metric: {metricname}"
                )))
            }
        };

        Ok(values)
    }

    /// Get metric names
    ///
    /// # Returns
    ///
    /// * `Vec<String>` - Names of available metrics
    pub fn get_metric_names() -> Vec<String> {
        vec![
            "threshold".to_string(),
            "tpr".to_string(),
            "fpr".to_string(),
            "precision".to_string(),
            "f1_score".to_string(),
            "accuracy".to_string(),
            "specificity".to_string(),
            "npv".to_string(),
            "mcc".to_string(),
            "kappa".to_string(),
            "youdens_j".to_string(),
            "balanced_accuracy".to_string(),
        ]
    }
}

/// Find the optimal threshold for binary classification
///
/// # Arguments
///
/// * `y_true` - Binary true labels (0 or 1)
/// * `y_score` - Predicted scores (probabilities)
/// * `strategy` - Strategy for finding optimal threshold
///
/// # Returns
///
/// * `Result<(f64, ThresholdMetrics)>` - Optimal threshold and its metrics
#[allow(dead_code)]
pub fn find_optimal_threshold<S1, S2>(
    y_true: &ArrayBase<S1, Ix1>,
    y_score: &ArrayBase<S2, Ix1>,
    strategy: OptimalThresholdStrategy,
) -> Result<(f64, ThresholdMetrics)>
where
    S1: Data,
    S2: Data,
    S1::Elem: Clone + Into<f64> + PartialEq,
    S2::Elem: Clone + Into<f64> + PartialOrd,
{
    let mut analyzer = ThresholdAnalyzer::new(y_true, y_score)?;
    let (threshold, metrics) = analyzer.find_optimal_threshold(strategy)?;
    Ok((threshold, metrics.clone()))
}

/// Get metrics at a specific threshold
///
/// # Arguments
///
/// * `y_true` - Binary true labels (0 or 1)
/// * `y_score` - Predicted scores (probabilities)
/// * `threshold` - Specific threshold to evaluate
///
/// # Returns
///
/// * `Result<ThresholdMetrics>` - Metrics at the specified threshold
#[allow(dead_code)]
pub fn threshold_metrics<S1, S2>(
    y_true: &ArrayBase<S1, Ix1>,
    y_score: &ArrayBase<S2, Ix1>,
    threshold: f64,
) -> Result<ThresholdMetrics>
where
    S1: Data,
    S2: Data,
    S1::Elem: Clone + Into<f64> + PartialEq,
    S2::Elem: Clone + Into<f64> + PartialOrd,
{
    let mut analyzer = ThresholdAnalyzer::new(y_true, y_score)?;
    let metrics = analyzer.get_metrics_at_threshold(threshold)?;
    Ok(metrics.clone())
}

/// Calculate metrics at all possible thresholds
///
/// # Arguments
///
/// * `y_true` - Binary true labels (0 or 1)
/// * `y_score` - Predicted scores (probabilities)
///
/// # Returns
///
/// * `Result<Vec<ThresholdMetrics>>` - Metrics at all thresholds
#[allow(dead_code)]
pub fn all_threshold_metrics<S1, S2>(
    y_true: &ArrayBase<S1, Ix1>,
    y_score: &ArrayBase<S2, Ix1>,
) -> Result<Vec<ThresholdMetrics>>
where
    S1: Data,
    S2: Data,
    S1::Elem: Clone + Into<f64> + PartialEq,
    S2::Elem: Clone + Into<f64> + PartialOrd,
{
    let mut analyzer = ThresholdAnalyzer::new(y_true, y_score)?;
    let metrics = analyzer.get_all_metrics()?;
    Ok(metrics.to_vec())
}