treeboost 0.1.0

High-performance Gradient Boosted Decision Tree engine for large-scale tabular data
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
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
# TreeBoost API Reference

Complete API documentation for TreeBoost's Rust and Python interfaces.

## Table of Contents

1. [UniversalModel]#universalmodel - **Start here.** The unified interface for all boosting modes
2. [Learners]#learners - LinearBooster, LinearTreeBooster, TreeBooster
3. [Preprocessing]#preprocessing - Scalers, encoders, imputers (serialize with model)
4. [AutoTuner]#autotuner - Hyperparameter optimization
5. [Dataset]#dataset - Data loading and preparation
6. [Loss Functions]#loss-functions - Training objectives
7. [Inference]#inference - Prediction and uncertainty
8. [Encoding]#encoding - Categorical feature handling
9. [Serialization]#serialization - Model persistence (rkyv, bincode)
10. [Incremental Learning]#incremental-learning - **NEW:** TRB format, warm-start updates, drift detection
11. [Backend Selection]#backend-selection - Hardware acceleration
12. [GBDTModel (Classic)]#gbdtmodel-classic - The original GBDT-only API (still available)

---

## UniversalModel

**The main entry point.** Unified interface for all boosting modes.

`UniversalModel` wraps `GBDTModel` internally—you get GPU acceleration, conformal prediction, and all mature features automatically. The hybrid modes (LinearThenTree, RandomForest) add specialized functionality on top.

### BoostingMode

```rust
use treeboost::BoostingMode;

BoostingMode::PureTree        // Standard GBDT (wraps GBDTModel - GPU, conformal, multi-class)
BoostingMode::LinearThenTree  // Linear model + GBDTModel on residuals
BoostingMode::RandomForest    // Parallel independent trees with averaging
```

| Mode             | Best For                                  | How It Works                                          |
| ---------------- | ----------------------------------------- | ----------------------------------------------------- |
| `PureTree`       | General tabular, categoricals             | Delegates to `GBDTModel` (full GPU/conformal support) |
| `LinearThenTree` | Time-series, trending data, extrapolation | Linear phase → `GBDTModel` on residuals               |
| `RandomForest`   | Noisy data, variance reduction            | Bootstrap sampling, parallel trees, averaging         |

### UniversalConfig

```rust
use treeboost::{UniversalConfig, BoostingMode, StackingStrategy};
use treeboost::learner::{TreeConfig, LinearConfig};

let config = UniversalConfig::new()
    .with_mode(BoostingMode::LinearThenTree)
    .with_num_rounds(100)           // Number of tree rounds
    .with_linear_rounds(10)         // Linear boosting iterations (LinearThenTree only)
    .with_learning_rate(0.1)
    .with_subsample(0.8)
    .with_validation_ratio(0.2)
    .with_early_stopping_rounds(10)
    .with_seed(42);

// Fine-tune tree component
let config = config.with_tree_config(
    TreeConfig::default()
        .with_max_depth(6)
        .with_max_leaves(31)
        .with_lambda(1.0)
);

// Fine-tune linear component (LinearThenTree only)
let config = config.with_linear_config(
    LinearConfig::default()
        .with_lambda(1.0)           // Regularization strength
        .with_l1_ratio(0.0)         // 0.0 = Ridge, 1.0 = LASSO, between = ElasticNet
);

// Optional: Multi-seed ensemble training
let config = config
    .with_ensemble_seeds(vec![1, 2, 3, 4, 5])  // Train 5 models with different seeds
    .with_stacking_strategy(StackingStrategy::Ridge {
        alpha: 0.01,
        rank_transform: false,
        fit_intercept: true,
        min_weight: 0.01,
    });
```

**Python:**

```python
from treeboost import UniversalConfig, BoostingMode

config = UniversalConfig()
config.mode = BoostingMode.LinearThenTree
config.num_rounds = 100
config.linear_rounds = 10
config.learning_rate = 0.1
```

### Training

```rust
use treeboost::{UniversalModel, UniversalConfig, BoostingMode};
use treeboost::loss::MseLoss;
use treeboost::dataset::DatasetLoader;

let loader = DatasetLoader::new(255);
let dataset = loader.load_parquet("train.parquet", "target", None)?;

let config = UniversalConfig::new()
    .with_mode(BoostingMode::LinearThenTree)
    .with_num_rounds(100);

let model = UniversalModel::train(&dataset, config, &MseLoss)?;
```

### Automatic Mode Selection (NEW)

TreeBoost can **automatically analyze your dataset** and pick the best boosting mode. This "MRI scan" approach analyzes data characteristics WITHOUT expensive training trials.

```rust
use treeboost::{UniversalModel, MseLoss};

// One-liner: Let TreeBoost pick the best mode
let model = UniversalModel::auto(&dataset, &MseLoss)?;

// See why it picked this mode
println!("Selected: {:?}", model.mode());
println!("Confidence: {:?}", model.selection_confidence());
println!("{}", model.analysis_report().unwrap());
```

**How It Works:**

1. **Subsample** - Work on 10k-50k rows (fast)
2. **Linear Probe** - Quick Ridge regression measures linear signal (R²)
3. **Tree Probe** - Shallow tree on residuals measures non-linear structure
4. **Score Modes** - Compute scores for each mode based on data characteristics
5. **Recommend** - Pick highest-scoring mode with confidence level

**ModeSelection Enum:**

```rust
use treeboost::ModeSelection;

// Let TreeBoost analyze and pick (recommended for new datasets)
ModeSelection::Auto

// Custom analysis settings
ModeSelection::AutoWithConfig(AnalysisConfig::fast())

// Explicitly specify mode (when you know what works)
ModeSelection::Fixed(BoostingMode::LinearThenTree)
```

**All Auto Methods:**

```rust
// Simple one-liner
let model = UniversalModel::auto(&dataset, &loss)?;

// Auto with custom training config
let config = UniversalConfig::new().with_num_rounds(200);
let model = UniversalModel::auto_with_config(&dataset, config, &loss)?;

// Full control: custom analysis + training config
let analysis_config = AnalysisConfig::thorough();
let model = UniversalModel::auto_with_analysis_config(
    &dataset, config, analysis_config, &loss
)?;

// Via train_with_selection (most explicit)
let model = UniversalModel::train_with_selection(
    &dataset, config, ModeSelection::Auto, &loss
)?;
```

**Analysis Access:**

```rust
// Check if auto-selected
model.was_auto_selected()  // bool

// Get analysis results
if let Some(analysis) = model.analysis() {
    println!("Linear R²: {:.2}", analysis.linear_r2);
    println!("Tree gain: {:.2}", analysis.tree_gain);
    println!("Noise floor: {:.2}", analysis.noise_floor);
    println!("Categorical ratio: {:.2}", analysis.categorical_ratio);
}

// Confidence level: High, Medium, or Low
model.selection_confidence()  // Option<Confidence>

// Pretty report (displays all metrics, scores, reasoning)
println!("{}", model.analysis_report().unwrap());

// Compact summary for logging
println!("{}", model.analysis_summary().unwrap());
```

