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
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
//! UniversalModel core implementation
//!
//! This module contains the UniversalModel struct and all its implementations.
//! Types like BoostingMode, ModeSelection, and UniversalConfig are defined in
//! separate submodules (mode.rs, config.rs) for better organization.

use super::{BoostingMode, ModeSelection, UniversalConfig};
use crate::analysis::{AnalysisConfig, Confidence, DatasetAnalysis};
use crate::booster::{GBDTConfig, GBDTModel};
use crate::dataset::BinnedDataset;
use crate::learner::{LinearBooster, TreeBooster, WeakLearner};
use crate::loss::LossFunction;
use crate::tree::Tree;
use crate::utils::features::extract_selected_features;
use crate::Result;
use rand::SeedableRng;
use rayon::prelude::*;
use rkyv::{Archive, Deserialize, Serialize};

// Note: BoostingMode, ModeSelection, and UniversalConfig are now defined in
// separate modules (mode.rs and config.rs) and imported via super::.

// =============================================================================
// UniversalModel
// =============================================================================

/// Use [`UniversalModel::auto()`] to let TreeBoost analyze your data and pick the best mode.
/// The analysis result is stored and can be retrieved with [`UniversalModel::analysis()`].
#[derive(Debug, Clone, Archive, Serialize, Deserialize, serde::Serialize, serde::Deserialize)]
pub struct UniversalModel {
    /// Training configuration
    config: UniversalConfig,

    /// GBDTModel for PureTree mode (wraps the mature implementation)
    /// Used when ensemble_seeds is None
    gbdt_model: Option<GBDTModel>,

    /// Multi-seed GBDT ensemble (when ensemble_seeds is Some)
    /// - For PureTree mode: Multiple GBDTs trained directly
    /// - For LinearThenTree mode: Multiple GBDTs trained on linear residuals
    /// - For RandomForest mode: Not used (RF already uses multiple trees)
    gbdt_ensemble: Option<Vec<GBDTModel>>,

    /// Stacker weights for ensemble combination
    /// Only populated when gbdt_ensemble.is_some()
    stacker_weights: Option<Vec<f32>>,

    /// Stacker intercept for Ridge stacking
    /// Only populated when using Ridge stacking strategy
    stacker_intercept: Option<f32>,

    /// Linear booster (for LinearThenTree mode)
    linear_booster: Option<LinearBooster>,

    /// Ensemble of trained trees (for LinearThenTree and RandomForest modes)
    /// Used when NOT using gbdt_model or gbdt_ensemble
    trees: Vec<Tree>,

    /// Base prediction (for LinearThenTree and RandomForest modes)
    base_prediction: f32,

    /// Number of features
    num_features: usize,

    /// Analysis result (if auto mode was used)
    ///
    /// Stores the dataset analysis that led to mode selection.
    /// Use `analysis()` to retrieve and `analysis_report()` to get a formatted report.
    #[rkyv(with = rkyv::with::Skip)]
    #[serde(skip)]
    analysis: Option<DatasetAnalysis>,

    /// Raw features for LinearThenTree prediction (optional)
    ///
    /// When LTT is trained with raw features, we store them for prediction.
    /// This avoids the lossy bin-center approximation.
    #[rkyv(with = rkyv::with::Skip)]
    #[serde(skip)]
    raw_features_for_linear: Option<Vec<f32>>,

    /// Feature indices to use for linear model (optional)
    ///
    /// When set, only these feature indices from raw_features are used for
    /// the linear model. This allows feature selection for linear while
    /// trees use all features.
    #[rkyv(with = rkyv::with::Skip)]
    #[serde(skip)]
    linear_feature_indices: Option<Vec<usize>>,

    /// Number of features used by linear model (may differ from num_features)
    #[rkyv(with = rkyv::with::Skip)]
    #[serde(skip)]
    num_linear_features: Option<usize>,

    /// Feature extractor for LinearThenTree inference (optional)
    ///
    /// Stores feature extraction configuration (which columns to exclude,
    /// auto-exclusion settings) used during training. This ensures consistent
    /// feature extraction during inference for linear component.
    #[rkyv(with = rkyv::with::Skip)]
    #[serde(skip)]
    feature_extractor: Option<crate::dataset::feature_extractor::FeatureExtractor>,
}

impl UniversalModel {
    /// Set feature extractor for LinearThenTree inference
    ///
    /// This is called after training to store the feature extraction configuration
    /// (which columns to exclude, auto-exclusion settings). This ensures consistent
    /// feature extraction during inference for the linear component.
    pub fn with_feature_extractor(
        mut self,
        extractor: crate::dataset::feature_extractor::FeatureExtractor,
    ) -> Self {
        self.feature_extractor = Some(extractor);
        self
    }

    /// Get the feature extractor (if set)
    pub fn feature_extractor(
        &self,
    ) -> Option<&crate::dataset::feature_extractor::FeatureExtractor> {
        self.feature_extractor.as_ref()
    }

    /// Apply linear model shrinkage factor to predictions (batch)
    ///
    /// In LinearThenTree mode, the linear model's contribution to the ensemble
    /// is weighted by `shrinkage_factor`:
    ///
    /// ```text
    /// ensemble_pred = base + shrinkage_factor * linear_pred + tree_pred
    /// ```
    ///
    /// This shrinkage factor controls ensemble weighting (not optimization step size).
    /// See `LinearConfig::shrinkage_factor` for details.
    ///
    /// # Arguments
    /// * `predictions` - Mutable slice of predictions to update
    /// * `linear_preds` - Linear model predictions to add (with shrinkage applied)
    #[inline]
    fn apply_linear_shrinkage(&self, predictions: &mut [f32], linear_preds: &[f32]) {
        let shrinkage = self.config.linear_config.shrinkage_factor;
        let len = predictions.len().min(linear_preds.len());
        for i in 0..len {
            predictions[i] += shrinkage * linear_preds[i];
        }
    }

    /// Apply linear model shrinkage factor to a single prediction
    ///
    /// Applies the same ensemble weighting logic as `apply_linear_shrinkage`
    /// but for a single scalar value.
    ///
    /// # Arguments
    /// * `linear_pred` - Raw linear prediction
    ///
    /// # Returns
    /// Linear prediction scaled by shrinkage factor
    #[inline]
    fn apply_linear_shrinkage_scalar(&self, linear_pred: f32) -> f32 {
        self.config.linear_config.shrinkage_factor * linear_pred
    }

    /// Train a UniversalModel on binned data
    ///
    /// # Arguments
    /// * `dataset` - Binned training data
    /// * `config` - Training configuration (fully serializable and persisted)
    /// * `loss_fn` - Loss function for computing gradients during training
    ///
    /// # Important Notes
    ///
    /// - The **loss function is NOT persisted** in the saved model. It is used only
    ///   during training to compute gradients. The trained model is loss-function-agnostic
    ///   and will work correctly with any data processed with the same preprocessing.
    /// - The **config is fully persisted** and can be exported via [`config()`] or
    ///   saved to JSON for inspection and reuse.
    /// - For reproducibility, store your loss function choice separately if needed.
    ///
    /// # Example
    /// ```ignore
    /// // Train with MSE loss
    /// let model = UniversalModel::train(&dataset, config, &MseLoss)?;
    ///
    /// // Save config and model
    /// let config_json = serde_json::to_string_pretty(model.config())?;
    /// std::fs::write("config.json", config_json)?;
    /// model.save("model.rkyv")?;
    ///
    /// // Later: Load and use (loss function is already baked into the model)
    /// let loaded = UniversalModel::load("model.rkyv")?;
    /// let preds = loaded.predict(&test_dataset)?;  // No need to specify loss again
    /// ```
    pub fn train(
        dataset: &BinnedDataset,
        config: UniversalConfig,
        loss_fn: &dyn LossFunction,
    ) -> Result<Self> {
        let feature_extractor = config.feature_extractor.clone();
        match config.mode {
            BoostingMode::PureTree => {
                Self::train_pure_tree(dataset, config, loss_fn, None, feature_extractor)
            }
            BoostingMode::LinearThenTree => Self::train_linear_then_tree(
                dataset,
                None,
                None,
                config,
                loss_fn,
                None,
                feature_extractor,
            ),
            BoostingMode::RandomForest => {
                Self::train_random_forest(dataset, config, loss_fn, None, feature_extractor)
            }
        }
    }

    /// Train LinearThenTree with raw features (recommended for best accuracy)
    ///
    /// For LinearThenTree mode, passing raw (unbinned) features significantly improves
    /// the linear model's accuracy. Without raw features, LTT uses bin-center approximations
    /// which lose precision that linear models need.
    ///
    /// # Arguments
    /// * `dataset` - Binned dataset (for tree training)
    /// * `raw_features` - Original features, row-major f32 array (num_rows * num_features)
    /// * `config` - Training configuration
    /// * `loss_fn` - Loss function
    ///
    /// # Example
    /// ```ignore
    /// let model = UniversalModel::train_with_raw_features(
    ///     &binned_dataset,
    ///     &scaled_features,  // Original StandardScaler'd features
    ///     config,
    ///     &MseLoss,
    /// )?;
    /// ```
    pub fn train_with_raw_features(
        dataset: &BinnedDataset,
        raw_features: &[f32],
        config: UniversalConfig,
        loss_fn: &dyn LossFunction,
    ) -> Result<Self> {
        let feature_extractor = config.feature_extractor.clone();
        match config.mode {
            BoostingMode::PureTree => {
                Self::train_pure_tree(dataset, config, loss_fn, None, feature_extractor)
            }
            BoostingMode::LinearThenTree => Self::train_linear_then_tree(
                dataset,
                Some(raw_features),
                None,
                config,
                loss_fn,
                None,
                feature_extractor,
            ),
            BoostingMode::RandomForest => {
                Self::train_random_forest(dataset, config, loss_fn, None, feature_extractor)
            }
        }
    }

    /// Train LinearThenTree with feature selection for linear model
    ///
    /// This allows using a curated subset of features for the linear model
    /// while trees use all features. This can improve linear generalization
    /// by excluding meaningless features (like row IDs) from linear.
    ///
    /// # Arguments
    /// * `dataset` - Binned dataset (for tree training with all features)
    /// * `raw_features` - All features, row-major f32 array
    /// * `linear_feature_indices` - Which feature indices to use for linear model
    /// * `config` - Training configuration
    /// * `loss_fn` - Loss function
    pub fn train_with_linear_feature_selection(
        dataset: &BinnedDataset,
        raw_features: &[f32],
        linear_feature_indices: &[usize],
        config: UniversalConfig,
        loss_fn: &dyn LossFunction,
    ) -> Result<Self> {
        let feature_extractor = config.feature_extractor.clone();
        match config.mode {
            BoostingMode::PureTree => {
                Self::train_pure_tree(dataset, config, loss_fn, None, feature_extractor)
            }
            BoostingMode::LinearThenTree => Self::train_linear_then_tree(
                dataset,
                Some(raw_features),
                Some(linear_feature_indices),
                config,
                loss_fn,
                None,
                feature_extractor,
            ),
            BoostingMode::RandomForest => {
                Self::train_random_forest(dataset, config, loss_fn, None, feature_extractor)
            }
        }
    }

    // =========================================================================
    // Automatic Mode Selection
    // =========================================================================

