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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
use super::super::forest::model::Forest;
use super::*;
use crate::api::ModelError;
use crate::artifact::{
    ArtifactError, ModelArtifact, RANDOM_FOREST_CLASSIFIER_ARTIFACT_KIND,
    RANDOM_FOREST_REGRESSOR_ARTIFACT_KIND,
};
use crate::data::{BinaryTargets, ClassTargets, DenseMatrix, RegressionTargets, SampleWeights};
use crate::ensemble::HistGradientBoostingRegressor;
use crate::ensemble::NJobs;
use crate::linear_model::Ridge;
use crate::tree::MaxFeatures;
use crate::tree::{FEATURE_MASK, LEFT_IS_LEAF, PackedNode, RIGHT_IS_LEAF};
use sha2::{Digest, Sha256};

fn matrix(rows: &[&[f32]]) -> DenseMatrix {
    let cols = rows.first().map_or(0, |row| row.len());
    assert!(rows.iter().all(|row| row.len() == cols));
    let values = rows.iter().flat_map(|row| row.iter().copied()).collect();
    DenseMatrix::new(values, rows.len(), cols).unwrap()
}

fn classifier_params(random_state: u64) -> RandomForestClassifierParams {
    RandomForestClassifierParams::default()
        .with_n_estimators(31)
        .with_max_depth(Some(8))
        .with_max_features(MaxFeatures::All)
        .with_random_state(random_state)
}

fn regressor_params(random_state: u64) -> RandomForestRegressorParams {
    RandomForestRegressorParams::default()
        .with_n_estimators(31)
        .with_max_depth(Some(8))
        .with_max_features(MaxFeatures::All)
        .with_random_state(random_state)
}

#[test]
fn classifies_separable_data_and_probabilities_are_bounded() {
    let x = matrix(&[
        &[-3.0],
        &[-2.0],
        &[-1.0],
        &[-0.5],
        &[0.5],
        &[1.0],
        &[2.0],
        &[3.0],
    ]);
    let y = BinaryTargets::new(vec![0, 0, 0, 0, 1, 1, 1, 1]).unwrap();
    let forest = RandomForestClassifier::fit(&x.as_view(), &y, classifier_params(4)).unwrap();
    let mut predictions = vec![0.0; x.rows()];
    forest
        .predict_positive_proba_into(&x.as_view(), &mut predictions)
        .unwrap();
    assert!(predictions.iter().all(|&p| (0.0..=1.0).contains(&p)));
    assert!(predictions[..4].iter().all(|&p| p < 0.5));
    assert!(predictions[4..].iter().all(|&p| p > 0.5));
}

#[test]
fn nonlinear_forest_learns_repeated_xor() {
    let x = matrix(&[
        &[0.0, 0.0],
        &[0.0, 1.0],
        &[1.0, 0.0],
        &[1.0, 1.0],
        &[0.0, 0.0],
        &[0.0, 1.0],
        &[1.0, 0.0],
        &[1.0, 1.0],
        &[0.0, 0.0],
        &[0.0, 1.0],
        &[1.0, 0.0],
        &[1.0, 1.0],
    ]);
    let y = BinaryTargets::new(vec![0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0]).unwrap();
    let cfg = classifier_params(99).with_n_estimators(101);
    let forest = RandomForestClassifier::fit(&x.as_view(), &y, cfg).unwrap();
    for (row, &expected) in y.as_slice().iter().take(4).enumerate() {
        let p = forest
            .predict_positive_proba_one(x.row(row).unwrap())
            .unwrap();
        assert_eq!(p >= 0.5, expected == 1, "row {row}: {p}");
    }
}

#[test]
fn regresses_piecewise_values() {
    let x = matrix(&[
        &[-3.0],
        &[-2.0],
        &[-1.0],
        &[0.0],
        &[1.0],
        &[2.0],
        &[3.0],
        &[4.0],
    ]);
    let y = RegressionTargets::new(vec![-6.0, -4.0, -2.0, 0.0, 2.0, 4.0, 6.0, 8.0]).unwrap();
    let cfg = regressor_params(7).with_n_estimators(61);
    let forest = RandomForestRegressor::fit(&x.as_view(), &y, cfg).unwrap();
    let mut output = vec![0.0; x.rows()];
    forest.predict_into(&x.as_view(), &mut output).unwrap();
    let mae = output
        .iter()
        .zip(y.as_slice())
        .map(|(a, b)| (a - b).abs())
        .sum::<f32>()
        / output.len() as f32;
    assert!(mae < 1.5, "mae={mae}, predictions={output:?}");
}

#[test]
fn reference_defaults_distinguish_classification_and_regression() {
    assert_eq!(
        RandomForestClassifierParams::default().max_features(),
        MaxFeatures::Sqrt
    );
    assert_eq!(
        RandomForestRegressorParams::default().max_features(),
        MaxFeatures::All
    );
}

#[test]
fn exact_classifier_split_and_leaf_probabilities_match_the_oracle() {
    let x = matrix(&[&[0.0], &[1.0], &[2.0], &[3.0]]);
    let y = BinaryTargets::new(vec![0, 0, 1, 1]).unwrap();
    let cfg = RandomForestClassifierParams::default()
        .with_n_estimators(1)
        .with_bootstrap(false)
        .with_max_features(MaxFeatures::All);
    let forest = RandomForestClassifier::fit(&x.as_view(), &y, cfg).unwrap();
    let tree = &forest.binary_trees()[0];
    assert_eq!(tree.nodes.len(), 1);
    let root = &tree.nodes[0];
    assert_eq!(root.feature_and_flags & FEATURE_MASK, 0);
    assert_eq!(root.threshold, 1.5);
    assert_ne!(root.feature_and_flags & LEFT_IS_LEAF, 0);
    assert_ne!(root.feature_and_flags & RIGHT_IS_LEAF, 0);
    assert_eq!(f32::from_bits(root.left), 0.0);
    assert_eq!(f32::from_bits(root.right), 1.0);

    let leaf_cfg = RandomForestClassifierParams::default()
        .with_n_estimators(1)
        .with_bootstrap(false)
        .with_min_samples_split(5);
    let leaf = RandomForestClassifier::fit(&x.as_view(), &y, leaf_cfg).unwrap();
    assert_eq!(leaf.predict_positive_proba_one(&[100.0]).unwrap(), 0.5);
}

#[test]
fn exact_regression_leaf_is_the_target_mean() {
    let x = matrix(&[&[0.0], &[1.0], &[2.0], &[3.0]]);
    let y = RegressionTargets::new(vec![1.0, 2.0, 3.0, 4.0]).unwrap();
    let cfg = RandomForestRegressorParams::default()
        .with_n_estimators(1)
        .with_bootstrap(false)
        .with_min_samples_split(5);
    let forest = RandomForestRegressor::fit(&x.as_view(), &y, cfg).unwrap();
    assert_eq!(forest.predict_one(&[-50.0]).unwrap(), 2.5);

    let stump_cfg = RandomForestRegressorParams::default()
        .with_n_estimators(1)
        .with_bootstrap(false)
        .with_max_depth(Some(1));
    let stump = RandomForestRegressor::fit(&x.as_view(), &y, stump_cfg).unwrap();
    assert_eq!(stump.core.trees[0].nodes[0].threshold, 1.5);
    assert_eq!(stump.predict_one(&[-50.0]).unwrap(), 1.5);
    assert_eq!(stump.predict_one(&[100.0]).unwrap(), 3.5);
}