**Decision Logic:**

| Data Pattern                             | Recommended Mode | Why                                            |
| ---------------------------------------- | ---------------- | ---------------------------------------------- |
| High linear R² (>0.3) + tree gain (>0.1) | LinearThenTree   | Linear captures trend, trees capture residuals |
| Weak linear signal + categorical-heavy   | PureTree         | Trees handle categoricals natively             |
| High noise floor (>0.4)                  | RandomForest     | Bagging reduces variance                       |

**When to Use:**

- **New datasets** - Let TreeBoost explore the data
-**Unsure which mode** - Analysis provides explanation
-**Documentation** - Report explains the decision
-**Benchmarking** - Use fixed mode for reproducibility
-**Known best mode** - Skip analysis overhead

### Serialization

UniversalModel supports zero-copy serialization via rkyv, making it perfect for production deployment.

```rust
// Save model for inference
model.save("model.rkyv")?;

// Load model for inference
let loaded = UniversalModel::load("model.rkyv")?;

// Get config for inspection/reuse
let config = model.config();  // &UniversalConfig
let config_json = serde_json::to_string_pretty(config)?;
std::fs::write("config.json", config_json)?;
```

**Typical workflow:**

```rust
// 1. Train with AutoML
let auto = AutoModel::train(&df, "target")?;

// 2. Export discovered configuration to JSON (useful for inspection and reuse)
auto.save_config("best_config.json")?;

// 3. Save trained model for inference
auto.save("model.rkyv")?;

// 4. Later: Load model for predictions (no need to retrain)
let loaded = UniversalModel::load("model.rkyv")?;
let predictions = loaded.predict(&dataset);
```

### Prediction

```rust
// Batch prediction
let predictions = model.predict(&dataset);

// Single row
let pred = model.predict_row(&dataset, row_idx);

// Model info
model.mode()           // BoostingMode
model.num_trees()      // usize
model.has_linear()     // bool (true if LinearThenTree)
model.num_features()   // usize
```

### Conformal Prediction (PureTree only)

```rust
// Predictions with uncertainty intervals
let (predictions, lower, upper) = model.predict_with_intervals(&dataset)?;

// Check calibration status
let quantile = model.conformal_quantile();  // Option<f32>
```

### Classification (PureTree only)

```rust
// Binary classification
let probabilities = model.predict_proba(&dataset)?;        // Vec<f32> in [0, 1]
let classes = model.predict_class(&dataset, 0.5)?;         // Vec<u32> (0 or 1)

// Multi-class
let is_mc = model.is_multiclass();                         // bool
let num_classes = model.get_num_classes();                 // usize
let probs = model.predict_proba_multiclass(&dataset)?;     // Vec<Vec<f32>>
let classes = model.predict_class_multiclass(&dataset)?;   // Vec<u32>
let logits = model.predict_raw_multiclass(&dataset)?;      // Vec<Vec<f32>>
```

### Feature Importance

```rust
// Works for all modes
let importances = model.feature_importance();  // Vec<f32>, normalized to sum to 1.0
```

### Raw Prediction (PureTree only)

Predict directly from unbinned f64 features without creating a `BinnedDataset`:

```rust
let features: &[f64] = &[1.0, 2.0, 3.0];  // row-major
let predictions = model.predict_raw(features)?;

// With intervals
let (preds, lower, upper) = model.predict_raw_with_intervals(features)?;

// Classification from raw features
let probs = model.predict_proba_raw(features)?;
let classes = model.predict_class_raw(features, 0.5)?;
```

### Method Summary

**Training Methods:**

| Method                                                     | Returns      | Description                    |
| ---------------------------------------------------------- | ------------ | ------------------------------ |
| `train(&dataset, config, &loss)`                           | Result<Self> | Train with explicit config     |
| `auto(&dataset, &loss)`                                    | Result<Self> | Auto-select mode (NEW)         |
| `auto_with_config(&dataset, config, &loss)`                | Result<Self> | Auto-select with custom config |
| `train_with_selection(&dataset, config, selection, &loss)` | Result<Self> | Full control via ModeSelection |

**Prediction Methods:**

| Method                               | Returns                 | Modes    | Description                  |
| ------------------------------------ | ----------------------- | -------- | ---------------------------- |
| `predict(&dataset)`                  | Vec<f32>                | All      | Point predictions            |
| `predict_row(&dataset, idx)`         | f32                     | All      | Single row prediction        |
| `predict_with_intervals(&dataset)`   | Result<(Vec, Vec, Vec)> | PureTree | Conformal intervals          |
| `predict_proba(&dataset)`            | Result<Vec<f32>>        | PureTree | Binary probabilities         |
| `predict_class(&dataset, threshold)` | Result<Vec<u32>>        | PureTree | Binary classes               |
| `predict_proba_multiclass(&dataset)` | Result<Vec<Vec<f32>>>   | PureTree | Multi-class probabilities    |
| `predict_class_multiclass(&dataset)` | Result<Vec<u32>>        | PureTree | Multi-class predictions      |
| `feature_importance()`               | Vec<f32>                | All      | Normalized importance scores |
| `predict_raw(features)`              | Result<Vec<f32>>        | PureTree | From unbinned features       |
| `conformal_quantile()`               | Option<f32>             | PureTree | Calibrated quantile          |
| `is_multiclass()`                    | bool                    | All      | Check if multi-class model   |
| `get_num_classes()`                  | usize                   | All      | Number of classes            |

**Analysis Methods (for auto-selected models):**

| Method                   | Returns                  | Description                  |
| ------------------------ | ------------------------ | ---------------------------- |
| `was_auto_selected()`    | bool                     | True if auto mode was used   |
| `analysis()`             | Option<&DatasetAnalysis> | Full analysis results        |
| `selection_confidence()` | Option<Confidence>       | High/Medium/Low confidence   |
| `analysis_report()`      | Option<AnalysisReport>   | Formatted diagnostic report  |
| `analysis_summary()`     | Option<String>           | One-line summary for logging |

---

## Learners

Low-level weak learners. Use these directly when building custom boosting loops.

### LinearBooster

Ridge, LASSO, or Elastic Net via Coordinate Descent. Used internally by `LinearThenTree` mode.

```rust
use treeboost::learner::{LinearBooster, LinearConfig, LinearPreset, WeakLearner};

// Ridge (L2) - default, most stable
let config = LinearConfig::default()
    .with_preset(LinearPreset::Ridge)
    .with_lambda(1.0);

// LASSO (L1) - sparse solutions, feature selection
let config = LinearConfig::default()
    .with_preset(LinearPreset::Lasso)
    .with_lambda(1.0);

// Elastic Net - mix of L1 + L2
let config = LinearConfig::default()
    .with_preset(LinearPreset::ElasticNet)
    .with_lambda(1.0)
    .with_l1_ratio(0.5);  // 50% L1, 50% L2

let mut booster = LinearBooster::new(num_features, config);

// Fit on gradients (gradient boosting style)
booster.fit_on_gradients(&features, num_features, &gradients, &hessians)?;

// Predict
let predictions = booster.predict_batch(&features, num_features);

// Sparsity info (LASSO/ElasticNet)
booster.num_nonzero_weights()  // How many features selected
booster.selected_features()    // Which feature indices
```