    /// Train with automatic mode selection
    ///
    /// This is TreeBoost's "smart" entry point. It:
    /// 1. Analyzes your dataset (lightweight probes on subsamples)
    /// 2. Picks the best boosting mode with confidence score
    /// 3. Trains the model with optimal settings
    /// 4. Stores the analysis for inspection
    ///
    /// # Example
    ///
    /// ```ignore
    /// use treeboost::{UniversalModel, MseLoss};
    ///
    /// let model = UniversalModel::auto(&dataset, &MseLoss)?;
    ///
    /// // See what mode was selected and why
    /// println!("Mode: {:?}", model.mode());
    /// println!("Confidence: {:?}", model.selection_confidence());
    /// println!("{}", model.analysis_report().unwrap());
    /// ```
    ///
    /// # When to Use
    ///
    /// Use `auto()` when:
    /// - You're not sure which mode is best for your data
    /// - You want TreeBoost to explain its decision
    /// - You want a simple one-liner that "just works"
    ///
    /// Use `train()` when:
    /// - You know the best mode for your data
    /// - You need fine-grained control over configuration
    /// - You're running benchmarks and want deterministic mode
    pub fn auto(dataset: &BinnedDataset, loss_fn: &dyn LossFunction) -> Result<Self> {
        Self::auto_with_config(dataset, UniversalConfig::default(), loss_fn)
    }

    /// Train with automatic mode selection and custom configuration
    ///
    /// Like `auto()`, but lets you customize other settings (num_rounds, tree config, etc.).
    /// The mode will be overridden by the analysis recommendation.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let config = UniversalConfig::new()
    ///     .with_num_rounds(200)
    ///     .with_learning_rate(0.05);
    ///
    /// let model = UniversalModel::auto_with_config(&dataset, config, &MseLoss)?;
    /// ```
    pub fn auto_with_config(
        dataset: &BinnedDataset,
        config: UniversalConfig,
        loss_fn: &dyn LossFunction,
    ) -> Result<Self> {
        Self::auto_with_analysis_config(dataset, config, AnalysisConfig::default(), loss_fn)
    }

    /// Train with automatic mode selection and custom analysis configuration
    ///
    /// Full control over both model config and analysis settings.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let config = UniversalConfig::new().with_num_rounds(200);
    /// let analysis_config = AnalysisConfig::fast(); // Quick analysis
    ///
    /// let model = UniversalModel::auto_with_analysis_config(
    ///     &dataset, config, analysis_config, &MseLoss
    /// )?;
    /// ```
    pub fn auto_with_analysis_config(
        dataset: &BinnedDataset,
        mut config: UniversalConfig,
        analysis_config: AnalysisConfig,
        loss_fn: &dyn LossFunction,
    ) -> Result<Self> {
        // Step 1: Analyze the dataset
        let analysis = DatasetAnalysis::analyze_with_config(dataset, analysis_config)?;

        // Step 2: Get the recommended mode
        let recommended_mode = analysis.recommend_mode();

        // Step 3: Update config with recommended mode
        config.mode = recommended_mode;

        // Step 4: Train with the recommended mode
        let model = match config.mode {
            BoostingMode::PureTree => {
                Self::train_pure_tree(dataset, config, loss_fn, Some(analysis), None)
            }
            BoostingMode::LinearThenTree => Self::train_linear_then_tree(
                dataset,
                None,
                None,
                config,
                loss_fn,
                Some(analysis),
                None,
            ),
            BoostingMode::RandomForest => {
                Self::train_random_forest(dataset, config, loss_fn, Some(analysis), None)
            }
        }?;

        Ok(model)
    }

    /// Train using a ModeSelection strategy
    ///
    /// This is the most flexible entry point, supporting:
    /// - `ModeSelection::Auto` - Automatic analysis and selection
    /// - `ModeSelection::AutoWithConfig(config)` - Auto with custom analysis
    /// - `ModeSelection::Fixed(mode)` - Explicit mode specification
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Auto mode
    /// let model = UniversalModel::train_with_selection(
    ///     &dataset, config, ModeSelection::Auto, &MseLoss
    /// )?;
    ///
    /// // Fixed mode
    /// let model = UniversalModel::train_with_selection(
    ///     &dataset, config, ModeSelection::Fixed(BoostingMode::LinearThenTree), &MseLoss
    /// )?;
    /// ```
    pub fn train_with_selection(
        dataset: &BinnedDataset,
        mut config: UniversalConfig,
        selection: ModeSelection,
        loss_fn: &dyn LossFunction,
    ) -> Result<Self> {
        match selection {
            ModeSelection::Auto => Self::auto_with_config(dataset, config, loss_fn),
            ModeSelection::AutoWithConfig(analysis_config) => {
                Self::auto_with_analysis_config(dataset, config, analysis_config, loss_fn)
            }
            ModeSelection::Fixed(mode) => {
                config.mode = mode;
                Self::train(dataset, config, loss_fn)
            }
        }
    }

    // =========================================================================
    // Config Conversion
    // =========================================================================

    /// Convert UniversalConfig to GBDTConfig for delegation to GBDTModel
    pub(crate) fn to_gbdt_config(
        config: &UniversalConfig,
        loss_fn: &dyn LossFunction,
    ) -> GBDTConfig {
        let mut gbdt_config = GBDTConfig::new()
            .with_num_rounds(config.num_rounds)
            .with_learning_rate(config.learning_rate)
            .with_max_depth(config.tree_config.max_depth)
            .with_max_leaves(config.tree_config.max_leaves)
            .with_lambda(config.tree_config.lambda)
            .with_entropy_weight(config.tree_config.entropy_weight) // Pass entropy regularization
            .with_subsample(config.subsample)
            .with_seed(config.seed);

        // Early stopping
        if config.early_stopping_rounds > 0 && config.validation_ratio > 0.0 {
            gbdt_config = gbdt_config
                .with_early_stopping(config.early_stopping_rounds, config.validation_ratio);
        }

        // Conformal calibration
        if config.calibration_ratio > 0.0 {
            gbdt_config.calibration_ratio = config.calibration_ratio;
            gbdt_config.conformal_quantile = config.conformal_quantile;
        }

        // Set loss type based on loss_fn type name (best effort)
        let loss_name = std::any::type_name_of_val(loss_fn);
        if loss_name.contains("PseudoHuber") {
            gbdt_config = gbdt_config.with_pseudo_huber_loss(1.0);
        } else if loss_name.contains("BinaryLogLoss") || loss_name.contains("LogLoss") {
            gbdt_config = gbdt_config.with_binary_logloss();
        }
        // Default is MSE which is already the default

        gbdt_config
    }

    // =========================================================================
    // PureTree Mode - Delegates to GBDTModel
    // =========================================================================

    fn train_pure_tree(
        dataset: &BinnedDataset,
        config: UniversalConfig,
        loss_fn: &dyn LossFunction,
        analysis: Option<DatasetAnalysis>,
        feature_extractor: Option<crate::dataset::feature_extractor::FeatureExtractor>,
    ) -> Result<Self> {
        let num_features = dataset.num_features();

        // Check if ensemble training is requested
        let (gbdt_model, gbdt_ensemble, stacker_weights, stacker_intercept) =
            if let Some(ref seeds) = config.ensemble_seeds {
                // Multi-seed ensemble training
                Self::train_gbdt_ensemble(dataset, &config, loss_fn, seeds)?
            } else {
                // Single GBDT training (standard path)
                let gbdt_config = Self::to_gbdt_config(&config, loss_fn);
                let gbdt_model = GBDTModel::train_binned(dataset, gbdt_config)?;
                (Some(gbdt_model), None, None, None)
            };

        Ok(Self {
            config,
            gbdt_model,
            gbdt_ensemble,
            stacker_weights,
            stacker_intercept,
            linear_booster: None,
            trees: Vec::new(),
            base_prediction: 0.0, // Not used - GBDTModel handles this
            num_features,
            analysis,
            raw_features_for_linear: None,
            linear_feature_indices: None,
            num_linear_features: None,
            feature_extractor,
        })
    }

    // =========================================================================
    // LinearThenTree Mode - Linear phase + GBDTModel on residuals
    // =========================================================================
    // Uses Newton-step Coordinate Descent for the linear phase (gradient/hessian).
    // This provides implicit regularization via learning_rate and captures global
    // trends that trees cannot extrapolate.
    //
    // NOTE ON INTENTIONAL CODE DUPLICATION:
    // The linear training loop in this method (Phase 1, lines 580-606) is duplicated
    // in `AutoBuilder::train_ltt_ensemble()` (src/model/builder.rs). This duplication
    // is intentional and acceptable because:
    // - This method is part of UniversalModel (core training API)
    // - AutoBuilder::train_ltt_ensemble is high-level AutoML orchestration
    // - Extracting shared logic would require complex trait/closure patterns
    // - The duplication is ~25 lines and stable (rarely changes)
    // - Clear architectural separation outweighs DRY principle
    //
    // See: AutoBuilder::train_ltt_ensemble() in src/model/builder.rs for the other copy.

    fn train_linear_then_tree(
        dataset: &BinnedDataset,
        raw_features_opt: Option<&[f32]>,
        linear_feature_indices_opt: Option<&[usize]>,
        config: UniversalConfig,
        loss_fn: &dyn LossFunction,
        analysis: Option<DatasetAnalysis>,
        feature_extractor: Option<crate::dataset::feature_extractor::FeatureExtractor>,
    ) -> Result<Self> {
        let targets = dataset.targets();
        let num_rows = dataset.num_rows();
        let num_features = dataset.num_features();

        // Determine which features to use for linear model
        let linear_indices: Option<Vec<usize>> = linear_feature_indices_opt.map(|v| v.to_vec());

        // Get raw features: use provided ones or extract from bins (lossy fallback)
        let raw_features: Vec<f32> = if let Some(provided) = raw_features_opt {
            provided.to_vec()
        } else {
            // Memory safety check for bin-center extraction fallback
            let estimated_bytes = config.estimate_linear_memory(num_rows, num_features);
            let estimated_mb = estimated_bytes / (1024 * 1024);

            if config.max_linear_memory_mb > 0 && estimated_mb > config.max_linear_memory_mb {
                return Err(crate::TreeBoostError::Config(format!(
                    "LinearThenTree mode would require ~{}MB for raw feature extraction \
                     ({}rows × {}features × 4bytes), exceeding limit of {}MB. \
                     Options: (1) Increase max_linear_memory_mb, (2) Use PureTree mode, \
                     (3) Reduce dataset size, (4) Use fewer features.",
                    estimated_mb, num_rows, num_features, config.max_linear_memory_mb
                )));
            }

            // Warn on very large allocations (>1GB) even without explicit limit
            if estimated_mb > 1024 {
                eprintln!(
                    "Warning: LinearThenTree will allocate ~{}MB for raw features. \
                     Consider setting max_linear_memory_mb to prevent OOM.",
                    estimated_mb
                );
            }

            // Fallback: extract from bins (lossy - linear model will be less accurate)
            Self::extract_raw_features(dataset)
        };

        // Calculate actual number of features in raw_features
        // (may differ from num_features if FeatureExtractor was used)
        let num_raw_features = if num_rows > 0 {
            raw_features.len() / num_rows
        } else {
            num_features
        };

        // Determine number of features for linear model
        let num_linear_features = linear_indices
            .as_ref()
            .map(|v| v.len())
            .unwrap_or(num_raw_features);

        // Base prediction (mean target)
        let base_prediction = loss_fn.initial_prediction(targets);

        // =====================================================================
        // Phase 1: Fit Linear Model using Newton-step Coordinate Descent
        // =====================================================================
        // Iterative gradient-based fitting with learning_rate provides implicit
        // regularization. This is more robust than closed-form Ridge for
        // generalization.

        // Extract selected features for linear model if indices are specified
        let linear_features = extract_selected_features(
            &raw_features,
            num_rows,
            num_raw_features,
            linear_indices.as_deref(),
        );

        let mut linear_booster =
            LinearBooster::new(num_linear_features, config.linear_config.clone());

        // Current predictions start from base
        let mut predictions = vec![base_prediction; num_rows];

        // Iteratively fit linear model
        for _round in 0..config.linear_rounds {
            // Compute gradients and hessians
            let mut gradients = vec![0.0f32; num_rows];
            let mut hessians = vec![0.0f32; num_rows];

            for i in 0..num_rows {
                let (g, h) = loss_fn.gradient_hessian(targets[i], predictions[i]);
                gradients[i] = g;
                hessians[i] = h;
            }

            // Fit linear model on gradients (Newton step)
            linear_booster.fit_on_gradients(
                &linear_features,
                num_linear_features,
                &gradients,
                &hessians,
            )?;

            // Update predictions with shrinkage factor (ensemble weighting)
            let shrinkage = config.linear_config.shrinkage_factor;
            for (i, pred) in predictions.iter_mut().enumerate().take(num_rows) {
                let linear_pred =
                    linear_booster.predict_row(&linear_features, num_linear_features, i);
                *pred += shrinkage * linear_pred;
            }
        }

        // =====================================================================
        // Phase 2: Train GBDTModel on Residuals
        // =====================================================================

        // Clone dataset and modify targets to residuals
        let mut residual_dataset = dataset.clone();
        {
            let residual_targets = residual_dataset.targets_mut();
            for i in 0..num_rows {
                residual_targets[i] = targets[i] - predictions[i];
            }
        }

        // Check if ensemble training is requested
        let (gbdt_model, gbdt_ensemble, stacker_weights, stacker_intercept) =
            if let Some(ref seeds) = config.ensemble_seeds {
                // Multi-seed ensemble training
                Self::train_gbdt_ensemble(&residual_dataset, &config, loss_fn, seeds)?
            } else {
                // Single GBDT training (standard path)
                let gbdt_config = Self::to_gbdt_config(&config, loss_fn);
                let gbdt_model = GBDTModel::train_binned(&residual_dataset, gbdt_config)?;
                (Some(gbdt_model), None, None, None)
            };

        Ok(Self {
            config,
            gbdt_model,
            gbdt_ensemble,
            stacker_weights,
            stacker_intercept,
            linear_booster: Some(linear_booster),
            trees: Vec::new(), // Not used - GBDTModel stores trees
            base_prediction,
            num_features,
            analysis,
            raw_features_for_linear: Some(raw_features),
            linear_feature_indices: linear_indices.map(|v| v.to_vec()),
            num_linear_features: Some(num_linear_features),
            feature_extractor,
        })
    }

