ferricml 0.2.1

A lean, pure-Rust toolkit for classical machine learning
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
//! The label-set confusion matrix every averaged classification score reads.

use super::{Average, Averaging, MetricError, ZeroDivision, validate_lengths};

/// One class's contribution to a [`ConfusionMatrix`].
///
/// These four counts are what every per-class classification score is built
/// from, so a metric that needs a new combination of them needs no new access
/// to the matrix.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ClassCounts {
    true_positives: u64,
    false_positives: u64,
    false_negatives: u64,
}

impl ClassCounts {
    /// Rows of this class predicted as this class.
    pub const fn true_positives(self) -> u64 {
        self.true_positives
    }

    /// Rows of another class predicted as this class.
    pub const fn false_positives(self) -> u64 {
        self.false_positives
    }

    /// Rows of this class predicted as another class.
    pub const fn false_negatives(self) -> u64 {
        self.false_negatives
    }

    /// True rows carrying this class.
    pub const fn support(self) -> u64 {
        self.true_positives + self.false_negatives
    }

    /// Rows predicted as this class.
    pub const fn predicted(self) -> u64 {
        self.true_positives + self.false_positives
    }
}

/// Counts for a classification result over any observed label set.
///
/// Rows are expected labels and columns are predicted labels, both ordered by
/// [`ConfusionMatrix::labels`] — the sorted union of the labels observed in
/// either input. Every averaged score in FerricML is derived from this one
/// validated pass over the data, so binary and multiclass evaluation share a
/// single definition of what is being counted.
///
/// ```
/// use ferricml::metrics::{Average, ConfusionMatrix};
///
/// let matrix = ConfusionMatrix::new(&[0, 1, 2, 2], &[0, 1, 2, 1])?;
/// assert_eq!(matrix.labels(), &[0, 1, 2]);
/// // Single-label micro-averaging pools every class, so it equals accuracy.
/// assert_eq!(matrix.precision(Average::Micro), Ok(matrix.accuracy()));
/// # Ok::<(), ferricml::metrics::MetricError>(())
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ConfusionMatrix {
    labels: Vec<u8>,
    counts: Vec<u64>,
    total: u64,
}

impl ConfusionMatrix {
    /// Counts one classification result over the observed label set.
    pub fn new(expected: &[u8], predicted: &[u8]) -> Result<Self, MetricError> {
        validate_lengths(expected.len(), predicted.len())?;

        let mut observed = [false; 256];
        for &label in expected.iter().chain(predicted) {
            observed[label as usize] = true;
        }
        let labels = (0..=u8::MAX)
            .filter(|&label| observed[label as usize])
            .collect::<Vec<_>>();
        let mut position = [0_usize; 256];
        for (index, &label) in labels.iter().enumerate() {
            position[label as usize] = index;
        }

        let mut counts = vec![0_u64; labels.len() * labels.len()];
        for (&expected, &predicted) in expected.iter().zip(predicted) {
            let row = position[expected as usize];
            let column = position[predicted as usize];
            counts[row * labels.len() + column] += 1;
        }
        Ok(Self {
            labels,
            counts,
            total: expected.len() as u64,
        })
    }

    /// Sorted labels observed in either input, in row and column order.
    pub fn labels(&self) -> &[u8] {
        &self.labels
    }

    /// Number of observed labels.
    pub fn n_labels(&self) -> usize {
        self.labels.len()
    }

    /// Row-major counts with expected labels as rows and predicted as columns.
    pub fn counts(&self) -> &[u64] {
        &self.counts
    }

    /// Total counted observations.
    pub const fn total(&self) -> u64 {
        self.total
    }

    /// The four counts for one label position, or `None` when out of range.
    pub fn class_counts(&self, index: usize) -> Option<ClassCounts> {
        if index >= self.labels.len() {
            return None;
        }
        Some(self.counts_at(index))
    }

    /// Fraction of rows whose predicted label equals the expected label.
    ///
    /// The matrix cannot be empty, so accuracy is always defined.
    pub fn accuracy(&self) -> f64 {
        let correct = (0..self.labels.len())
            .map(|index| self.counts[index * self.labels.len() + index])
            .sum::<u64>();
        correct as f64 / self.total as f64
    }

    /// Mean recall over the classes that have true rows.
    ///
    /// This is the macro-averaged recall of every class actually present in the
    /// expected labels, which is what makes it insensitive to class imbalance.
    /// A class predicted but never observed has no recall and no place in the
    /// mean, so — unlike [`ConfusionMatrix::recall`] with [`Average::Macro`] —
    /// this is always defined.
    pub fn balanced_accuracy(&self) -> f64 {
        let mut total = 0.0_f64;
        let mut classes = 0.0_f64;
        for index in 0..self.labels.len() {
            let counts = self.counts_at(index);
            if counts.support() == 0 {
                continue;
            }
            total += counts.true_positives() as f64 / counts.support() as f64;
            classes += 1.0;
        }
        debug_assert!(classes > 0.0, "a non-empty matrix has a class with support");
        total / classes
    }