#[test]
fn model_is_identical_across_repeats_and_thread_counts() {
    let x = matrix(&[
        &[0.0, 3.0],
        &[1.0, 2.0],
        &[2.0, 1.0],
        &[3.0, 0.0],
        &[4.0, 7.0],
        &[5.0, 6.0],
        &[6.0, 5.0],
        &[7.0, 4.0],
    ]);
    let y = BinaryTargets::new(vec![0, 1, 1, 0, 1, 0, 0, 1]).unwrap();
    let one = RandomForestClassifier::fit(&x.as_view(), &y, classifier_params(123)).unwrap();
    let repeat = RandomForestClassifier::fit(&x.as_view(), &y, classifier_params(123)).unwrap();
    let parallel_config = classifier_params(123).with_n_jobs(crate::ensemble::NJobs::Count(4));
    let parallel = RandomForestClassifier::fit(&x.as_view(), &y, parallel_config).unwrap();
    assert_eq!(one.to_bytes(), repeat.to_bytes());
    assert_eq!(one.to_bytes(), parallel.to_bytes());
}

#[test]
fn packed_classifier_and_regressor_encoding_is_deterministic() {
    let x = matrix(&[
        &[0.0, 3.0],
        &[1.0, 2.0],
        &[2.0, 1.0],
        &[3.0, 0.0],
        &[4.0, 7.0],
        &[5.0, 6.0],
        &[6.0, 5.0],
        &[7.0, 4.0],
    ]);
    let classifier = RandomForestClassifier::fit(
        &x.as_view(),
        &BinaryTargets::new(vec![0, 1, 1, 0, 1, 0, 0, 1]).unwrap(),
        classifier_params(123),
    )
    .unwrap();
    let regressor = RandomForestRegressor::fit(
        &x.as_view(),
        &RegressionTargets::new(vec![0.0, 1.0, 1.5, 2.5, 8.0, 7.0, 6.0, 5.0]).unwrap(),
        regressor_params(123),
    )
    .unwrap();
    let regressor_repeat = RandomForestRegressor::fit(
        &x.as_view(),
        &RegressionTargets::new(vec![0.0, 1.0, 1.5, 2.5, 8.0, 7.0, 6.0, 5.0]).unwrap(),
        regressor_params(123),
    )
    .unwrap();
    assert_eq!(regressor.to_bytes(), regressor_repeat.to_bytes());

    // Non-empty and self-consistent, but deliberately not frozen: FerricML is
    // pre-1.0 and does not yet promise byte stability for packed trees.
    for (name, bytes) in [
        ("classifier", classifier.to_bytes()),
        ("regressor", regressor.to_bytes()),
    ] {
        assert!(!bytes.is_empty(), "{name} packed bytes are empty");
    }
}

#[test]
fn bootstrap_and_seed_are_deterministic_but_seed_affects_model() {
    let x = matrix(&[&[0.0], &[1.0], &[2.0], &[3.0], &[4.0], &[5.0]]);
    let y = BinaryTargets::new(vec![0, 1, 0, 1, 0, 1]).unwrap();
    let a = RandomForestClassifier::fit(&x.as_view(), &y, classifier_params(8)).unwrap();
    let b = RandomForestClassifier::fit(&x.as_view(), &y, classifier_params(8)).unwrap();
    let c = RandomForestClassifier::fit(&x.as_view(), &y, classifier_params(9)).unwrap();
    assert_eq!(a.to_bytes(), b.to_bytes());
    assert_ne!(a.to_bytes(), c.to_bytes());
}

// ------------------------------------------------------------ sample weights

/// Eight rows of exactly representable features and integer targets.
///
/// Every weighted accumulation over this fixture is a sum of small integers
/// scaled by small integers, so the weighted and duplicated arithmetic below is
/// exact rather than merely close. That is what lets the equivalences be
/// asserted on fitted bytes instead of on a tolerance.
fn weight_fixture() -> (DenseMatrix, Vec<u8>, Vec<f32>) {
    let x = matrix(&[
        &[0.0, 3.0],
        &[1.0, 2.0],
        &[2.0, 1.0],
        &[3.0, 0.0],
        &[4.0, 7.0],
        &[5.0, 6.0],
        &[6.0, 5.0],
        &[7.0, 4.0],
    ]);
    let labels = vec![0, 1, 1, 0, 1, 0, 0, 1];
    let values = vec![0.0, 1.0, 2.0, 3.0, 8.0, 7.0, 6.0, 5.0];
    (x, labels, values)
}

/// The same fixture with row `repeat` present `times` times, in place.
fn duplicated_fixture(repeat: usize, times: usize) -> (DenseMatrix, Vec<u8>, Vec<f32>) {
    let (x, labels, values) = weight_fixture();
    let mut rows = Vec::new();
    let mut duplicated_labels = Vec::new();
    let mut duplicated_values = Vec::new();
    for row in 0..labels.len() {
        let copies = if row == repeat { times } else { 1 };
        for _ in 0..copies {
            rows.extend_from_slice(x.as_view().row(row).unwrap());
            duplicated_labels.push(labels[row]);
            duplicated_values.push(values[row]);
        }
    }
    let columns = x.as_view().columns();
    let matrix = DenseMatrix::new(rows, duplicated_labels.len(), columns).unwrap();
    (matrix, duplicated_labels, duplicated_values)
}

/// Weights of exactly one must not perturb one bit of the unweighted fit.
///
/// Asserted on the packed model bytes and on the whole fitted value, not on
/// predictions: a difference small enough to leave every prediction unchanged
/// would still mean the weighted path is a second implementation of the
/// unweighted one, which is exactly what the declaration promises it is not.
#[test]
fn unit_weights_reproduce_the_unweighted_fit_bit_for_bit() {
    let (x, labels, values) = weight_fixture();
    let binary = BinaryTargets::new(labels.clone()).unwrap();
    let classes = ClassTargets::new(labels).unwrap();
    let regression = RegressionTargets::new(values).unwrap();
    let ones = SampleWeights::new(vec![1.0; x.as_view().rows()]).unwrap();

    for bootstrap in [false, true] {
        let classifier_params = classifier_params(123).with_bootstrap(bootstrap);
        let regressor_params = regressor_params(123).with_bootstrap(bootstrap);

        let plain =
            RandomForestClassifier::fit(&x.as_view(), &binary, classifier_params.clone()).unwrap();
        let weighted = RandomForestClassifier::fit_weighted(
            &x.as_view(),
            &binary,
            &ones,
            classifier_params.clone(),
        )
        .unwrap();
        assert_eq!(
            plain.to_bytes(),
            weighted.to_bytes(),
            "binary classifier, bootstrap = {bootstrap}"
        );
        assert_eq!(plain, weighted);

        let plain = RandomForestClassifier::fit_multiclass(
            &x.as_view(),
            &classes,
            classifier_params.clone(),
        )
        .unwrap();
        let weighted = RandomForestClassifier::fit_multiclass_weighted(
            &x.as_view(),
            &classes,
            &ones,
            classifier_params,
        )
        .unwrap();
        assert_eq!(
            plain, weighted,
            "multiclass classifier, bootstrap = {bootstrap}"
        );

        let plain = RandomForestRegressor::fit(&x.as_view(), &regression, regressor_params.clone())
            .unwrap();
        let weighted =
            RandomForestRegressor::fit_weighted(&x.as_view(), &regression, &ones, regressor_params)
                .unwrap();
        assert_eq!(
            plain.to_bytes(),
            weighted.to_bytes(),
            "regressor, bootstrap = {bootstrap}"
        );
        assert_eq!(
            plain.to_artifact([5; 32]).unwrap(),
            weighted.to_artifact([5; 32]).unwrap()
        );
    }
}