    // =========================================================================
    // RandomForest Mode
    // =========================================================================

    fn train_random_forest(
        dataset: &BinnedDataset,
        config: UniversalConfig,
        loss_fn: &dyn LossFunction,
        analysis: Option<DatasetAnalysis>,
        feature_extractor: Option<crate::dataset::feature_extractor::FeatureExtractor>,
    ) -> Result<Self> {
        let targets = dataset.targets();
        let num_rows = dataset.num_rows();
        let num_features = dataset.num_features();

        // Initial prediction (mean for RF)
        let base_prediction = loss_fn.initial_prediction(targets);

        // RF uses learning_rate = 1.0 (each tree contributes fully)
        let tree_config = config.tree_config.clone().with_learning_rate(1.0);

        // Train trees in parallel with bootstrap samples
        let trees: Vec<Tree> = (0..config.num_rounds)
            .into_par_iter()
            .filter_map(|seed_offset| {
                // Bootstrap sample
                let mut rng = rand::rngs::StdRng::seed_from_u64(config.seed + seed_offset as u64);
                let bootstrap_indices: Vec<usize> = (0..num_rows)
                    .map(|_| {
                        use rand::Rng;
                        rng.gen_range(0..num_rows)
                    })
                    .collect();

                // Compute gradients for this bootstrap sample
                // For RF, we fit to residuals from base prediction
                let mut gradients = vec![0.0f32; num_rows];
                let mut hessians = vec![0.0f32; num_rows];

                for &idx in &bootstrap_indices {
                    let (g, h) = loss_fn.gradient_hessian(targets[idx], base_prediction);
                    gradients[idx] = g;
                    hessians[idx] = h;
                }

                // Grow tree on bootstrap sample
                let mut booster = TreeBooster::new(tree_config.clone());
                if booster
                    .fit_on_gradients(dataset, &gradients, &hessians, Some(&bootstrap_indices))
                    .is_ok()
                {
                    booster.take_tree()
                } else {
                    None
                }
            })
            .collect();

        Ok(Self {
            config,
            gbdt_model: None, // RandomForest uses self.trees, not GBDTModel
            gbdt_ensemble: None,
            stacker_weights: None,
            stacker_intercept: None,
            linear_booster: None,
            trees,
            base_prediction,
            num_features,
            analysis,
            raw_features_for_linear: None,
            linear_feature_indices: None,
            num_linear_features: None,
            feature_extractor,
        })
    }

    // =========================================================================
    // Helper Methods
    // =========================================================================

    /// Predict using GBDT ensemble with stacker weights
    fn predict_ensemble(&self, dataset: &BinnedDataset, ensemble: &[GBDTModel]) -> Vec<f32> {
        let num_rows = dataset.num_rows();

        // Get predictions from all ensemble members
        let member_predictions: Vec<Vec<f32>> = ensemble
            .iter()
            .map(|model| model.predict(dataset))
            .collect();

        // Combine using stacker weights
        if let Some(ref weights) = self.stacker_weights {
            let mut combined = vec![0.0; num_rows];

            // Weighted sum of member predictions
            for (pred, &weight) in member_predictions.iter().zip(weights.iter()) {
                for i in 0..num_rows {
                    combined[i] += pred[i] * weight;
                }
            }

            // Add intercept if present (Ridge stacking)
            if let Some(intercept) = self.stacker_intercept {
                for i in 0..num_rows {
                    combined[i] += intercept;
                }
            }

            combined
        } else {
            // Fallback to equal-weight averaging if weights not available
            let mut combined = vec![0.0; num_rows];
            let scale = 1.0 / ensemble.len() as f32;

            for pred in &member_predictions {
                for i in 0..num_rows {
                    combined[i] += pred[i] * scale;
                }
            }

            combined
        }
    }

    /// Train multi-seed GBDT ensemble and fit stacker
    ///
    /// Returns (None, Some(ensemble), Some(weights), Some(intercept))
    fn train_gbdt_ensemble(
        dataset: &BinnedDataset,
        config: &UniversalConfig,
        loss_fn: &dyn LossFunction,
        seeds: &[u64],
    ) -> Result<(
        Option<GBDTModel>,
        Option<Vec<GBDTModel>>,
        Option<Vec<f32>>,
        Option<f32>,
    )> {
        use crate::ensemble::{RidgeStacker, Stacker, StackingConfig};

        // Train multiple GBDTs with different seeds
        let mut models = Vec::with_capacity(seeds.len());
        let mut oof_predictions = Vec::with_capacity(seeds.len());

        for &seed in seeds {
            let mut gbdt_config = Self::to_gbdt_config(config, loss_fn);
            gbdt_config.seed = seed;

            let model = GBDTModel::train_binned(dataset, gbdt_config)?;

            // Get OOF predictions for stacking
            let preds = model.predict(dataset);
            oof_predictions.push(preds);

            models.push(model);
        }

        // Fit stacker on OOF predictions
        let (weights, intercept) = match &config.stacking_strategy {
            super::config::StackingStrategy::Ridge {
                alpha,
                rank_transform,
                fit_intercept,
                min_weight,
            } => {
                let stacking_config = StackingConfig {
                    alpha: *alpha,
                    rank_transform: *rank_transform,
                    fit_intercept: *fit_intercept,
                    min_weight: *min_weight,
                };

                let mut stacker = RidgeStacker::new(stacking_config);
                stacker.fit(&oof_predictions, dataset.targets());

                let weights = stacker.weights().map(|w| w.to_vec());
                let intercept = if *fit_intercept {
                    Some(stacker.intercept())
                } else {
                    None
                };

                (weights, intercept)
            }
            super::config::StackingStrategy::Average => {
                // Equal weights for all models
                let weights = vec![1.0 / seeds.len() as f32; seeds.len()];
                (Some(weights), None)
            }
        };

        Ok((None, Some(models), weights, intercept))
    }

    /// Extract raw feature values from BinnedDataset using bin-center approximation
    ///
    /// **⚠️ Note**: This is a fallback method with accuracy limitations. For best results,
    /// use `FeatureExtractor` with the original DataFrame instead. See the
    /// `feature_extractor` module documentation for a detailed comparison.
    ///
    /// This method approximates raw values by using the midpoint of bin boundaries.
    /// While functional, this loses precision compared to extracting from the original
    /// DataFrame with `FeatureExtractor`.
    ///
    /// Returns row-major f32 array for linear model training.
    ///
    /// # When to Use
    /// - Only when you have a BinnedDataset but not the original DataFrame
    /// - When using UniversalModel directly (advanced usage)
    /// - When slight accuracy loss is acceptable
    ///
    /// # Alternative (Recommended)
    /// ```rust,ignore
    /// use treeboost::dataset::feature_extractor::FeatureExtractor;
    ///
    /// let extractor = FeatureExtractor::new();
    /// let (raw_features, num_features) = extractor.extract(&df, "target")?;
    /// // Use raw_features with train_with_raw_features()
    /// ```
    /// Extract raw features from bins (lossy approximation)
    ///
    /// **Deprecated approach**: This method is kept for compatibility but delegates
    /// to `BinnedDataset::extract_raw_features_from_bins()` for consistency across
    /// the codebase. For best accuracy in LinearThenTree mode, pass actual raw features
    /// to training instead of relying on bin-center approximation.
    ///
    /// # Historical Note
    ///
    /// Previous implementation used bin-center approximation (midpoint between boundaries)
    /// for improved accuracy. Current implementation uses bin split values for consistency.
    /// The difference is negligible in practice, and users needing high accuracy should
    /// pass raw features directly rather than relying on either approximation.
    pub fn extract_raw_features(dataset: &BinnedDataset) -> Vec<f32> {
        dataset.extract_raw_features_from_bins()
    }

    /// Extract linear features for LinearThenTree mode
    ///
    /// Combines raw feature extraction with optional feature selection.
    /// Returns (linear_features, num_linear_features).
    fn get_linear_features(&self, dataset: &BinnedDataset) -> (Vec<f32>, usize) {
        let num_rows = dataset.num_rows();

        // Get raw features (from stored or extract from bins)
        let raw_features = self
            .raw_features_for_linear
            .clone()
            .unwrap_or_else(|| Self::extract_raw_features(dataset));

        let num_raw_features = if num_rows > 0 {
            raw_features.len() / num_rows
        } else {
            self.num_features
        };

        // Extract selected features if feature selection was used
        let linear_features = crate::utils::features::extract_selected_features(
            &raw_features,
            num_rows,
            num_raw_features,
            self.linear_feature_indices.as_deref(),
        );

        let num_linear_features = self.num_linear_features.unwrap_or(num_raw_features);

        (linear_features, num_linear_features)
    }