**Critical:** `lambda >= 1e-6` is enforced. Setting `lambda=0` causes NaN on correlated features.

**Internal standardization:** The booster standardizes features internally. You don't need to pre-scale.

### LinearTreeBooster

Decision trees with Ridge regression in each leaf. Perfect for piecewise linear data.

```rust
use treeboost::learner::{LinearTreeBooster, LinearTreeConfig, TreeConfig, LinearConfig, LinearPreset};

let config = LinearTreeConfig::new()
    .with_tree_config(
        TreeConfig::default()
            .with_max_depth(3)           // Shallow - leaves do the work
            .with_min_samples_leaf(50)
    )
    .with_linear_config(
        LinearConfig::default()
            .with_preset(LinearPreset::Ridge)
            .with_lambda(0.1)
    )
    .with_min_samples_for_linear(20);    // Below this, use constant leaf

let mut booster = LinearTreeBooster::new(config);

// Needs both binned dataset AND raw features
booster.fit_on_gradients(&dataset, &raw_features, num_features, &gradients, &hessians)?;

let predictions = booster.predict_batch(&dataset, &raw_features, num_features);
```

**When to use:**

- Data has piecewise linear structure (tax brackets, physical systems)
- Want smoother predictions than standard trees
- Need 10-100x fewer trees for same accuracy

### TreeBooster

Standard histogram-based decision tree. The workhorse of GBDT.

```rust
use treeboost::learner::{TreeBooster, TreeConfig, WeakLearner};

let config = TreeConfig::default()
    .with_max_depth(6)
    .with_max_leaves(31)
    .with_lambda(1.0)
    .with_learning_rate(0.1);

let mut booster = TreeBooster::new(config);
booster.fit_on_gradients(&dataset, &gradients, &hessians, None)?;

if let Some(tree) = booster.tree() {
    tree.predict_batch_add(&dataset, &mut predictions);
}
```

---

## Preprocessing

Transforms that fit on training data and apply consistently to train/test. Serialize with your model.

### Scalers

```rust
use treeboost::preprocessing::{StandardScaler, MinMaxScaler, RobustScaler, Scaler};

// StandardScaler: zero mean, unit variance
let mut scaler = StandardScaler::new();
scaler.fit(&features, num_features)?;
scaler.transform(&mut features, num_features)?;

// MinMaxScaler: scale to [0, 1] or custom range
let mut scaler = MinMaxScaler::new(0.0, 1.0);

// RobustScaler: median/IQR (robust to outliers)
let mut scaler = RobustScaler::new();
```

**For linear models:** Scaling is essential. Linear models are sensitive to feature magnitudes.

**For trees:** Scaling helps with regularization fairness but isn't strictly required.

### Encoders

```rust
use treeboost::preprocessing::{FrequencyEncoder, LabelEncoder, OneHotEncoder, UnknownStrategy};

// FrequencyEncoder: category → count (best for trees)
let mut encoder = FrequencyEncoder::new();
encoder.fit(&categories)?;
let encoded = encoder.transform(&categories)?;

// LabelEncoder: string → integer
let mut encoder = LabelEncoder::new(UnknownStrategy::Error);  // or ::MapToZero
encoder.fit(&categories)?;

// OneHotEncoder: category → binary columns (for linear models)
// WARNING: High cardinality = memory explosion. Use for linear component only.
let mut encoder = OneHotEncoder::new(UnknownStrategy::Ignore);
encoder.fit(&categories)?;
let (encoded, num_new_cols) = encoder.transform(&data, num_features, cat_col_idx)?;
```

| Encoder                | Best For                              | Cardinality           |
| ---------------------- | ------------------------------------- | --------------------- |
| `FrequencyEncoder`     | Trees (GBDT)                          | Any                   |
| `LabelEncoder`         | Trees (GBDT)                          | Any                   |
| `OneHotEncoder`        | Linear models only                    | Low (<100 categories) |
| `OrderedTargetEncoder` | High-cardinality + leakage prevention | Any                   |

### Imputers

```rust
use treeboost::preprocessing::{SimpleImputer, ImputeStrategy, IndicatorImputer};

// SimpleImputer: fill missing values
let mut imputer = SimpleImputer::new(ImputeStrategy::Mean);  // or Median, Mode, Constant(0.0)
imputer.fit(&features, num_features)?;
imputer.transform(&mut features, num_features)?;

// IndicatorImputer: add binary "was_missing" columns
let mut indicator = IndicatorImputer::new();
indicator.fit(&features, num_features)?;
let (features_with_indicators, new_num_features) = indicator.transform(&features, num_features)?;
```

### PipelineBuilder

Chain transforms together:

```rust
use treeboost::preprocessing::{PipelineBuilder, ImputeStrategy};

let pipeline = PipelineBuilder::new()
    .add_simple_imputer(&["age", "income"], ImputeStrategy::Median)
    .add_standard_scaler(&["age", "income"])
    .add_frequency_encoder(&["category", "region"])
    .build();

// Fit on training data
pipeline.fit(&train_df)?;

// Transform (same pipeline state for train and test)
let train_transformed = pipeline.transform(&train_df)?;
let test_transformed = pipeline.transform(&test_df)?;
```

### Time-Series Features

```rust
use treeboost::preprocessing::{LagGenerator, RollingGenerator, RollingStat, EwmaGenerator};

// Lag features: x_{t-1}, x_{t-2}, ...
let lag_gen = LagGenerator::new(vec![1, 2, 7]);  // 1-day, 2-day, 7-day lags

// Rolling statistics
let rolling = RollingGenerator::new(7, vec![RollingStat::Mean, RollingStat::Std]);

// Exponentially weighted moving average
let ewma = EwmaGenerator::new(0.3);  // alpha = 0.3
```

## GBDTModel (Classic)

The original GBDT-only API. Still fully supported—use it if you don't need hybrid modes.

> **Tip:** `GBDTModel` is equivalent to `UniversalModel` with `BoostingMode::PureTree`.

### GBDTConfig

Configuration object for gradient boosted decision tree training. Uses a builder pattern for chaining configurations.

**Rust Creation:**

```rust
use treeboost::GBDTConfig;

let config = GBDTConfig::new()
    .with_num_rounds(100)
    .with_max_depth(6)
    .with_learning_rate(0.1)
    .with_lambda(1.0)
    .with_entropy_weight(0.1)
    .with_subsample(0.8)
    .with_colsample(0.8)
    .with_seed(42);
```

**Python Creation:**

```python
from treeboost import GBDTConfig

config = GBDTConfig()
config.num_rounds = 100
config.max_depth = 6
config.learning_rate = 0.1
config.lambda_reg = 1.0
config.entropy_weight = 0.1
config.subsample = 0.8
config.colsample = 0.8
config.seed = 42
```

**Core Hyperparameters:**