/// An integer weight is the same fit as repeating the row that many times.
///
/// Bootstrapping is off, because a resample draws from the rows it is given:
/// adding rows changes what is drawn, so the two datasets would stop being the
/// same training problem before any weight was applied.
#[test]
fn an_integer_weight_is_the_same_fit_as_repeating_the_row() {
    let repeat = 2;
    let times = 3;
    let (x, labels, values) = weight_fixture();
    let (duplicated_x, duplicated_labels, duplicated_values) = duplicated_fixture(repeat, times);
    let mut weights = vec![1.0_f32; labels.len()];
    weights[repeat] = times as f32;
    let weights = SampleWeights::new(weights).unwrap();

    let classifier_params = classifier_params(123).with_bootstrap(false);
    let regressor_params = regressor_params(123).with_bootstrap(false);

    let weighted = RandomForestClassifier::fit_weighted(
        &x.as_view(),
        &BinaryTargets::new(labels.clone()).unwrap(),
        &weights,
        classifier_params.clone(),
    )
    .unwrap();
    let repeated = RandomForestClassifier::fit(
        &duplicated_x.as_view(),
        &BinaryTargets::new(duplicated_labels.clone()).unwrap(),
        classifier_params.clone(),
    )
    .unwrap();
    assert_eq!(
        weighted.to_bytes(),
        repeated.to_bytes(),
        "binary classifier"
    );

    let weighted = RandomForestClassifier::fit_multiclass_weighted(
        &x.as_view(),
        &ClassTargets::new(labels).unwrap(),
        &weights,
        classifier_params.clone(),
    )
    .unwrap();
    let repeated = RandomForestClassifier::fit_multiclass(
        &duplicated_x.as_view(),
        &ClassTargets::new(duplicated_labels).unwrap(),
        classifier_params,
    )
    .unwrap();
    assert_eq!(weighted, repeated, "multiclass classifier");

    let weighted = RandomForestRegressor::fit_weighted(
        &x.as_view(),
        &RegressionTargets::new(values).unwrap(),
        &weights,
        regressor_params.clone(),
    )
    .unwrap();
    let repeated = RandomForestRegressor::fit(
        &duplicated_x.as_view(),
        &RegressionTargets::new(duplicated_values).unwrap(),
        regressor_params,
    )
    .unwrap();
    assert_eq!(weighted.to_bytes(), repeated.to_bytes(), "regressor");
}

/// A weight of zero removes the row from training entirely.
///
/// This is the `times = 0` case of the duplication rule, and it is also why a
/// bootstrap resample draws from the positively weighted rows only: a row that
/// is not in the sample cannot consume one of its draws.
#[test]
fn a_zero_weight_row_is_the_same_fit_as_a_deleted_row() {
    let (x, labels, values) = weight_fixture();
    let dropped = 4;
    let mut weights = vec![1.0_f32; labels.len()];
    weights[dropped] = 0.0;
    let weights = SampleWeights::new(weights).unwrap();

    let mut kept_rows = Vec::new();
    let mut kept_values = Vec::new();
    for (row, &value) in values.iter().enumerate() {
        if row != dropped {
            kept_rows.extend_from_slice(x.as_view().row(row).unwrap());
            kept_values.push(value);
        }
    }
    let kept = DenseMatrix::new(kept_rows, labels.len() - 1, x.as_view().columns()).unwrap();

    for bootstrap in [false, true] {
        let params = regressor_params(123).with_bootstrap(bootstrap);
        let weighted = RandomForestRegressor::fit_weighted(
            &x.as_view(),
            &RegressionTargets::new(values.clone()).unwrap(),
            &weights,
            params.clone(),
        )
        .unwrap();
        let deleted = RandomForestRegressor::fit(
            &kept.as_view(),
            &RegressionTargets::new(kept_values.clone()).unwrap(),
            params,
        )
        .unwrap();
        assert_eq!(
            weighted.to_bytes(),
            deleted.to_bytes(),
            "bootstrap = {bootstrap}"
        );
    }
}

#[test]
fn weighted_fits_stay_deterministic_across_repeats_and_thread_counts() {
    let (x, labels, values) = weight_fixture();
    let weights = SampleWeights::new(vec![0.5, 2.0, 1.25, 3.0, 0.25, 1.0, 4.0, 0.75]).unwrap();
    let regression = RegressionTargets::new(values).unwrap();
    let classes = ClassTargets::new(labels).unwrap();

    let serial = regressor_params(77);
    let parallel = regressor_params(77).with_n_jobs(crate::ensemble::NJobs::Count(4));
    let one =
        RandomForestRegressor::fit_weighted(&x.as_view(), &regression, &weights, serial.clone())
            .unwrap();
    let repeat =
        RandomForestRegressor::fit_weighted(&x.as_view(), &regression, &weights, serial).unwrap();
    let threaded =
        RandomForestRegressor::fit_weighted(&x.as_view(), &regression, &weights, parallel).unwrap();
    assert_eq!(one.to_bytes(), repeat.to_bytes());
    assert_eq!(one.to_bytes(), threaded.to_bytes());

    let serial = multiclass_params(77);
    let parallel = multiclass_params(77).with_n_jobs(crate::ensemble::NJobs::Count(4));
    let one =
        RandomForestClassifier::fit_multiclass_weighted(&x.as_view(), &classes, &weights, serial)
            .unwrap();
    let threaded =
        RandomForestClassifier::fit_multiclass_weighted(&x.as_view(), &classes, &weights, parallel)
            .unwrap();
    // The retained thread count is part of the fitted parameters, so the two
    // values differ there by construction; the fitted trees must not.
    assert_eq!(
        one.predict_proba(&x.as_view()).unwrap(),
        threaded.predict_proba(&x.as_view()).unwrap()
    );
}

/// A length mismatch is reported before any training work begins.
#[test]
fn weighted_fitting_rejects_a_length_mismatch_before_training() {
    let (x, labels, values) = weight_fixture();
    let short = SampleWeights::new(vec![1.0; labels.len() - 1]).unwrap();
    let expected = ModelError::SampleWeightLength {
        rows: labels.len(),
        weights: labels.len() - 1,
    };
    assert_eq!(
        RandomForestClassifier::fit_weighted(
            &x.as_view(),
            &BinaryTargets::new(labels.clone()).unwrap(),
            &short,
            classifier_params(1),
        )
        .unwrap_err(),
        expected
    );
    assert_eq!(
        RandomForestClassifier::fit_multiclass_weighted(
            &x.as_view(),
            &ClassTargets::new(labels).unwrap(),
            &short,
            classifier_params(1),
        )
        .unwrap_err(),
        expected
    );
    assert_eq!(
        RandomForestRegressor::fit_weighted(
            &x.as_view(),
            &RegressionTargets::new(values).unwrap(),
            &short,
            regressor_params(1),
        )
        .unwrap_err(),
        expected
    );
}

/// Weights change the model. Without this the equivalences above would be
/// satisfied by an implementation that ignored its weights entirely.
#[test]
fn weights_are_not_inert() {
    let (x, labels, values) = weight_fixture();
    let regression = RegressionTargets::new(values).unwrap();
    let mut skewed = vec![1.0_f32; labels.len()];
    skewed[0] = 9.0;
    let skewed = SampleWeights::new(skewed).unwrap();
    let plain = RandomForestRegressor::fit(&x.as_view(), &regression, regressor_params(5)).unwrap();
    let weighted = RandomForestRegressor::fit_weighted(
        &x.as_view(),
        &regression,
        &skewed,
        regressor_params(5),
    )
    .unwrap();
    assert_ne!(plain.to_bytes(), weighted.to_bytes());
}

// --------------------------------------------------- classifier persistence