    /// Extract raw feature values for a single row
    ///
    /// More efficient than extract_raw_features() when only predicting one row.
    fn extract_raw_features_row(dataset: &BinnedDataset, row_idx: usize) -> Vec<f32> {
        let feature_info = dataset.all_feature_info();
        let mut raw_features = Vec::with_capacity(feature_info.len());

        for (f, info) in feature_info.iter().enumerate() {
            let boundaries = &info.bin_boundaries;
            let bin = dataset.get_bin(row_idx, f) as usize;

            // Convert bin back to approximate raw value using bin center
            let raw_value = if boundaries.is_empty() {
                bin as f32
            } else if bin == 0 {
                boundaries.first().copied().unwrap_or(0.0) as f32
            } else if bin >= boundaries.len() {
                boundaries.last().copied().unwrap_or(0.0) as f32
            } else {
                // Midpoint between bin boundaries
                ((boundaries[bin - 1] + boundaries[bin.min(boundaries.len() - 1)]) / 2.0) as f32
            };

            raw_features.push(raw_value);
        }

        raw_features
    }

    // =========================================================================
    // Prediction
    // =========================================================================

    /// Predict for all rows in dataset
    pub fn predict(&self, dataset: &BinnedDataset) -> Vec<f32> {
        match self.config.mode {
            BoostingMode::PureTree => {
                // Check for ensemble first, then single model
                if let Some(ref ensemble) = self.gbdt_ensemble {
                    self.predict_ensemble(dataset, ensemble)
                } else {
                    // Delegate to single GBDTModel
                    self.gbdt_model
                        .as_ref()
                        .map(|m| m.predict(dataset))
                        .unwrap_or_else(|| vec![0.0; dataset.num_rows()])
                }
            }
            BoostingMode::LinearThenTree => {
                // Linear contribution + GBDTModel (trained on residuals)
                let num_rows = dataset.num_rows();
                let mut predictions = vec![self.base_prediction; num_rows];

                // Add linear contribution with shrinkage
                if let Some(ref linear) = self.linear_booster {
                    let raw_features = Self::extract_raw_features(dataset);
                    let linear_preds = linear.predict_batch(&raw_features, self.num_features);
                    self.apply_linear_shrinkage(&mut predictions, &linear_preds);
                }

                // Add tree contribution (either ensemble or single GBDT, trained on residuals)
                // IMPORTANT: Subtract gbdt's base_prediction to avoid double-counting
                // gbdt.predict() returns (gbdt_base + tree_sum), but we already have ltt_base
                if let Some(ref ensemble) = self.gbdt_ensemble {
                    let tree_preds = self.predict_ensemble(dataset, ensemble);
                    // Ensemble already accounts for base predictions internally
                    for i in 0..num_rows {
                        predictions[i] += tree_preds[i];
                    }
                } else if let Some(ref gbdt) = self.gbdt_model {
                    let tree_preds = gbdt.predict(dataset);
                    let gbdt_base = gbdt.base_prediction();
                    for i in 0..num_rows {
                        predictions[i] += tree_preds[i] - gbdt_base;
                    }
                }

                predictions
            }
            BoostingMode::RandomForest => {
                // RandomForest: Each tree is independent, predictions averaged
                let num_rows = dataset.num_rows();
                let mut predictions = vec![self.base_prediction; num_rows];

                if !self.trees.is_empty() {
                    let mut tree_sum = vec![0.0f32; num_rows];
                    for tree in &self.trees {
                        tree.predict_batch_add(dataset, &mut tree_sum);
                    }
                    let scale = 1.0 / self.trees.len() as f32;
                    for i in 0..num_rows {
                        predictions[i] += tree_sum[i] * scale;
                    }
                }

                predictions
            }
        }
    }

    /// Predict for all rows using raw features (recommended for LinearThenTree)
    ///
    /// For LinearThenTree mode, using raw (unbinned) features for the linear model
    /// gives significantly better accuracy than the bin-center approximation.
    ///
    /// # Arguments
    /// * `dataset` - Binned dataset (used for tree predictions)
    /// * `raw_features` - Original features, row-major f32 array (num_rows * num_features)
    ///
    /// # Note
    /// For PureTree and RandomForest, `raw_features` is ignored (trees use binned data).
    pub fn predict_with_raw_features(
        &self,
        dataset: &BinnedDataset,
        raw_features: &[f32],
    ) -> Vec<f32> {
        match self.config.mode {
            BoostingMode::PureTree => {
                // Check for ensemble first, then single model (raw features not used for trees)
                if let Some(ref ensemble) = self.gbdt_ensemble {
                    self.predict_ensemble(dataset, ensemble)
                } else {
                    self.gbdt_model
                        .as_ref()
                        .map(|m| m.predict(dataset))
                        .unwrap_or_else(|| vec![0.0; dataset.num_rows()])
                }
            }
            BoostingMode::LinearThenTree => {
                // Linear contribution uses raw features + GBDTModel uses binned
                let num_rows = dataset.num_rows();
                let mut predictions = vec![self.base_prediction; num_rows];

                // Add linear contribution using raw features (possibly selected subset)
                if let Some(ref linear) = self.linear_booster {
                    // Calculate actual number of features in raw_features array
                    // (may differ from self.num_features if FeatureExtractor was used)
                    let num_raw_features = if num_rows > 0 {
                        raw_features.len() / num_rows
                    } else {
                        self.num_features
                    };

                    let num_lin_feats = self.num_linear_features.unwrap_or(num_raw_features);

                    // Extract selected features if indices are specified
                    let linear_features = extract_selected_features(
                        raw_features,
                        num_rows,
                        num_raw_features,
                        self.linear_feature_indices.as_deref(),
                    );

                    let linear_preds = linear.predict_batch(&linear_features, num_lin_feats);

                    // Apply shrinkage factor (ensemble weighting)
                    self.apply_linear_shrinkage(&mut predictions, &linear_preds);
                }

                // Add tree contribution (either ensemble or single GBDT, trees use binned data)
                // IMPORTANT: Subtract gbdt's base_prediction to avoid double-counting
                if let Some(ref ensemble) = self.gbdt_ensemble {
                    let tree_preds = self.predict_ensemble(dataset, ensemble);
                    for i in 0..num_rows {
                        predictions[i] += tree_preds[i];
                    }
                } else if let Some(ref gbdt) = self.gbdt_model {
                    let tree_preds = gbdt.predict(dataset);
                    let gbdt_base = gbdt.base_prediction();
                    for i in 0..num_rows {
                        predictions[i] += tree_preds[i] - gbdt_base;
                    }
                }

                predictions
            }
            BoostingMode::RandomForest => {
                // RandomForest: trees don't use raw features
                self.predict(dataset)
            }
        }
    }

    /// Predict using only linear component (LinearThenTree mode only)
    ///
    /// Returns predictions from base + linear model, without tree contribution.
    /// Useful for comparing linear-only vs full LinearThenTree performance.
    pub fn predict_linear_only(
        &self,
        dataset: &BinnedDataset,
        raw_features: &[f32],
    ) -> Result<Vec<f32>> {
        if !matches!(self.config.mode, BoostingMode::LinearThenTree) {
            return Err(crate::TreeBoostError::Config(
                "predict_linear_only() only available for LinearThenTree mode".to_string(),
            ));
        }

        let num_rows = dataset.num_rows();
        let mut predictions = vec![self.base_prediction; num_rows];

        // Add only linear contribution (no trees)
        if let Some(ref linear) = self.linear_booster {
            let num_raw_features = if num_rows > 0 {
                raw_features.len() / num_rows
            } else {
                self.num_features
            };

            let num_lin_feats = self.num_linear_features.unwrap_or(num_raw_features);

            // Extract selected features if indices are specified
            let linear_features = extract_selected_features(
                raw_features,
                num_rows,
                num_raw_features,
                self.linear_feature_indices.as_deref(),
            );

            let linear_preds = linear.predict_batch(&linear_features, num_lin_feats);

            // NOTE: For linear-only, we return the full linear model prediction
            // WITHOUT the boosting shrinkage (shrinkage_factor). This shows the
            // standalone linear model's performance for fair comparison.
            for i in 0..num_rows.min(linear_preds.len()) {
                predictions[i] += linear_preds[i];
            }
        }

        Ok(predictions)
    }

    /// Predict for a single row
    pub fn predict_row(&self, dataset: &BinnedDataset, row_idx: usize) -> f32 {
        match self.config.mode {
            BoostingMode::PureTree => {
                // Delegate to GBDTModel
                self.gbdt_model
                    .as_ref()
                    .map(|m| m.predict(dataset)[row_idx]) // GBDTModel doesn't have predict_row
                    .unwrap_or(0.0)
            }
            BoostingMode::LinearThenTree => {
                let mut pred = self.base_prediction;

                // Add linear contribution with shrinkage
                if let Some(ref linear) = self.linear_booster {
                    let raw_features = Self::extract_raw_features_row(dataset, row_idx);
                    let linear_pred = linear.predict_row(&raw_features, self.num_features, 0);
                    pred += self.apply_linear_shrinkage_scalar(linear_pred);
                }

                // Add tree contribution from GBDTModel
                // Subtract gbdt's base_prediction to avoid double-counting
                if let Some(ref gbdt) = self.gbdt_model {
                    pred += gbdt.predict(dataset)[row_idx] - gbdt.base_prediction();
                }

                pred
            }
            BoostingMode::RandomForest => {
                let mut pred = self.base_prediction;

                if !self.trees.is_empty() {
                    let tree_sum: f32 = self
                        .trees
                        .iter()
                        .map(|t| t.predict_row(dataset, row_idx))
                        .sum();
                    pred += tree_sum / self.trees.len() as f32;
                }

                pred
            }
        }
    }

    // =========================================================================
    // Accessors
    // =========================================================================

    /// Get the boosting mode
    pub fn mode(&self) -> BoostingMode {
        self.config.mode
    }

    /// Get training configuration
    pub fn config(&self) -> &UniversalConfig {
        &self.config
    }

    /// Save the model to a file using zero-copy rkyv serialization
    ///
    /// # Example
    /// ```ignore
    /// model.save("model.rkyv")?;
    /// let loaded = UniversalModel::load("model.rkyv")?;
    /// ```
    pub fn save(&self, path: impl AsRef<std::path::Path>) -> Result<()> {
        crate::serialize::save_universal_model(self, path)
    }

    /// Load a model from a file using zero-copy rkyv deserialization
    ///
    /// # Example
    /// ```ignore
    /// let model = UniversalModel::load("model.rkyv")?;
    /// let predictions = model.predict(&dataset);
    /// ```
    pub fn load(path: impl AsRef<std::path::Path>) -> Result<Self> {
        crate::serialize::load_universal_model(path)
    }

    /// Get number of trees
    pub fn num_trees(&self) -> usize {
        // Check GBDTModel first (PureTree and LinearThenTree modes)
        if let Some(ref gbdt) = self.gbdt_model {
            gbdt.num_trees()
        } else {
            // RandomForest uses self.trees
            self.trees.len()
        }
    }