| Parameter         | Type  | Default | Description                            |
| ----------------- | ----- | ------- | -------------------------------------- |
| `num_rounds`      | int   | 100     | Number of boosting iterations (trees)  |
| `max_depth`       | int   | 6       | Maximum tree depth                     |
| `learning_rate`   | float | 0.1     | Shrinkage per boosting round (0.0-1.0) |
| `max_leaves`      | int   | 31      | Maximum leaves per tree                |
| `lambda`          | float | 1.0     | L2 leaf regularization                 |
| `min_split_gain`  | float | 0.0     | Minimum gain to split a node           |
| `min_leaf_weight` | float | 0.0     | Minimum sum of weights in leaf         |

**Advanced Hyperparameters:**

| Parameter           | Type        | Default | Description                                            |
| ------------------- | ----------- | ------- | ------------------------------------------------------ |
| `entropy_weight`    | float       | 0.0     | Shannon entropy penalty (0.0-1.0) for drift prevention |
| `subsample`         | float       | 1.0     | Row sampling ratio per round (0.0-1.0)                 |
| `colsample`         | float       | 1.0     | Feature sampling ratio per tree (0.0-1.0)              |
| `colsample_bylevel` | float       | 1.0     | Feature sampling ratio per level (0.0-1.0)             |
| `seed`              | Option<u64> | None    | Random seed for reproducibility                        |

**Loss Function Selection:**

```rust
// Regression losses (default: MSE)
config.with_mse_loss()                          // Mean Squared Error
config.with_pseudo_huber_loss(delta: 1.0)      // Robust to outliers

// Classification losses
config.with_binary_logloss()                    // Binary classification
config.with_multiclass_logloss(num_classes: 10)// Multi-class classification
```

**Conformal Prediction (Uncertainty):**

```rust
config
    .with_calibration_ratio(0.2)    // Reserve 20% of data for calibration
    .with_conformal_quantile(0.9)   // 90% prediction intervals (0.5-0.99)
```

**Constraints:**

```rust
use treeboost::tree::MonotonicConstraint;

// Monotonic constraints enforce monotonicity on features
config.set_monotonic_constraints(vec![
    MonotonicConstraint::Increasing,   // Feature 0 must increase
    MonotonicConstraint::None,         // Feature 1 unrestricted
    MonotonicConstraint::Decreasing,   // Feature 2 must decrease
]);

// Interaction groups restrict feature interactions
config.set_interaction_groups(vec![
    vec![0, 1, 2],  // Features 0,1,2 can interact with each other
    vec![3, 4],     // Features 3,4 in separate interaction group
    vec![5],        // Feature 5 cannot interact with others
]);
```

**Early Stopping:**

```rust
config.with_early_stopping(patience: 10, val_ratio: 0.2);
// Stops if validation loss doesn't improve for 10 rounds
// Uses 20% of data for validation
```

**Backend Selection:**

```rust
use treeboost::backend::BackendType;

config.with_backend(BackendType::Auto);    // (Default) Auto-select best backend
config.with_backend(BackendType::Cuda);    // NVIDIA CUDA GPU
config.with_backend(BackendType::Wgpu);    // WebGPU (all GPUs)
config.with_backend(BackendType::Avx512);  // AVX-512 CPU (x86-64 only)
config.with_backend(BackendType::Sve2);    // SVE2 CPU (ARM only)
config.with_backend(BackendType::Scalar);  // Scalar fallback (any CPU)
```

---

### GBDTModel

Trained gradient boosted decision tree model. All predictions use the CPU (no GPU overhead for inference).

**Training from BinnedDataset (Recommended):**

**Rust:**

```rust
use treeboost::{GBDTModel, GBDTConfig};
use treeboost::dataset::DatasetLoader;

let loader = DatasetLoader::new(255);  // 255 bins
let dataset = loader.load_parquet("train.parquet", "target", None)?;
let model = GBDTModel::train_binned(&dataset, config)?;
```

**Python:**

```python
from treeboost import GBDTModel, DatasetLoader

loader = DatasetLoader(num_bins=255)
dataset = loader.load_parquet("train.parquet", target="target")
model = GBDTModel.train_binned(dataset, config)
```

**Training from Raw Arrays:**

**Rust:**

```rust
let model = GBDTModel::train(
    &features,     // &[f32] in column-major layout
    num_features,  // usize
    &targets,      // &[f32]
    config,        // GBDTConfig
    None           // Optional feature names
)?;
```

**Python:**

```python
import numpy as np

X = np.random.randn(10000, 50).astype(np.float32)
y = np.random.randn(10000).astype(np.float32)

model = GBDTModel.train(X, y, config)
```

**Train and Save in One Step:**

**Rust:**

```rust
use treeboost::ModelFormat;

let model = GBDTModel::train_with_output(
    &dataset,
    config.clone(),
    "output_dir",
    &[ModelFormat::Rkyv, ModelFormat::Bincode]
)?;
// Saves to: output_dir/model.rkyv, output_dir/model.bin, output_dir/config.json
```

**Python:**

```python
model = GBDTModel.train(X, y, config, output_dir="models/my_model")
# Saves to: models/my_model/model.rkyv and models/my_model/config.json
```

**Making Predictions:**

**Rust:**

```rust
// Point predictions only
let predictions: Vec<f32> = model.predict(&dataset);

// With confidence intervals (if conformal enabled)
let (predictions, lower, upper) = model.predict_with_intervals(&dataset);

// Feature importance (gain-based)
let importances = model.feature_importance();

// Model metadata
let num_trees = model.num_trees();
```

**Python:**

```python
# Point predictions
predictions = model.predict(X)

# With intervals
predictions, lower, upper = model.predict_with_intervals(X)

# Feature importance
importances = model.feature_importance()

# Metadata
num_trees = model.num_trees()
```

**Serialization:**

**Rust:**

```rust
use treeboost::serialize::{save_model, load_model, save_model_bincode, load_model_bincode};
use treeboost::ModelFormat;

// rkyv format (zero-copy, fastest loading)
save_model(&model, "model.rkyv")?;
let model = load_model("model.rkyv")?;

// bincode format (compact, smaller files)
save_model_bincode(&model, "model.bin")?;
let model = load_model_bincode("model.bin")?;

// Save with config.json for reproducibility
model.save_to_directory(
    "output_dir",
    &config,
    &[ModelFormat::Rkyv, ModelFormat::Bincode]
)?;
// Creates: output_dir/model.rkyv, output_dir/model.bin, output_dir/config.json
```

**Python:**

```python
# Save single format
model.save("model.rkyv", format="rkyv")
model.save("model.bin", format="bincode")

# Load model
model = GBDTModel.load("model.rkyv", format="rkyv")

# Save with config
model.save_to_directory("output_dir", config, formats=["rkyv", "bincode"])
```

**Methods:**