fn classifier_artifact_fixture() -> (DenseMatrix, RandomForestClassifier, RandomForestClassifier) {
    let (x, labels, _) = weight_fixture();
    let params = classifier_params(21)
        .with_n_estimators(4)
        .with_max_depth(Some(3));
    let binary = RandomForestClassifier::fit(
        &x.as_view(),
        &BinaryTargets::new(labels.clone()).unwrap(),
        params.clone(),
    )
    .unwrap();
    // Non-contiguous labels, so a decoder that reconstructed the class list
    // instead of reading it would relabel every prediction.
    let multiclass = RandomForestClassifier::fit_multiclass(
        &x.as_view(),
        &ClassTargets::new(vec![3, 7, 10, 3, 7, 10, 3, 7]).unwrap(),
        params,
    )
    .unwrap();
    (x, binary, multiclass)
}

#[test]
fn classifier_artifacts_round_trip_both_leaf_representations() {
    let (x, binary, multiclass) = classifier_artifact_fixture();
    let schema = [17; 32];
    for (name, model) in [("binary", &binary), ("multiclass", &multiclass)] {
        let bytes = model.to_artifact(schema).unwrap();
        assert_eq!(
            bytes,
            model.to_artifact(schema).unwrap(),
            "{name} is stable"
        );
        let decoded = RandomForestClassifier::from_artifact(&bytes, schema).unwrap();
        assert_eq!(&decoded, model, "{name} round trip");
        assert_eq!(decoded.classes(), model.classes(), "{name} classes");
        assert_eq!(
            decoded.predict(&x.as_view()).unwrap(),
            model.predict(&x.as_view()).unwrap(),
            "{name} labels"
        );
        assert_eq!(
            decoded.predict_proba(&x.as_view()).unwrap(),
            model.predict_proba(&x.as_view()).unwrap(),
            "{name} probabilities"
        );
        assert_eq!(
            decoded.to_artifact(schema).unwrap(),
            bytes,
            "{name} re-encodes to exactly the bytes it decoded from"
        );
        assert_eq!(
            RandomForestClassifier::from_artifact(&bytes, [18; 32]).unwrap_err(),
            ArtifactError::FeatureSchemaMismatch,
            "{name} is schema-bound"
        );
    }

    // The two flavours are different models and their artifacts differ; the
    // regressor's kind is a different kind again.
    assert_ne!(
        binary.to_artifact(schema).unwrap(),
        multiclass.to_artifact(schema).unwrap()
    );
    assert_eq!(
        RandomForestRegressor::from_artifact(&binary.to_artifact(schema).unwrap(), schema)
            .unwrap_err(),
        ArtifactError::UnsupportedModelKind {
            found: RANDOM_FOREST_CLASSIFIER_ARTIFACT_KIND
        }
    );
}

#[test]
fn classifier_artifacts_restore_every_retained_parameter() {
    let (x, labels, _) = weight_fixture();
    let params = RandomForestClassifierParams::default()
        .with_n_estimators(3)
        .with_max_depth(Some(5))
        .with_min_samples_split(3)
        .with_min_samples_leaf(2)
        .with_max_features(MaxFeatures::Count(2))
        .with_bootstrap(false)
        .with_random_state(4_242)
        .with_n_jobs(crate::ensemble::NJobs::Count(2));
    let model = RandomForestClassifier::fit(
        &x.as_view(),
        &BinaryTargets::new(labels).unwrap(),
        params.clone(),
    )
    .unwrap();
    let bytes = model.to_artifact([1; 32]).unwrap();
    let decoded = RandomForestClassifier::from_artifact(&bytes, [1; 32]).unwrap();
    assert_eq!(decoded.get_params(), &params);
    assert_eq!(decoded.n_features_in(), model.n_features_in());
}

/// A single observed class round trips in both flavours, which is the shape
/// with one probability column of `1.0`.
#[test]
fn classifier_artifacts_round_trip_single_class_and_root_leaf_trees() {
    let x = matrix(&[&[0.0], &[1.0], &[2.0], &[3.0]]);
    let params = RandomForestClassifierParams::default()
        .with_n_estimators(2)
        .with_bootstrap(false)
        .with_max_features(MaxFeatures::All)
        .with_random_state(3);
    let binary = RandomForestClassifier::fit(
        &x.as_view(),
        &BinaryTargets::new(vec![1, 1, 1, 1]).unwrap(),
        params.clone(),
    )
    .unwrap();
    assert_eq!(binary.classes(), [1]);
    let bytes = binary.to_artifact([2; 32]).unwrap();
    let decoded = RandomForestClassifier::from_artifact(&bytes, [2; 32]).unwrap();
    assert_eq!(decoded, binary);
    assert_eq!(decoded.predict_proba(&x.as_view()).unwrap(), vec![1.0; 4]);

    let multiclass = RandomForestClassifier::fit_multiclass(
        &x.as_view(),
        &ClassTargets::new(vec![9, 9, 9, 9]).unwrap(),
        params,
    )
    .unwrap();
    assert_eq!(multiclass.classes(), [9]);
    let bytes = multiclass.to_artifact([2; 32]).unwrap();
    let decoded = RandomForestClassifier::from_artifact(&bytes, [2; 32]).unwrap();
    assert_eq!(decoded, multiclass);
    assert_eq!(decoded.predict_proba(&x.as_view()).unwrap(), vec![1.0; 4]);
    assert_eq!(decoded.to_artifact([2; 32]).unwrap(), bytes);
}

#[test]
fn classifier_artifact_rejects_invalid_metadata_and_framing() {
    let (_, binary, multiclass) = classifier_artifact_fixture();
    let schema = [17; 32];
    let bytes = binary.to_artifact(schema).unwrap();
    let multiclass_bytes = multiclass.to_artifact(schema).unwrap();

    // Metadata words, in the order the writer emits them.
    const OBJECTIVE: usize = 0;
    const FLAVOUR: usize = 4;
    const N_FEATURES: usize = 8;
    const N_ESTIMATORS: usize = 12;
    const MIN_SAMPLES_SPLIT: usize = 20;
    const MIN_SAMPLES_LEAF: usize = 24;
    const MAX_FEATURES_TAG: usize = 28;
    const CLASS_COUNT: usize = 64;
    const FIRST_CLASS: usize = 68;

    for (name, offset, value) in [
        ("objective version", OBJECTIVE, 9_u32),
        ("forest flavour", FLAVOUR, 3),
        ("zero feature width", N_FEATURES, 0),
        ("estimator count disagrees with tree count", N_ESTIMATORS, 9),
        ("min_samples_split below two", MIN_SAMPLES_SPLIT, 1),
        ("zero min_samples_leaf", MIN_SAMPLES_LEAF, 0),
        ("unknown max_features tag", MAX_FEATURES_TAG, 9),
        ("zero class count", CLASS_COUNT, 0),
        ("a binary class label outside {0, 1}", FIRST_CLASS, 5),
    ] {
        let mut mutated = bytes.clone();
        metadata_u32(&mut mutated, offset, value);
        assert_eq!(
            RandomForestClassifier::from_artifact(&mutated, schema).unwrap_err(),
            ArtifactError::InvalidPayload,
            "{name} was accepted"
        );
    }

    // Relabelling a binary forest as multiclass leaves it without the leaf
    // probability component the multiclass reader requires, so the two
    // flavours cannot be crossed by rewriting the tag.
    let mut crossed = bytes.clone();
    metadata_u32(&mut crossed, FLAVOUR, 2);
    assert!(RandomForestClassifier::from_artifact(&crossed, schema).is_err());

    // A multiclass class list must stay strictly increasing: [3, 7, 10] with
    // the first label raised to 20 is no longer sorted.
    let mut unsorted = multiclass_bytes.clone();
    metadata_u32(&mut unsorted, FIRST_CLASS, 20);
    assert_eq!(
        RandomForestClassifier::from_artifact(&unsorted, schema).unwrap_err(),
        ArtifactError::InvalidPayload
    );

    assert_eq!(
        RandomForestClassifier::from_artifact(&bytes[..bytes.len() - 1], schema).unwrap_err(),
        ArtifactError::ChecksumMismatch
    );
    let mut trailing = bytes;
    trailing.push(0);
    assert_eq!(
        RandomForestClassifier::from_artifact(&trailing, schema).unwrap_err(),
        ArtifactError::ChecksumMismatch
    );
}