    /// Get base prediction
    ///
    /// For PureTree, this delegates to GBDTModel.
    /// For LinearThenTree, returns the original base prediction (GBDTModel was trained on residuals).
    /// For RandomForest, returns the stored base prediction.
    pub fn base_prediction(&self) -> f32 {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| m.base_prediction())
                .unwrap_or(self.base_prediction),
            // LinearThenTree and RandomForest: use stored base_prediction
            // (GBDTModel in LinearThenTree was trained on residuals, so its base is ~0)
            BoostingMode::LinearThenTree | BoostingMode::RandomForest => self.base_prediction,
        }
    }

    /// Check if model has linear component
    pub fn has_linear(&self) -> bool {
        self.linear_booster.is_some()
    }

    /// Get linear booster reference (if present)
    pub fn linear_booster(&self) -> Option<&LinearBooster> {
        self.linear_booster.as_ref()
    }

    /// Get underlying GBDTModel (for PureTree and LinearThenTree modes)
    pub fn gbdt_model(&self) -> Option<&GBDTModel> {
        self.gbdt_model.as_ref()
    }

    /// Get trees (only for RandomForest mode; PureTree/LinearThenTree use GBDTModel)
    pub fn trees(&self) -> &[Tree] {
        &self.trees
    }

    /// Get number of features
    pub fn num_features(&self) -> usize {
        self.num_features
    }

    // =========================================================================
    // Analysis and Mode Selection Info
    // =========================================================================

    /// Get the dataset analysis that led to mode selection (if auto mode was used)
    ///
    /// Returns `Some(analysis)` if the model was trained with `auto()` or `train_with_selection(Auto)`.
    /// Returns `None` if a fixed mode was specified.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let model = UniversalModel::auto(&dataset, &MseLoss)?;
    ///
    /// 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);
    /// }
    /// ```
    pub fn analysis(&self) -> Option<&DatasetAnalysis> {
        self.analysis.as_ref()
    }

    /// Get the confidence in the mode selection (if auto mode was used)
    ///
    /// Returns the confidence level from the analysis:
    /// - `High`: Very clear signal, strongly recommend this mode
    /// - `Medium`: Reasonable signal, this mode is likely best
    /// - `Low`: Weak signal, consider validating with cross-validation
    ///
    /// Returns `None` if a fixed mode was specified.
    pub fn selection_confidence(&self) -> Option<Confidence> {
        self.analysis.as_ref().map(|a| a.confidence())
    }

    /// Check if the mode was automatically selected
    pub fn was_auto_selected(&self) -> bool {
        self.analysis.is_some()
    }

    /// Get a formatted analysis report (if auto mode was used)
    ///
    /// Returns a human-readable report explaining:
    /// - Dataset characteristics (linear signal, tree gain, noise floor)
    /// - Mode scores for each option
    /// - Why the selected mode was chosen
    /// - Alternative modes to consider
    ///
    /// # Example
    ///
    /// ```ignore
    /// let model = UniversalModel::auto(&dataset, &MseLoss)?;
    ///
    /// if let Some(report) = model.analysis_report() {
    ///     println!("{}", report);
    /// }
    /// ```
    pub fn analysis_report(&self) -> Option<crate::analysis::AnalysisReport<'_>> {
        self.analysis.as_ref().map(|a| a.report())
    }

    /// Get a compact single-line summary of the analysis (if auto mode was used)
    ///
    /// Useful for logging or progress output.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let model = UniversalModel::auto(&dataset, &MseLoss)?;
    ///
    /// if let Some(summary) = model.analysis_summary() {
    ///     log::info!("{}", summary);
    /// }
    /// ```
    pub fn analysis_summary(&self) -> Option<String> {
        self.analysis.as_ref().map(crate::analysis::compact_summary)
    }

    // =========================================================================
    // Conformal Prediction (PureTree only)
    // =========================================================================

    /// Predict with conformal prediction intervals
    ///
    /// Returns (predictions, lower_bounds, upper_bounds) for uncertainty quantification.
    ///
    /// # Note
    /// Only supported in PureTree mode (delegates to GBDTModel).
    /// LinearThenTree and RandomForest modes return an error.
    pub fn predict_with_intervals(
        &self,
        dataset: &BinnedDataset,
    ) -> Result<(Vec<f32>, Vec<f32>, Vec<f32>)> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_with_intervals(dataset)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            BoostingMode::LinearThenTree | BoostingMode::RandomForest => {
                Err(crate::TreeBoostError::Config(
                    "Conformal prediction only supported in PureTree mode".to_string(),
                ))
            }
        }
    }

    /// Get the calibrated conformal quantile (if available)
    pub fn conformal_quantile(&self) -> Option<f32> {
        self.gbdt_model
            .as_ref()
            .and_then(|m| m.conformal_quantile())
    }

    // =========================================================================
    // Classification (PureTree only)
    // =========================================================================

    /// Binary classification: predict probabilities
    ///
    /// Returns probabilities in [0, 1] for binary classification.
    /// Requires model trained with binary log loss.
    pub fn predict_proba(&self, dataset: &BinnedDataset) -> Result<Vec<f32>> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_proba(dataset)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Classification only supported in PureTree mode".to_string(),
            )),
        }
    }

    /// Binary classification: predict classes
    ///
    /// Returns 0 or 1 based on threshold (default: 0.5).
    pub fn predict_class(&self, dataset: &BinnedDataset, threshold: f32) -> Result<Vec<u32>> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_class(dataset, threshold)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Classification only supported in PureTree mode".to_string(),
            )),
        }
    }

    /// Check if model is multi-class
    pub fn is_multiclass(&self) -> bool {
        self.gbdt_model.as_ref().is_some_and(|m| m.is_multiclass())
    }

    /// Get number of classes (1 for regression, 2+ for classification)
    pub fn get_num_classes(&self) -> usize {
        self.gbdt_model
            .as_ref()
            .map(|m| m.get_num_classes())
            .unwrap_or(1)
    }

    /// Multi-class classification: predict probabilities for each class
    ///
    /// Returns Vec<Vec<f32>> where each inner vec contains probabilities for all classes.
    pub fn predict_proba_multiclass(&self, dataset: &BinnedDataset) -> Result<Vec<Vec<f32>>> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_proba_multiclass(dataset)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Multi-class only supported in PureTree mode".to_string(),
            )),
        }
    }

    /// Multi-class classification: predict class labels
    pub fn predict_class_multiclass(&self, dataset: &BinnedDataset) -> Result<Vec<u32>> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_class_multiclass(dataset)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Multi-class only supported in PureTree mode".to_string(),
            )),
        }
    }

    /// Multi-class classification: predict raw logits
    pub fn predict_raw_multiclass(&self, dataset: &BinnedDataset) -> Result<Vec<Vec<f32>>> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_raw_multiclass(dataset)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Multi-class only supported in PureTree mode".to_string(),
            )),
        }
    }

    // =========================================================================
    // Feature Importance
    // =========================================================================

    /// Get feature importance scores
    ///
    /// Returns importance scores for each feature based on gain/split frequency.
    /// For PureTree/LinearThenTree, delegates to GBDTModel.
    /// For RandomForest, computes importance from split frequencies across all trees.
    pub fn feature_importance(&self) -> Vec<f32> {
        use crate::tree::NodeType;

        if let Some(ref gbdt) = self.gbdt_model {
            gbdt.feature_importance()
        } else if !self.trees.is_empty() {
            // RandomForest: count split frequencies per feature across all trees
            let mut importances = vec![0.0f32; self.num_features];
            for tree in &self.trees {
                for node in tree.nodes() {
                    if let NodeType::Internal { feature_idx, .. } = node.node_type {
                        if feature_idx < importances.len() {
                            // Count splits per feature (weighted by samples in node)
                            importances[feature_idx] += node.num_samples as f32;
                        }
                    }
                }
            }
            // Normalize
            let total: f32 = importances.iter().sum();
            if total > 0.0 {
                for imp in &mut importances {
                    *imp /= total;
                }
            }
            importances
        } else {
            vec![0.0; self.num_features]
        }
    }

    // =========================================================================
    // Raw Prediction (from unbinned features)
    // =========================================================================

    /// Predict from raw (unbinned) feature values
    ///
    /// Useful when you have raw feature values and don't want to create a BinnedDataset.
    /// Only supported in PureTree mode.
    ///
    /// # Arguments
    /// * `features` - Raw feature values for one or more rows, flattened row-major
    pub fn predict_raw(&self, features: &[f64]) -> Result<Vec<f32>> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_raw(features)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Raw prediction only supported in PureTree mode".to_string(),
            )),
        }
    }

    /// Predict with intervals from raw (unbinned) feature values
    pub fn predict_raw_with_intervals(
        &self,
        features: &[f64],
    ) -> Result<(Vec<f32>, Vec<f32>, Vec<f32>)> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_raw_with_intervals(features)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Raw prediction only supported in PureTree mode".to_string(),
            )),
        }
    }

    /// Binary classification probability from raw features
    pub fn predict_proba_raw(&self, features: &[f64]) -> Result<Vec<f32>> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_proba_raw(features)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Raw prediction only supported in PureTree mode".to_string(),
            )),
        }
    }

    /// Binary classification class from raw features
    pub fn predict_class_raw(&self, features: &[f64], threshold: f32) -> Result<Vec<u32>> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_class_raw(features, threshold)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Raw prediction only supported in PureTree mode".to_string(),
            )),
        }
    }

    /// Multi-class probabilities from raw features
    pub fn predict_proba_multiclass_raw(&self, features: &[f64]) -> Result<Vec<Vec<f32>>> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_proba_multiclass_raw(features)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Raw prediction only supported in PureTree mode".to_string(),
            )),
        }
    }

    /// Multi-class class labels from raw features
    pub fn predict_class_multiclass_raw(&self, features: &[f64]) -> Result<Vec<u32>> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_class_multiclass_raw(features)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Raw prediction only supported in PureTree mode".to_string(),
            )),
        }
    }

    /// Multi-class raw logits from raw features
    pub fn predict_raw_multiclass_raw(&self, features: &[f64]) -> Result<Vec<Vec<f32>>> {
        match self.config.mode {
            BoostingMode::PureTree => self
                .gbdt_model
                .as_ref()
                .map(|m| Ok(m.predict_raw_multiclass_raw(features)))
                .unwrap_or_else(|| {
                    Err(crate::TreeBoostError::Config(
                        "No model trained".to_string(),
                    ))
                }),
            _ => Err(crate::TreeBoostError::Config(
                "Raw prediction only supported in PureTree mode".to_string(),
            )),
        }
    }

    // =========================================================================
    // Incremental Learning Support
    // =========================================================================

    /// Update the model with new training data (incremental learning)
    ///
    /// This method continues training from the current model state:
    /// - **PureTree**: Appends new trees trained on residuals
    /// - **LinearThenTree**: Updates linear weights (warm_fit) + appends new trees
    /// - **RandomForest**: Appends new bootstrap trees
    ///
    /// # Arguments
    /// * `dataset` - New data to train on (must have same feature schema)
    /// * `loss_fn` - Loss function (should match original training)
    /// * `additional_rounds` - Number of new boosting rounds (trees) to add
    ///
    /// # Example
    /// ```ignore
    /// // Train on January data
    /// let model = UniversalModel::train(&jan_data, config, &MseLoss)?;
    ///
    /// // Update with February data (10 more trees)
    /// model.update(&feb_data, &MseLoss, 10)?;
    /// ```
    pub fn update(
        &mut self,
        dataset: &BinnedDataset,
        loss_fn: &dyn LossFunction,
        additional_rounds: usize,
    ) -> Result<IncrementalUpdateReport> {
        // Validate feature compatibility
        if dataset.num_features() != self.num_features {
            return Err(crate::TreeBoostError::Config(format!(
                "Feature count mismatch: model has {} features, dataset has {}",
                self.num_features,
                dataset.num_features()
            )));
        }

        let num_rows = dataset.num_rows();
        let trees_before = self.num_trees();

        match self.config.mode {
            BoostingMode::PureTree => {
                self.update_pure_tree(dataset, loss_fn, additional_rounds)?;
            }
            BoostingMode::LinearThenTree => {
                self.update_linear_then_tree(dataset, loss_fn, additional_rounds)?;
            }
            BoostingMode::RandomForest => {
                self.update_random_forest(dataset, loss_fn, additional_rounds)?;
            }
        }

        let trees_after = self.num_trees();

        Ok(IncrementalUpdateReport {
            rows_trained: num_rows,
            trees_before,
            trees_after,
            trees_added: trees_after - trees_before,
            mode: self.config.mode,
        })
    }

    /// Update PureTree model by appending trees trained on residuals
    fn update_pure_tree(
        &mut self,
        dataset: &BinnedDataset,
        loss_fn: &dyn LossFunction,
        additional_rounds: usize,
    ) -> Result<()> {
        let gbdt = self.gbdt_model.as_mut().ok_or_else(|| {
            crate::TreeBoostError::Config("No GBDTModel available for update".to_string())
        })?;

        // Compute current predictions
        let predictions = gbdt.predict(dataset);

        // Create residual targets (avoids cloning targets only to overwrite)
        let residual_targets: Vec<f32> = dataset
            .targets()
            .iter()
            .zip(predictions.iter())
            .map(|(target, pred)| target - pred)
            .collect();

        // Create residual dataset with new targets
        let residual_dataset = dataset.with_targets(residual_targets);

        // Train new trees on residuals using same config
        let mut update_config = self.config.clone();
        update_config.num_rounds = additional_rounds;
        let gbdt_config = Self::to_gbdt_config(&update_config, loss_fn);
        let new_model = GBDTModel::train_binned(&residual_dataset, gbdt_config)?;

        // Append new trees
        gbdt.append_trees(new_model.trees().to_vec());

        Ok(())
    }

    /// Update LinearThenTree model
    fn update_linear_then_tree(
        &mut self,
        dataset: &BinnedDataset,
        loss_fn: &dyn LossFunction,
        additional_rounds: usize,
    ) -> Result<()> {
        use crate::learner::incremental::IncrementalLearner;

        let num_rows = dataset.num_rows();
        let targets = dataset.targets();

        // Compute current predictions BEFORE borrowing linear_booster mutably
        let current_preds = self.predict(dataset);

        // Extract linear features once (avoids duplication)
        let (linear_features, num_linear_features) = self.get_linear_features(dataset);

        // Update linear booster with warm_fit if present
        if let Some(ref mut linear_booster) = self.linear_booster {
            // Compute gradients
            let (gradients, hessians): (Vec<f32>, Vec<f32>) = targets
                .iter()
                .zip(current_preds.iter())
                .map(|(t, p)| loss_fn.gradient_hessian(*t, *p))
                .unzip();

            // Warm fit linear booster
            linear_booster.warm_fit(
                &linear_features,
                num_linear_features,
                &gradients,
                &hessians,
            )?;
        }

        // Now update trees on residuals from full ensemble
        if let Some(ref mut gbdt) = self.gbdt_model {
            // Get linear predictions (using already-extracted features)
            let linear_preds = if let Some(ref linear_booster) = self.linear_booster {
                linear_booster.predict_batch(&linear_features, num_linear_features)
            } else {
                vec![0.0; num_rows]
            };

            // Get existing tree predictions
            let tree_preds = gbdt.predict(dataset);

            // Compute residual targets: target - base - shrinkage*linear - tree
            let shrinkage = self.config.linear_config.shrinkage_factor;
            let residual_targets: Vec<f32> = targets
                .iter()
                .zip(linear_preds.iter())
                .zip(tree_preds.iter())
                .map(|((t, lp), tp)| t - self.base_prediction - shrinkage * lp - tp)
                .collect();

            // Create residual dataset with new targets (avoids full clone)
            let residual_dataset = dataset.with_targets(residual_targets);

            // Train new trees on residuals
            let mut update_config = self.config.clone();
            update_config.num_rounds = additional_rounds;
            let gbdt_config = Self::to_gbdt_config(&update_config, loss_fn);
            let new_model = GBDTModel::train_binned(&residual_dataset, gbdt_config)?;

            gbdt.append_trees(new_model.trees().to_vec());
        }

        Ok(())
    }

    /// Update RandomForest by adding more bootstrap trees
    fn update_random_forest(
        &mut self,
        dataset: &BinnedDataset,
        loss_fn: &dyn LossFunction,
        additional_rounds: usize,
    ) -> Result<()> {
        use crate::learner::TreeBooster;

        let num_rows = dataset.num_rows();
        let targets = dataset.targets();

        // RF trees are trained on bootstrap samples from base prediction
        let tree_config = self.config.tree_config.clone().with_learning_rate(1.0);

        // Use next seed offset based on current tree count
        let seed_offset_start = self.trees.len() as u64;

        let new_trees: Vec<Tree> = (0..additional_rounds)
            .into_par_iter()
            .filter_map(|i| {
                let mut rng = rand::rngs::StdRng::seed_from_u64(
                    self.config.seed + seed_offset_start + i as u64,
                );

                // Bootstrap sample
                let bootstrap_indices: Vec<usize> = (0..num_rows)
                    .map(|_| {
                        use rand::Rng;
                        rng.gen_range(0..num_rows)
                    })
                    .collect();

                // Compute gradients for bootstrap sample
                let mut gradients = vec![0.0f32; num_rows];
                let mut hessians = vec![0.0f32; num_rows];

                for &idx in &bootstrap_indices {
                    let (g, h) = loss_fn.gradient_hessian(targets[idx], self.base_prediction);
                    gradients[idx] = g;
                    hessians[idx] = h;
                }

                // Grow tree
                let mut booster = TreeBooster::new(tree_config.clone());
                if booster
                    .fit_on_gradients(dataset, &gradients, &hessians, Some(&bootstrap_indices))
                    .is_ok()
                {
                    booster.take_tree()
                } else {
                    None
                }
            })
            .collect();

        self.trees.extend(new_trees);

        Ok(())
    }

    /// Save model to TRB (TreeBoost) incremental format
    ///
    /// TRB format supports incremental updates without rewriting the entire file.
    /// Use this format when you plan to update the model with new data.
    ///
    /// # Example
    /// ```ignore
    /// model.save_trb("model.trb", "Initial training on January data")?;
    ///
    /// // Later, after updating:
    /// model.save_trb_update("model.trb", 1000, "February update")?;
    /// ```
    pub fn save_trb(&self, path: impl AsRef<std::path::Path>, description: &str) -> Result<()> {
        use crate::serialize::{TrbHeader, TrbWriter, FORMAT_VERSION};
        use std::time::{SystemTime, UNIX_EPOCH};

        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        let header = TrbHeader {
            format_version: FORMAT_VERSION,
            model_type: "universal".to_string(),
            created_at: timestamp,
            boosting_mode: format!("{:?}", self.config.mode),
            num_features: self.num_features,
            base_blob_size: 0, // Will be filled by TrbWriter
            metadata: description.to_string(),
        };

        // Serialize the model to rkyv bytes
        let blob = crate::serialize::rkyv_io::serialize_universal_model(self)?;

        TrbWriter::new(path, header, &blob)?;

        Ok(())
    }

    /// Append an update to an existing TRB file
    ///
    /// This appends a new segment without rewriting the base model.
    /// The model must be loaded with `load_trb()` before calling this.
    ///
    /// # Arguments
    /// * `path` - Path to existing .trb file
    /// * `rows_trained` - Number of rows used in this update
    /// * `description` - Description of this update
    pub fn save_trb_update(
        &self,
        path: impl AsRef<std::path::Path>,
        rows_trained: usize,
        description: &str,
    ) -> Result<()> {
        use crate::serialize::{open_for_append, TrbUpdateHeader, UpdateType};
        use std::time::{SystemTime, UNIX_EPOCH};

        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        // Use Snapshot type for all modes - stores full model state
        // This is simpler and more robust than incremental tree-only updates
        let update_type = UpdateType::Snapshot;

        let header = TrbUpdateHeader {
            update_type,
            created_at: timestamp,
            rows_trained,
            description: description.to_string(),
        };

        // Serialize the updated model
        let blob = crate::serialize::rkyv_io::serialize_universal_model(self)?;

        // Open and append
        let mut writer = open_for_append(path)?;
        writer.append_update(&header, &blob)?;

        Ok(())
    }

    /// Load model from TRB format
    ///
    /// Loads the base model and applies all update segments.
    ///
    /// # Example
    /// ```ignore
    /// let model = UniversalModel::load_trb("model.trb")?;
    /// ```
    pub fn load_trb(path: impl AsRef<std::path::Path>) -> Result<Self> {
        use crate::serialize::{TrbReader, TrbSegment, UpdateType};

        let mut reader = TrbReader::open(path)?;

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

        // The last snapshot-type segment (or base if no snapshots) is the current state
        let mut model: Option<Self> = None;

        for segment in segments {
            match segment {
                TrbSegment::Base { blob, .. } => {
                    model = Some(crate::serialize::rkyv_io::deserialize_universal_model(
                        &blob,
                    )?);
                }
                TrbSegment::Update { header, blob } => {
                    match header.update_type {
                        UpdateType::Snapshot => {
                            // Replace with snapshot
                            model = Some(crate::serialize::rkyv_io::deserialize_universal_model(
                                &blob,
                            )?);
                        }
                        UpdateType::Trees => {
                            // For trees-only updates, we'd need to deserialize and merge
                            // For now, we just use snapshots for simplicity
                            if model.is_none() {
                                model = Some(
                                    crate::serialize::rkyv_io::deserialize_universal_model(&blob)?,
                                );
                            }
                        }
                        UpdateType::Linear | UpdateType::Preprocessor => {
                            // These would need specialized handling
                            // For now, fall through
                        }
                    }
                }
            }
        }

        model.ok_or_else(|| {
            crate::TreeBoostError::Serialization("No valid model found in TRB file".to_string())
        })
    }

    /// Check if model is compatible with dataset for incremental update
    pub fn is_compatible_for_update(&self, dataset: &BinnedDataset) -> bool {
        self.num_features == dataset.num_features()
    }

    /// Get mutable reference to underlying GBDTModel (for advanced manipulation)
    pub fn gbdt_model_mut(&mut self) -> Option<&mut GBDTModel> {
        self.gbdt_model.as_mut()
    }

    /// Get mutable reference to linear booster (for advanced manipulation)
    pub fn linear_booster_mut(&mut self) -> Option<&mut LinearBooster> {
        self.linear_booster.as_mut()
    }

    /// Get mutable reference to trees (for RandomForest mode)
    pub fn trees_mut(&mut self) -> &mut Vec<Tree> {
        &mut self.trees
    }
}