| Method                                    | Returns                        | Description                            |
| ----------------------------------------- | ------------------------------ | -------------------------------------- |
| `num_trees()`                             | usize                          | Number of trees in ensemble            |
| `predict(dataset)`                        | Vec<f32>                       | Point predictions                      |
| `predict_with_intervals(dataset)`         | (Vec<f32>, Vec<f32>, Vec<f32>) | Predictions with lower/upper bounds    |
| `feature_importance()`                    | Vec<f32>                       | Feature importance scores (gain-based) |
| `save_to_directory(dir, config, formats)` | Result<()>                     | Save model + config.json               |

---

## AutoTuner

Automatic hyperparameter optimization using efficient search strategies.

### TunerConfig

Configuration for the AutoTuner.

**Rust:**

```rust
use treeboost::tuner::{TunerConfig, GridStrategy, EvalStrategy};

let config = TunerConfig::new()
    .with_iterations(3)                                      // Number of refinement iterations
    .with_grid_strategy(GridStrategy::LatinHypercube { n_samples: 50 })
    .with_eval_strategy(EvalStrategy::holdout(0.2))          // 20% validation split
    .with_verbose(true)                                      // Print progress
    .with_output_dir("results/")                             // Save trial logs
    .with_save_model_formats(vec![ModelFormat::Rkyv]);       // Save best models
```

**Python:**

```python
from treeboost import TunerConfig, GridStrategy, EvalStrategy

config = (
    TunerConfig.preset("quick")  # Preset: 2 iterations, simple space
    .with_grid_strategy(GridStrategy.lhs(50))
    .with_eval_strategy(EvalStrategy.holdout(0.2))
    .with_verbose(True)
)
# Other presets: "smoketest", "balanced", "thorough"
```

### GridStrategy

Hyperparameter space sampling strategy.

```rust
use treeboost::tuner::GridStrategy;

// Cartesian product grid (exhaustive)
GridStrategy::Cartesian { points_per_dim: 3 }
// Tries 3^n configurations for n parameters

// Latin Hypercube Sampling (efficient exploration)
GridStrategy::LatinHypercube { n_samples: 50 }
// Evenly samples 50 points in parameter space

// Random search
GridStrategy::Random { n_samples: 50 }
// Randomly samples 50 points
```

**Python:**

```python
from treeboost import GridStrategy

GridStrategy.cartesian(3)      # 3 points per dimension
GridStrategy.lhs(50)           # 50 LHS samples
GridStrategy.random(50)        # 50 random samples
```

### EvalStrategy

Evaluation strategy for measuring hyperparameter quality.

```rust
use treeboost::tuner::EvalStrategy;

// Holdout split
EvalStrategy::holdout(0.2)                // 20% validation set
    .with_folds(5)                        // Add 5-fold CV on validation set

// Conformal prediction
EvalStrategy::conformal(0.1, 0.9)         // 10% calibration, 90% coverage
```

**Python:**

```python
from treeboost import EvalStrategy

EvalStrategy.holdout(0.2)                 # 20% holdout
EvalStrategy.holdout(0.2).with_folds(5)   # 5-fold CV
EvalStrategy.conformal(0.1, 0.9)          # Conformal intervals
```

### ParameterSpace

Defines the hyperparameter search space.

**Rust:**

```rust
use treeboost::tuner::{ParameterSpace, ParamBounds, SpacePreset};

let space = ParameterSpace::new()
    .with_param("max_depth", ParamBounds::discrete(3, 10), 6.0)
    .with_param("learning_rate", ParamBounds::log_continuous(0.005, 0.5), 0.05)
    .with_param("lambda", ParamBounds::continuous(0.0, 5.0), 1.0);

// Or use presets
let space = ParameterSpace::with_preset(SpacePreset::Regression);     // Best for regression
let space = ParameterSpace::with_preset(SpacePreset::Classification); // Best for classification
let space = ParameterSpace::with_preset(SpacePreset::Minimal);        // Quick tuning: depth + LR only
```

**Python:**

```python
from treeboost import ParameterSpace, ParamBounds

space = ParameterSpace()
space.add_discrete("max_depth", [3, 4, 5, 6, 7, 8, 9, 10])
space.add_continuous("learning_rate", 0.005, 0.5, scale="log")
space.add_continuous("lambda", 0.0, 5.0)

# Or presets
space = ParameterSpace.preset("regression")
space = ParameterSpace.preset("classification")
```

### AutoTuner Usage

**Rust:**

```rust
use treeboost::{AutoTuner, GBDTConfig};

let base_config = GBDTConfig::new().with_num_rounds(100).with_seed(42);

let mut tuner = AutoTuner::new(base_config)
    .with_config(tuner_config)
    .with_space(param_space)
    .with_seed(42)
    .with_callback(|trial, current, total| {
        println!("Trial {}/{}: val_loss={:.6}", current, total, trial.val_metric);
    });

let (best_config, history) = tuner.tune(&dataset)?;

// Access results
println!("Best validation loss: {:.6}", history.best().unwrap().val_metric);
println!("Total trials: {}", history.len());
println!("Best parameters: {:?}", history.best().unwrap().params);

// Train final model
let final_model = GBDTModel::train_binned(&dataset, best_config)?;
```

**Python:**

```python
from treeboost import AutoTuner

tuner = AutoTuner(base_config)
tuner.config = tuner_config
tuner.space = param_space

best_config, history = tuner.tune(X, y)

# Access results
print(f"Best validation loss: {history.best().val_metric:.6f}")
print(f"Total trials: {history.len()}")
for trial in history.top_n(5):
    print(f"  Trial {trial.trial_id}: loss={trial.val_metric:.6f}")

# Train final model
final_model = GBDTModel.train(X, y, best_config)
```

### SearchHistory

Results from hyperparameter search.

**Methods:**

| Method     | Returns          | Description                     |
| ---------- | ---------------- | ------------------------------- |
| `len()`    | int              | Total number of trials          |
| `best()`   | TrialResult      | Best trial by validation metric |
| `top_n(n)` | Vec<TrialResult> | Top n trials                    |
| `trials()` | Vec<TrialResult> | All trials                      |

### TrialResult

Result of a single hyperparameter trial.

**Fields:**

| Field          | Type             | Description                       |
| -------------- | ---------------- | --------------------------------- |
| `trial_id`     | int              | Unique trial identifier           |
| `iteration`    | int              | Which iteration (1-3 typically)   |
| `val_metric`   | float            | Validation loss/metric            |
| `train_metric` | float            | Training loss                     |
| `f1_score`     | Option<float>    | F1 score (if classification)      |
| `roc_auc`      | Option<float>    | ROC-AUC score (if classification) |
| `num_trees`    | int              | Number of trees in this trial     |
| `params`       | Dict[str, float] | Hyperparameters used              |

---

## Dataset

Data loading and preparation utilities.

### DatasetLoader

Loads data from various formats and performs binning.

**Rust:**

```rust
use treeboost::dataset::DatasetLoader;

let loader = DatasetLoader::new(255);  // 255 bins

// From Parquet
let dataset = loader.load_parquet("train.parquet", "target", None)?;

// From CSV
let dataset = loader.load_csv(
    "train.csv",
    "target",
    Some(vec!["feature1", "feature2"])  // Optional feature selection
)?;

// From raw arrays
let dataset = loader.from_arrays(
    &features,  // &[f32]
    &targets,   // &[f32]
    Some(vec!["f0", "f1", "f2"])  // Feature names
)?;
```