    /// Matthews correlation between the expected and predicted labels.
    ///
    /// The coefficient runs from `-1.0` for complete disagreement through
    /// `0.0` for chance agreement to `1.0` for a perfect result, over any
    /// number of classes. It is undefined — rather than reported as zero —
    /// when either side of the result is constant, because a constant vector
    /// has no variance to correlate.
    pub fn matthews_correlation(&self) -> Result<f64, MetricError> {
        let labels = self.labels.len();
        let total = self.total as f64;
        let mut correct = 0.0_f64;
        let mut agreement = 0.0_f64;
        let mut expected_squares = 0.0_f64;
        let mut predicted_squares = 0.0_f64;
        for index in 0..labels {
            let counts = self.counts_at(index);
            let support = counts.support() as f64;
            let predicted = counts.predicted() as f64;
            correct += counts.true_positives() as f64;
            agreement += support * predicted;
            expected_squares += support * support;
            predicted_squares += predicted * predicted;
        }
        let expected_variance = total * total - expected_squares;
        let predicted_variance = total * total - predicted_squares;
        if expected_variance <= 0.0 || predicted_variance <= 0.0 {
            return Err(MetricError::Undefined);
        }
        Ok((correct * total - agreement) / (expected_variance * predicted_variance).sqrt())
    }

    /// Positive predictive value, combined the requested way.
    pub fn precision(&self, averaging: impl Into<Averaging>) -> Result<f64, MetricError> {
        self.average(averaging.into(), Score::Precision)
    }

    /// True-positive rate, combined the requested way.
    pub fn recall(&self, averaging: impl Into<Averaging>) -> Result<f64, MetricError> {
        self.average(averaging.into(), Score::Recall)
    }

    /// Harmonic mean of precision and recall, combined the requested way.
    pub fn f1(&self, averaging: impl Into<Averaging>) -> Result<f64, MetricError> {
        self.fbeta(1.0, averaging)
    }

    /// Weighted harmonic mean of precision and recall, combined the requested
    /// way.
    ///
    /// `beta` weighs recall over precision: `beta` above one favors recall,
    /// below one favors precision. It must be finite and strictly positive.
    pub fn fbeta(&self, beta: f64, averaging: impl Into<Averaging>) -> Result<f64, MetricError> {
        if !beta.is_finite() || beta <= 0.0 {
            return Err(MetricError::InvalidBeta);
        }
        self.average(
            averaging.into(),
            Score::FBeta {
                squared: beta * beta,
            },
        )
    }

    fn counts_at(&self, index: usize) -> ClassCounts {
        let labels = self.labels.len();
        let true_positives = self.counts[index * labels + index];
        let row = self.counts[index * labels..(index + 1) * labels]
            .iter()
            .sum::<u64>();
        let column = (0..labels)
            .map(|other| self.counts[other * labels + index])
            .sum::<u64>();
        ClassCounts {
            true_positives,
            false_positives: column - true_positives,
            false_negatives: row - true_positives,
        }
    }

    fn average(&self, averaging: Averaging, score: Score) -> Result<f64, MetricError> {
        match averaging.average() {
            Average::Binary => {
                if self.labels.last().is_some_and(|&label| label > 1) {
                    return Err(MetricError::NotBinary {
                        labels: self.labels.len(),
                    });
                }
                let positive = self.labels.iter().position(|&label| label == 1);
                let counts = positive.map_or(ClassCounts::default(), |index| self.counts_at(index));
                score.of(counts).ok_or(MetricError::Undefined)
            }
            Average::Micro => {
                let pooled =
                    (0..self.labels.len()).fold(ClassCounts::default(), |mut sum, index| {
                        let counts = self.counts_at(index);
                        sum.true_positives += counts.true_positives;
                        sum.false_positives += counts.false_positives;
                        sum.false_negatives += counts.false_negatives;
                        sum
                    });
                score.of(pooled).ok_or(MetricError::Undefined)
            }
            Average::Macro => self.combine(score, averaging.zero_division(), |_| Some(1.0)),
            Average::Weighted => self.combine(score, averaging.zero_division(), |counts| {
                // A class with no true rows carries no weight, so it never
                // reaches the zero-division policy at all.
                (counts.support() > 0).then(|| counts.support() as f64)
            }),
        }
    }