/// Report from an incremental training update
#[derive(Debug, Clone)]
pub struct IncrementalUpdateReport {
    /// Number of rows in the new training data
    pub rows_trained: usize,
    /// Number of trees before update
    pub trees_before: usize,
    /// Number of trees after update
    pub trees_after: usize,
    /// Number of trees added
    pub trees_added: usize,
    /// Boosting mode
    pub mode: BoostingMode,
}

impl std::fmt::Display for IncrementalUpdateReport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Incremental Update: {} rows, {} trees added ({} -> {}), mode={:?}",
            self.rows_trained, self.trees_added, self.trees_before, self.trees_after, self.mode
        )
    }
}

// =============================================================================
// TunableModel Implementation
// =============================================================================

use crate::tuner::{ParamValue, TunableModel};
use std::collections::HashMap;

impl TunableModel for UniversalModel {
    type Config = UniversalConfig;

    fn train(dataset: &BinnedDataset, config: &Self::Config) -> crate::Result<Self> {
        // Create a default MSE loss for tuning (loss type could be parameterized later)
        let loss_fn = crate::loss::MseLoss::new();
        Self::train(dataset, config.clone(), &loss_fn)
    }

    fn predict(&self, dataset: &BinnedDataset) -> Vec<f32> {
        UniversalModel::predict(self, dataset)
    }

