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
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
//! A fitted classifier wrapped in a fitted calibration map.

use crate::api::{
    Capabilities, Classifier, Estimator, HasCapabilities, ModelError, ProbabilisticClassifier,
};
use crate::data::{BinaryTargets, MatrixView};

use super::{
    Calibrator, IsotonicRegression, IsotonicRegressionParams, PlattCalibrator, PlattParams,
};

/// A fitted binary classifier whose probabilities are recalibrated.
///
/// The wrapper owns an already-fitted model and an already-fitted
/// [`Calibrator`], and is itself an ordinary [`Classifier`]: it composes with
/// the scorer, cross-validation, and permutation-importance paths without any
/// of them learning that calibration exists.
///
/// # What is calibrated
///
/// The score handed to the calibrator is the wrapped model's **positive-class
/// probability**, taken through
/// [`predict_class_proba_into`](ProbabilisticClassifier::predict_class_proba_into). That is
/// the one score the [`ProbabilisticClassifier`] contract requires, which is
/// what makes this wrapper work for a model FerricML does not ship. Platt's
/// original formulation calibrates a raw decision function instead;
/// `decision_function` is an inherent method of individual estimators rather
/// than part of the object-safe classifier contract, so a wrapper generic over
/// `C: ProbabilisticClassifier` cannot reach one. A monotone remap of the
/// probability is well defined for every classifier that produces one, and is
/// what makes calibrating a forest — the case that motivates the whole feature
/// — possible at all.
///
/// # Held-out calibration is the caller's explicit choice
///
/// The calibration rows are a parameter. Nothing here reuses the wrapped
/// model's training rows implicitly, because a calibrator fitted on the rows
/// its model already memorised measures that memory rather than the model's
/// probabilities: a model that separates its training data perfectly yields a
/// calibration sample with no overlap, and the fitted map is a step, not a
/// correction.
///
/// # Labels follow the calibrated probabilities
///
/// [`predict`](Classifier::predict) is the argmax of this model's *own*
/// probabilities, not a pass-through of the wrapped model's labels. A row whose
/// calibrated probability crosses `0.5` does change label, which is the point
/// of correcting an overconfident model. A classifier whose labels disagreed
/// with its own probabilities would be a silent wrong answer.
///
/// # Ranking is preserved only by a strictly increasing calibrator
///
/// Calibration is monotone, and monotone is weaker than ranking-preserving. The
/// [`Calibrator`] contract states the three cases; the one that matters here is
/// that a Platt fit whose [`slope`](PlattCalibrator::slope) is negative is a
/// strictly *decreasing* map, and reverses every pairwise comparison — a model
/// with ROC AUC `auc` becomes one with `1.0 - auc`. That is not a fitting
/// failure: it is the maximum-likelihood answer for a calibration sample whose
/// positive rows carry a lower mean score than its negative rows, which a small
/// held-out fold can easily be. Isotonic on such a fold pools instead, which
/// loses ordering rather than inverting it, and in the extreme is constant.
///
/// The fitted map is reachable through [`calibrator`](Self::calibrator), so a
/// caller that depends on ranking checks the sign it depends on:
///
/// ```
/// # use ferricml::api::ProbabilisticClassifier;
/// # use ferricml::calibration::{CalibratedClassifier, PlattParams};
/// # use ferricml::data::{BinaryTargets, DenseMatrix};
/// # use ferricml::linear_model::{LogisticRegression, LogisticRegressionParams};
/// # let values: Vec<f32> = (0..20).map(|index| index as f32 - 10.0).collect();
/// # let labels: Vec<u8> = (0..20).map(|index| u8::from(index >= 10)).collect();
/// # let data = DenseMatrix::new(values, 20, 1)?;
/// # let labels = BinaryTargets::new(labels)?;
/// # let model = LogisticRegression::fit(&data.as_view(), &labels, LogisticRegressionParams::default())?;
/// # let holdout = DenseMatrix::new(vec![-7.5_f32, -5.5, -3.5, -1.5, 1.5, 3.5, 5.5, 7.5], 8, 1)?;
/// # let holdout_labels = BinaryTargets::new(vec![0, 0, 0, 0, 1, 1, 1, 1])?;
/// let calibrated = CalibratedClassifier::fit_platt(
///     model,
///     &holdout.as_view(),
///     &holdout_labels,
///     PlattParams::default(),
/// )?;
/// assert!(
///     calibrated.calibrator().slope() > 0.0,
///     "this calibration fold inverts the model's ranking",
/// );
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ```
/// use ferricml::api::{Classifier, ProbabilisticClassifier};
/// use ferricml::calibration::{CalibratedClassifier, PlattParams};
/// use ferricml::data::{BinaryTargets, DenseMatrix};
/// use ferricml::linear_model::{LogisticRegression, LogisticRegressionParams};
/// use ferricml::metrics::roc_auc_score;
///
/// let values: Vec<f32> = (0..20).map(|index| index as f32 - 10.0).collect();
/// let labels: Vec<u8> = (0..20).map(|index| u8::from(index >= 10)).collect();
/// let data = DenseMatrix::new(values, 20, 1)?;
/// let labels = BinaryTargets::new(labels)?;
///
/// let model = LogisticRegression::fit(
///     &data.as_view(),
///     &labels,
///     LogisticRegressionParams::default(),
/// )?;
/// let before = model.predict_proba(&data.as_view())?;
///
/// // Calibration rows are always supplied explicitly, never taken from the
/// // wrapped model's own training rows implicitly. These are held out of the
/// // fit above, which is the workflow this module is written for.
/// let holdout = DenseMatrix::new(
///     vec![-7.5_f32, -5.5, -3.5, -1.5, 1.5, 3.5, 5.5, 7.5],
///     8,
///     1,
/// )?;
/// let holdout_labels = BinaryTargets::new(vec![0, 0, 0, 0, 1, 1, 1, 1])?;
/// let calibrated = CalibratedClassifier::fit_platt(
///     model,
///     &holdout.as_view(),
///     &holdout_labels,
///     PlattParams::default(),
/// )?;
/// let after = calibrated.predict_proba(&data.as_view())?;
///
/// // The composition is an ordinary Classifier, so it reaches scoring,
/// // cross-validation and permutation importance unchanged.
/// assert_eq!(calibrated.classes(), &[0, 1]);
///
/// // This fold fitted a positive slope, so the map is strictly increasing and
/// // cannot reorder two rows: every threshold-sweeping score is unchanged.
/// // The condition is asserted rather than assumed, because it is the
/// // condition — a fold whose positive rows score below its negative rows
/// // fits a negative slope, and that map takes ROC AUC to `1.0 - auc`.
/// assert!(calibrated.calibrator().slope() > 0.0);
///
/// // Scored against labels the model does not reproduce exactly, so the two
/// // AUCs are strictly between 0.5 and 1 and could disagree if they were free
/// // to. Two of the twenty labels are flipped.
/// let noisy = BinaryTargets::new(
///     (0..20).map(|index| u8::from((index >= 10) != (index == 8 || index == 11))).collect(),
/// )?;
/// let positive_before: Vec<f32> = before.chunks(2).map(|row| row[1]).collect();
/// let positive_after: Vec<f32> = after.chunks(2).map(|row| row[1]).collect();
/// let raw = roc_auc_score(noisy.as_slice(), &positive_before)?;
/// assert!(raw > 0.5 && raw < 1.0);
/// assert_eq!(raw, roc_auc_score(noisy.as_slice(), &positive_after)?);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct CalibratedClassifier<C, K> {
    inner: C,
    calibrator: K,
}

impl<C: ProbabilisticClassifier, K: Calibrator> CalibratedClassifier<C, K> {
    /// Composes an already-fitted model with an already-fitted calibrator.
    ///
    /// The wrapped model must be binary over classes `[0, 1]`: the calibration
    /// map is univariate, so a wider class set is
    /// [`ModelError::MulticlassOutput`] rather than a silently calibrated
    /// column.
    pub fn new(inner: C, calibrator: K) -> Result<Self, ModelError> {
        validate_binary_inner(&inner)?;
        Ok(Self { inner, calibrator })
    }

    /// Returns the wrapped fitted model.
    pub const fn inner(&self) -> &C {
        &self.inner
    }

    /// Returns the fitted calibration map.
    pub const fn calibrator(&self) -> &K {
        &self.calibrator
    }

    /// Consumes the wrapper and returns its two fitted parts.
    pub fn into_parts(self) -> (C, K) {
        (self.inner, self.calibrator)
    }

    /// Predicts labels into caller-owned label and score storage.
    ///
    /// This is the allocation-free form of [`Classifier::predict_into`], which
    /// has nowhere in its signature to put the intermediate scores and so
    /// allocates them. `scores` must hold one value per row and is left holding
    /// this model's calibrated positive-class probabilities.
    pub fn predict_into_with(
        &self,
        data: &MatrixView<'_>,
        scores: &mut [f32],
        output: &mut [u8],
    ) -> Result<(), ModelError> {
        if output.len() != data.rows() {
            return Err(ModelError::OutputLength {
                expected: data.rows(),
                actual: output.len(),
            });
        }
        self.positive_probabilities_into(data, scores)?;
        for (label, &probability) in output.iter_mut().zip(scores.iter()) {
            // The same comparison `predict_proba`'s argmax would make, on the
            // same two values, so labels cannot disagree with probabilities.
            *label = u8::from(probability > 1.0 - probability);
        }
        Ok(())
    }

    /// Rejects a batch whose width differs from the wrapped model's.
    ///
    /// The wrapper's own entry points run this before allocating any scratch or
    /// output storage. The wrapped model repeats the check when it is reached,
    /// and reports the same error from the same two numbers, so this changes
    /// when a mismatch is noticed rather than what is reported.
    fn check_batch_width(&self, data: &MatrixView<'_>) -> Result<(), ModelError> {
        let expected = self.inner.n_features_in();
        if data.columns() != expected {
            return Err(ModelError::FeatureDimension {
                expected,
                actual: data.columns(),
            });
        }
        Ok(())
    }

    /// Writes the calibrated positive-class probability of each row.
    ///
    /// # Why the clamp is here
    ///
    /// This is the one routine every probability path on the wrapper reaches —
    /// [`predict_proba_into`](ProbabilisticClassifier::predict_proba_into),
    /// [`predict_class_proba_into`](ProbabilisticClassifier::predict_class_proba_into),
    /// and [`predict_into`](Classifier::predict_into) through
    /// `predict_into_with` — so it is the boundary that produces the value, and
    /// rule 5 of [the accumulation policy](crate::numeric) puts saturation
    /// there rather than in a consumer.
    ///
    /// [`Calibrator::calibrate`] documents a result in `0.0..=1.0`, and that is
    /// a promise the wrapper cannot delegate. The trait is deliberately open,
    /// so a caller-supplied calibrator can return anything at all; and one
    /// *shipped* calibrator can too, because [`IsotonicRegression`] implements
    /// [`Calibrator`] for both of its constructors while only
    /// `fit_calibration` averages `0`/`1` labels. An `IsotonicRegression`
    /// fitted through [`Regressor`](crate::api::Regressor) over unbounded
    /// targets composes here through [`Self::new`] and its map is then a
    /// regression surface, not a probability.
    ///
    /// Nothing detected that, because a row is written as `[1 - p, p]` and so
    /// sums to exactly one whatever `p` is: the battery's row-sum obligation
    /// passes on `[-48.0, 49.0]`. Clamping first makes the row sum and the
    /// per-slot bound describe the same value.
    ///
    /// The clamp is a no-op for both fitted compositions —
    /// [`Self::fit_isotonic`] and [`Self::fit_platt`] already produce
    /// probabilities in range — so no fitted value moves.
    fn positive_probabilities_into(
        &self,
        data: &MatrixView<'_>,
        output: &mut [f32],
    ) -> Result<(), ModelError> {
        self.inner.predict_class_proba_into(data, 1, output)?;
        self.calibrator.calibrate_in_place(output);
        for slot in output.iter_mut() {
            *slot = slot.clamp(0.0, 1.0);
        }
        Ok(())
    }
}

impl<C: ProbabilisticClassifier> CalibratedClassifier<C, IsotonicRegression> {
    /// Fits an isotonic calibration map on caller-supplied held-out rows.
    pub fn fit_isotonic(
        inner: C,
        data: &MatrixView<'_>,
        targets: &BinaryTargets,
        params: IsotonicRegressionParams,
    ) -> Result<Self, ModelError> {
        let scores = calibration_scores(&inner, data, targets)?;
        let calibrator = IsotonicRegression::fit_calibration(&scores, targets, params)?;
        Ok(Self { inner, calibrator })
    }
}

impl<C: ProbabilisticClassifier> CalibratedClassifier<C, PlattCalibrator> {
    /// Fits a Platt calibration map on caller-supplied held-out rows.
    pub fn fit_platt(
        inner: C,
        data: &MatrixView<'_>,
        targets: &BinaryTargets,
        params: PlattParams,
    ) -> Result<Self, ModelError> {
        let scores = calibration_scores(&inner, data, targets)?;
        let calibrator = PlattCalibrator::fit(&scores, targets, params)?;
        Ok(Self { inner, calibrator })
    }

    /// Writes one raw calibrated decision score per row.
    ///
    /// This is the score whose logistic squashing is
    /// [`predict_class_proba`](ProbabilisticClassifier::predict_class_proba) for class `1`.
    /// It exists only on a Platt-calibrated model, which is exactly what
    /// [`Capabilities::decision_function`] declares.
    pub fn decision_function_into(
        &self,
        data: &MatrixView<'_>,
        output: &mut [f32],
    ) -> Result<(), ModelError> {
        self.inner.predict_class_proba_into(data, 1, output)?;
        for slot in output.iter_mut() {
            *slot = self.calibrator.decision_score(*slot);
        }
        Ok(())
    }

    /// Returns one raw calibrated decision score per row.
    pub fn decision_function(&self, data: &MatrixView<'_>) -> Result<Vec<f32>, ModelError> {
        self.check_batch_width(data)?;
        let mut output = vec![0.0; data.rows()];
        self.decision_function_into(data, &mut output)?;
        Ok(output)
    }
}

/// Validates the calibration sample and returns the wrapped model's scores.
///
/// Every shape check happens before the score buffer is allocated, so a
/// rejected call does no work and writes nothing.
fn calibration_scores<C: ProbabilisticClassifier>(
    inner: &C,
    data: &MatrixView<'_>,
    targets: &BinaryTargets,
) -> Result<Vec<f32>, ModelError> {
    validate_binary_inner(inner)?;
    if data.rows() == 0 || data.columns() == 0 {
        return Err(ModelError::EmptyData);
    }
    if data.columns() != inner.n_features_in() {
        return Err(ModelError::FeatureDimension {
            expected: inner.n_features_in(),
            actual: data.columns(),
        });
    }
    if data.rows() != targets.len() {
        return Err(ModelError::TargetLength {
            rows: data.rows(),
            targets: targets.len(),
        });
    }
    let mut scores = vec![0.0; data.rows()];
    inner.predict_class_proba_into(data, 1, &mut scores)?;
    Ok(scores)
}

/// Rejects a wrapped model whose class set the univariate map cannot address.
fn validate_binary_inner<C: Classifier>(inner: &C) -> Result<(), ModelError> {
    match inner.classes() {
        [0, 1] => Ok(()),
        classes if classes.len() < 2 => Err(ModelError::RequiresTwoClasses),
        classes if classes.len() > 2 => Err(ModelError::MulticlassOutput {
            columns: classes.len(),
        }),
        // Two classes, but the positive one is not label `1`, so there is no
        // positive-class probability column to calibrate.
        _ => Err(ModelError::UnknownClass { class: 1 }),
    }
}

impl<C: ProbabilisticClassifier, K: Calibrator> Estimator for CalibratedClassifier<C, K> {
    fn n_features_in(&self) -> usize {
        self.inner.n_features_in()
    }
}

impl<C: ProbabilisticClassifier> HasCapabilities for CalibratedClassifier<C, IsotonicRegression> {
    /// Calibrated probabilities, and deliberately not the wrapped model's
    /// other declarations.
    ///
    /// Producing a probability is the whole point of the wrapper, so
    /// `probability` is declared here whatever the wrapped model declares.
    /// Everything else is declared away structurally rather than intersected,
    /// because an intersection would have promised an entry point that does not
    /// exist on the wrapper at all: the composition owns already-fitted parts,
    /// so it has no weighted fitting entry point whatever the wrapped model can
    /// do; it has no artifact kind; and its own `fit` takes binary targets, so
    /// it offers no multiclass entry point either. `decision_function` is the
    /// one field that varies between the two calibrators, and it is absent here
    /// — see the Platt composition below.
    const CAPABILITIES: Capabilities = Capabilities::NONE.with_probability(true);
}

impl<C: ProbabilisticClassifier> HasCapabilities for CalibratedClassifier<C, PlattCalibrator> {
    /// A raw decision score, which the parametric calibrator genuinely has.
    ///
    /// `slope * score + intercept` is a real-valued score whose sigmoid is the
    /// calibrated probability. The isotonic composition has no such score — its
    /// map is a piecewise-linear step, not a squashed line — which is what
    /// makes this declaration vary rather than being a constant dressed up as
    /// a capability.
    const CAPABILITIES: Capabilities = Capabilities::NONE
        .with_decision_function(true)
        .with_probability(true);
}

impl<C: ProbabilisticClassifier, K: Calibrator> Classifier for CalibratedClassifier<C, K> {
    fn classes(&self) -> &[u8] {
        // Construction rejected anything other than `[0, 1]`, so this is the
        // wrapped model's own class list rather than a second copy that could
        // drift from it.
        self.inner.classes()
    }

    fn predict_into(&self, data: &MatrixView<'_>, output: &mut [u8]) -> Result<(), ModelError> {
        if output.len() != data.rows() {
            return Err(ModelError::OutputLength {
                expected: data.rows(),
                actual: output.len(),
            });
        }
        // This is an `_into` method, so the scratch buffer below is the only
        // allocation it makes at all. A batch it will refuse must not pay for
        // it.
        self.check_batch_width(data)?;
        let mut scores = vec![0.0; data.rows()];
        self.predict_into_with(data, &mut scores, output)
    }
}

impl<C: ProbabilisticClassifier, K: Calibrator> ProbabilisticClassifier
    for CalibratedClassifier<C, K>
{
    fn predict_proba_into(
        &self,
        data: &MatrixView<'_>,
        output: &mut [f32],
    ) -> Result<(), ModelError> {
        let rows = data.rows();
        let expected = rows
            .checked_mul(2)
            .ok_or(ModelError::OutputShapeOverflow { rows, columns: 2 })?;
        if output.len() != expected {
            return Err(ModelError::OutputLength {
                expected,
                actual: output.len(),
            });
        }
        // Park the calibrated positive probabilities in the upper half, then
        // expand forwards into both columns. Row `i` writes slots `2i` and
        // `2i + 1` and every still-unread source sits at `rows + j` for
        // `j > i`, which is strictly above `2i + 1` while `i < rows`. So the
        // whole matrix is produced without a second buffer.
        self.positive_probabilities_into(data, &mut output[rows..])?;
        for index in 0..rows {
            let positive = output[rows + index];
            output[2 * index] = 1.0 - positive;
            output[2 * index + 1] = positive;
        }
        Ok(())
    }

    fn predict_class_proba_into(
        &self,
        data: &MatrixView<'_>,
        class: u8,
        output: &mut [f32],
    ) -> Result<(), ModelError> {
        // Width before class: the shape of the input is validated before the
        // content of the request. The wrapped model repeats the check when it
        // is reached, so this changes when a mismatch is noticed rather than
        // what is reported — but it is what makes this primitive report the
        // same error its allocating partner does.
        self.check_batch_width(data)?;
        if class > 1 {
            return Err(ModelError::UnknownClass { class });
        }
        self.positive_probabilities_into(data, output)?;
        if class == 0 {
            for slot in output.iter_mut() {
                *slot = 1.0 - *slot;
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::{DenseMatrix, RegressionTargets};
    use crate::dummy::{DummyClassifier, DummyClassifierParams};

    /// A classifier whose positive probability is a fixed function of column 0.
    ///
    /// Wrapping a stub rather than a shipped estimator is the point: the
    /// wrapper is generic over the public contract, so this file proves it
    /// against the contract and nothing else.
    #[derive(Clone, Debug, PartialEq)]
    struct Overconfident {
        classes: Vec<u8>,
    }

    impl Overconfident {
        fn binary() -> Self {
            Self {
                classes: vec![0, 1],
            }
        }

        fn probability(row: &[f32]) -> f32 {
            (row[0] * 4.0 - 2.0).clamp(0.0, 1.0)
        }
    }

    impl Estimator for Overconfident {
        fn n_features_in(&self) -> usize {
            1
        }
    }

    impl Classifier for Overconfident {
        fn classes(&self) -> &[u8] {
            &self.classes
        }

        fn predict_into(&self, data: &MatrixView<'_>, output: &mut [u8]) -> Result<(), ModelError> {
            self.validate(data, output.len())?;
            for (slot, row) in output.iter_mut().zip(data.iter_rows()) {
                *slot = u8::from(Self::probability(row) > 0.5);
            }
            Ok(())
        }
    }

    impl ProbabilisticClassifier for Overconfident {
        fn predict_proba_into(
            &self,
            data: &MatrixView<'_>,
            output: &mut [f32],
        ) -> Result<(), ModelError> {
            self.validate(data, output.len() / 2)?;
            if output.len() != data.rows() * 2 {
                return Err(ModelError::OutputLength {
                    expected: data.rows() * 2,
                    actual: output.len(),
                });
            }
            for (slots, row) in output.chunks_exact_mut(2).zip(data.iter_rows()) {
                let positive = Self::probability(row);
                slots[0] = 1.0 - positive;
                slots[1] = positive;
            }
            Ok(())
        }

        fn predict_class_proba_into(
            &self,
            data: &MatrixView<'_>,
            class: u8,
            output: &mut [f32],
        ) -> Result<(), ModelError> {
            self.validate(data, output.len())?;
            if !self.classes.contains(&class) {
                return Err(ModelError::UnknownClass { class });
            }
            for (slot, row) in output.iter_mut().zip(data.iter_rows()) {
                let positive = Self::probability(row);
                *slot = if class == 1 { positive } else { 1.0 - positive };
            }
            Ok(())
        }
    }

    impl Overconfident {
        fn validate(&self, data: &MatrixView<'_>, rows: usize) -> Result<(), ModelError> {
            if data.columns() != self.n_features_in() {
                return Err(ModelError::FeatureDimension {
                    expected: self.n_features_in(),
                    actual: data.columns(),
                });
            }
            if rows != data.rows() {
                return Err(ModelError::OutputLength {
                    expected: data.rows(),
                    actual: rows,
                });
            }
            Ok(())
        }
    }

    fn sample() -> (DenseMatrix, BinaryTargets) {
        let values: Vec<f32> = (0..24).map(|step| step as f32 / 23.0).collect();
        let labels: Vec<u8> = (0..24).map(|step| u8::from(step % 3 != 0)).collect();
        (
            DenseMatrix::new(values, 24, 1).unwrap(),
            BinaryTargets::new(labels).unwrap(),
        )
    }

    #[test]
    fn calibrated_probabilities_form_valid_rows_and_agree_with_their_columns() {
        let (data, labels) = sample();
        let model = CalibratedClassifier::fit_isotonic(
            Overconfident::binary(),
            &data.as_view(),
            &labels,
            IsotonicRegressionParams,
        )
        .unwrap();
        assert_eq!(model.classes(), &[0, 1]);
        assert_eq!(model.n_features_in(), 1);

        let matrix = model.predict_proba(&data.as_view()).unwrap();
        let positive = model.predict_class_proba(&data.as_view(), 1).unwrap();
        let negative = model.predict_class_proba(&data.as_view(), 0).unwrap();
        let predicted = model.predict(&data.as_view()).unwrap();
        assert_eq!(matrix.len(), data.rows() * 2);
        for (index, row) in matrix.chunks_exact(2).enumerate() {
            assert_eq!(row[1], positive[index]);
            assert_eq!(row[0], negative[index]);
            assert!((row[0] + row[1] - 1.0).abs() <= 1.0e-6, "row {index}");
            assert!((0.0..=1.0).contains(&row[1]));
            let expected = u8::from(row[1] > row[0]);
            assert_eq!(predicted[index], expected, "row {index}");
        }
    }

    #[test]
    fn the_allocation_free_paths_agree_with_the_allocating_ones() {
        let (data, labels) = sample();
        let model = CalibratedClassifier::fit_platt(
            Overconfident::binary(),
            &data.as_view(),
            &labels,
            PlattParams::default(),
        )
        .unwrap();
        let view = data.as_view();

        let mut matrix = vec![f32::MAX; data.rows() * 2];
        model.predict_proba_into(&view, &mut matrix).unwrap();
        assert_eq!(matrix, model.predict_proba(&view).unwrap());

        let mut labels_into = vec![u8::MAX; data.rows()];
        let mut scores = vec![f32::MAX; data.rows()];
        model
            .predict_into_with(&view, &mut scores, &mut labels_into)
            .unwrap();
        assert_eq!(labels_into, model.predict(&view).unwrap());
        assert_eq!(scores, model.predict_class_proba(&view, 1).unwrap());

        let mut decisions = vec![f32::MAX; data.rows()];
        model.decision_function_into(&view, &mut decisions).unwrap();
        assert_eq!(decisions, model.decision_function(&view).unwrap());
        for (&decision, &probability) in decisions.iter().zip(&scores) {
            assert_eq!(crate::numeric::sigmoid_f32(decision), probability);
        }
    }

    #[test]
    fn every_prediction_path_validates_before_writing() {
        let (data, labels) = sample();
        let model = CalibratedClassifier::fit_isotonic(
            Overconfident::binary(),
            &data.as_view(),
            &labels,
            IsotonicRegressionParams,
        )
        .unwrap();
        let wide = DenseMatrix::new(vec![0.5; data.rows() * 2], data.rows(), 2).unwrap();

        let mut sentinel = vec![f32::MAX; data.rows() * 2];
        assert_eq!(
            model.predict_proba_into(&wide.as_view(), &mut sentinel),
            Err(ModelError::FeatureDimension {
                expected: 1,
                actual: 2,
            })
        );
        assert!(sentinel.iter().all(|value| *value == f32::MAX));
        assert_eq!(
            model.predict_proba_into(&data.as_view(), &mut sentinel[..3]),
            Err(ModelError::OutputLength {
                expected: 48,
                actual: 3,
            })
        );
        assert!(sentinel.iter().all(|value| *value == f32::MAX));

        let mut labels_sentinel = vec![u8::MAX; data.rows()];
        assert_eq!(
            model.predict_into(&data.as_view(), &mut labels_sentinel[..2]),
            Err(ModelError::OutputLength {
                expected: 24,
                actual: 2,
            })
        );
        assert!(labels_sentinel.iter().all(|value| *value == u8::MAX));
        assert_eq!(
            model.predict_class_proba_into(&data.as_view(), 2, &mut sentinel[..data.rows()]),
            Err(ModelError::UnknownClass { class: 2 })
        );
        assert!(sentinel.iter().all(|value| *value == f32::MAX));
    }

    #[test]
    fn a_wrapped_model_the_map_cannot_address_is_rejected_at_construction() {
        let (data, labels) = sample();
        let single = DummyClassifier::fit(
            &data.as_view(),
            &BinaryTargets::new(vec![1; data.rows()]).unwrap(),
            DummyClassifierParams,
        )
        .unwrap();
        assert_eq!(single.classes(), &[1]);
        assert_eq!(
            CalibratedClassifier::fit_isotonic(
                single,
                &data.as_view(),
                &labels,
                IsotonicRegressionParams
            )
            .unwrap_err(),
            ModelError::RequiresTwoClasses
        );

        let wide = Overconfident {
            classes: vec![0, 1, 2],
        };
        assert_eq!(
            CalibratedClassifier::fit_isotonic(
                wide,
                &data.as_view(),
                &labels,
                IsotonicRegressionParams
            )
            .unwrap_err(),
            ModelError::MulticlassOutput { columns: 3 }
        );

        let relabelled = Overconfident {
            classes: vec![3, 7],
        };
        assert_eq!(
            CalibratedClassifier::fit_isotonic(
                relabelled,
                &data.as_view(),
                &labels,
                IsotonicRegressionParams
            )
            .unwrap_err(),
            ModelError::UnknownClass { class: 1 }
        );
    }

    #[test]
    fn fitting_validates_the_calibration_sample_before_predicting_on_it() {
        let (data, labels) = sample();
        let wide = DenseMatrix::new(vec![0.5; data.rows() * 2], data.rows(), 2).unwrap();
        assert_eq!(
            CalibratedClassifier::fit_isotonic(
                Overconfident::binary(),
                &wide.as_view(),
                &labels,
                IsotonicRegressionParams
            )
            .unwrap_err(),
            ModelError::FeatureDimension {
                expected: 1,
                actual: 2,
            }
        );
        assert_eq!(
            CalibratedClassifier::fit_isotonic(
                Overconfident::binary(),
                &data.as_view(),
                &BinaryTargets::new(vec![0, 1]).unwrap(),
                IsotonicRegressionParams,
            )
            .unwrap_err(),
            ModelError::TargetLength {
                rows: 24,
                targets: 2,
            }
        );
        assert_eq!(
            CalibratedClassifier::fit_isotonic(
                Overconfident::binary(),
                &data.as_view(),
                &BinaryTargets::new(vec![0; data.rows()]).unwrap(),
                IsotonicRegressionParams,
            )
            .unwrap_err(),
            ModelError::RequiresTwoClasses
        );
    }

    #[test]
    fn refitting_the_same_calibration_sample_reproduces_the_same_model() {
        let (data, labels) = sample();
        for _ in 0..2 {
            assert_eq!(
                CalibratedClassifier::fit_isotonic(
                    Overconfident::binary(),
                    &data.as_view(),
                    &labels,
                    IsotonicRegressionParams,
                )
                .unwrap(),
                CalibratedClassifier::fit_isotonic(
                    Overconfident::binary(),
                    &data.as_view(),
                    &labels,
                    IsotonicRegressionParams,
                )
                .unwrap()
            );
            assert_eq!(
                CalibratedClassifier::fit_platt(
                    Overconfident::binary(),
                    &data.as_view(),
                    &labels,
                    PlattParams::default()
                )
                .unwrap(),
                CalibratedClassifier::fit_platt(
                    Overconfident::binary(),
                    &data.as_view(),
                    &labels,
                    PlattParams::default()
                )
                .unwrap()
            );
        }
    }

    /// A composed calibrator cannot put a non-probability in a probability.
    ///
    /// [`Calibrator::calibrate`] documents a result in `0.0..=1.0`, and the
    /// wrapper's own contract is that `predict_proba` returns probabilities.
    /// Neither is something [`CalibratedClassifier`] can delegate: the trait is
    /// open by design, and even the shipped [`IsotonicRegression`] implements
    /// it for *both* its constructors, while only `fit_calibration` averages
    /// `0`/`1` labels. Fitted as an ordinary regressor over unbounded targets
    /// it is a regression surface, and [`Self::new`] accepts it.
    ///
    /// Measured before the clamp: `predict_proba` returned rows like
    /// `[38.71, -37.71]` — every one of 12 slots outside `0.0..=1.0`, worst
    /// excess `37.7`. The rows still summed to exactly `1.0`, which is the
    /// whole reason nothing caught it: the conformance battery's probability
    /// obligation checks the row sum, and `[1 - p, p]` sums to one for any `p`
    /// at all. `predict` reported class `0` for every row while the class-`1`
    /// column read `-37.7`.
    ///
    /// The three paths are asserted separately because each writes the row
    /// differently, and the isotonic map here is deliberately anti-monotone in
    /// its own second half so that both ends of the range are exercised rather
    /// than only the top.
    #[test]
    fn a_calibrator_that_is_not_a_probability_map_is_saturated_not_forwarded() {
        let (data, _) = sample();
        // An isotonic fit over unbounded regression targets: a legitimate
        // `Regressor`, and a `Calibrator` whose map leaves `0.0..=1.0` badly.
        let knots = DenseMatrix::new(vec![0.0_f32, 0.25, 0.5, 0.75, 1.0], 5, 1).unwrap();
        let unbounded = RegressionTargets::new(vec![-40.0_f32, -12.0, 0.5, 17.0, 49.0]).unwrap();
        let calibrator =
            IsotonicRegression::fit(&knots.as_view(), &unbounded, IsotonicRegressionParams)
                .unwrap();
        // Non-vacuity, stated rather than assumed: the map really does leave
        // the range on the values this model will feed it.
        assert!(calibrator.map(0.0) < 0.0 && calibrator.map(1.0) > 1.0);

        let model = CalibratedClassifier::new(Overconfident::binary(), calibrator).unwrap();
        let rows = data.rows();

        let probabilities = model.predict_proba(&data.as_view()).unwrap();
        assert_eq!(probabilities.len(), rows * 2);
        for (row, slots) in probabilities.chunks_exact(2).enumerate() {
            for (column, &value) in slots.iter().enumerate() {
                assert!(
                    (0.0..=1.0).contains(&value),
                    "row {row} column {column} is {value}, which is not a probability"
                );
            }
            // The row sum stays the obligation it always was — it is simply no
            // longer the only one, because it holds for any `[1 - p, p]`.
            assert!((slots.iter().sum::<f32>() - 1.0).abs() <= 2.0 * f32::EPSILON);
        }

        for class in [0_u8, 1] {
            let column = model.predict_class_proba(&data.as_view(), class).unwrap();
            for (row, &value) in column.iter().enumerate() {
                assert!(
                    (0.0..=1.0).contains(&value),
                    "class {class} row {row} is {value}"
                );
            }
            // The single-column path and the matrix path are the same numbers.
            for (row, &value) in column.iter().enumerate() {
                assert_eq!(value, probabilities[2 * row + usize::from(class)]);
            }
        }

        // A label still agrees with the probability it was read from.
        let labels = model.predict(&data.as_view()).unwrap();
        for (row, &label) in labels.iter().enumerate() {
            let positive = probabilities[2 * row + 1];
            assert_eq!(label, u8::from(positive > 1.0 - positive), "row {row}");
        }
    }

    /// Saturating the composition moves nothing that was already a probability.
    ///
    /// The clamp is only defensible if it is unobservable on the two fitted
    /// constructors, which are the ones every existing artifact and fixture
    /// came from. Asserted bit-for-bit against the calibrator's own map rather
    /// than to a tolerance.
    #[test]
    fn saturation_does_not_move_a_calibrator_that_was_already_in_range() {
        let (data, labels) = sample();
        let isotonic = CalibratedClassifier::fit_isotonic(
            Overconfident::binary(),
            &data.as_view(),
            &labels,
            IsotonicRegressionParams,
        )
        .unwrap();
        let platt = CalibratedClassifier::fit_platt(
            Overconfident::binary(),
            &data.as_view(),
            &labels,
            PlattParams::default(),
        )
        .unwrap();
        for (name, positives) in [
            (
                "isotonic",
                isotonic.predict_class_proba(&data.as_view(), 1).unwrap(),
            ),
            (
                "platt",
                platt.predict_class_proba(&data.as_view(), 1).unwrap(),
            ),
        ] {
            let mut raw = vec![0.0_f32; data.rows()];
            Overconfident::binary()
                .predict_class_proba_into(&data.as_view(), 1, &mut raw)
                .unwrap();
            for (row, &value) in positives.iter().enumerate() {
                let uncalibrated = if name == "isotonic" {
                    isotonic.calibrator.calibrate(raw[row])
                } else {
                    platt.calibrator.calibrate(raw[row])
                };
                assert_eq!(
                    value.to_bits(),
                    uncalibrated.to_bits(),
                    "{name} row {row}: saturation moved a value that was in range"
                );
            }
        }
    }

    #[test]
    fn the_composition_declares_only_what_it_really_offers() {
        assert_eq!(
            <CalibratedClassifier<Overconfident, IsotonicRegression> as HasCapabilities>::CAPABILITIES,
            Capabilities::NONE.with_probability(true)
        );
        assert_eq!(
            <CalibratedClassifier<Overconfident, PlattCalibrator> as HasCapabilities>::CAPABILITIES,
            Capabilities::NONE
                .with_decision_function(true)
                .with_probability(true)
        );
    }
}