/// The multiclass leaf record carries a reserved zero where a scalar leaf
/// carries its value, so a nonzero there would be a second encoding of one
/// model.
#[test]
fn multiclass_leaf_records_reserve_their_scalar_slot() {
    let (_, _, multiclass) = classifier_artifact_fixture();
    let schema = [17; 32];
    let bytes = multiclass.to_artifact(schema).unwrap();

    // Skip the metadata component, whose declared length is its own header's
    // third word, and find the first leaf record in the first tree.
    let metadata_len = u32::from_le_bytes(
        bytes[PAYLOAD_START + 4..PAYLOAD_START + 8]
            .try_into()
            .unwrap(),
    ) as usize;
    let first_tree = PAYLOAD_START + 8 + metadata_len;
    let leaf = (first_tree..bytes.len() - 20)
        .step_by(4)
        .find(|&offset| bytes[offset..offset + 20] == [0_u8; 20])
        .expect("a leaf record with its reserved zero");

    let mut mutated = bytes;
    mutated[leaf + 4..leaf + 8].copy_from_slice(&0x3f80_0000_u32.to_le_bytes());
    resign(&mut mutated);
    assert_eq!(
        RandomForestClassifier::from_artifact(&mutated, schema).unwrap_err(),
        ArtifactError::InvalidPayload
    );
}

#[test]
fn rejects_invalid_configuration_and_data() {
    let x = matrix(&[&[0.0], &[1.0]]);
    let y = BinaryTargets::new(vec![0, 1]).unwrap();
    let cfg = classifier_params(1).with_n_estimators(0);
    assert_eq!(
        RandomForestClassifier::fit(&x.as_view(), &y, cfg).unwrap_err(),
        ModelError::InvalidEstimatorCount
    );
    let cfg = classifier_params(1).with_min_samples_split(1);
    assert_eq!(
        RandomForestClassifier::fit(&x.as_view(), &y, cfg).unwrap_err(),
        ModelError::InvalidMinSamplesSplit
    );
    let cfg = classifier_params(1).with_min_samples_leaf(0);
    assert_eq!(
        RandomForestClassifier::fit(&x.as_view(), &y, cfg).unwrap_err(),
        ModelError::InvalidMinSamplesLeaf
    );
    let cfg = classifier_params(1).with_max_features(MaxFeatures::Count(2));
    assert!(matches!(
        RandomForestClassifier::fit(&x.as_view(), &y, cfg),
        Err(ModelError::InvalidMaxFeatures { .. })
    ));
}

#[test]
fn checks_prediction_dimensions_and_output_size() {
    let x = matrix(&[&[0.0], &[1.0]]);
    let y = BinaryTargets::new(vec![0, 1]).unwrap();
    let forest = RandomForestClassifier::fit(&x.as_view(), &y, classifier_params(2)).unwrap();
    assert!(matches!(
        forest.predict_positive_proba_one(&[0.0, 1.0]),
        Err(ModelError::FeatureDimension { .. })
    ));
    let mut too_short = [0.0];
    assert!(matches!(
        forest.predict_positive_proba_into(&x.as_view(), &mut too_short),
        Err(ModelError::OutputLength { .. })
    ));
}

#[test]
fn every_packed_tree_has_valid_topology() {
    let x = matrix(&[
        &[0.0, 2.0],
        &[1.0, 3.0],
        &[2.0, 0.0],
        &[3.0, 1.0],
        &[4.0, 6.0],
        &[5.0, 7.0],
        &[6.0, 4.0],
        &[7.0, 5.0],
    ]);
    let y = BinaryTargets::new(vec![0, 0, 1, 1, 0, 1, 0, 1]).unwrap();
    let forest = RandomForestClassifier::fit(&x.as_view(), &y, classifier_params(55)).unwrap();
    assert_eq!(std::mem::size_of::<PackedNode>(), 16);
    for tree in forest.binary_trees() {
        assert!(!tree.nodes.is_empty() || tree.root_leaf.is_some());
        assert!(tree.root_leaf.is_none_or(f32::is_finite));
        for node in &tree.nodes {
            assert!(((node.feature_and_flags & FEATURE_MASK) as usize) < forest.n_features_in());
            assert!(node.threshold.is_finite());
            if node.feature_and_flags & LEFT_IS_LEAF != 0 {
                assert!(f32::from_bits(node.left).is_finite());
            } else {
                assert!((node.left as usize) < tree.nodes.len());
            }
            if node.feature_and_flags & RIGHT_IS_LEAF != 0 {
                assert!(f32::from_bits(node.right).is_finite());
            } else {
                assert!((node.right as usize) < tree.nodes.len());
            }
        }
    }
}

#[test]
fn pathological_deep_tree_uses_an_explicit_builder_stack() {
    const ROWS: usize = 4096;
    let values = (0..ROWS).map(|row| row as f32).collect();
    let labels = (0..ROWS).map(|row| (row & 1) as u8).collect();
    let x = DenseMatrix::new(values, ROWS, 1).unwrap();
    let y = BinaryTargets::new(labels).unwrap();
    let cfg = RandomForestClassifierParams::default()
        .with_n_estimators(1)
        .with_bootstrap(false)
        .with_max_features(MaxFeatures::All);
    let forest = RandomForestClassifier::fit(&x.as_view(), &y, cfg).unwrap();
    assert_eq!(forest.binary_trees()[0].nodes.len(), ROWS - 1);
}

/// Offsets into a version-2 envelope carrying one input schema: the fixed
/// header is 24 bytes, one schema record is 36, and each component adds an
/// 8-byte header.
const PAYLOAD_START: usize = 24 + 36;
const METADATA_START: usize = PAYLOAD_START + 8;
const METADATA_FIELD_BYTES: usize = 13 * 4 + 8;
const FIRST_TREE_START: usize = METADATA_START + METADATA_FIELD_BYTES;

// Byte offsets of each metadata field, relative to the component payload.
// `RANDOM_STATE` is the only 64-bit field, so later offsets are not multiples
// of four.
const OBJECTIVE_VERSION: usize = 0;
const N_FEATURES: usize = 4;
const N_ESTIMATORS: usize = 8;
const MAX_DEPTH: usize = 12;
const MIN_SAMPLES_SPLIT: usize = 16;
const MIN_SAMPLES_LEAF: usize = 20;
const MAX_FEATURES_TAG: usize = 24;
const MAX_FEATURES_COUNT: usize = 28;
const BOOTSTRAP: usize = 32;
const N_JOBS_TAG: usize = 44;
const N_JOBS_COUNT: usize = 48;
const TREE_COUNT: usize = 52;
const TOTAL_NODES: usize = 56;

fn write_metadata_u32(bytes: &mut [u8], offset: usize, value: u32) {
    let offset = METADATA_START + offset;
    bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
}

fn metadata_u32(bytes: &mut [u8], offset: usize, value: u32) {
    write_metadata_u32(bytes, offset, value);
    resign(bytes);
}

fn resign(bytes: &mut [u8]) {
    let checksum_offset = bytes.len() - 32;
    let checksum = Sha256::digest(&bytes[..checksum_offset]);
    bytes[checksum_offset..].copy_from_slice(&checksum);
}