**Python:**

```python
from treeboost import DatasetLoader

loader = DatasetLoader(num_bins=255)

dataset = loader.load_parquet("train.parquet", target="target")
dataset = loader.load_csv("train.csv", target="target")

import numpy as np
X = np.random.randn(10000, 50).astype(np.float32)
y = np.random.randn(10000).astype(np.float32)
dataset = loader.from_numpy(X, y, feature_names=[f"f{i}" for i in range(50)])
```

### BinnedDataset

Quantized dataset with feature binning.

**Properties:**

```rust
dataset.num_rows()                    // usize
dataset.num_features()                // usize
dataset.feature_info()                // Vec<FeatureInfo>
dataset.targets()                     // &[f32]
dataset.feature_names()               // Vec<&str>
```

---

## Loss Functions

Training objectives for regression and classification.

**Rust:**

```rust
use treeboost::loss::{MseLoss, PseudoHuberLoss, BinaryLogLoss};

let mse = MseLoss::new();
let loss_value = mse.loss(target, prediction);
let grad = mse.gradient(target, prediction);
let hess = mse.hessian(target, prediction);
let (grad, hess) = mse.gradient_hessian(target, prediction);

let huber = PseudoHuberLoss::new(delta: 1.0);
// Same methods as MSE

let logloss = BinaryLogLoss::new();
let prob = logloss.to_probability(raw_logit);
let class = logloss.to_class(prob, threshold: 0.5);
```

**Supported Loss Functions:**

- `MseLoss` - Mean Squared Error (regression)
- `PseudoHuberLoss` - Huber loss (robust regression, outlier-resistant)
- `BinaryLogLoss` - Logistic loss (binary classification)
- `MultiClassLogLoss` - Softmax loss (multi-class classification)

---

## Inference

Prediction and uncertainty quantification.

### Prediction with Intervals

**Rust:**

```rust
use treeboost::inference::Prediction;

// Without intervals
let pred = Prediction::point(0.5);
assert_eq!(pred.point, 0.5);

// With conformal intervals
let pred = Prediction::with_interval(0.5, 0.3, 0.7);
assert_eq!(pred.point, 0.5);
assert_eq!(pred.lower, 0.3);
assert_eq!(pred.upper, 0.7);
assert_eq!(pred.interval_width, 0.4);
```

### ConformalPredictor

Uncertainty quantification using split conformal prediction.

**Rust:**

```rust
use treeboost::inference::ConformalPredictor;

// From residuals (training - predictions on calibration set)
let predictor = ConformalPredictor::from_residuals(residuals, coverage: 0.9)?;

// From quantile directly
let predictor = ConformalPredictor::from_quantile(quantile: 0.15, coverage: 0.9)?;

// Single prediction
let pred = predictor.predict(point_pred);

// Batch predictions
let preds = predictor.predict_batch(&point_predictions);

// Measurement
let actual_coverage = predictor.empirical_coverage(&y_true, &predictions);
```

---

## Encoding

High-cardinality categorical feature handling.

### OrderedTargetEncoder

Stateful encoding of categorical features without target leakage.

**Rust:**

```rust
use treeboost::encoding::OrderedTargetEncoder;

let mut encoder = OrderedTargetEncoder::new(smoothing: 10.0);

// Training (streaming encoding)
for (category, target) in training_pairs {
    let encoded_val = encoder.encode_and_update(&category, target);
}

// Inference (fixed statistics)
let encoded = encoder.encode_inference("category_value");

// Get serializable mapping
let mapping = encoder.get_encoding_map();
let encoded_batch = mapping.encode_batch(&categories);
```

### CategoryFilter

Rare category filtering using Count-Min Sketch.

**Rust:**

```rust
use treeboost::encoding::CategoryFilter;

let filter = CategoryFilter::default_for_high_cardinality();
// Or: CategoryFilter::new(eps: 0.001, confidence: 0.99, min_count: 5)

// Two-pass workflow
filter.count_batch(&categories);      // First pass: count
filter.finalize();                     // Mark frequent ones
let is_frequent = filter.is_frequent("category");
let filtered = filter.filter_batch(&categories);  // "rare" → "unknown"
```

---

## Serialization

Model persistence and format handling.

### ModelFormat

Enumeration of supported serialization formats.

```rust
use treeboost::ModelFormat;

ModelFormat::Rkyv      // Zero-copy (fastest loading)
ModelFormat::Bincode   // Compact binary format
```

### Save/Load Functions

**Rust:**

```rust
use treeboost::serialize::{save_model, load_model, save_model_bincode, load_model_bincode};

// rkyv (recommended for production)
save_model(&model, "model.rkyv")?;
let model = load_model("model.rkyv")?;

// bincode (compact files)
save_model_bincode(&model, "model.bin")?;
let model = load_model_bincode("model.bin")?;
```

**Python:**

```python
from treeboost import GBDTModel

model.save("model.rkyv", format="rkyv")
model = GBDTModel.load("model.rkyv", format="rkyv")
```

---

## Incremental Learning

Update models with new data without full retraining. TreeBoost provides a custom TRB (TreeBoost) file format optimized for incremental updates.

### Why Incremental Learning?

| Scenario | Without Incremental | With Incremental |
|----------|---------------------|------------------|
| Daily model updates | Retrain on full history | Add trees from new data only |
| Storage for updates | New file per version | Single file, append updates |
| Compute cost | O(total_data) | O(new_data) |
| Recovery from crash | Restart training | Resume from last checkpoint |

### TRB Workflow Overview

TRB format stores `UniversalModel` only. The typical workflow:

1. **Initial training** — Use `AutoModel` for convenience (handles DataFrames)
2. **Save to TRB** — Extract `UniversalModel` and save
3. **Incremental updates** — Load `UniversalModel`, update with `BinnedDataset`
4. **Inference** — Load `UniversalModel`, predict with `BinnedDataset`

```rust
use treeboost::{AutoModel, UniversalModel};
use treeboost::dataset::DatasetLoader;
use treeboost::loss::MseLoss;

// 1. Initial training via AutoModel (convenience)
let auto = AutoModel::train(&df, "target")?;

// 2. Save UniversalModel to TRB
auto.inner().save_trb("model.trb", "Initial training")?;

// 3. Later: Load and update (UniversalModel + BinnedDataset)
let mut model = UniversalModel::load_trb("model.trb")?;
let loader = DatasetLoader::new(255);
let new_data = loader.load_parquet("new_data.parquet", "target", None)?;

let report = model.update(&new_data, &MseLoss, 10)?;
println!("Trees: {} -> {} (+{})", report.trees_before, report.trees_after, report.trees_added);

// 4. Append update to file (O(1), no rewrite)
model.save_trb_update("model.trb", new_data.num_rows(), "Update batch")?;

// 5. Inference
let model = UniversalModel::load_trb("model.trb")?;
let predictions = model.predict(&new_data);
```