    fn num_trees(&self) -> usize {
        // Delegate to UniversalModel::num_trees() which handles all modes correctly
        UniversalModel::num_trees(self)
    }

    fn apply_params(config: &mut Self::Config, params: &HashMap<String, ParamValue>) {
        for (name, value) in params {
            match (name.as_str(), value) {
                // Categorical: boosting mode
                ("mode", ParamValue::Categorical(v)) => {
                    config.mode = match v.as_str() {
                        "PureTree" => BoostingMode::PureTree,
                        "LinearThenTree" => BoostingMode::LinearThenTree,
                        "RandomForest" => BoostingMode::RandomForest,
                        _ => BoostingMode::PureTree, // Default fallback
                    };
                }
                // Numeric parameters
                ("num_rounds", ParamValue::Numeric(v)) => config.num_rounds = *v as usize,
                ("learning_rate", ParamValue::Numeric(v)) => config.learning_rate = *v,
                ("subsample", ParamValue::Numeric(v)) => config.subsample = *v,
                ("validation_ratio", ParamValue::Numeric(v)) => config.validation_ratio = *v,
                ("early_stopping_rounds", ParamValue::Numeric(v)) => {
                    config.early_stopping_rounds = *v as usize
                }
                ("linear_rounds", ParamValue::Numeric(v)) => config.linear_rounds = *v as usize,
                // Tree config parameters (prefixed with tree_)
                ("tree_max_depth", ParamValue::Numeric(v)) => {
                    config.tree_config = config.tree_config.clone().with_max_depth(*v as usize)
                }
                ("tree_max_leaves", ParamValue::Numeric(v)) => {
                    config.tree_config = config.tree_config.clone().with_max_leaves(*v as usize)
                }
                ("tree_lambda", ParamValue::Numeric(v)) => {
                    config.tree_config = config.tree_config.clone().with_lambda(*v)
                }
                // Linear config parameters (prefixed with linear_)
                ("linear_lambda", ParamValue::Numeric(v)) => {
                    config.linear_config = config.linear_config.clone().with_lambda(*v)
                }
                ("linear_max_iter", ParamValue::Numeric(v)) => {
                    config.linear_config = config.linear_config.clone().with_max_iter(*v as usize)
                }
                _ => {} // Unknown params are ignored
            }
        }
    }