fn artifact_fixture() -> (DenseMatrix, RandomForestRegressor) {
    let x = matrix(&[
        &[0.0, 3.0],
        &[1.0, 2.0],
        &[2.0, 1.0],
        &[3.0, 0.0],
        &[4.0, 7.0],
        &[5.0, 6.0],
        &[6.0, 5.0],
        &[7.0, 4.0],
    ]);
    let y = RegressionTargets::new(vec![0.0, 1.0, 1.5, 2.5, 8.0, 7.0, 6.0, 5.0]).unwrap();
    let model = RandomForestRegressor::fit(
        &x.as_view(),
        &y,
        RandomForestRegressorParams::default()
            .with_n_estimators(4)
            .with_max_depth(Some(4))
            .with_max_features(MaxFeatures::All)
            .with_random_state(5),
    )
    .unwrap();
    (x, model)
}

#[test]
fn regressor_artifact_round_trip_is_deterministic_schema_bound_and_kind_isolated() {
    let (x, model) = artifact_fixture();
    let schema = [17; 32];
    let left = model.to_artifact(schema).unwrap();
    assert_eq!(left, model.to_artifact(schema).unwrap());

    let decoded = RandomForestRegressor::from_artifact(&left, schema).unwrap();
    assert_eq!(decoded, model);
    assert_eq!(decoded.to_bytes(), model.to_bytes());
    assert_eq!(
        decoded.predict(&x.as_view()).unwrap(),
        model.predict(&x.as_view()).unwrap()
    );
    assert_eq!(decoded.to_artifact(schema).unwrap(), left);

    assert_eq!(
        RandomForestRegressor::from_artifact(&left, [18; 32]).unwrap_err(),
        ArtifactError::FeatureSchemaMismatch
    );
    assert_eq!(
        Ridge::from_artifact(&left, schema).unwrap_err(),
        ArtifactError::UnsupportedModelKind {
            found: RANDOM_FOREST_REGRESSOR_ARTIFACT_KIND,
        }
    );
    assert_eq!(
        HistGradientBoostingRegressor::from_artifact(&left, schema).unwrap_err(),
        ArtifactError::UnsupportedModelKind {
            found: RANDOM_FOREST_REGRESSOR_ARTIFACT_KIND,
        }
    );

    let mut corrupted = left;
    corrupted[FIRST_TREE_START + 20] ^= 1;
    assert_eq!(
        RandomForestRegressor::from_artifact(&corrupted, schema).unwrap_err(),
        ArtifactError::ChecksumMismatch
    );
}

#[test]
fn regressor_artifact_restores_every_retained_parameter() {
    let x = matrix(&[&[0.0, 1.0], &[1.0, 0.0], &[2.0, 3.0], &[3.0, 2.0]]);
    let y = RegressionTargets::new(vec![0.0, 1.0, 4.0, 9.0]).unwrap();
    let schema = [2; 32];
    for params in [
        RandomForestRegressorParams::default().with_n_estimators(3),
        RandomForestRegressorParams::default()
            .with_n_estimators(2)
            .with_max_depth(None)
            .with_min_samples_split(3)
            .with_min_samples_leaf(2)
            .with_max_features(MaxFeatures::Sqrt)
            .with_bootstrap(false)
            .with_random_state(u64::MAX)
            .with_n_jobs(NJobs::Count(2)),
        RandomForestRegressorParams::default()
            .with_n_estimators(2)
            .with_max_depth(Some(1))
            .with_max_features(MaxFeatures::Count(2))
            .with_n_jobs(NJobs::All),
    ] {
        let model = RandomForestRegressor::fit(&x.as_view(), &y, params.clone()).unwrap();
        let decoded =
            RandomForestRegressor::from_artifact(&model.to_artifact(schema).unwrap(), schema)
                .unwrap();
        assert_eq!(decoded.get_params(), &params);
        assert_eq!(decoded.n_features_in(), model.n_features_in());
        assert_eq!(decoded, model);
    }
}

#[test]
fn regressor_artifact_round_trips_single_leaf_trees() {
    let x = matrix(&[&[1.0], &[1.0], &[1.0], &[1.0]]);
    let y = RegressionTargets::new(vec![2.5, 2.5, 2.5, 2.5]).unwrap();
    let model = RandomForestRegressor::fit(
        &x.as_view(),
        &y,
        RandomForestRegressorParams::default().with_n_estimators(3),
    )
    .unwrap();
    let schema = [6; 32];
    let bytes = model.to_artifact(schema).unwrap();
    // Three one-node logical trees: every tree collapsed to a root leaf.
    let total_nodes = u32::from_le_bytes(
        bytes[METADATA_START + TOTAL_NODES..METADATA_START + TOTAL_NODES + 4]
            .try_into()
            .unwrap(),
    );
    assert_eq!(total_nodes, 3);
    let decoded = RandomForestRegressor::from_artifact(&bytes, schema).unwrap();
    assert_eq!(decoded, model);
    assert_eq!(decoded.predict_one(&[1.0]).unwrap(), 2.5);
}

#[test]
fn regressor_artifact_rejects_invalid_metadata_tree_records_and_framing() {
    let (_, model) = artifact_fixture();
    let schema = [29; 32];
    let bytes = model.to_artifact(schema).unwrap();
    assert!(RandomForestRegressor::from_artifact(&bytes, schema).is_ok());

    // Metadata field offset, and the value that breaks it.
    for (name, offset, value) in [
        ("objective version", OBJECTIVE_VERSION, 2),
        ("zero feature width", N_FEATURES, 0),
        ("feature width beyond the ceiling", N_FEATURES, 1_000_001),
        (
            "estimator count disagreeing with the trees",
            N_ESTIMATORS,
            3,
        ),
        ("max_depth beyond the ceiling", MAX_DEPTH, 1_048_577),
        ("min_samples_split below two", MIN_SAMPLES_SPLIT, 1),
        ("zero min_samples_leaf", MIN_SAMPLES_LEAF, 0),
        ("unknown max_features tag", MAX_FEATURES_TAG, 9),
        (
            "max_features count set for the All tag",
            MAX_FEATURES_COUNT,
            3,
        ),
        ("non-boolean bootstrap flag", BOOTSTRAP, 2),
        ("unknown n_jobs tag", N_JOBS_TAG, 9),
        ("n_jobs count set for the Serial tag", N_JOBS_COUNT, 1),
        ("tree count disagreeing with the estimators", TREE_COUNT, 3),
        ("declared node total below the tree count", TOTAL_NODES, 3),
        (
            "declared node total beyond the ceiling",
            TOTAL_NODES,
            1_048_577,
        ),
    ] {
        let mut corrupted = bytes.clone();
        metadata_u32(&mut corrupted, offset, value);
        assert_eq!(
            RandomForestRegressor::from_artifact(&corrupted, schema).unwrap_err(),
            ArtifactError::InvalidPayload,
            "{name} was accepted"
        );
    }

    // A max_features count must stay inside the fitted width.
    let mut inconsistent = bytes.clone();
    write_metadata_u32(&mut inconsistent, MAX_FEATURES_TAG, 3);
    write_metadata_u32(&mut inconsistent, MAX_FEATURES_COUNT, 3);
    resign(&mut inconsistent);
    assert_eq!(
        RandomForestRegressor::from_artifact(&inconsistent, schema).unwrap_err(),
        ArtifactError::InvalidPayload
    );

    let mut component_kind = bytes.clone();
    component_kind[FIRST_TREE_START..FIRST_TREE_START + 2].copy_from_slice(&3_u16.to_le_bytes());
    resign(&mut component_kind);
    assert_eq!(
        RandomForestRegressor::from_artifact(&component_kind, schema).unwrap_err(),
        ArtifactError::InvalidPayload
    );

    // The first logical record of the first tree: tag, then feature index.
    let record = FIRST_TREE_START + 8 + 12;
    assert_eq!(
        u32::from_le_bytes(bytes[record..record + 4].try_into().unwrap()),
        1,
        "fixture must start with a branch record"
    );
    let mut feature = bytes.clone();
    feature[record + 4..record + 8].copy_from_slice(&7_u32.to_le_bytes());
    resign(&mut feature);
    assert_eq!(
        RandomForestRegressor::from_artifact(&feature, schema).unwrap_err(),
        ArtifactError::InvalidPayload
    );

    assert_eq!(
        RandomForestRegressor::from_artifact(&bytes[..bytes.len() - 1], schema).unwrap_err(),
        ArtifactError::ChecksumMismatch
    );

    let mut trailing = bytes.clone();
    trailing.insert(bytes.len() - 32, 0);
    resign(&mut trailing);
    assert_eq!(
        RandomForestRegressor::from_artifact(&trailing, schema).unwrap_err(),
        ArtifactError::TrailingBytes
    );

    assert_eq!(
        RandomForestRegressor::from_artifact(&[], schema).unwrap_err(),
        ArtifactError::Truncated
    );
}