    /// Combines per-class scores under one weighting and one empty-denominator
    /// policy, in ascending label order so the sum is deterministic.
    fn combine(
        &self,
        score: Score,
        zero_division: ZeroDivision,
        weight: impl Fn(ClassCounts) -> Option<f64>,
    ) -> Result<f64, MetricError> {
        let mut total = 0.0_f64;
        let mut divisor = 0.0_f64;
        for index in 0..self.labels.len() {
            let counts = self.counts_at(index);
            let Some(weight) = weight(counts) else {
                continue;
            };
            let value = match score.of(counts) {
                Some(value) => value,
                None => match zero_division {
                    ZeroDivision::Error => return Err(MetricError::Undefined),
                    ZeroDivision::Zero => 0.0,
                    ZeroDivision::Skip => continue,
                },
            };
            total += weight * value;
            divisor += weight;
        }
        if divisor == 0.0 {
            return Err(MetricError::Undefined);
        }
        Ok(total / divisor)
    }
}

/// A per-class score expressed as one ratio over [`ClassCounts`].
#[derive(Clone, Copy, Debug)]
enum Score {
    Precision,
    Recall,
    FBeta { squared: f64 },
}

impl Score {
    /// Returns the score, or `None` when its denominator is empty.
    fn of(self, counts: ClassCounts) -> Option<f64> {
        let (numerator, denominator) = match self {
            Self::Precision => (counts.true_positives() as f64, counts.predicted() as f64),
            Self::Recall => (counts.true_positives() as f64, counts.support() as f64),
            Self::FBeta { squared } => {
                let scale = 1.0 + squared;
                (
                    scale * counts.true_positives() as f64,
                    scale * counts.true_positives() as f64
                        + squared * counts.false_negatives() as f64
                        + counts.false_positives() as f64,
                )
            }
        };
        (denominator > 0.0).then(|| numerator / denominator)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metrics::{f1_score, precision_score, recall_score};

    const THREE_CLASS_EXPECTED: [u8; 8] = [0, 1, 2, 2, 1, 0, 1, 2];
    const THREE_CLASS_PREDICTED: [u8; 8] = [0, 2, 2, 2, 1, 0, 0, 1];

    fn three_class() -> ConfusionMatrix {
        ConfusionMatrix::new(&THREE_CLASS_EXPECTED, &THREE_CLASS_PREDICTED).unwrap()
    }

    fn assert_near(actual: Result<f64, MetricError>, expected: f64) {
        let actual = actual.expect("score is defined");
        assert!(
            (actual - expected).abs() < 1.0e-12,
            "expected {expected}, got {actual}"
        );
    }

    #[test]
    fn the_matrix_counts_the_observed_label_union_in_sorted_order() {
        let matrix = three_class();
        assert_eq!(matrix.labels(), &[0, 1, 2]);
        assert_eq!(matrix.n_labels(), 3);
        assert_eq!(matrix.counts(), &[2, 0, 0, 1, 1, 1, 0, 1, 2]);
        assert_eq!(matrix.total(), 8);
        assert_eq!(matrix.counts().iter().sum::<u64>(), matrix.total());

        let class_one = matrix.class_counts(1).unwrap();
        assert_eq!(class_one.true_positives(), 1);
        assert_eq!(class_one.false_positives(), 1);
        assert_eq!(class_one.false_negatives(), 2);
        assert_eq!(class_one.support(), 3);
        assert_eq!(class_one.predicted(), 2);
        assert_eq!(matrix.class_counts(3), None);

        // A label absent from both inputs is absent from the matrix.
        let sparse = ConfusionMatrix::new(&[0, 7], &[7, 7]).unwrap();
        assert_eq!(sparse.labels(), &[0, 7]);
        assert_eq!(sparse.counts(), &[0, 1, 0, 1]);
    }

    #[test]
    fn micro_averaging_equals_accuracy_for_single_label_predictions() {
        for (expected, predicted) in [
            (&THREE_CLASS_EXPECTED[..], &THREE_CLASS_PREDICTED[..]),
            (&[0, 1, 2][..], &[0, 1, 1][..]),
            (&[0, 0, 1][..], &[0, 2, 1][..]),
            (&[3, 3, 3][..], &[3, 3, 3][..]),
            (&[0, 1][..], &[1, 0][..]),
        ] {
            let matrix = ConfusionMatrix::new(expected, predicted).unwrap();
            let accuracy = matrix.accuracy();
            assert_eq!(matrix.precision(Average::Micro), Ok(accuracy));
            assert_eq!(matrix.recall(Average::Micro), Ok(accuracy));
            assert_eq!(matrix.f1(Average::Micro), Ok(accuracy));
            assert_eq!(matrix.fbeta(2.0, Average::Micro), Ok(accuracy));
            assert_eq!(matrix.fbeta(0.5, Average::Micro), Ok(accuracy));
        }
    }

    #[test]
    fn binary_averaging_reproduces_the_binary_metrics_exactly() {
        for (expected, predicted) in [
            (&[0, 0, 1, 1][..], &[0, 1, 1, 1][..]),
            (&[0, 0, 0, 1, 1, 1][..], &[0, 1, 0, 1, 0, 1][..]),
            (&[1, 1][..], &[0, 1][..]),
            (&[0, 0][..], &[0, 1][..]),
            (&[0, 0][..], &[0, 0][..]),
        ] {
            let matrix = ConfusionMatrix::new(expected, predicted).unwrap();
            assert_eq!(
                matrix.precision(Average::Binary),
                precision_score(expected, predicted)
            );
            assert_eq!(
                matrix.recall(Average::Binary),
                recall_score(expected, predicted)
            );
            assert_eq!(matrix.f1(Average::Binary), f1_score(expected, predicted));
            assert_eq!(matrix.accuracy(), matrix.precision(Average::Micro).unwrap());
        }
    }

    #[test]
    fn a_wider_label_set_rejects_binary_averaging_instead_of_scoring_one_class() {
        let matrix = three_class();
        assert_eq!(
            matrix.precision(Average::Binary),
            Err(MetricError::NotBinary { labels: 3 })
        );
        assert_eq!(
            matrix.f1(Average::Binary),
            Err(MetricError::NotBinary { labels: 3 })
        );
        // An all-negative result is still a binary label set.
        let negative = ConfusionMatrix::new(&[0, 0], &[0, 0]).unwrap();
        assert_eq!(
            negative.recall(Average::Binary),
            Err(MetricError::Undefined)
        );
    }

    #[test]
    fn macro_and_weighted_averages_match_the_reference_conventions() {
        let matrix = three_class();
        assert_near(matrix.precision(Average::Macro), 0.611_111_111_111_111);
        assert_near(matrix.recall(Average::Macro), 0.666_666_666_666_666_6);
        assert_near(matrix.f1(Average::Macro), 0.622_222_222_222_222_2);
        assert_near(matrix.fbeta(2.0, Average::Macro), 0.644_300_144_300_144_3);
        assert_near(matrix.precision(Average::Weighted), 0.604_166_666_666_666_6);
        assert_near(matrix.recall(Average::Weighted), 0.625);
        assert_near(matrix.f1(Average::Weighted), 0.6);
        assert_near(
            matrix.fbeta(2.0, Average::Weighted),
            0.611_201_298_701_298_7,
        );

        // A class present only in the predictions carries no averaging weight.
        let absent_truth = ConfusionMatrix::new(&[0, 0, 1], &[0, 2, 1]).unwrap();
        assert_near(absent_truth.precision(Average::Weighted), 1.0);
        assert_near(absent_truth.f1(Average::Weighted), 0.777_777_777_777_777_7);
        assert_near(
            absent_truth.precision(Average::Macro),
            0.666_666_666_666_666_6,
        );
        assert_near(absent_truth.f1(Average::Macro), 0.555_555_555_555_555_5);
    }

    #[test]
    fn an_empty_denominator_is_an_error_until_the_caller_says_otherwise() {
        // Class 2 is never predicted, so it has no precision.
        let matrix = ConfusionMatrix::new(&[0, 1, 2], &[0, 1, 1]).unwrap();
        assert_eq!(
            matrix.precision(Average::Macro),
            Err(MetricError::Undefined)
        );
        assert_eq!(
            matrix.precision(Average::Weighted),
            Err(MetricError::Undefined)
        );
        for average in [Average::Macro, Average::Weighted] {
            assert_near(
                matrix.precision(Averaging::new(average).with_zero_division(ZeroDivision::Skip)),
                0.75,
            );
            assert_near(
                matrix.precision(Averaging::new(average).with_zero_division(ZeroDivision::Zero)),
                0.5,
            );
        }
        // Recall and F-score are defined for every observed label here.
        assert_near(matrix.recall(Average::Macro), 0.666_666_666_666_666_6);
        assert_near(matrix.f1(Average::Macro), 0.555_555_555_555_555_5);

        // Skipping every class leaves nothing to average.
        let nothing_predicted = ConfusionMatrix::new(&[0, 0], &[0, 0]).unwrap();
        assert_eq!(
            nothing_predicted
                .recall(Averaging::new(Average::Macro).with_zero_division(ZeroDivision::Skip)),
            Ok(1.0)
        );
        let absent = ConfusionMatrix::new(&[0, 0], &[1, 1]).unwrap();
        assert_eq!(
            absent.recall(Averaging::new(Average::Macro).with_zero_division(ZeroDivision::Skip)),
            Ok(0.0)
        );
    }

    #[test]
    fn beta_selects_between_precision_and_recall_and_rejects_invalid_values() {
        // Recall 1.0 beats precision 2/3, so a recall-heavy beta scores higher.
        let matrix = ConfusionMatrix::new(&[0, 0, 1, 1], &[0, 1, 1, 1]).unwrap();
        let precision_heavy = matrix.fbeta(0.5, Average::Binary).unwrap();
        let balanced = matrix.f1(Average::Binary).unwrap();
        let recall_heavy = matrix.fbeta(2.0, Average::Binary).unwrap();
        assert!(precision_heavy < balanced && balanced < recall_heavy);
        assert_near(Ok(recall_heavy), 0.909_090_909_090_909_1);
        assert_near(Ok(precision_heavy), 0.714_285_714_285_714_2);

        for beta in [0.0, -1.0, f64::NAN, f64::INFINITY] {
            assert_eq!(
                matrix.fbeta(beta, Average::Binary),
                Err(MetricError::InvalidBeta)
            );
        }
    }

    #[test]
    fn a_perfect_result_scores_one_under_every_average() {
        let matrix = ConfusionMatrix::new(&[0, 1, 2], &[0, 1, 2]).unwrap();
        for average in [Average::Micro, Average::Macro, Average::Weighted] {
            assert_eq!(matrix.precision(average), Ok(1.0));
            assert_eq!(matrix.recall(average), Ok(1.0));
            assert_eq!(matrix.f1(average), Ok(1.0));
        }
        assert_eq!(matrix.accuracy(), 1.0);
    }

    #[test]
    fn balanced_accuracy_is_macro_recall_over_the_classes_that_occur() {
        let matrix = three_class();
        assert_near(Ok(matrix.balanced_accuracy()), 0.666_666_666_666_666_6);
        assert_eq!(
            Ok(matrix.balanced_accuracy()),
            matrix.recall(Average::Macro)
        );

        // Class 2 is predicted but never true, so it has no recall to average.
        let absent_truth = ConfusionMatrix::new(&[0, 0, 1], &[0, 2, 1]).unwrap();
        assert_eq!(
            absent_truth.recall(Average::Macro),
            Err(MetricError::Undefined)
        );
        assert_near(Ok(absent_truth.balanced_accuracy()), 0.75);

        // Imbalance is what balanced accuracy exists to expose.
        let mut expected = vec![0_u8; 90];
        expected.extend(std::iter::repeat_n(1, 10));
        let majority = ConfusionMatrix::new(&expected, &[0_u8; 100]).unwrap();
        assert_eq!(majority.accuracy(), 0.9);
        assert_eq!(majority.balanced_accuracy(), 0.5);
    }

    #[test]
    fn matthews_correlation_spans_disagreement_to_agreement() {
        assert_near(
            three_class().matthews_correlation(),
            0.452_380_952_380_952_4,
        );
        assert_near(
            ConfusionMatrix::new(&[0, 0, 1, 1], &[0, 1, 1, 1])
                .unwrap()
                .matthews_correlation(),
            0.577_350_269_189_625_8,
        );
        assert_eq!(
            ConfusionMatrix::new(&[0, 1, 2], &[0, 1, 2])
                .unwrap()
                .matthews_correlation(),
            Ok(1.0)
        );
        assert_eq!(
            ConfusionMatrix::new(&[0, 1, 0, 1], &[1, 0, 1, 0])
                .unwrap()
                .matthews_correlation(),
            Ok(-1.0)
        );
        // A constant expected or predicted vector has no variance to correlate.
        for (expected, predicted) in [
            (&[0, 0][..], &[0, 0][..]),
            (&[0, 1][..], &[1, 1][..]),
            (&[1, 1][..], &[0, 1][..]),
        ] {
            assert_eq!(
                ConfusionMatrix::new(expected, predicted)
                    .unwrap()
                    .matthews_correlation(),
                Err(MetricError::Undefined)
            );
        }
    }

    #[test]
    fn matrix_construction_validates_before_counting() {
        assert_eq!(
            ConfusionMatrix::new(&[0], &[]),
            Err(MetricError::LengthMismatch {
                expected: 1,
                actual: 0,
            })
        );
        assert_eq!(ConfusionMatrix::new(&[], &[]), Err(MetricError::Empty));
    }
}