    fn valid_params() -> &'static [&'static str] {
        &[
            // Categorical
            "mode",
            // Numeric
            "num_rounds",
            "learning_rate",
            "subsample",
            "validation_ratio",
            "early_stopping_rounds",
            "linear_rounds",
            // Tree config
            "tree_max_depth",
            "tree_max_leaves",
            "tree_lambda",
            // Linear config
            "linear_lambda",
            "linear_max_iter",
        ]
    }

    fn default_config() -> Self::Config {
        UniversalConfig::default()
    }

    fn get_learning_rate(config: &Self::Config) -> f32 {
        config.learning_rate
    }

    fn configure_validation(
        config: &mut Self::Config,
        validation_ratio: f32,
        early_stopping_rounds: usize,
    ) {
        config.validation_ratio = validation_ratio;
        config.early_stopping_rounds = early_stopping_rounds;
    }

    fn set_num_rounds(config: &mut Self::Config, num_rounds: usize) {
        config.num_rounds = num_rounds;
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dataset::{FeatureInfo, FeatureType};
    use crate::loss::MseLoss;
    use rkyv::rancor::Error as RkyvError;

    fn create_test_dataset(num_rows: usize, num_features: usize) -> BinnedDataset {
        let mut features = Vec::with_capacity(num_rows * num_features);
        for f in 0..num_features {
            for r in 0..num_rows {
                features.push(((r * 3 + f * 7) % 256) as u8);
            }
        }

        // Linear relationship with some noise
        let targets: Vec<f32> = (0..num_rows)
            .map(|i| (i as f32) * 0.1 + (i % 10) as f32 * 0.01)
            .collect();

        let feature_info = (0..num_features)
            .map(|i| FeatureInfo {
                name: format!("f{}", i),
                feature_type: FeatureType::Numeric,
                num_bins: 255,
                bin_boundaries: (0..255).map(|b| b as f64).collect(),
            })
            .collect();

        BinnedDataset::new(num_rows, features, targets, feature_info)
    }

    // ========================================
    // Test Helper Functions
    // ========================================

    /// Test helper: Verify serde serialization roundtrip for BoostingMode
    fn assert_serde_roundtrip_mode(mode: &BoostingMode) {
        let json = serde_json::to_string(mode).expect("Failed to serialize");
        assert!(!json.is_empty(), "Serialized JSON should not be empty");

        let loaded: BoostingMode = serde_json::from_str(&json).expect("Failed to deserialize");
        assert_eq!(loaded, *mode, "Deserialized value should match original");
    }

    /// Test helper: Verify model predictions match after serialization
    fn assert_model_predictions_match(
        original: &UniversalModel,
        loaded: &UniversalModel,
        dataset: &BinnedDataset,
        tolerance: f32,
    ) {
        let original_preds = original.predict(dataset);
        let loaded_preds = loaded.predict(dataset);

        assert_eq!(
            original_preds.len(),
            loaded_preds.len(),
            "Prediction count mismatch"
        );

        for (i, (&orig, &load)) in original_preds.iter().zip(loaded_preds.iter()).enumerate() {
            assert!(
                (orig - load).abs() < tolerance,
                "Prediction mismatch at index {}: {} vs {} (diff: {})",
                i,
                orig,
                load,
                (orig - load).abs()
            );
        }
    }

    /// Test helper: Train a model with specified mode
    fn train_test_model(mode: BoostingMode, num_rounds: usize) -> (UniversalModel, BinnedDataset) {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(mode)
            .with_num_rounds(num_rounds)
            .with_linear_rounds(1);

        let model = UniversalModel::train(&dataset, config, &loss).unwrap();
        (model, dataset)
    }

    #[test]
    fn test_universal_config_defaults() {
        let config = UniversalConfig::default();
        assert_eq!(config.mode, BoostingMode::PureTree);
        assert_eq!(config.num_rounds, 100);
        assert_eq!(config.learning_rate, 0.1);
    }

    #[test]
    fn test_universal_config_builder() {
        let config = UniversalConfig::new()
            .with_mode(BoostingMode::LinearThenTree)
            .with_num_rounds(50)
            .with_learning_rate(0.05)
            .with_linear_rounds(5);

        assert_eq!(config.mode, BoostingMode::LinearThenTree);
        assert_eq!(config.num_rounds, 50);
        assert_eq!(config.learning_rate, 0.05);
        assert_eq!(config.linear_rounds, 5);
    }

    #[test]
    fn test_pure_tree_training() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::PureTree)
            .with_num_rounds(5);

        let model = UniversalModel::train(&dataset, config, &loss).unwrap();

        assert_eq!(model.mode(), BoostingMode::PureTree);
        assert_eq!(model.num_trees(), 5);
        assert!(!model.has_linear());
    }

    #[test]
    fn test_pure_tree_prediction() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::PureTree)
            .with_num_rounds(5);

        let model = UniversalModel::train(&dataset, config, &loss).unwrap();
        let predictions = model.predict(&dataset);

        assert_eq!(predictions.len(), 100);
        assert!(predictions.iter().all(|p| p.is_finite()));
    }

    #[test]
    fn test_linear_then_tree_training() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::LinearThenTree)
            .with_num_rounds(5)
            .with_linear_rounds(3);

        let model = UniversalModel::train(&dataset, config, &loss).unwrap();

        assert_eq!(model.mode(), BoostingMode::LinearThenTree);
        assert_eq!(model.num_trees(), 5);
        assert!(model.has_linear());
    }

    #[test]
    fn test_linear_then_tree_prediction() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::LinearThenTree)
            .with_num_rounds(5)
            .with_linear_rounds(3);

        let model = UniversalModel::train(&dataset, config, &loss).unwrap();
        let predictions = model.predict(&dataset);

        assert_eq!(predictions.len(), 100);
        assert!(predictions.iter().all(|p| p.is_finite()));
    }

    #[test]
    fn test_random_forest_training() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::RandomForest)
            .with_num_rounds(10);

        let model = UniversalModel::train(&dataset, config, &loss).unwrap();

        assert_eq!(model.mode(), BoostingMode::RandomForest);
        assert!(model.num_trees() <= 10); // Some trees might fail to grow
        assert!(!model.has_linear());
    }

    #[test]
    fn test_random_forest_prediction() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::RandomForest)
            .with_num_rounds(10);

        let model = UniversalModel::train(&dataset, config, &loss).unwrap();
        let predictions = model.predict(&dataset);

        assert_eq!(predictions.len(), 100);
        assert!(predictions.iter().all(|p| p.is_finite()));
    }

    #[test]
    fn test_single_row_prediction_matches_batch() {
        let dataset = create_test_dataset(50, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::PureTree)
            .with_num_rounds(5);

        let model = UniversalModel::train(&dataset, config, &loss).unwrap();

        let batch_preds = model.predict(&dataset);
        for i in 0..50 {
            let single_pred = model.predict_row(&dataset, i);
            assert!((batch_preds[i] - single_pred).abs() < 1e-5);
        }
    }

    // =========================================================================
    // Auto Mode Selection Tests
    // =========================================================================

    #[test]
    fn test_auto_selects_mode_and_trains() {
        let dataset = create_test_dataset(200, 5);
        let loss = MseLoss;

        // Auto mode should analyze and train
        let model = UniversalModel::auto(&dataset, &loss).unwrap();

        // Model should have selected a mode
        assert!(matches!(
            model.mode(),
            BoostingMode::PureTree | BoostingMode::LinearThenTree | BoostingMode::RandomForest
        ));

        // Should have analysis attached
        assert!(model.was_auto_selected());
        assert!(model.analysis().is_some());
        assert!(model.selection_confidence().is_some());

        // Should predict successfully
        let predictions = model.predict(&dataset);
        assert_eq!(predictions.len(), 200);
        assert!(predictions.iter().all(|p| p.is_finite()));
    }

    #[test]
    fn test_auto_with_config() {
        let dataset = create_test_dataset(200, 5);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_num_rounds(10)
            .with_learning_rate(0.05);

        let model = UniversalModel::auto_with_config(&dataset, config, &loss).unwrap();

        // Should use custom config settings
        assert_eq!(model.config().num_rounds, 10);
        assert_eq!(model.config().learning_rate, 0.05);

        // Should still be auto-selected
        assert!(model.was_auto_selected());
    }

    #[test]
    fn test_analysis_report_generation() {
        let dataset = create_test_dataset(200, 5);
        let loss = MseLoss;

        let model = UniversalModel::auto(&dataset, &loss).unwrap();

        // Report should be available
        let report = model.analysis_report();
        assert!(report.is_some());

        // Report should be displayable (non-empty)
        let report_string = format!("{}", report.unwrap());
        assert!(!report_string.is_empty());
        assert!(report_string.contains("TreeBoost"));
    }

    #[test]
    fn test_analysis_summary() {
        let dataset = create_test_dataset(200, 5);
        let loss = MseLoss;

        let model = UniversalModel::auto(&dataset, &loss).unwrap();

        let summary = model.analysis_summary();
        assert!(summary.is_some());

        let summary_str = summary.unwrap();
        assert!(summary_str.contains("TreeBoost"));
        assert!(summary_str.contains("Recommended"));
    }

    #[test]
    fn test_train_with_selection_fixed() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new().with_num_rounds(5);

        // Fixed mode should use the specified mode
        let model = UniversalModel::train_with_selection(
            &dataset,
            config,
            ModeSelection::Fixed(BoostingMode::RandomForest),
            &loss,
        )
        .unwrap();

        assert_eq!(model.mode(), BoostingMode::RandomForest);
        // Fixed mode should NOT have analysis attached
        assert!(!model.was_auto_selected());
    }

    #[test]
    fn test_train_with_selection_auto() {
        let dataset = create_test_dataset(200, 5);
        let loss = MseLoss;

        let config = UniversalConfig::new().with_num_rounds(10);

        let model =
            UniversalModel::train_with_selection(&dataset, config, ModeSelection::Auto, &loss)
                .unwrap();

        // Auto selection should have analysis
        assert!(model.was_auto_selected());
        assert!(model.analysis().is_some());
    }

    #[test]
    fn test_mode_selection_default() {
        // Default should be Fixed(PureTree) for backwards compatibility
        let selection = ModeSelection::default();
        assert!(matches!(
            selection,
            ModeSelection::Fixed(BoostingMode::PureTree)
        ));
    }

    #[test]
    fn test_analysis_contains_metrics() {
        let dataset = create_test_dataset(200, 5);
        let loss = MseLoss;

        let model = UniversalModel::auto(&dataset, &loss).unwrap();
        let analysis = model.analysis().unwrap();

        // Analysis should have valid metrics
        assert!(analysis.linear_r2 >= 0.0 && analysis.linear_r2 <= 1.0);
        assert!(analysis.tree_gain >= 0.0);
        assert!(analysis.categorical_ratio >= 0.0 && analysis.categorical_ratio <= 1.0);
        assert!(analysis.noise_floor >= 0.0 && analysis.noise_floor <= 1.0);
        assert_eq!(analysis.num_rows, 200);
        assert_eq!(analysis.num_features, 5);
    }

    // ========================================
    // Serialization Tests (serde)
    // ========================================

    #[test]
    fn test_universal_config_serde_serialization() {
        let config = UniversalConfig::new()
            .with_mode(BoostingMode::LinearThenTree)
            .with_num_rounds(150)
            .with_learning_rate(0.05);

        let json = serde_json::to_string(&config).unwrap();
        assert!(!json.is_empty());

        let loaded: UniversalConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(loaded.mode, config.mode);
        assert_eq!(loaded.num_rounds, config.num_rounds);
        assert!((loaded.learning_rate - config.learning_rate).abs() < 1e-6);
    }

    #[test]
    fn test_boosting_mode_serde_serialization() {
        assert_serde_roundtrip_mode(&BoostingMode::PureTree);
        assert_serde_roundtrip_mode(&BoostingMode::LinearThenTree);
        assert_serde_roundtrip_mode(&BoostingMode::RandomForest);
    }

    #[test]
    fn test_puretree_model_serde_serialization() {
        let (model, dataset) = train_test_model(BoostingMode::PureTree, 10);

        let json = serde_json::to_string(&model).unwrap();
        assert!(!json.is_empty());

        let loaded: UniversalModel = serde_json::from_str(&json).unwrap();
        assert_eq!(loaded.mode(), BoostingMode::PureTree);
        assert_eq!(loaded.num_features(), 3);

        assert_model_predictions_match(&model, &loaded, &dataset, 1e-4);
    }

    #[test]
    fn test_linear_then_tree_model_serde_serialization() {
        let (model, dataset) = train_test_model(BoostingMode::LinearThenTree, 10);

        let json = serde_json::to_string(&model).unwrap();
        assert!(!json.is_empty());

        let loaded: UniversalModel = serde_json::from_str(&json).unwrap();
        assert_eq!(loaded.mode(), BoostingMode::LinearThenTree);

        assert_model_predictions_match(&model, &loaded, &dataset, 1e-4);
    }

    #[test]
    fn test_random_forest_model_serde_serialization() {
        let (model, dataset) = train_test_model(BoostingMode::RandomForest, 5);

        let json = serde_json::to_string(&model).unwrap();
        assert!(!json.is_empty());

        let loaded: UniversalModel = serde_json::from_str(&json).unwrap();
        assert_eq!(loaded.mode(), BoostingMode::RandomForest);

        assert_model_predictions_match(&model, &loaded, &dataset, 1e-4);
    }

    // ========================================
    // Serialization Tests (rkyv)
    // ========================================

    #[test]
    fn test_universal_config_rkyv_serialization() {
        let config = UniversalConfig::new()
            .with_mode(BoostingMode::LinearThenTree)
            .with_num_rounds(150)
            .with_learning_rate(0.05);

        let bytes = rkyv::to_bytes::<RkyvError>(&config).unwrap();
        assert!(!bytes.is_empty());

        let loaded: UniversalConfig = rkyv::from_bytes::<_, RkyvError>(&bytes).unwrap();
        assert_eq!(loaded.mode, config.mode);
        assert_eq!(loaded.num_rounds, config.num_rounds);
        assert!((loaded.learning_rate - config.learning_rate).abs() < 1e-6);
    }

    #[test]
    fn test_puretree_model_rkyv_serialization() {
        let (model, dataset) = train_test_model(BoostingMode::PureTree, 10);

        let bytes = rkyv::to_bytes::<RkyvError>(&model).unwrap();
        assert!(!bytes.is_empty());

        let loaded: UniversalModel = rkyv::from_bytes::<_, RkyvError>(&bytes).unwrap();
        assert_eq!(loaded.mode(), BoostingMode::PureTree);
        assert_eq!(loaded.num_features(), 3);

        assert_model_predictions_match(&model, &loaded, &dataset, 1e-4);
    }

    #[test]
    fn test_linear_then_tree_model_rkyv_serialization() {
        let (model, dataset) = train_test_model(BoostingMode::LinearThenTree, 10);

        let bytes = rkyv::to_bytes::<RkyvError>(&model).unwrap();
        assert!(!bytes.is_empty());

        let loaded: UniversalModel = rkyv::from_bytes::<_, RkyvError>(&bytes).unwrap();
        assert_eq!(loaded.mode(), BoostingMode::LinearThenTree);

        assert_model_predictions_match(&model, &loaded, &dataset, 1e-4);
    }

    #[test]
    fn test_random_forest_model_rkyv_serialization() {
        let (model, dataset) = train_test_model(BoostingMode::RandomForest, 5);

        let bytes = rkyv::to_bytes::<RkyvError>(&model).unwrap();
        assert!(!bytes.is_empty());

        let loaded: UniversalModel = rkyv::from_bytes::<_, RkyvError>(&bytes).unwrap();
        assert_eq!(loaded.mode(), BoostingMode::RandomForest);

        assert_model_predictions_match(&model, &loaded, &dataset, 1e-4);
    }

    // ========================================
    // Incremental Learning Tests
    // ========================================

    #[test]
    fn test_puretree_incremental_update() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::PureTree)
            .with_num_rounds(5);

        let mut model = UniversalModel::train(&dataset, config, &loss).unwrap();
        let trees_before = model.num_trees();
        assert_eq!(trees_before, 5);

        // Update with 5 more trees
        let report = model.update(&dataset, &loss, 5).unwrap();

        assert_eq!(report.trees_before, 5);
        assert_eq!(report.trees_after, 10);
        assert_eq!(report.trees_added, 5);
        assert_eq!(model.num_trees(), 10);
    }

    #[test]
    fn test_linear_then_tree_incremental_update() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::LinearThenTree)
            .with_num_rounds(5)
            .with_linear_rounds(2);

        let mut model = UniversalModel::train(&dataset, config, &loss).unwrap();
        let trees_before = model.num_trees();
        assert_eq!(trees_before, 5);
        assert!(model.has_linear());

        // Update with 5 more trees
        let report = model.update(&dataset, &loss, 5).unwrap();

        assert_eq!(report.trees_before, 5);
        assert_eq!(report.trees_after, 10);
        assert_eq!(report.trees_added, 5);
    }

    #[test]
    fn test_random_forest_incremental_update() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::RandomForest)
            .with_num_rounds(5);

        let mut model = UniversalModel::train(&dataset, config, &loss).unwrap();
        let trees_before = model.num_trees();
        assert_eq!(trees_before, 5);

        // Update with 5 more trees
        let report = model.update(&dataset, &loss, 5).unwrap();

        assert_eq!(report.trees_before, 5);
        assert_eq!(report.trees_after, 10);
        assert_eq!(report.trees_added, 5);
    }

    #[test]
    fn test_incremental_update_report_display() {
        let report = IncrementalUpdateReport {
            rows_trained: 1000,
            trees_before: 10,
            trees_after: 20,
            trees_added: 10,
            mode: BoostingMode::PureTree,
        };

        let display = format!("{}", report);
        assert!(display.contains("1000 rows"));
        assert!(display.contains("10 trees added"));
    }

    #[test]
    fn test_incremental_update_feature_mismatch() {
        let dataset1 = create_test_dataset(100, 3);
        let dataset2 = create_test_dataset(100, 5); // Different feature count
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::PureTree)
            .with_num_rounds(5);

        let mut model = UniversalModel::train(&dataset1, config, &loss).unwrap();

        // Update with mismatched features should fail
        let result = model.update(&dataset2, &loss, 5);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Feature count mismatch"));
    }

    #[test]
    fn test_is_compatible_for_update() {
        let dataset1 = create_test_dataset(100, 3);
        let dataset2 = create_test_dataset(50, 3); // Same features, different rows
        let dataset3 = create_test_dataset(100, 5); // Different features
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::PureTree)
            .with_num_rounds(5);

        let model = UniversalModel::train(&dataset1, config, &loss).unwrap();

        assert!(model.is_compatible_for_update(&dataset1));
        assert!(model.is_compatible_for_update(&dataset2));
        assert!(!model.is_compatible_for_update(&dataset3));
    }

    #[test]
    fn test_trb_save_and_load() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::PureTree)
            .with_num_rounds(5);

        let model = UniversalModel::train(&dataset, config, &loss).unwrap();

        // Save to temp file
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("model.trb");

        model.save_trb(&path, "Test model").unwrap();

        // Load back
        let loaded = UniversalModel::load_trb(&path).unwrap();

        assert_eq!(loaded.mode(), BoostingMode::PureTree);
        assert_eq!(loaded.num_features(), 3);
        assert_eq!(loaded.num_trees(), 5);

        // Predictions should match
        assert_model_predictions_match(&model, &loaded, &dataset, 1e-4);
    }

    #[test]
    fn test_trb_save_update_and_load() {
        let dataset = create_test_dataset(100, 3);
        let loss = MseLoss;

        let config = UniversalConfig::new()
            .with_mode(BoostingMode::PureTree)
            .with_num_rounds(5);

        let mut model = UniversalModel::train(&dataset, config, &loss).unwrap();

        // Save initial model
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("model.trb");
        model.save_trb(&path, "Initial training").unwrap();

        let initial_size = std::fs::metadata(&path).unwrap().len();

        // Update model
        model.update(&dataset, &loss, 5).unwrap();
        assert_eq!(model.num_trees(), 10);

        // Save update
        model.save_trb_update(&path, 100, "Update batch").unwrap();

        // File should be larger
        let updated_size = std::fs::metadata(&path).unwrap().len();
        assert!(
            updated_size > initial_size,
            "TRB file should grow after update"
        );

        // Load and verify
        let loaded = UniversalModel::load_trb(&path).unwrap();
        assert_eq!(loaded.num_trees(), 10);

        // Predictions should match
        assert_model_predictions_match(&model, &loaded, &dataset, 1e-4);
    }
}