#[test]
fn regressor_artifact_refuses_models_whose_averaged_prediction_overflows() {
    let x = matrix(&[&[0.0], &[1.0], &[2.0], &[3.0]]);
    let y = RegressionTargets::new(vec![f32::MAX; 4]).unwrap();
    let model = RandomForestRegressor::fit(
        &x.as_view(),
        &y,
        RandomForestRegressorParams::default()
            .with_n_estimators(4)
            .with_bootstrap(false),
    )
    .unwrap();
    assert_eq!(
        model.to_artifact([1; 32]).unwrap_err(),
        ArtifactError::InvalidPayload
    );
}

// --------------------------------------------------------------- multiclass

/// Twelve rows, two features, three classes with clearly separated regions.
fn three_class_problem() -> (DenseMatrix, ClassTargets) {
    let x = matrix(&[
        &[0.0, 0.0],
        &[0.5, 0.2],
        &[0.2, 0.6],
        &[1.0, 0.3],
        &[2.0, 0.1],
        &[1.8, 0.5],
        &[2.2, 0.9],
        &[0.3, 2.0],
        &[0.8, 2.4],
        &[1.2, 2.2],
        &[1.0, 3.0],
        &[0.1, 1.0],
    ]);
    (
        x,
        ClassTargets::new(vec![0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 0]).unwrap(),
    )
}

fn multiclass_params(random_state: u64) -> RandomForestClassifierParams {
    RandomForestClassifierParams::default()
        .with_n_estimators(17)
        .with_max_features(MaxFeatures::All)
        .with_random_state(random_state)
}

#[test]
fn the_ensemble_averages_per_tree_probability_vectors_rather_than_voting() {
    // Four depth-one trees over three classes. A hard vote over four trees can
    // only ever produce multiples of a quarter, so any other value is decisive
    // evidence that whole distributions are being averaged.
    let (x, y) = three_class_problem();
    let model = RandomForestClassifier::fit_multiclass(
        &x.as_view(),
        &y,
        RandomForestClassifierParams::default()
            .with_n_estimators(4)
            .with_max_depth(Some(1))
            .with_bootstrap(false)
            .with_max_features(MaxFeatures::All)
            .with_random_state(0),
    )
    .unwrap();

    let query = matrix(&[&[0.2, 0.3], &[2.0, 0.4], &[1.0, 3.0], &[1.0, 1.2]]);
    let probabilities = model.predict_proba(&query.as_view()).unwrap();
    assert_eq!(probabilities.len(), query.rows() * 3);

    let quarter = 0.25_f32;
    let off_grid = probabilities
        .iter()
        .filter(|value| (*value / quarter).fract() != 0.0)
        .count();
    assert!(
        off_grid > 0,
        "every value was a multiple of 1/4, which a hard vote could also have \
         produced: {probabilities:?}"
    );

    // And the averaging identity itself: the ensemble row is the mean of the
    // per-tree rows, to the rounding of one division.
    for (row_index, row) in query.iter_rows().enumerate() {
        let mut expected = [0.0_f64; 3];
        let Forest::Multiclass(trees) = &model.core.forest else {
            unreachable!("multiclass fit");
        };
        for tree in trees {
            for (slot, &value) in expected.iter_mut().zip(tree.probabilities(row)) {
                *slot += f64::from(value);
            }
        }
        for (class, expected) in expected.iter().enumerate() {
            let actual = f64::from(probabilities[row_index * 3 + class]);
            assert!(
                (actual - expected / 4.0).abs() <= 1.0e-6,
                "row {row_index} class {class}: {actual} vs {}",
                expected / 4.0
            );
        }
    }
}

#[test]
fn multiclass_labels_never_disagree_with_the_probability_argmax() {
    for classes in 2..=6_u8 {
        let rows = 240;
        let mut values = Vec::with_capacity(rows * 3);
        let mut labels = Vec::with_capacity(rows);
        let mut state = 0x9e37_79b9_u64 + u64::from(classes);
        for row in 0..rows {
            for _ in 0..3 {
                state = state
                    .wrapping_mul(6_364_136_223_846_793_005)
                    .wrapping_add(1_442_695_040_888_963_407);
                values.push(((state >> 33) as f32 / (1_u32 << 31) as f32) * 4.0 - 2.0);
            }
            labels.push((row % usize::from(classes)) as u8);
        }
        let data = DenseMatrix::new(values, rows, 3).unwrap();
        let targets = ClassTargets::new(labels).unwrap();
        let model =
            RandomForestClassifier::fit_multiclass(&data.as_view(), &targets, multiclass_params(3))
                .unwrap();

        let width = usize::from(classes);
        let predicted = model.predict(&data.as_view()).unwrap();
        let probabilities = model.predict_proba(&data.as_view()).unwrap();
        let mut into = vec![0_u8; rows];
        model.predict_into(&data.as_view(), &mut into).unwrap();
        assert_eq!(into, predicted);
        for (index, (label, row)) in predicted
            .iter()
            .zip(probabilities.chunks_exact(width))
            .enumerate()
        {
            let mut best = 0;
            for class in 1..width {
                if row[class] > row[best] {
                    best = class;
                }
            }
            assert_eq!(*label, model.classes()[best], "row {index}");
            assert_eq!(
                model.predict_one(data.row(index).unwrap()).unwrap(),
                *label,
                "scalar path, row {index}"
            );
        }
    }
}

#[test]
fn multiclass_probability_rows_stay_inside_the_frozen_tolerance() {
    let (x, y) = three_class_problem();
    for estimators in [1_usize, 3, 17] {
        let model = RandomForestClassifier::fit_multiclass(
            &x.as_view(),
            &y,
            multiclass_params(1).with_n_estimators(estimators),
        )
        .unwrap();
        for row in model
            .predict_proba(&x.as_view())
            .unwrap()
            .chunks_exact(model.classes().len())
        {
            let sum = row.iter().sum::<f32>();
            // Not renormalized: one is reached only to `n_classes` ulps.
            assert!(
                (sum - 1.0).abs() <= model.classes().len() as f32 * f32::EPSILON,
                "{estimators} trees, row {row:?} sums to {sum}"
            );
            assert!(row.iter().all(|&value| (0.0..=1.0).contains(&value)));
        }
    }
}