### IncrementalUpdateReport

Returned by `UniversalModel::update()`:

```rust
pub struct IncrementalUpdateReport {
    pub trees_before: usize,      // Tree count before update
    pub trees_after: usize,       // Tree count after update
    pub trees_added: usize,       // trees_after - trees_before
    pub mode: BoostingMode,       // PureTree, LinearThenTree, or RandomForest
}
```

### TRB File Format

Custom journaled format for incremental model persistence:

```
┌─────────────────────────────────────────────────────────────┐
│ TrbHeader (JSON + length prefix)                            │
│   - magic: "TRB1"                                           │
│   - format_version: 1                                       │
│   - model_type: "UniversalModel"                           │
│   - boosting_mode: "PureTree" | "LinearThenTree" | ...     │
│   - num_features: 50                                        │
│   - created_at: 1704067200 (Unix timestamp)                │
│   - metadata: "Initial training description"                │
│   - base_blob_size: 1234567                                │
├─────────────────────────────────────────────────────────────┤
│ Base Model Blob (rkyv serialized UniversalModel)            │
│   + CRC32 checksum (4 bytes)                               │
├─────────────────────────────────────────────────────────────┤
│ Update 1: TrbUpdateHeader (JSON) + Blob + CRC32            │
│   - update_type: Trees | Linear | Full                     │
│   - created_at: timestamp                                   │
│   - rows_trained: 5000                                     │
│   - description: "February update"                         │
├─────────────────────────────────────────────────────────────┤
│ Update 2: TrbUpdateHeader + Blob + CRC32                   │
│ ...                                                         │
└─────────────────────────────────────────────────────────────┘
```

**Key Properties:**

- **O(1) appends** — Updates append to file end, base model never rewritten
- **CRC32 per segment** — Corruption detected at segment level
- **Crash recovery** — Truncated writes detected and skipped
- **Forward compatible** — Unknown JSON fields ignored (safe for version upgrades)
- **File locking** — Exclusive locks prevent concurrent writes

### TrbReader / TrbWriter

Low-level API for TRB file manipulation:

```rust
use treeboost::serialize::{TrbWriter, TrbReader, TrbHeader, TrbUpdateHeader, UpdateType};

// Writing
let header = TrbHeader {
    format_version: 1,
    model_type: "UniversalModel".to_string(),
    boosting_mode: "PureTree".to_string(),
    num_features: 50,
    created_at: current_timestamp(),
    metadata: "My model".to_string(),
    base_blob_size: base_blob.len() as u64,
};

let writer = TrbWriter::new("model.trb", header, &base_blob)?;
drop(writer);  // Finishes write

// Appending updates
let mut writer = open_for_append("model.trb")?;
let update_header = TrbUpdateHeader {
    update_type: UpdateType::Trees,
    created_at: current_timestamp(),
    rows_trained: 5000,
    description: "February update".to_string(),
};
writer.append_update(&update_header, &update_blob)?;

// Reading
let mut reader = TrbReader::open("model.trb")?;
let header = reader.header();  // &TrbHeader
let base_blob = reader.read_base_blob()?;

// Iterate updates (handles crash recovery automatically)
for (update_header, blob) in reader.iter_updates()? {
    println!("Update: {} rows", update_header.rows_trained);
}

// Load all segments at once
let segments = reader.load_all_segments()?;
```

### MmapTrbReader (mmap feature)

Memory-mapped TRB reader for true zero-copy I/O. Requires the `mmap` feature:

```bash
cargo build --release --features mmap
```

**When to use:**
- Large models (100MB+) where heap allocation is expensive
- Inference servers requiring minimal startup time
- Running multiple model instances (OS deduplicates pages)
- Memory-constrained environments

**Comparison:**

| Reader | Load Time | Memory | Use Case |
|--------|-----------|--------|----------|
| `TrbReader` | O(model_size) | O(model_size) | Default, works everywhere |
| `MmapTrbReader` | O(1) initial | O(1) initial | Large models, servers |

**Usage:**

```rust
#[cfg(feature = "mmap")]
{
    use treeboost::serialize::MmapTrbReader;

    // Open with memory mapping - instant, no heap allocation
    let reader = MmapTrbReader::open("model.trb")?;

    // Option 1: Deserialize (still faster than TrbReader due to mmap)
    let model = reader.load_model()?;
    let predictions = model.predict(&dataset);

    // Option 2: Zero-copy blob access
    let base_blob = reader.base_blob_bytes()?;  // &[u8] into mmap

    // Iterate updates (zero-copy slices)
    for (header, blob) in reader.iter_updates()? {
        println!("Update: {} rows", header.rows_trained);
    }
}
```

**API:**

| Method | Description |
|--------|-------------|
| `open(path)` | Open TRB file with memory mapping |
| `header()` | Get the `TrbHeader` |
| `load_model()` | Deserialize `UniversalModel` |
| `base_blob_bytes()` | Zero-copy access to base blob |
| `iter_updates()` | Iterate update segments (zero-copy) |
| `mapped_size()` | Size of memory-mapped region |

### UniversalModel Incremental API

Lower-level API for custom update logic:

```rust
use treeboost::{UniversalModel, UniversalConfig, BoostingMode};
use treeboost::loss::MseLoss;

// Train initial model
let config = UniversalConfig::new()
    .with_mode(BoostingMode::PureTree)
    .with_num_rounds(100);
let mut model = UniversalModel::train(&dataset, config, &MseLoss)?;

// Update with new data
let report = model.update(&new_dataset, &MseLoss, 10)?;  // Add 10 trees
println!("Added {} trees", report.trees_added);

// Save/load TRB
model.save_trb("model.trb", "description")?;
let loaded = UniversalModel::load_trb("model.trb")?;
```

### IncrementalUpdateReport

Returned by `UniversalModel::update()`:

```rust
pub struct IncrementalUpdateReport {
    pub trees_before: usize,
    pub trees_after: usize,
    pub trees_added: usize,
    pub mode: BoostingMode,
}
```

### Drift Detection

Monitor distribution shifts before updating:

```rust
use treeboost::monitoring::{
    IncrementalDriftDetector,
    DriftRecommendation,
    check_drift
};

// Create detector from training data
let detector = IncrementalDriftDetector::from_dataset(&train_data)
    .with_thresholds(0.1, 0.25);  // warning, critical

// Check new data before updating
let result = detector.check_update(&new_data);

println!("Mean drift: {:.4}", result.mean_drift);
println!("Max drift feature: {:?}", result.max_drift_feature);
println!("Alert level: {:?}", result.shift_result.alert);

match result.recommendation {
    DriftRecommendation::ProceedNormally => {
        // Safe to update incrementally
        model.update(&new_data, &loss, 10)?;
    }
    DriftRecommendation::ProceedWithCaution => {
        // Update but monitor performance closely
        model.update(&new_data, &loss, 10)?;
    }
    DriftRecommendation::ConsiderRetrain => {
        // Significant drift - consider full retrain
        println!("Warning: {}", result);
    }
    DriftRecommendation::RetrainRecommended => {
        // Critical drift - full retrain recommended
        println!("Critical: {}", result);
    }
}
```