#[test]
fn multiclass_columns_follow_sorted_labels_with_no_contiguity_assumption() {
    let (x, y) = three_class_problem();
    let base =
        RandomForestClassifier::fit_multiclass(&x.as_view(), &y, multiclass_params(5)).unwrap();
    assert_eq!(base.classes(), &[0, 1, 2]);

    let relabelled = ClassTargets::new(
        y.as_slice()
            .iter()
            .map(|&label| match label {
                0 => 7,
                1 => 3,
                _ => 10,
            })
            .collect(),
    )
    .unwrap();
    let permuted =
        RandomForestClassifier::fit_multiclass(&x.as_view(), &relabelled, multiclass_params(5))
            .unwrap();
    assert_eq!(permuted.classes(), &[3, 7, 10]);

    // The trees are identical — only the class column order changed — so the
    // probability rows must be an exact permutation, not merely similar.
    let base_probabilities = base.predict_proba(&x.as_view()).unwrap();
    let permuted_probabilities = permuted.predict_proba(&x.as_view()).unwrap();
    for (base, permuted) in base_probabilities
        .chunks_exact(3)
        .zip(permuted_probabilities.chunks_exact(3))
    {
        assert_eq!(permuted, [base[1], base[0], base[2]]);
    }
    assert_eq!(
        permuted.predict(&x.as_view()).unwrap(),
        base.predict(&x.as_view())
            .unwrap()
            .into_iter()
            .map(|label| match label {
                0 => 7,
                1 => 3,
                _ => 10,
            })
            .collect::<Vec<_>>()
    );
    for (index, &label) in permuted.classes().iter().enumerate() {
        let column = permuted.predict_class_proba(&x.as_view(), label).unwrap();
        for (row, &value) in column.iter().enumerate() {
            assert_eq!(value, permuted_probabilities[row * 3 + index]);
        }
        assert_eq!(
            permuted
                .predict_class_proba_one(x.row(0).unwrap(), label)
                .unwrap(),
            permuted_probabilities[index]
        );
    }
    assert_eq!(
        permuted.predict_class_proba(&x.as_view(), 0).unwrap_err(),
        ModelError::UnknownClass { class: 0 }
    );
}

#[test]
fn a_strict_subset_tie_selects_the_lowest_tied_class_not_the_first() {
    // One feature held constant, so no split can separate anything and every
    // tree is a single leaf holding the class frequencies: 1/5, 2/5, 2/5.
    let x = matrix(&[&[1.0], &[1.0], &[1.0], &[1.0], &[1.0]]);
    let targets = ClassTargets::new(vec![5, 20, 20, 9, 9]).unwrap();
    let model = RandomForestClassifier::fit_multiclass(
        &x.as_view(),
        &targets,
        RandomForestClassifierParams::default()
            .with_n_estimators(3)
            .with_bootstrap(false)
            .with_max_features(MaxFeatures::All)
            .with_random_state(0),
    )
    .unwrap();
    assert_eq!(model.classes(), &[5, 9, 20]);

    let probabilities = model.predict_proba_one(&[1.0]).unwrap();
    assert_eq!(probabilities, vec![0.2, 0.4, 0.4]);
    assert_eq!(
        probabilities[1].to_bits(),
        probabilities[2].to_bits(),
        "the tie must be exact, not near"
    );
    // Lowest *tied* index: label 9, not the first class 5.
    assert_eq!(model.predict_one(&[1.0]).unwrap(), 9);
    assert_eq!(model.predict(&x.as_view()).unwrap(), vec![9; 5]);
}

#[test]
fn a_single_observed_class_fits_and_returns_one_all_ones_column() {
    let (x, _) = three_class_problem();
    for label in [0_u8, 3, 200] {
        let targets = ClassTargets::new(vec![label; x.rows()]).unwrap();
        let model =
            RandomForestClassifier::fit_multiclass(&x.as_view(), &targets, multiclass_params(9))
                .unwrap();
        assert_eq!(model.classes(), &[label]);
        let probabilities = model.predict_proba(&x.as_view()).unwrap();
        assert_eq!(probabilities, vec![1.0; x.rows()]);
        assert_eq!(model.predict(&x.as_view()).unwrap(), vec![label; x.rows()]);
        assert_eq!(
            model.predict_proba_one(x.row(0).unwrap()).unwrap(),
            vec![1.0]
        );
        assert_eq!(
            model.predict_class_proba(&x.as_view(), label).unwrap(),
            vec![1.0; x.rows()]
        );
    }
}

#[test]
fn multiclass_fits_are_deterministic_across_thread_counts() {
    let (x, y) = three_class_problem();
    let serial =
        RandomForestClassifier::fit_multiclass(&x.as_view(), &y, multiclass_params(11)).unwrap();
    let repeat =
        RandomForestClassifier::fit_multiclass(&x.as_view(), &y, multiclass_params(11)).unwrap();
    let parallel = RandomForestClassifier::fit_multiclass(
        &x.as_view(),
        &y,
        multiclass_params(11).with_n_jobs(NJobs::Count(4)),
    )
    .unwrap();
    assert_eq!(serial, repeat);
    // The thread count is retained in the parameters, so the trees themselves
    // are what must match — a parallel fit is the same forest, not the same
    // configuration.
    assert_eq!(serial.core.forest, parallel.core.forest);
    let different =
        RandomForestClassifier::fit_multiclass(&x.as_view(), &y, multiclass_params(12)).unwrap();
    assert_ne!(serial.core.forest, different.core.forest);
}

#[test]
fn a_multiclass_fit_has_no_positive_class_and_validates_before_writing() {
    let (x, y) = three_class_problem();
    let model =
        RandomForestClassifier::fit_multiclass(&x.as_view(), &y, multiclass_params(2)).unwrap();
    let expected = ModelError::MulticlassOutput { columns: 3 };
    assert_eq!(
        model
            .predict_positive_proba_one(x.row(0).unwrap())
            .unwrap_err(),
        expected
    );
    let mut sentinel = vec![9.0_f32; x.rows()];
    assert_eq!(
        model
            .predict_positive_proba_into(&x.as_view(), &mut sentinel)
            .unwrap_err(),
        expected
    );
    assert!(sentinel.iter().all(|&value| value == 9.0));

    let wrong_width = DenseMatrix::new(vec![1.0; x.rows() * 3], x.rows(), 3).unwrap();
    let mut probabilities = vec![9.0_f32; x.rows() * 3];
    assert_eq!(
        model
            .predict_proba_into(&wrong_width.as_view(), &mut probabilities)
            .unwrap_err(),
        ModelError::FeatureDimension {
            expected: 2,
            actual: 3
        }
    );
    assert!(probabilities.iter().all(|&value| value == 9.0));

    let mut short = vec![9.0_f32; 2];
    assert_eq!(
        model
            .predict_proba_into(&x.as_view(), &mut short)
            .unwrap_err(),
        ModelError::OutputLength {
            expected: x.rows() * 3,
            actual: 2
        }
    );
    assert!(short.iter().all(|&value| value == 9.0));
}

#[test]
fn multiclass_fitting_validates_targets_and_configuration_before_training() {
    let (x, _) = three_class_problem();
    assert_eq!(
        RandomForestClassifier::fit_multiclass(
            &x.as_view(),
            &ClassTargets::new(vec![0, 1, 2]).unwrap(),
            multiclass_params(0),
        )
        .unwrap_err(),
        ModelError::TargetLength {
            rows: 12,
            targets: 3
        }
    );
    let targets = ClassTargets::new(vec![0; x.rows()]).unwrap();
    assert_eq!(
        RandomForestClassifier::fit_multiclass(
            &x.as_view(),
            &targets,
            multiclass_params(0).with_n_estimators(0),
        )
        .unwrap_err(),
        ModelError::InvalidEstimatorCount
    );
    assert_eq!(
        RandomForestClassifier::fit_multiclass(
            &x.as_view(),
            &targets,
            multiclass_params(0).with_min_samples_split(1),
        )
        .unwrap_err(),
        ModelError::InvalidMinSamplesSplit
    );
}

#[test]
fn separable_multiclass_regions_are_recovered() {
    let (x, y) = three_class_problem();
    let model =
        RandomForestClassifier::fit_multiclass(&x.as_view(), &y, multiclass_params(4)).unwrap();
    assert_eq!(model.predict(&x.as_view()).unwrap(), y.as_slice());
}