### DriftHistory

Track drift across multiple updates:

```rust
use treeboost::monitoring::DriftHistory;

let mut history = DriftHistory::new();

// Record each update's drift check
for batch in batches {
    let result = detector.check_update(&batch);
    history.record(&result);

    if !result.has_critical_drift() {
        model.update(&batch, &loss, 10)?;
    }
}

println!("{}", history);
// Output:
// Drift History (10 updates):
//   Drift rate: 20.0% (1 warnings, 1 critical)
//   Mean drift: 0.0823, Max drift: 0.3412
//   Frequently drifted:
//     - feature_5 (3 times)
//     - feature_12 (2 times)
```

### EMA-Based Scaler Updates

StandardScaler supports exponential moving average for adapting to drift:

```rust
use treeboost::preprocessing::StandardScaler;

// Create scaler with EMA mode (alpha=0.1)
let mut scaler = StandardScaler::with_forget_factor(0.1);

// Each partial_fit blends new statistics with history
// new_stat = (1 - alpha) * old_stat + alpha * batch_stat
scaler.partial_fit(&batch1, num_features)?;  // 100% batch1
scaler.partial_fit(&batch2, num_features)?;  // 90% batch1, 10% batch2
scaler.partial_fit(&batch3, num_features)?;  // 81% batch1, 9% batch2, 10% batch3

// After 10 batches, batch1 influence: (1-0.1)^10 ≈ 35%
```

### CLI for Incremental Learning

```bash
# Update existing TRB model with new data
treeboost update --model model.trb --data new.csv --target price \
  --rounds 10 --description "March 2024 update"

# Inspect TRB file (shows update history)
treeboost info --model model.trb

# Force load despite corrupted updates
treeboost info --model model.trb --force
```

### Method Summary

**UniversalModel (TRB format):**

| Method | Returns | Description |
|--------|---------|-------------|
| `load_trb(path)` | Result<UniversalModel> | Load model from TRB file |
| `save_trb(path, desc)` | Result<()> | Save model to TRB format |
| `save_trb_update(path, rows, desc)` | Result<()> | Append update to existing TRB |
| `update(&dataset, &loss, rounds)` | Result<IncrementalUpdateReport> | Add trees from BinnedDataset |
| `predict(&dataset)` | Vec<f32> | Inference on BinnedDataset |

**AutoModel (initial training only):**

| Method | Returns | Description |
|--------|---------|-------------|
| `train(&df, target)` | Result<AutoModel> | Train from DataFrame |
| `inner()` | &UniversalModel | Get underlying model for TRB save |

**IncrementalDriftDetector:**

| Method | Returns | Description |
|--------|---------|-------------|
| `from_dataset(&data)` | Self | Create from reference distribution |
| `with_thresholds(warn, crit)` | Self | Set PSI thresholds |
| `check_update(&data)` | IncrementalDriftResult | Check for drift |

---

## Backend Selection

Hardware acceleration and device configuration.

### BackendType

Enumeration of available compute backends.

```rust
use treeboost::backend::BackendType;

BackendType::Auto      // Auto-select best available (default)
BackendType::Cuda      // NVIDIA CUDA GPU
BackendType::Wgpu      // WebGPU (portable, all GPUs)
BackendType::Avx512    // AVX-512 CPU (x86-64 only)
BackendType::Sve2      // SVE2 CPU (ARM only)
BackendType::Scalar    // Scalar fallback (any CPU, safest)
```

### Backend Performance

| Backend | Hardware   | Training Speed | Inference | Best For                  |
| ------- | ---------- | -------------- | --------- | ------------------------- |
| CUDA    | NVIDIA GPU | 10-50x faster  | CPU       | Large datasets on NVIDIA  |
| WGPU    | Any GPU    | 5-20x faster   | CPU       | Portability, any GPU      |
| AVX-512 | x86-64 CPU | 3-5x faster    | CPU       | CPU-only with modern CPUs |
| SVE2    | ARM CPU    | 2-3x faster    | CPU       | ARM servers/clusters      |
| Scalar  | Any CPU    | 1x (baseline)  | CPU       | Maximum compatibility     |

**Inference Note:** All backends use optimized CPU inference (Rayon parallelism). GPU acceleration is for training only, so deployment doesn't require expensive GPU VMs.

---

## Complete Example

**Rust:**

```rust
use treeboost::{GBDTConfig, GBDTModel, AutoTuner, TunerConfig, GridStrategy, EvalStrategy, ParameterSpace, ModelFormat};
use treeboost::dataset::DatasetLoader;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 1. Load and prepare data
    let loader = DatasetLoader::new(255);
    let dataset = loader.load_parquet("train.parquet", "target", None)?;

    // 2. Configure base model
    let base_config = GBDTConfig::new()
        .with_num_rounds(100)
        .with_seed(42);

    // 3. Tune hyperparameters
    let tuner_config = TunerConfig::new()
        .with_iterations(3)
        .with_grid_strategy(GridStrategy::LatinHypercube { n_samples: 50 })
        .with_eval_strategy(EvalStrategy::holdout(0.2).with_folds(5))
        .with_verbose(true);

    let mut tuner = AutoTuner::new(base_config)
        .with_config(tuner_config)
        .with_space(ParameterSpace::with_preset(SpacePreset::Regression))
        .with_seed(42);

    let (best_config, history) = tuner.tune(&dataset)?;
    println!("Best validation loss: {:.6}", history.best().unwrap().val_metric);

    // 4. Train final model
    let model = GBDTModel::train_binned(&dataset, best_config.clone())?;

    // 5. Make predictions
    let predictions = model.predict(&dataset);

    // 6. Save model with config
    model.save_to_directory(
        "final_model",
        &best_config,
        &[ModelFormat::Rkyv, ModelFormat::Bincode]
    )?;

    println!("Model trained with {} trees", model.num_trees());
    Ok(())
}
```

**Python:**

```python
from treeboost import GBDTConfig, GBDTModel, AutoTuner, DatasetLoader
from treeboost import TunerConfig, GridStrategy, EvalStrategy, ParameterSpace

# 1. Load data
loader = DatasetLoader(num_bins=255)
dataset = loader.load_parquet("train.parquet", target="target")

# 2. Configure and tune
base_config = GBDTConfig().with_num_rounds(100).with_seed(42)

tuner = AutoTuner(base_config)
tuner.config = (
    TunerConfig.preset("thorough")
    .with_grid_strategy(GridStrategy.lhs(50))
    .with_eval_strategy(EvalStrategy.holdout(0.2).with_folds(5))
)
tuner.space = ParameterSpace.preset("regression")

best_config, history = tuner.tune(X, y)

# 3. Train final model
model = GBDTModel.train(X, y, best_config)

# 4. Predict
predictions = model.predict(X)

# 5. Save
model.save_to_directory("final_model", best_config, formats=["rkyv", "bincode"])
```