reserving-kernels 0.2.0

Pure Rust numeric kernels for P&C actuarial reserving, pricing, and capital modeling.
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
//! Over-Dispersed Poisson (ODP) GLM and residual bootstrap reserving kernel.
//!
//! Implements the classical ODP reserving and bootstrap framework from:
//!
//! * England & Verrall (1999), *Analytic and Bootstrap Estimates of Prediction
//!   Errors in Claims Reserving*, Insurance: Mathematics and Economics,
//!   Vol. 25, No. 3, pp. 281–293.
//! * England & Verrall (2002), *Stochastic Claims Reserving in General
//!   Insurance*, British Actuarial Journal, Vol. 8, No. 3, pp. 443–518.
//! * Shapland (2016), *Using the ODP Bootstrap Model: A Practitioner's Guide*,
//!   CAS Monograph No. 4.
//!
//! ## Model Specification
//!
//! For incremental payments `q_{i,j}` with `E[q_{i,j}] = μ_{i,j}` and
//! `Var(q_{i,j}) = φ · μ_{i,j}`, the log-linear ODP GLM
//! `log(μ_{i,j}) = c + α_i + β_j` produces fitted expectations identical to
//! volume-weighted chain ladder via backwards recursion from the observed
//! latest diagonal.
//!
//! Two mutually exclusive residual adjustments are supported:
//!
//! * [`ResidualAdjustment::DegreesOfFreedom`] — England & Verrall (1999, 2002)
//!   global degrees-of-freedom scaling `r_adj = sqrt(N / (N - p)) * r`.
//! * [`ResidualAdjustment::HatMatrixLeverage`] — Shapland (2016, CAS Monograph
//!   4, §3.4) leverage scaling `r_H = r / sqrt(1 - h_{ii})` computed from the
//!   exact GLM hat matrix `H = W^{1/2} X (X^T W X)^{-1} X^T W^{1/2}`, excluding
//!   structurally saturated corner cells (`1 - h_{ii} ≈ 0`).
//!
//! Process variance in future cells is sampled using either a moment-matched
//! Gamma distribution (`shape = μ* / φ`, `scale = φ`) or a scaled
//! Over-Dispersed Poisson (`φ · Poisson(μ* / φ)`), driven by a deterministic
//! SplitMix64-seeded Xoshiro256** PRNG with a 256-strip Ziggurat normal sampler.
//! Because both Gamma and scaled Poisson are exact additive families under
//! common dispersion `φ`, per-origin future reserves are sampled directly in
//! `O(n)` per iteration while preserving exact distributional equivalence
//! (including Shapland reflection for negative pseudo-increments).

use std::f64::consts::TAU;

use crate::ldf::{cdfs_from_ldfs, volume_weighted_ldfs};
use crate::linalg::{invert_spd, solve_lower_into};
use crate::reserve_distribution::{ReserveDistributionSummary, summarize_with_scratch};
use crate::triangle::{cumulative_to_incremental, latest_diagonal};
use crate::types::TriangleView;
use crate::validate::{
    MonotonicityPolicy, ValidationError, validate_monotone_cumulative, validate_tail,
    validate_triangle,
};

// Re-export the crate's Cholesky helper for `crate::dependence`.
pub(crate) use crate::linalg::cholesky_lower;

// ===========================================================================
// Public Configuration & Result Types
// ===========================================================================

/// Compact bootstrap configuration for the legacy [`bootstrap`] entry point.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct BootstrapConfig {
    /// Number of bootstrap iterations.
    pub iterations: usize,
    /// Deterministic seed for reproducible resampling.
    pub seed: u64,
}

impl Default for BootstrapConfig {
    fn default() -> Self {
        Self {
            iterations: 1000,
            seed: 0xC0DE5EED,
        }
    }
}

/// Residual degrees-of-freedom / leverage adjustment convention.
///
/// The two conventions are mutually exclusive (Shapland 2016, §3.4): do not
/// apply both simultaneously.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum ResidualAdjustment {
    /// England & Verrall (1999, 2002): multiply Pearson residuals by
    /// `sqrt(N / (N - p))` over all `N` active observed cells.
    #[default]
    DegreesOfFreedom,
    /// Shapland (2016, CAS Monograph 4, §3.4): divide non-saturated Pearson
    /// residuals by `sqrt(1 - h_{ii})` and exclude structurally saturated
    /// corner cells (`1 - h_{ii} <= 1e-10`) from the resampling pool.
    HatMatrixLeverage,
}

/// Future incremental process-variance distribution for ODP bootstrap draws.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum ProcessDistribution {
    /// Moment-matched Gamma: `Gamma(shape = μ* / φ, scale = φ)` with mean `μ*`
    /// and variance `φ · μ*` (England & Verrall 2002; Shapland 2016 §3.5).
    #[default]
    Gamma,
    /// Scaled Poisson: `φ · Poisson(μ* / φ)` with mean `μ*` and variance `φ · μ*`.
    OverDispersedPoisson,
    /// Omit process variance (parameter estimation uncertainty only).
    None,
}

/// Full configuration for [`bootstrap_odp`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct OdpBootstrapOptions {
    /// Number of bootstrap iterations.
    pub iterations: usize,
    /// Deterministic PRNG seed.
    pub seed: u64,
    /// Residual scaling convention (degrees of freedom vs. hat-matrix leverage).
    pub residual_adjustment: ResidualAdjustment,
    /// Process-variance distribution for future cells.
    pub process_distribution: ProcessDistribution,
    /// Whether to center the eligible adjusted residual pool to zero mean
    /// before resampling (Shapland 2016, §3.4.3).
    pub center_residuals: bool,
    /// Monotonicity policy for triangle validation.
    pub monotonicity_policy: MonotonicityPolicy,
}

impl Default for OdpBootstrapOptions {
    fn default() -> Self {
        Self::england_verrall(1000, 42069)
    }
}

impl OdpBootstrapOptions {
    /// Classic England & Verrall (1999, 2002) specification: global
    /// `sqrt(N / (N - p))` residual scaling, Gamma process variance, uncentered.
    pub const fn england_verrall(iterations: usize, seed: u64) -> Self {
        Self {
            iterations,
            seed,
            residual_adjustment: ResidualAdjustment::DegreesOfFreedom,
            process_distribution: ProcessDistribution::Gamma,
            center_residuals: false,
            monotonicity_policy: MonotonicityPolicy::Strict,
        }
    }

    /// Shapland (2016, CAS Monograph 4) practitioner specification: exact
    /// hat-matrix leverage `1 / sqrt(1 - h_{ii})`, centered eligible pool,
    /// Gamma process variance.
    pub const fn shapland(iterations: usize, seed: u64) -> Self {
        Self {
            iterations,
            seed,
            residual_adjustment: ResidualAdjustment::HatMatrixLeverage,
            process_distribution: ProcessDistribution::Gamma,
            center_residuals: true,
            monotonicity_policy: MonotonicityPolicy::Strict,
        }
    }

    /// Set the number of bootstrap iterations.
    pub const fn with_iterations(mut self, iterations: usize) -> Self {
        self.iterations = iterations;
        self
    }

    /// Set the deterministic PRNG seed.
    pub const fn with_seed(mut self, seed: u64) -> Self {
        self.seed = seed;
        self
    }

    /// Set the residual adjustment convention (`DegreesOfFreedom` vs. `HatMatrixLeverage`).
    pub const fn with_residual_adjustment(mut self, adjustment: ResidualAdjustment) -> Self {
        self.residual_adjustment = adjustment;
        self
    }

    /// Set the future process-variance distribution (`Gamma`, `OverDispersedPoisson`, or `None`).
    pub const fn with_process_distribution(mut self, dist: ProcessDistribution) -> Self {
        self.process_distribution = dist;
        self
    }

    /// Enable or disable centering the eligible adjusted residual pool before resampling.
    pub const fn with_centered_residuals(mut self, center: bool) -> Self {
        self.center_residuals = center;
        self
    }

    /// Set the triangle monotonicity policy.
    pub const fn with_policy(mut self, policy: MonotonicityPolicy) -> Self {
        self.monotonicity_policy = policy;
        self
    }
}

/// Bootstrap reserve summary shared with the generic reserve-distribution layer.
pub type BootstrapSummary = ReserveDistributionSummary;

/// Compact bootstrap output returned by [`bootstrap`].
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct BootstrapResult {
    /// Original chain-ladder / ODP total reserve.
    pub base_total_reserve: f64,
    /// Simulated reserve totals by iteration.
    pub simulated_totals: Vec<f64>,
    /// Summary percentiles and moments.
    pub summary: BootstrapSummary,
}

/// Cell-level residual and hat-matrix leverage record from [`fit_odp`].
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct OdpCellResidual {
    /// Origin row index.
    pub origin: usize,
    /// Development column index.
    pub dev: usize,
    /// Observed incremental payment `q_{i,j}`.
    pub observed_incremental: f64,
    /// ODP GLM / chain-ladder fitted incremental expectation `μ_{i,j}`.
    pub fitted_incremental: f64,
    /// Unscaled Pearson residual `(q_{i,j} - μ_{i,j}) / sqrt(μ_{i,j})`.
    pub raw_pearson_residual: f64,
    /// Diagonal entry `h_{ii}` of the GLM hat matrix `H`.
    pub leverage: f64,
    /// `true` when `1 - h_{ii} <= 1e-10` (structurally saturated corner cell).
    pub saturated: bool,
}

/// Immutable ODP GLM fit, dispersion, leverages, and analytic prediction errors.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct OdpFit {
    /// Number of origin rows.
    pub n_origins: usize,
    /// Number of development columns.
    pub n_devs: usize,
    /// Volume-weighted chain-ladder LDFs (length `n - 1`).
    pub ldfs: Vec<f64>,
    /// Multiplicative tail factor (`>= 1.0`).
    pub tail: f64,
    /// Full `n × n` fitted cumulative grid (row-major; observed region by
    /// backward recursion, future region by chain-ladder projection).
    pub fitted_cumulative: Vec<f64>,
    /// Full `n × n` fitted incremental grid (row-major).
    pub fitted_incremental: Vec<f64>,
    /// Observed `n × n` incremental grid (`NaN` in unobserved lower-right).
    pub observed_incremental: Vec<f64>,
    /// Residual and leverage diagnostics for every active observed cell.
    pub residuals: Vec<OdpCellResidual>,
    /// Count of active observed cells entering the ODP fit (`N`).
    pub n_observed: usize,
    /// Effective parameter rank of the ODP design matrix (`p = 2n - 1` for full triangles).
    pub rank: usize,
    /// Residual degrees of freedom (`N - p`).
    pub degrees_of_freedom: usize,
    /// Pearson dispersion parameter `φ = sum(r^2) / (N - p)`.
    pub dispersion: f64,
    /// Point-estimate reserves by origin (including tail when `tail > 1`).
    pub origin_reserves: Vec<f64>,
    /// Aggregate point-estimate reserve.
    pub total_reserve: f64,
    /// Analytic ODP process standard error by origin (pre-tail).
    pub analytic_origin_process_se: Vec<f64>,
    /// Analytic ODP parameter estimation standard error by origin (pre-tail).
    pub analytic_origin_param_se: Vec<f64>,
    /// Analytic ODP total prediction error by origin (England & Verrall 1999, Table 2).
    pub analytic_origin_prediction_error: Vec<f64>,
    /// Analytic ODP aggregate process standard error `sqrt(φ · R)`.
    pub analytic_total_process_se: f64,
    /// Analytic ODP aggregate parameter estimation standard error.
    pub analytic_total_param_se: f64,
    /// Analytic ODP aggregate prediction error (England & Verrall 1999, 2002).
    pub analytic_total_prediction_error: f64,
}

/// Full ODP bootstrap output with fit diagnostics, per-origin distributions,
/// and explicit failure accounting.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct OdpBootstrapResult {
    /// Underlying ODP GLM fit and analytic prediction errors.
    pub fit: OdpFit,
    /// Options used to generate this simulation.
    pub options: OdpBootstrapOptions,
    /// Base chain-ladder / ODP total reserve.
    pub base_total_reserve: f64,
    /// Simulated aggregate reserves for successful iterations.
    pub simulated_totals: Vec<f64>,
    /// Simulated reserves per origin: `simulated_by_origin[origin][iter]`.
    pub simulated_by_origin: Vec<Vec<f64>>,
    /// Distribution summary for each origin year.
    pub origin_summaries: Vec<ReserveDistributionSummary>,
    /// Distribution summary for the aggregate portfolio reserve.
    pub summary: ReserveDistributionSummary,
    /// Number of bootstrap draws rejected due to degenerate pseudo-triangles
    /// (non-positive column sums or non-finite refits). Never silently clipped.
    pub failed_iterations: usize,
    /// Arithmetic mean of the adjusted residual pool prior to optional centering.
    pub residual_pool_mean_before_centering: f64,
}

// ===========================================================================
// 256-Strip Ziggurat Tables for Standard Normal N(0, 1)
// (Marsaglia & Tsang 2000; Doornik 2005; exact equal-area strips to ~2e-15)
// ===========================================================================

#[allow(clippy::excessive_precision)]
const ZIG_NORM_R: f64 = 3.65415288536100924e+00;
#[allow(clippy::excessive_precision)]
const ZIG_NORM_INV_R: f64 = 2.73661237329758222e-01;

/// Pre-scaled strip half-widths `x[i] * 2^-51` for exact symmetric 52-bit shifts.
#[allow(clippy::excessive_precision)]
#[rustfmt::skip]
const ZIG_NORM_W: [f64; 256] = [
    1.73672541216026340e-15, 1.62276986753129703e-15, 1.53178727416111455e-15, 1.47448486035975978e-15,
    1.43199898696613284e-15, 1.39794366727752419e-15, 1.36936068351285195e-15, 1.34463009250111733e-15,
    1.32276557701953283e-15, 1.30312066346899871e-15, 1.28524793190961128e-15, 1.26882448142550120e-15,
    1.25360939266025688e-15, 1.23941797891632531e-15, 1.22610544174505652e-15, 1.21355608186668990e-15,
    1.20167593925438206e-15, 1.19038762992828923e-15, 1.17962663529558048e-15, 1.16933857869109619e-15,
    1.15947718914491932e-15, 1.15000275378397944e-15, 1.14088092425827821e-15, 1.13208178401648483e-15,
    1.12357911071108373e-15, 1.11534978658531569e-15, 1.10737332249357560e-15, 1.09963147017856302e-15,
    1.09210790381495648e-15, 1.08478795644034286e-15, 1.07765840026681103e-15, 1.07070726236329957e-15,
    1.06392366906768027e-15, 1.05729771390098922e-15, 1.05082034483551986e-15, 1.04448326760004725e-15,
    1.03827886235154011e-15, 1.03220011154864585e-15, 1.02624053726135694e-15, 1.02039414646831276e-15,
    1.01465538314670881e-15, 1.00901908616374609e-15, 1.00348045214361829e-15, 9.98035002618364886e-16,
    9.92678554880797846e-16, 9.87407196048067304e-16, 9.82217259919054503e-16, 9.77105306270721081e-16,
    9.72068102290162982e-16, 9.67102605882305622e-16, 9.62205950629484033e-16, 9.57375432209745160e-16,
    9.52608496106628067e-16, 9.47902726465174022e-16, 9.43255835967670851e-16, 9.38665656618666373e-16,
    9.34130131342526710e-16, 9.29647306308641870e-16, 9.25215323909569530e-16, 9.20832416326230957e-16,
    9.16496899621913729e-16, 9.12207168313484807e-16, 9.07961690374007196e-16, 9.03759002626012147e-16,
    8.99597706489110330e-16, 8.95476464049507169e-16, 8.91393994422409008e-16, 8.87349070381313749e-16,
    8.83340515230841091e-16, 8.79367199902104669e-16, 8.75428040251717888e-16, 8.71521994547370097e-16,
    8.67648061124558555e-16, 8.63805276200526185e-16, 8.59992711832766756e-16, 8.56209474010623629e-16,
    8.52454700869559916e-16, 8.48727561018615789e-16, 8.45027251972409973e-16, 8.41352998679803121e-16,
    8.37704052142022459e-16, 8.34079688113663175e-16, 8.30479205880537668e-16, 8.26901927108847298e-16,
    8.23347194760605338e-16, 8.19814372070653583e-16, 8.16302841580988043e-16, 8.12812004228450472e-16,
    8.09341278482150286e-16, 8.05890099527265881e-16, 8.02457918492126209e-16, 7.99044201715713301e-16,
    7.95648430052936913e-16, 7.92270098215227480e-16, 7.88908714144175817e-16, 7.85563798416108701e-16,
    7.82234883675641479e-16, 7.78921514096385636e-16, 7.75623244867117738e-16, 7.72339641701830249e-16,
    7.69070280372192306e-16, 7.65814746261049029e-16, 7.62572633935675970e-16, 7.59343546739589345e-16,
    7.56127096401792568e-16, 7.52922902662406231e-16, 7.49730592913698329e-16, 7.46549801855589297e-16,
    7.43380171164765187e-16, 7.40221349176580262e-16, 7.37072990578983395e-16, 7.33934756117740870e-16,
    7.30806312312274682e-16, 7.27687331181469666e-16, 7.24577489978839093e-16, 7.21476470936471065e-16,
    7.18383961017206680e-16, 7.15299651674530694e-16, 7.12223238619678278e-16, 7.09154421595487736e-16,
    7.06092904156548511e-16, 7.03038393455215375e-16, 6.99990600033076791e-16, 6.96949237617483333e-16,
    6.93914022922757200e-16, 6.90884675455717296e-16, 6.87860917325166761e-16, 6.84842473055004125e-16,
    6.81829069400625700e-16, 6.78820435168298322e-16, 6.75816301037190453e-16, 6.72816399383753509e-16,
    6.69820464108156184e-16, 6.66828230462474985e-16, 6.63839434880350960e-16, 6.60853814807826269e-16,
    6.57871108535074527e-16, 6.54891055028741935e-16, 6.51913393764616367e-16, 6.48937864560340148e-16,
    6.45964207407883707e-16, 6.42992162305490100e-16, 6.40021469088802966e-16, 6.37051867260881988e-16,
    6.34083095820806494e-16, 6.31114893090560917e-16, 6.28146996539890023e-16, 6.25179142608800574e-16,
    6.22211066527379383e-16, 6.19242502132585295e-16, 6.16273181681660223e-16, 6.13302835661791692e-16,
    6.10331192595644535e-16, 6.07357978842361158e-16, 6.04382918393613093e-16, 6.01405732664266896e-16,
    5.98426140277203304e-16, 5.95443856841806472e-16, 5.92458594725613986e-16, 5.89470062818587669e-16,
    5.86477966289437117e-16, 5.83482006333389797e-16, 5.80481879910769157e-16, 5.77477279475697074e-16,
    5.74467892694196679e-16, 5.71453402150920989e-16, 5.68433485043682002e-16, 5.65407812864896635e-16,
    5.62376051069004938e-16, 5.59337858724846799e-16, 5.56292888151909609e-16, 5.53240784539278853e-16,
    5.50181185546034217e-16, 5.47113720881736705e-16, 5.44038011865547399e-16, 5.40953670962399925e-16,
    5.37860301294524071e-16, 5.34757496126473645e-16, 5.31644838321662347e-16, 5.28521899768238889e-16,
    5.25388240771946115e-16, 5.22243409413404864e-16, 5.19086940867035028e-16, 5.15918356678574433e-16,
    5.12737163997880354e-16, 5.09542854763389919e-16, 5.06334904834272739e-16, 5.03112773065932658e-16,
    4.99875900324091668e-16, 4.96623708432218620e-16, 4.93355599046542278e-16, 4.90070952452300263e-16,
    4.86769126274221756e-16, 4.83449454093501688e-16, 4.80111243962702438e-16, 4.76753776809086152e-16,
    4.73376304715833357e-16, 4.69978049069420384e-16, 4.66558198560088404e-16, 4.63115907020817356e-16,
    4.59650291088495059e-16, 4.56160427669004794e-16, 4.52645351185714954e-16, 4.49104050588288485e-16,
    4.45535466095792352e-16, 4.41938485644707683e-16, 4.38311941008546798e-16, 4.34654603551288730e-16,
    4.30965179571630542e-16, 4.27242305188998743e-16, 4.23484540715208218e-16, 4.19690364447408414e-16,
    4.15858165808281775e-16, 4.11986237748075556e-16, 4.08072768309605617e-16, 4.04115831241434458e-16,
    4.00113375525468080e-16, 3.96063213662565057e-16, 3.91963008532578115e-16, 3.87810258612503849e-16,
    3.83602281296774122e-16, 3.79336194015496967e-16, 3.75008892787479409e-16, 3.70617027772362203e-16,
    3.66156975296536877e-16, 3.61624805715984870e-16, 3.57016246339536469e-16, 3.52326638460021892e-16,
    3.47550887317299159e-16, 3.42683403531195187e-16, 3.37718034173533949e-16, 3.32647981168418826e-16,
    3.27465704079399184e-16, 3.22162803505500827e-16, 3.16729880185819877e-16, 3.11156363389217596e-16,
    3.05430300071926326e-16, 2.99538093367823101e-16, 2.93464174847290854e-16, 2.87190589041141424e-16,
    2.80696460024850205e-16, 2.73957296851426679e-16, 2.66944074736485216e-16, 2.59621997725283540e-16,
    2.51948798292744913e-16, 2.43872345574290521e-16, 2.35327189140461874e-16, 2.26229403922184264e-16,
    2.16468605768957550e-16, 2.05895006284824550e-16, 1.94297201531359847e-16, 1.81361208101194749e-16,
    1.66587336315867911e-16, 1.49097409624961075e-16, 1.27087048348114564e-16, 9.55866035145697372e-17,
];

/// Inner acceptance thresholds `x[i + 1]` for strip `i` (`x[256] = 0.0`).
#[allow(clippy::excessive_precision)]
#[rustfmt::skip]
const ZIG_NORM_X_INNER: [f64; 256] = [
    3.65415288536100924e+00, 3.44927829856143164e+00, 3.32024473383982599e+00, 3.22457505204780182e+00,
    3.14788928951800129e+00, 3.08352613200214387e+00, 3.02783779176959378e+00, 2.97860327988184359e+00,
    2.93436686720888806e+00, 2.89412105361341299e+00, 2.85713873087322501e+00, 2.82287739682644334e+00,
    2.79092117400192796e+00, 2.76094400527998696e+00, 2.73268535904401189e+00, 2.70593365612306291e+00,
    2.68051464328574562e+00, 2.65628303757674411e+00, 2.63311639363158312e+00, 2.61091051848882438e+00,
    2.58957598670828704e+00, 2.56903545268184441e+00, 2.54922155032478370e+00, 2.53007523215985497e+00,
    2.51154444162669499e+00, 2.49358304127104757e+00, 2.47614993967052355e+00, 2.45920837433470574e+00,
    2.44272531820036498e+00, 2.42667098493714750e+00, 2.41101841390111993e+00, 2.39574311978192789e+00,
    2.38082279517208617e+00, 2.36623705671729168e+00, 2.35196722737914543e+00, 2.33799614879652928e+00,
    2.32430801887113292e+00, 2.31088825060137237e+00, 2.29772334890286434e+00, 2.28480080272449282e+00,
    2.27210899022838264e+00, 2.25963709517378808e+00, 2.24737503294739005e+00, 2.23531338492992182e+00,
    2.22344334009251110e+00, 2.21175664288416174e+00, 2.20024554661127691e+00, 2.18890277162636160e+00,
    2.17772146774029363e+00, 2.16669518035430908e+00, 2.15581781987673793e+00, 2.14508363404788938e+00,
    2.13448718284601746e+00, 2.12402331568952407e+00, 2.11368715068665391e+00, 2.10347405571487789e+00,
    2.09337963113879244e+00, 2.08339969399830505e+00, 2.07353026351874359e+00, 2.06376754781173277e+00,
    2.05410793165065275e+00, 2.04454796521753224e+00, 2.03508435372961971e+00, 2.02571394786385506e+00,
    2.01643373490620492e+00, 2.00724083056052960e+00, 1.99813247135842031e+00, 1.98910600761743894e+00,
    1.98015889690047753e+00, 1.97128869793366013e+00, 1.96249306494436371e+00, 1.95376974238464762e+00,
    1.94511656000867905e+00, 1.93653142827569535e+00, 1.92801233405266648e+00, 1.91955733659318883e+00,
    1.91116456377125399e+00, 1.90283220855042989e+00, 1.89455852567070537e+00, 1.88634182853678345e+00,
    1.87818048629299650e+00, 1.87007292107126744e+00, 1.86201760539967487e+00, 1.85401305976020270e+00,
    1.84605785028518632e+00, 1.83815058658280739e+00, 1.83028991968275778e+00, 1.82247454009388643e+00,
    1.81470317596628328e+00, 1.80697459135082172e+00, 1.79928758454972093e+00, 1.79164098655216342e+00,
    1.78403365954944237e+00, 1.77646449552452368e+00, 1.76893241491126929e+00, 1.76143636531891112e+00,
    1.75397532031767245e+00, 1.74654827828172321e+00, 1.73915426128591255e+00, 1.73179231405296408e+00,
    1.72446150294804568e+00, 1.71716091501782397e+00, 1.70988965707130269e+00, 1.70264685479992406e+00,
    1.69543165193456247e+00, 1.68824320943719641e+00, 1.68108070472517479e+00, 1.67394333092612602e+00,
    1.66683029616166634e+00, 1.65974082285818336e+00, 1.65267414708305682e+00, 1.64562951790478329e+00,
    1.63860619677554875e+00, 1.63160345693487452e+00, 1.62462058283303579e+00, 1.61765686957301646e+00,
    1.61071162236983101e+00, 1.60378415602609548e+00, 1.59687379442278909e+00, 1.58997987002419161e+00,
    1.58310172339603006e+00, 1.57623870273590727e+00, 1.56939016341512461e+00, 1.56255546753104557e+00,
    1.55573398346917724e+00, 1.54892508547417429e+00, 1.54212815322900276e+00, 1.53534257144151498e+00,
    1.52856772943771313e+00, 1.52180302076099894e+00, 1.51504784277671556e+00, 1.50830159628131244e+00,
    1.50156368511546479e+00, 1.49483351578049461e+00, 1.48811049705744858e+00, 1.48139403962818839e+00,
    1.47468355569785659e+00, 1.46797845861808085e+00, 1.46127816251027665e+00, 1.45458208188841143e+00,
    1.44788963128057735e+00, 1.44120022484872523e+00, 1.43451327600589340e+00, 1.42782819703025732e+00,
    1.42114439867531028e+00, 1.41446128977547247e+00, 1.40777827684640022e+00, 1.40109476367925234e+00,
    1.39441015092814236e+00, 1.38772383568997748e+00, 1.38103521107585681e+00, 1.37434366577316758e+00,
    1.36764858359747765e+00, 1.36094934303328441e+00, 1.35424531676263626e+00, 1.34753587118058848e+00,
    1.34082036589640530e+00, 1.33409815321936143e+00, 1.32736857762792715e+00, 1.32063097522105766e+00,
    1.31388467315022184e+00, 1.30712898903073249e+00, 1.30036323033083856e+00, 1.29358669373694912e+00,
    1.28679866449324498e+00, 1.27999841571381934e+00, 1.27318520766535781e+00, 1.26635828701823083e+00,
    1.25951688606371559e+00, 1.25266022189489856e+00, 1.24578749554862855e+00, 1.23889789110568871e+00,
    1.23199057474613749e+00, 1.22506469375653237e+00, 1.21811937548548310e+00, 1.21115372624370066e+00,
    1.20416683014438308e+00, 1.19715774787944307e+00, 1.19012551542669365e+00, 1.18306914268268848e+00,
    1.17598761201545376e+00, 1.16887987673083482e+00, 1.16174485944561323e+00, 1.15458145035992943e+00,
    1.14738850542085080e+00, 1.14016484436815313e+00, 1.13290924865253562e+00, 1.12562045921553522e+00,
    1.11829717411934682e+00, 1.11093804601357782e+00, 1.10354167942464176e+00, 1.09610662785202351e+00,
    1.08863139065398196e+00, 1.08111440970340600e+00, 1.07355406579243851e+00, 1.06594867476212474e+00,
    1.05829648333067716e+00, 1.05059566459093201e+00, 1.04284431314415116e+00, 1.03504043983344318e+00,
    1.02718196603564804e+00, 1.01926671746548658e+00, 1.01129241743999820e+00, 1.00325667954467534e+00,
    9.95156999635093409e-01, 9.86990747099065091e-01, 9.78755155294227297e-01, 9.70447311064227103e-01,
    9.62064143223043233e-01, 9.53602409881088686e-01, 9.45058684468168098e-01, 9.36429340286577871e-01,
    9.27710533402002824e-01, 9.18898183649593303e-01, 9.09987953496721236e-01, 9.00975224461224689e-01,
    8.91855070732944566e-01, 8.82622229585168672e-01, 8.73271068088864011e-01, 8.63795545553312061e-01,
    8.54189171008167158e-01, 8.44444954909157275e-01, 8.34555354086385592e-01, 8.24512208752295694e-01,
    8.14306670135218735e-01, 8.03929116989974824e-01, 7.93369058840627028e-01, 7.82615023307236868e-01,
    7.71654424224571978e-01, 7.60473406430111964e-01, 7.49056662017819397e-01, 7.37387211434299639e-01,
    7.25446140910003923e-01, 7.13212285190980122e-01, 7.00661841106819505e-01, 6.87767892795793090e-01,
    6.74499822837298479e-01, 6.60822574244424588e-01, 6.46695714894998885e-01, 6.32072236386066355e-01,
    6.16896990007756774e-01, 6.01104617755998327e-01, 5.84616766106385355e-01, 5.67338257053825123e-01,
    5.49151702327171809e-01, 5.29909720661565165e-01, 5.09423329602099240e-01, 4.87443966139244012e-01,
    4.63634336790890889e-01, 4.37518402207881185e-01, 4.08389134612001825e-01, 3.75121332878393110e-01,
    3.35737519214440450e-01, 2.86174591792092481e-01, 2.15241895984913806e-01, 0.00000000000000000e+00,
];

/// Density values `f[i] = exp(-0.5 * x[i]^2)` for wedge checks (`f[256] = 1.0`).
#[allow(clippy::excessive_precision)]
#[rustfmt::skip]
const ZIG_NORM_F: [f64; 257] = [
    1.26028593049859559e-03, 1.26028593049859559e-03, 2.60907274610215883e-03, 4.03797259336302443e-03,
    5.52240329925098895e-03, 7.05087547137321635e-03, 8.61658276939871945e-03, 1.02149714397014556e-02,
    1.18427578579078704e-02, 1.34974506017398587e-02, 1.51770883079353023e-02, 1.68800831525431419e-02,
    1.86051212757246155e-02, 2.03510962300444860e-02, 2.21170627073088294e-02, 2.39022033057958438e-02,
    2.57058040085488548e-02, 2.75272356696030368e-02, 2.93659397581332651e-02, 3.12214171919201963e-02,
    3.30932194585784670e-02, 3.49809414617160280e-02, 3.68842156885672290e-02, 3.88027074045260573e-02,
    4.07361106559408701e-02, 4.26841449164743689e-02, 4.46465522512943802e-02, 4.66230949019302982e-02,
    4.86135532158684519e-02, 5.06177238609476915e-02, 5.26354182767921064e-02, 5.46664613248888376e-02,
    5.67106901062028185e-02, 5.87679529209336748e-02, 6.08381083495397740e-02, 6.29210244377580302e-02,
    6.50165779712427588e-02, 6.71246538277883997e-02, 6.92451443970066721e-02, 7.13779490588902776e-02,
    7.35229737139811712e-02, 7.56801303589269697e-02, 7.78493367020959420e-02, 8.00305158146629447e-02,
    8.22235958132027517e-02, 8.44285095703532634e-02, 8.66451944505578497e-02, 8.88735920682756780e-02,
    9.11136480663735232e-02, 9.33653119126907488e-02, 9.56285367130087077e-02, 9.79032790388621732e-02,
    1.00189498768809698e-01, 1.02487158941934969e-01, 1.04796225622486777e-01, 1.07116667774683511e-01,
    1.09448457146811506e-01, 1.11791568163837868e-01, 1.14145977827838210e-01, 1.16511665625610661e-01,
    1.18888613442909824e-01, 1.21276805484790057e-01, 1.23676228201596392e-01, 1.26086870220185693e-01,
    1.28508722279999349e-01, 1.30941777173644136e-01, 1.33386029691668934e-01, 1.35841476571253533e-01,
    1.38308116448550511e-01, 1.40785949814444478e-01, 1.43274978973513212e-01, 1.45775208005993834e-01,
    1.48286642732574331e-01, 1.50809290681845481e-01, 1.53343161060262634e-01, 1.55888264724479003e-01,
    1.58444614155924091e-01, 1.61012223437510871e-01, 1.63591108232365501e-01, 1.66181285764481851e-01,
    1.68782774801211288e-01, 1.71395595637505727e-01, 1.74019770081838526e-01, 1.76655321443734747e-01,
    1.79302274522847416e-01, 1.81960655599522320e-01, 1.84630492426799020e-01, 1.87311814223800027e-01,
    1.90004651670464736e-01, 1.92709036903588898e-01, 1.95425003514134027e-01, 1.98152586545774861e-01,
    2.00891822494656314e-01, 2.03642749310334603e-01, 2.06405406397880464e-01, 2.09179834621124744e-01,
    2.11966076307029905e-01, 2.14764175251173306e-01, 2.17574176724330853e-01, 2.20396127480151666e-01,
    2.23230075763917152e-01, 2.26076071322379890e-01, 2.28934165414679924e-01, 2.31804410824338281e-01,
    2.34686861872329566e-01, 2.37581574431237619e-01, 2.40488605940500061e-01, 2.43408015422749785e-01,
    2.46339863501263301e-01, 2.49284212418527912e-01, 2.52241126055941567e-01, 2.55210669954661351e-01,
    2.58192911337618569e-01, 2.61187919132720492e-01, 2.64195763997260413e-01, 2.67216518343560749e-01,
    2.70250256365874852e-01, 2.73297054068576517e-01, 2.76356989295667710e-01, 2.79430141761637385e-01,
    2.82516593083707079e-01, 2.85616426815501201e-01, 2.88729728482182368e-01, 2.91856585617094655e-01,
    2.94997087799961311e-01, 2.98151326696684982e-01, 3.01319396100802550e-01, 3.04501391976649438e-01,
    3.07697412504291501e-01, 3.10907558126285954e-01, 3.14131931596336678e-01, 3.17370638029913055e-01,
    3.20623784956904856e-01, 3.23891482376390594e-01, 3.27173842813600846e-01, 3.30470981379162920e-01,
    3.33783015830717733e-01, 3.37110066637005379e-01, 3.40452257044521145e-01, 3.43809713146850049e-01,
    3.47182563956792922e-01, 3.50570941481405329e-01, 3.53974980800076000e-01, 3.57394820145779668e-01,
    3.60830600989647143e-01, 3.64282468129003112e-01, 3.67750569779031644e-01, 3.71235057668238555e-01,
    3.74736087137890195e-01, 3.78253817245618240e-01, 3.81788410873392714e-01, 3.85340034840076340e-01,
    3.88908860018787828e-01, 3.92495061459314676e-01, 3.96098818515831508e-01, 3.99720314980196334e-01,
    4.03359739221113622e-01, 4.07017284329472484e-01, 4.10693148270187325e-01, 4.14387534040890237e-01,
    4.18100649837847282e-01, 4.21832709229495006e-01, 4.25583931338021082e-01, 4.29354541029440595e-01,
    4.33144769112651373e-01, 4.36954852547984607e-01, 4.40785034665803044e-01, 4.44635565395738397e-01,
    4.48506701507202010e-01, 4.52398706861847522e-01, 4.56311852678715379e-01, 4.60246417812841757e-01,
    4.64202689048173189e-01, 4.68180961405692431e-01, 4.72181538467728978e-01, 4.76204732719504698e-01,
    4.80250865909045588e-01, 4.84320269426682048e-01, 4.88413284705456752e-01, 4.92530263643867261e-01,
    4.96671569052488437e-01, 5.00837575126147461e-01, 5.05028667943466902e-01, 5.09245245995746609e-01,
    5.13487720747325516e-01, 5.17756517229754909e-01, 5.22052074672320399e-01, 5.26374847171682925e-01,
    5.30725304403660503e-01, 5.35103932380456060e-01, 5.39511234256950467e-01, 5.43947731190024597e-01,
    5.48413963255264147e-01, 5.52910490425830625e-01, 5.57437893618764280e-01, 5.61996775814522675e-01,
    5.66587763256162669e-01, 5.71211506735251451e-01, 5.75868682972351942e-01, 5.80559996100789122e-01,
    5.85286179263369566e-01, 5.90047996332824121e-01, 5.94846243767985561e-01, 5.99681752619123487e-01,
    6.04555390697466000e-01, 6.09468064925771658e-01, 6.14420723888912002e-01, 6.19414360605832437e-01,
    6.24450015547024617e-01, 6.29528779924834803e-01, 6.34651799287621721e-01, 6.39820277453054698e-01,
    6.45035480820820406e-01, 6.50298743110814814e-01, 6.55611470579695377e-01, 6.60975147776661220e-01,
    6.66391343908748102e-01, 6.71861719897079990e-01, 6.77388036218771306e-01, 6.82972161644992526e-01,
    6.88616083004669477e-01, 6.94321916126114380e-01, 7.00091918136509284e-01, 7.05928501332751868e-01,
    7.11834248878245979e-01, 7.17811932630719518e-01, 7.23864533468627669e-01, 7.29995264561473678e-01,
    7.36207598126860097e-01, 7.42505296340148502e-01, 7.48892447219154267e-01, 7.55373506507093451e-01,
    7.61953346836792611e-01, 7.68637315798483489e-01, 7.75431304981184399e-01, 7.82341832654799618e-01,
    7.89376143566021704e-01, 7.96542330422956080e-01, 8.03849483170961387e-01, 8.11307874312653277e-01,
    8.18929191603699369e-01, 8.26726833946218376e-01, 8.34716292986880326e-01, 8.42915653112200958e-01,
    8.51346258458674621e-01, 8.60033621196328091e-01, 8.69008688036853494e-01, 8.78309655808913736e-01,
    8.87984660755829602e-01, 8.98095921898339533e-01, 9.08726440052126772e-01, 9.19991505039342683e-01,
    9.32060075959225798e-01, 9.45198953442294543e-01, 9.59879091800100892e-01, 9.77101701267664491e-01,
    1.00000000000000000e+00,
];

// ===========================================================================
// Deterministic PRNG & Exact Samplers (SplitMix64 + Xoshiro256** + Ziggurat)
// ===========================================================================

/// Deterministic SplitMix64-seeded Xoshiro256** PRNG with exact Ziggurat
/// Normal, Marsaglia–Tsang Gamma, and Knuth/Hörmann PTRD Poisson samplers.
/// Shared with [`crate::dependence`], [`crate::hertig`], [`crate::micro_reserving`],
/// and [`crate::subannual::odp_asymmetric`].
#[derive(Debug, Clone)]
pub(crate) struct DeterministicRng {
    s: [u64; 4],
}

impl DeterministicRng {
    pub(crate) fn from_seed(seed: u64) -> Self {
        let mut sm = seed;
        let mut next_sm = || {
            sm = sm.wrapping_add(0x9e3779b97f4a7c15);
            let mut z = sm;
            z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
            z ^ (z >> 31)
        };
        let mut s = [next_sm(), next_sm(), next_sm(), next_sm()];
        if s == [0; 4] {
            s[0] = 1;
        }
        Self { s }
    }

    #[inline]
    pub(crate) fn next_u64(&mut self) -> u64 {
        let result = self.s[1].wrapping_mul(5).rotate_left(7).wrapping_mul(9);
        let t = self.s[1] << 17;
        self.s[2] ^= self.s[0];
        self.s[3] ^= self.s[1];
        self.s[1] ^= self.s[2];
        self.s[0] ^= self.s[3];
        self.s[2] ^= t;
        self.s[3] = self.s[3].rotate_left(45);
        result
    }

    /// Uniform float strictly in `(0, 1)` (53-bit mantissa precision).
    #[inline]
    pub(crate) fn next_open01(&mut self) -> f64 {
        ((self.next_u64() >> 11) as f64 + 0.5) * (1.0 / 9_007_199_254_740_992.0)
    }

    /// Unbiased uniform index in `0..upper` via Lemire's nearly-divisionless multiply-high.
    #[inline]
    pub(crate) fn next_index(&mut self, upper: usize) -> usize {
        debug_assert!(upper > 0);
        let bound = upper as u64;
        let mut prod = (self.next_u64() as u128) * (bound as u128);
        let mut low = prod as u64;
        if low < bound {
            let threshold = bound.wrapping_neg() % bound;
            while low < threshold {
                prod = (self.next_u64() as u128) * (bound as u128);
                low = prod as u64;
            }
        }
        (prod >> 64) as usize
    }

    /// Standard normal `N(0, 1)` via 256-strip Ziggurat (98.51% branch-light fast
    /// path requiring 1 `next_u64` and zero transcendental functions).
    #[inline]
    pub(crate) fn next_standard_normal(&mut self) -> f64 {
        loop {
            let bits = self.next_u64();
            let i = (bits & 0xFF) as usize;
            // Signed 52-bit integer in [-2^51, 2^51 - 1] (disjoint from bits 0..8);
            // adding 0.5 is exact in IEEE-754 f64 and symmetric around 0.0.
            let j = ((bits as i64) >> 12) as f64 + 0.5;
            let x = j * ZIG_NORM_W[i];
            if x.abs() < ZIG_NORM_X_INNER[i] {
                return x;
            }
            if i == 0 {
                return self.sample_normal_tail(j >= 0.0);
            }
            let u = self.next_open01();
            if ZIG_NORM_F[i] + u * (ZIG_NORM_F[i + 1] - ZIG_NORM_F[i]) < (-0.5 * x * x).exp() {
                return x;
            }
        }
    }

    #[cold]
    fn sample_normal_tail(&mut self, positive: bool) -> f64 {
        // Marsaglia (1964) exact exponential tail for |Z| >= ZIG_NORM_R.
        loop {
            let tx = -self.next_open01().ln() * ZIG_NORM_INV_R;
            let ty = -self.next_open01().ln();
            if ty + ty >= tx * tx {
                let mag = ZIG_NORM_R + tx;
                return if positive { mag } else { -mag };
            }
        }
    }

    /// Exact Gamma(shape, scale) via Marsaglia & Tsang (2000).
    #[inline]
    pub(crate) fn sample_gamma(&mut self, shape: f64, scale: f64) -> f64 {
        if !(shape > 0.0) || !(scale > 0.0) || !shape.is_finite() || !scale.is_finite() {
            return 0.0;
        }
        if shape < 1.0 {
            let g = self.sample_gamma_ge1(shape + 1.0);
            let u = self.next_open01();
            return g * u.powf(1.0 / shape) * scale;
        }
        self.sample_gamma_ge1(shape) * scale
    }

    #[inline]
    fn sample_gamma_ge1(&mut self, shape: f64) -> f64 {
        let d = shape - (1.0 / 3.0);
        let c = (1.0 / 3.0) / d.sqrt();
        loop {
            let z = self.next_standard_normal();
            let v_root = 1.0 + c * z;
            if v_root <= 0.0 {
                continue;
            }
            let v = v_root * v_root * v_root;
            let u = self.next_open01();
            let z2 = z * z;
            if u < 1.0 - 0.0331 * (z2 * z2) {
                return d * v;
            }
            if u.ln() < 0.5 * z2 + d * (1.0 - v + v.ln()) {
                return d * v;
            }
        }
    }

    /// Exact Poisson(lambda) via Knuth (`lambda < 10`) and Hörmann PTRD (`lambda >= 10`).
    pub(crate) fn sample_poisson(&mut self, lambda: f64) -> f64 {
        if !(lambda > 0.0) || !lambda.is_finite() {
            return 0.0;
        }
        if lambda < 10.0 {
            let limit = (-lambda).exp();
            let mut k = 0u64;
            let mut p = 1.0;
            loop {
                k += 1;
                p *= self.next_open01();
                if p <= limit {
                    return (k - 1) as f64;
                }
            }
        }
        // Hörmann (1993), Insurance: Mathematics and Economics 12(1), pp. 39–45.
        // Exact for all lambda >= 10.
        let s = lambda.sqrt();
        let b = 0.931 + 2.53 * s;
        let a = -0.059 + 0.02483 * b;
        let inv_alpha = 1.1239 + 1.1328 / (b - 3.4);
        let v_r = 0.9277 - 3.6224 / (b - 2.0);
        let ln_lambda = lambda.ln();
        loop {
            let u = self.next_open01() - 0.5;
            let v = self.next_open01();
            let us = 0.5 - u.abs();
            let k = ((2.0 * a / us + b) * u + lambda + 0.43).floor();
            if us >= 0.07 && v <= v_r {
                return k;
            }
            if k < 0.0 || (us < 0.013 && v > us) {
                continue;
            }
            let lhs = (v * inv_alpha / (a / (us * us) + b)).ln();
            let rhs = -lambda + k * ln_lambda - ln_gamma(k + 1.0);
            if lhs <= rhs {
                return k;
            }
        }
    }
}

/// Branch-light batched unbiased index sampler with loop-invariant precomputed
/// Lemire rejection threshold.
///
/// For `bound <= 1024` (triangles up to `45 × 45`), `fill_weighted_deltas`
/// extracts 4 independent exact 16-bit Lemire indices per `next_u64()` with
/// zero per-element refill branches and fuses the `pool[idx] * sqrt_mu[k]`
/// product into a single pass. For larger pools (`bound <= u32::MAX`), it
/// extracts 2 independent 32-bit Lemire indices per `next_u64()`.
pub(crate) struct FastIndexSampler {
    bound: usize,
    threshold: u32,
}

impl FastIndexSampler {
    #[inline]
    pub(crate) fn new(bound: usize) -> Self {
        debug_assert!(bound > 0);
        let threshold = if bound <= 1024 {
            65_536u32 % (bound as u32)
        } else if bound <= u32::MAX as usize {
            let b = bound as u32;
            b.wrapping_neg() % b
        } else {
            0
        };
        Self { bound, threshold }
    }

    #[cold]
    fn sample_16_slow(rng: &mut DeterministicRng, bound: u32, threshold: u32) -> usize {
        loop {
            let mut w = rng.next_u64();
            for _ in 0..4 {
                let prod = ((w & 0xFFFF) as u32) * bound;
                if (prod & 0xFFFF) >= threshold {
                    return (prod >> 16) as usize;
                }
                w >>= 16;
            }
        }
    }

    #[cold]
    fn sample_32_slow(rng: &mut DeterministicRng, bound: u64, threshold: u32) -> usize {
        loop {
            let w = rng.next_u64();
            let p0 = (w & 0xFFFF_FFFF) * bound;
            if (p0 as u32) >= threshold {
                return (p0 >> 32) as usize;
            }
            let p1 = (w >> 32) * bound;
            if (p1 as u32) >= threshold {
                return (p1 >> 32) as usize;
            }
        }
    }

    #[inline]
    pub(crate) fn next(&mut self, rng: &mut DeterministicRng) -> usize {
        if self.bound <= 1 {
            return 0;
        }
        if self.bound <= u32::MAX as usize {
            let bound = self.bound as u64;
            let threshold = self.threshold;
            let prod = (rng.next_u64() >> 32) * bound;
            if (prod as u32) >= threshold {
                return (prod >> 32) as usize;
            }
            return Self::sample_32_slow(rng, bound, threshold);
        }
        rng.next_index(self.bound)
    }

    /// Fill `out[k] = pool[idx_k] * sqrt_mu[k]` for every cell using 4-wide
    /// 16-bit Lemire (1 `next_u64()` per 4 cells, 0 refill branches, parallel
    /// multiplies) for `bound <= 1024`, or 2-wide 32-bit Lemire for larger pools.
    #[inline]
    pub(crate) fn fill_weighted_deltas(
        &self,
        pool: &[f64],
        sqrt_mu: &[f64],
        rng: &mut DeterministicRng,
        out: &mut [f64],
    ) {
        debug_assert_eq!(out.len(), sqrt_mu.len());
        if self.bound <= 1 {
            let r0 = pool[0];
            for (dst, &sm) in out.iter_mut().zip(sqrt_mu.iter()) {
                *dst = r0 * sm;
            }
            return;
        }
        if self.bound <= 1024 {
            let bound = self.bound as u32;
            let threshold = self.threshold;
            let mut out_chunks = out.chunks_exact_mut(4);
            let mut sm_chunks = sqrt_mu.chunks_exact(4);
            for (oc, sc) in (&mut out_chunks).zip(&mut sm_chunks) {
                let w = rng.next_u64();
                let p0 = ((w & 0xFFFF) as u32) * bound;
                let p1 = (((w >> 16) & 0xFFFF) as u32) * bound;
                let p2 = (((w >> 32) & 0xFFFF) as u32) * bound;
                let p3 = ((w >> 48) as u32) * bound;
                let l0 = p0 & 0xFFFF;
                let l1 = p1 & 0xFFFF;
                let l2 = p2 & 0xFFFF;
                let l3 = p3 & 0xFFFF;
                if l0.min(l1).min(l2).min(l3) >= threshold {
                    oc[0] = pool[(p0 >> 16) as usize] * sc[0];
                    oc[1] = pool[(p1 >> 16) as usize] * sc[1];
                    oc[2] = pool[(p2 >> 16) as usize] * sc[2];
                    oc[3] = pool[(p3 >> 16) as usize] * sc[3];
                } else {
                    let i0 = if l0 >= threshold {
                        (p0 >> 16) as usize
                    } else {
                        Self::sample_16_slow(rng, bound, threshold)
                    };
                    let i1 = if l1 >= threshold {
                        (p1 >> 16) as usize
                    } else {
                        Self::sample_16_slow(rng, bound, threshold)
                    };
                    let i2 = if l2 >= threshold {
                        (p2 >> 16) as usize
                    } else {
                        Self::sample_16_slow(rng, bound, threshold)
                    };
                    let i3 = if l3 >= threshold {
                        (p3 >> 16) as usize
                    } else {
                        Self::sample_16_slow(rng, bound, threshold)
                    };
                    oc[0] = pool[i0] * sc[0];
                    oc[1] = pool[i1] * sc[1];
                    oc[2] = pool[i2] * sc[2];
                    oc[3] = pool[i3] * sc[3];
                }
            }
            let out_rem = out_chunks.into_remainder();
            let sm_rem = sm_chunks.remainder();
            if !out_rem.is_empty() {
                let mut w = rng.next_u64();
                for (dst, &sm) in out_rem.iter_mut().zip(sm_rem.iter()) {
                    let p = ((w & 0xFFFF) as u32) * bound;
                    w >>= 16;
                    let idx = if (p & 0xFFFF) >= threshold {
                        (p >> 16) as usize
                    } else {
                        Self::sample_16_slow(rng, bound, threshold)
                    };
                    *dst = pool[idx] * sm;
                }
            }
            return;
        }
        if self.bound <= u32::MAX as usize {
            let bound = self.bound as u64;
            let threshold = self.threshold;
            let mut out_chunks = out.chunks_exact_mut(2);
            let mut sm_chunks = sqrt_mu.chunks_exact(2);
            for (oc, sc) in (&mut out_chunks).zip(&mut sm_chunks) {
                let w = rng.next_u64();
                let p0 = (w & 0xFFFF_FFFF) * bound;
                let p1 = (w >> 32) * bound;
                let l0 = p0 as u32;
                let l1 = p1 as u32;
                if l0.min(l1) >= threshold {
                    oc[0] = pool[(p0 >> 32) as usize] * sc[0];
                    oc[1] = pool[(p1 >> 32) as usize] * sc[1];
                } else {
                    let i0 = if l0 >= threshold {
                        (p0 >> 32) as usize
                    } else {
                        Self::sample_32_slow(rng, bound, threshold)
                    };
                    let i1 = if l1 >= threshold {
                        (p1 >> 32) as usize
                    } else {
                        Self::sample_32_slow(rng, bound, threshold)
                    };
                    oc[0] = pool[i0] * sc[0];
                    oc[1] = pool[i1] * sc[1];
                }
            }
            let out_rem = out_chunks.into_remainder();
            let sm_rem = sm_chunks.remainder();
            if let (Some(dst), Some(&sm)) = (out_rem.first_mut(), sm_rem.first()) {
                let p = (rng.next_u64() >> 32) * bound;
                let idx = if (p as u32) >= threshold {
                    (p >> 32) as usize
                } else {
                    Self::sample_32_slow(rng, bound, threshold)
                };
                *dst = pool[idx] * sm;
            }
            return;
        }
        for (dst, &sm) in out.iter_mut().zip(sqrt_mu.iter()) {
            *dst = pool[rng.next_index(self.bound)] * sm;
        }
    }
}

/// Lanczos approximation to `ln Γ(z)` for `z > 0` (accuracy ~1e-14).
pub(crate) fn ln_gamma(z: f64) -> f64 {
    const COEFFS: [f64; 9] = [
        0.999_999_999_999_809_9,
        676.520_368_121_885_1,
        -1_259.139_216_722_402_8,
        771.323_428_777_653_1,
        -176.615_029_162_140_6,
        12.507_343_278_686_905,
        -0.138_571_095_265_720_12,
        9.984_369_578_019_572e-6,
        1.505_632_735_149_311_6e-7,
    ];
    let x = z - 1.0;
    let mut sum = COEFFS[0];
    for (idx, &coeff) in COEFFS.iter().enumerate().skip(1) {
        sum += coeff / (x + idx as f64);
    }
    let t = x + 7.5;
    0.5 * TAU.ln() + (x + 0.5) * t.ln() - t + sum.ln()
}

// ===========================================================================
// ODP GLM Fit (Exact O(n^2) Tridiagonal Green's Function + O(n^3) Fallback)
// ===========================================================================

struct OdpSolvedDiagnostics {
    residuals: Vec<OdpCellResidual>,
    n_observed: usize,
    rank: usize,
    degrees_of_freedom: usize,
    dispersion: f64,
    origin_reserves: Vec<f64>,
    total_reserve: f64,
    analytic_origin_process_se: Vec<f64>,
    analytic_origin_param_se: Vec<f64>,
    analytic_origin_prediction_error: Vec<f64>,
    analytic_total_process_se: f64,
    analytic_total_param_se: f64,
    analytic_total_prediction_error: f64,
}

/// Exact `O(n^2)` time, `O(n)` auxiliary-space solver for full strictly-positive
/// right triangles (`μ_{i,j} = α_i · β_j > 0` for all `i + j < n`).
///
/// In cumulative development coordinates `T_k = sum_{j=1}^k β_j v_j` (`T_0 = 0`),
/// the Schur complement of `X^T W X` reduces to an `(n - 1) × (n - 1)` symmetric
/// positive-definite tridiagonal matrix `K`. Its bidiagonal `L D L^T` factorization
/// takes `O(n)` time, its Green's function yields every hat-matrix leverage `h_{i,j}`
/// in `O(1)` (`O(n^2)` total), and every analytic ODP parameter variance collapses to
/// a 2-term closed form (`O(n)` total across all origins plus portfolio total).
///
/// Returns `None` if any cell has non-positive fitted mass or ill-conditioned pivot,
/// signaling the caller to fall back to the general `O(n^3)` SPD-inverse path.
fn try_solve_odp_tridiagonal(
    n: usize,
    ldfs: &[f64],
    tail: f64,
    latest: &[f64],
    fitted_cumulative: &[f64],
    fitted_incremental: &[f64],
    observed_incremental: &[f64],
) -> Option<OdpSolvedDiagnostics> {
    let m = n - 1;
    if latest.iter().any(|&v| !(v > 0.0) || !v.is_finite())
        || ldfs.iter().any(|&f| !(f > 1.0) || !f.is_finite())
    {
        return None;
    }

    // Rank-1 factorization μ_{i,j} = α_i · β_j with sum_j β_j = 1.
    let mut cdfs_to_last = vec![0.0; n];
    cdfs_from_ldfs(ldfs, 1.0, &mut cdfs_to_last);
    let mut b_cum = vec![0.0; n];
    let mut beta = vec![0.0; n];
    for j in 0..n {
        let inv_cdf = 1.0 / cdfs_to_last[j];
        if !inv_cdf.is_finite() || !(inv_cdf > 0.0) {
            return None;
        }
        b_cum[j] = inv_cdf;
        beta[j] = if j == 0 {
            inv_cdf
        } else {
            inv_cdf - b_cum[j - 1]
        };
        if !(beta[j] > 0.0) || !beta[j].is_finite() {
            return None;
        }
    }

    let mut alpha = vec![0.0; n];
    for i in 0..n {
        let a = fitted_cumulative[i * n + m];
        if !(a > 0.0) || !a.is_finite() {
            return None;
        }
        alpha[i] = a;
    }

    // A[k] = sum_{i=0}^{n-1-k} α_i.
    let mut a_sum = vec![0.0; n];
    let mut acc = 0.0;
    for k in (0..n).rev() {
        acc += alpha[m - k];
        a_sum[k] = acc;
    }

    // Assemble symmetric tridiagonal K of dimension m = n - 1 (indices 0..m correspond to k = 1..n).
    let mut d = vec![0.0; m];
    let mut e = vec![0.0; m.saturating_sub(1)];
    for k in 1..n {
        let idx = k - 1;
        if k < m {
            let off = a_sum[k + 1] / beta[k + 1];
            d[idx] = a_sum[k] / beta[k] + off - alpha[m - k] / b_cum[k];
            e[idx] = -off;
        } else {
            d[idx] = a_sum[k] / beta[k] - alpha[0] / b_cum[k];
        }
    }

    // Forward L D L^T sweep in O(n): L_{idx+1, idx} = -gamma[idx].
    let mut d_tilde = vec![0.0; m];
    let mut gamma = vec![0.0; m.saturating_sub(1)];
    if !d[0].is_finite() || d[0] <= 0.0 {
        return None;
    }
    d_tilde[0] = d[0];
    for idx in 0..(m - 1) {
        let g = -e[idx] / d_tilde[idx];
        let next_dt = d[idx + 1] + e[idx] * g;
        if !g.is_finite() || !next_dt.is_finite() || next_dt <= 1e-14 * d[idx + 1].abs().max(1e-30)
        {
            return None;
        }
        gamma[idx] = g;
        d_tilde[idx + 1] = next_dt;
    }

    // Backward diagonal recurrence for G = K^{-1} in O(n) (sum of positive squares).
    let mut g_diag = vec![0.0; m];
    g_diag[m - 1] = 1.0 / d_tilde[m - 1];
    for idx in (0..(m - 1)).rev() {
        g_diag[idx] = 1.0 / d_tilde[idx] + gamma[idx] * gamma[idx] * g_diag[idx + 1];
    }

    // Precompute w_j^T G w_j for j = 1..n in O(n).
    let mut w_g_w = vec![0.0; n];
    w_g_w[1] = g_diag[0] / (beta[1] * beta[1]);
    for j in 2..n {
        let g_jm1_j = gamma[j - 2] * g_diag[j - 1];
        w_g_w[j] = (g_diag[j - 1] - 2.0 * g_jm1_j + g_diag[j - 2]) / (beta[j] * beta[j]);
    }

    // Compute all N = n(n+1)/2 residuals & exact leverages in row-major order in O(n^2).
    let n_observed = n * (n + 1) / 2;
    let rank = 2 * n - 1;
    let degrees_of_freedom = n_observed - rank;
    let mut residuals = Vec::with_capacity(n_observed);
    let mut pearson_sq_sum = 0.0;
    let mut g_row = vec![0.0; n];

    for origin in 0..n {
        let ki = m - origin;
        if ki >= 1 {
            g_row[ki] = g_diag[ki - 1];
            for j in (1..ki).rev() {
                g_row[j] = gamma[j - 1] * g_row[j + 1];
            }
        }
        let inv_b_ki = 1.0 / b_cum[ki];
        let base_phi = inv_b_ki / alpha[origin];
        let z_g_z = if ki >= 1 {
            g_diag[ki - 1] * inv_b_ki * inv_b_ki
        } else {
            0.0
        };
        let row_off = origin * n;

        for dev in 0..=ki {
            let obs = observed_incremental[row_off + dev];
            let mu = fitted_incremental[row_off + dev];
            if !(mu > 0.0) || !mu.is_finite() {
                return None;
            }
            let raw_residual = (obs - mu) / mu.sqrt();
            pearson_sq_sum += raw_residual * raw_residual;

            let y_g_y = if dev == 0 {
                z_g_z
            } else if dev == 1 {
                let w_g_z = -g_row[1] * inv_b_ki / beta[1];
                w_g_w[1] + 2.0 * w_g_z + z_g_z
            } else {
                let w_g_z = -(g_row[dev] - g_row[dev - 1]) * inv_b_ki / beta[dev];
                w_g_w[dev] + 2.0 * w_g_z + z_g_z
            };
            let leverage = (mu * (base_phi + y_g_y)).clamp(0.0, 1.0);
            let saturated = (1.0 - leverage) <= 1e-10;

            residuals.push(OdpCellResidual {
                origin,
                dev,
                observed_incremental: obs,
                fitted_incremental: mu,
                raw_pearson_residual: raw_residual,
                leverage,
                saturated,
            });
        }
    }

    let dispersion = pearson_sq_sum / degrees_of_freedom as f64;

    // Precompute column G[k, m-1] for k = 0..m in O(n).
    let mut g_last = vec![0.0; m];
    g_last[m - 1] = g_diag[m - 1];
    for idx in (0..(m - 1)).rev() {
        g_last[idx] = gamma[idx] * g_last[idx + 1];
    }

    // Closed-form O(n) origin reserves & analytic prediction errors.
    let mut origin_reserves = vec![0.0; n];
    let mut analytic_origin_process_se = vec![0.0; n];
    let mut analytic_origin_param_se = vec![0.0; n];
    let mut analytic_origin_prediction_error = vec![0.0; n];
    let mut pre_tail_total_reserve = 0.0;
    let mut r_diag_sum = 0.0;
    let g_mm = g_diag[m - 1];

    for origin in 0..n {
        let ki = m - origin;
        let ai = alpha[origin];
        let r_pre_tail = if origin == 0 {
            0.0
        } else {
            ai * (1.0 - b_cum[ki])
        };
        pre_tail_total_reserve += r_pre_tail;
        let tail_increment = ai * (tail - 1.0);
        origin_reserves[origin] = r_pre_tail + tail_increment;

        let param_quad = if origin == 0 {
            0.0
        } else if origin < m {
            let inv_b = 1.0 / b_cum[ki];
            let diag_term = (r_pre_tail * r_pre_tail) * inv_b / ai;
            r_diag_sum += diag_term;
            diag_term
                + ai * ai * (g_diag[ki - 1] * inv_b * inv_b - 2.0 * g_last[ki - 1] * inv_b + g_mm)
        } else {
            let diag_term = (r_pre_tail * r_pre_tail) / (ai * b_cum[0]);
            r_diag_sum += diag_term;
            diag_term + ai * ai * g_mm
        };
        let param_var = (dispersion * param_quad).max(0.0);
        let proc_var = (dispersion * r_pre_tail).max(0.0);
        analytic_origin_process_se[origin] = proc_var.sqrt();
        analytic_origin_param_se[origin] = param_var.sqrt();
        analytic_origin_prediction_error[origin] = (proc_var + param_var).sqrt();
    }

    // Portfolio total parameter variance via one O(n) forward pass: y^T K^{-1} y = z^T D_tilde^{-1} z.
    let mut total_quad = r_diag_sum;
    let mut z_prev = 0.0;
    for idx in 0..m {
        let k = idx + 1;
        let y_k = if k < m {
            -alpha[m - k] / b_cum[k]
        } else {
            a_sum[0] - alpha[0]
        };
        let z_k = if idx == 0 {
            y_k
        } else {
            y_k + gamma[idx - 1] * z_prev
        };
        total_quad += (z_k * z_k) / d_tilde[idx];
        z_prev = z_k;
    }

    let total_reserve: f64 = origin_reserves.iter().sum();
    let total_param_var = (dispersion * total_quad).max(0.0);
    let total_proc_var = (dispersion * pre_tail_total_reserve).max(0.0);

    Some(OdpSolvedDiagnostics {
        residuals,
        n_observed,
        rank,
        degrees_of_freedom,
        dispersion,
        origin_reserves,
        total_reserve,
        analytic_origin_process_se,
        analytic_origin_param_se,
        analytic_origin_prediction_error,
        analytic_total_process_se: total_proc_var.sqrt(),
        analytic_total_param_se: total_param_var.sqrt(),
        analytic_total_prediction_error: (total_proc_var + total_param_var).sqrt(),
    })
}

/// General `O(n^3)` fallback for triangles with zero-mass fitted cells (e.g., an
/// LDF equal to `1.0` or a zero origin row). Uses `invert_spd` once in `O(p^3)`
/// and computes each hat-matrix leverage in `O(1)` via 3-sparse lookups (`O(N)`
/// total rather than `O(N p^2)`).
fn solve_odp_general(
    n: usize,
    tail: f64,
    fitted_cumulative: &[f64],
    fitted_incremental: &[f64],
    observed_incremental: &[f64],
) -> Result<OdpSolvedDiagnostics, ValidationError> {
    let mut active_origins = vec![false; n];
    let mut active_devs = vec![false; n];
    let mut active_cells: Vec<(usize, usize)> = Vec::with_capacity(n * (n + 1) / 2);
    for origin in 0..n {
        for dev in 0..(n - origin) {
            let mu = fitted_incremental[origin * n + dev];
            if mu.is_finite() && mu > 0.0 {
                active_origins[origin] = true;
                active_devs[dev] = true;
                active_cells.push((origin, dev));
            }
        }
    }

    let mut origin_col = vec![None; n];
    let mut dev_col = vec![None; n];
    let mut param_count = 1usize;
    let mut first_origin_skipped = false;
    for origin in 0..n {
        if active_origins[origin] {
            if !first_origin_skipped {
                first_origin_skipped = true;
            } else {
                origin_col[origin] = Some(param_count);
                param_count += 1;
            }
        }
    }
    let mut first_dev_skipped = false;
    for dev in 0..n {
        if active_devs[dev] {
            if !first_dev_skipped {
                first_dev_skipped = true;
            } else {
                dev_col[dev] = Some(param_count);
                param_count += 1;
            }
        }
    }

    let n_observed = active_cells.len();
    if n_observed <= param_count {
        return Err(ValidationError::InvalidParameter(
            "ODP model has non-positive residual degrees of freedom",
        ));
    }
    let degrees_of_freedom = n_observed - param_count;

    // Assemble Fisher information matrix M = X^T W X in O(N).
    let mut info = vec![0.0; param_count * param_count];
    for &(origin, dev) in &active_cells {
        let w = fitted_incremental[origin * n + dev];
        let mut cols = [0usize; 3];
        let mut ncols = 1usize;
        if let Some(c) = origin_col[origin] {
            cols[ncols] = c;
            ncols += 1;
        }
        if let Some(c) = dev_col[dev] {
            cols[ncols] = c;
            ncols += 1;
        }
        for a in 0..ncols {
            for b in 0..ncols {
                info[cols[a] * param_count + cols[b]] += w;
            }
        }
    }

    let chol = cholesky_lower(&info, param_count)?;
    let info_inv = invert_spd(&info, param_count)?;

    // Exact hat-matrix leverages in O(1) per cell via 3-sparse quadratic form x^T M^{-1} x.
    let mut residuals = Vec::with_capacity(n_observed);
    let mut pearson_sq_sum = 0.0;
    for &(origin, dev) in &active_cells {
        let obs = observed_incremental[origin * n + dev];
        let mu = fitted_incremental[origin * n + dev];
        let raw_residual = (obs - mu) / mu.sqrt();
        pearson_sq_sum += raw_residual * raw_residual;

        let mut cols = [0usize; 3];
        let mut ncols = 1usize;
        if let Some(c) = origin_col[origin] {
            cols[ncols] = c;
            ncols += 1;
        }
        if let Some(c) = dev_col[dev] {
            cols[ncols] = c;
            ncols += 1;
        }
        let mut qf = 0.0;
        for a in 0..ncols {
            let ca = cols[a];
            qf += info_inv[ca * param_count + ca];
            for b in (a + 1)..ncols {
                qf += 2.0 * info_inv[ca * param_count + cols[b]];
            }
        }
        let leverage = (mu * qf).clamp(0.0, 1.0);
        let saturated = (1.0 - leverage) <= 1e-10;

        residuals.push(OdpCellResidual {
            origin,
            dev,
            observed_incremental: obs,
            fitted_incremental: mu,
            raw_pearson_residual: raw_residual,
            leverage,
            saturated,
        });
    }

    let dispersion = pearson_sq_sum / degrees_of_freedom as f64;

    let mut origin_reserves = vec![0.0; n];
    let mut analytic_origin_process_se = vec![0.0; n];
    let mut analytic_origin_param_se = vec![0.0; n];
    let mut analytic_origin_prediction_error = vec![0.0; n];
    let mut g_total = vec![0.0; param_count];
    let mut g_origin = vec![0.0; param_count];
    let mut y_buf = vec![0.0; param_count];
    let mut pre_tail_total_reserve = 0.0;

    for origin in 0..n {
        g_origin.fill(0.0);
        let mut r_pre_tail = 0.0;
        for dev in (n - origin)..n {
            let mu = fitted_incremental[origin * n + dev];
            r_pre_tail += mu;
            g_origin[0] += mu;
            g_total[0] += mu;
            if let Some(c) = origin_col[origin] {
                g_origin[c] += mu;
                g_total[c] += mu;
            }
            if let Some(c) = dev_col[dev] {
                g_origin[c] += mu;
                g_total[c] += mu;
            }
        }
        pre_tail_total_reserve += r_pre_tail;
        let tail_increment = fitted_cumulative[origin * n + (n - 1)] * (tail - 1.0);
        origin_reserves[origin] = r_pre_tail + tail_increment;

        solve_lower_into(&chol, param_count, &g_origin, &mut y_buf);
        let param_var = (dispersion * y_buf.iter().map(|v| v * v).sum::<f64>()).max(0.0);
        let proc_var = (dispersion * r_pre_tail).max(0.0);
        analytic_origin_process_se[origin] = proc_var.sqrt();
        analytic_origin_param_se[origin] = param_var.sqrt();
        analytic_origin_prediction_error[origin] = (proc_var + param_var).sqrt();
    }

    let total_reserve: f64 = origin_reserves.iter().sum();
    solve_lower_into(&chol, param_count, &g_total, &mut y_buf);
    let total_param_var = (dispersion * y_buf.iter().map(|v| v * v).sum::<f64>()).max(0.0);
    let total_proc_var = (dispersion * pre_tail_total_reserve).max(0.0);

    Ok(OdpSolvedDiagnostics {
        residuals,
        n_observed,
        rank: param_count,
        degrees_of_freedom,
        dispersion,
        origin_reserves,
        total_reserve,
        analytic_origin_process_se,
        analytic_origin_param_se,
        analytic_origin_prediction_error,
        analytic_total_process_se: total_proc_var.sqrt(),
        analytic_total_param_se: total_param_var.sqrt(),
        analytic_total_prediction_error: (total_proc_var + total_param_var).sqrt(),
    })
}

/// Fit the Over-Dispersed Poisson GLM (`log μ_{i,j} = c + α_i + β_j`) to a
/// cumulative loss triangle.
///
/// Produces fitted expectations, unscaled Pearson residuals, exact GLM hat-
/// matrix diagonal leverages `h_{ii}`, Pearson dispersion `φ`, and England &
/// Verrall (1999, 2002) analytic prediction errors.
pub fn fit_odp(
    triangle: &TriangleView<'_>,
    tail: f64,
    policy: MonotonicityPolicy,
) -> Result<OdpFit, ValidationError> {
    validate_triangle(triangle)?;
    if policy == MonotonicityPolicy::Strict {
        validate_monotone_cumulative(triangle)?;
    }
    validate_tail(tail)?;

    let n = triangle.n_origins;
    if n < 3 {
        return Err(ValidationError::InvalidParameter(
            "ODP model requires at least a 3x3 triangle for positive degrees of freedom",
        ));
    }

    // Single-pass volume-weighted LDFs without duplicating triangle validation.
    let mut ldfs = vec![0.0; n - 1];
    volume_weighted_ldfs(triangle.data, n, n, &mut ldfs);
    for &f in &ldfs {
        if !f.is_finite() {
            return Err(ValidationError::NotFinite);
        }
        if f <= 0.0 {
            return Err(ValidationError::DivideByZero("development denominator"));
        }
        if f < 1.0 {
            return Err(ValidationError::InvalidParameter(
                "ODP GLM requires non-negative fitted expectations (all volume-weighted LDFs >= 1.0)",
            ));
        }
    }

    let observed_incremental = cumulative_to_incremental(triangle);
    let latest = latest_diagonal(triangle);

    // Exact ODP GLM MLE via backward + forward chain-ladder recursion in O(n^2).
    let mut fitted_cumulative = vec![0.0; n * n];
    for origin in 0..n {
        let d = n - 1 - origin;
        let row_off = origin * n;
        fitted_cumulative[row_off + d] = latest[origin];
        for dev in (0..d).rev() {
            fitted_cumulative[row_off + dev] = fitted_cumulative[row_off + dev + 1] / ldfs[dev];
        }
        for dev in (d + 1)..n {
            fitted_cumulative[row_off + dev] = fitted_cumulative[row_off + dev - 1] * ldfs[dev - 1];
        }
    }

    let mut fitted_incremental = vec![0.0; n * n];
    for origin in 0..n {
        let row_off = origin * n;
        fitted_incremental[row_off] = fitted_cumulative[row_off];
        for dev in 1..n {
            fitted_incremental[row_off + dev] =
                fitted_cumulative[row_off + dev] - fitted_cumulative[row_off + dev - 1];
        }
    }

    let solved = match try_solve_odp_tridiagonal(
        n,
        &ldfs,
        tail,
        &latest,
        &fitted_cumulative,
        &fitted_incremental,
        &observed_incremental,
    ) {
        Some(fast) => fast,
        None => solve_odp_general(
            n,
            tail,
            &fitted_cumulative,
            &fitted_incremental,
            &observed_incremental,
        )?,
    };

    Ok(OdpFit {
        n_origins: n,
        n_devs: n,
        ldfs,
        tail,
        fitted_cumulative,
        fitted_incremental,
        observed_incremental,
        residuals: solved.residuals,
        n_observed: solved.n_observed,
        rank: solved.rank,
        degrees_of_freedom: solved.degrees_of_freedom,
        dispersion: solved.dispersion,
        origin_reserves: solved.origin_reserves,
        total_reserve: solved.total_reserve,
        analytic_origin_process_se: solved.analytic_origin_process_se,
        analytic_origin_param_se: solved.analytic_origin_param_se,
        analytic_origin_prediction_error: solved.analytic_origin_prediction_error,
        analytic_total_process_se: solved.analytic_total_process_se,
        analytic_total_param_se: solved.analytic_total_param_se,
        analytic_total_prediction_error: solved.analytic_total_prediction_error,
    })
}

/// Build the adjusted residual pool for resampling along with its pre-centering mean.
pub(crate) fn build_adjusted_residual_pool(
    fit: &OdpFit,
    adjustment: ResidualAdjustment,
    center: bool,
) -> Result<(Vec<f64>, f64), ValidationError> {
    let mut pool = Vec::with_capacity(fit.residuals.len());
    match adjustment {
        ResidualAdjustment::DegreesOfFreedom => {
            let scale = (fit.n_observed as f64 / fit.degrees_of_freedom as f64).sqrt();
            for cell in &fit.residuals {
                pool.push(cell.raw_pearson_residual * scale);
            }
        }
        ResidualAdjustment::HatMatrixLeverage => {
            for cell in &fit.residuals {
                if !cell.saturated {
                    pool.push(cell.raw_pearson_residual / (1.0 - cell.leverage).sqrt());
                }
            }
        }
    }
    if pool.is_empty() {
        return Err(ValidationError::InvalidParameter(
            "no eligible residuals available for ODP bootstrap resampling",
        ));
    }
    let pool_mean = pool.iter().sum::<f64>() / pool.len() as f64;
    if center {
        for r in &mut pool {
            *r -= pool_mean;
        }
    }
    Ok((pool, pool_mean))
}

/// Compact reusable scratch buffers for zero-allocation, single-pass ODP
/// bootstrap iterations.
///
/// Packs only the `N = n(n+1)/2` upper-triangle `sqrt(mu)` values (half the
/// memory of an `n × n` grid), precomputes baseline column denominators and
/// latest-diagonal sums so the hot loop accumulates only zero-mean FMA deltas,
/// and eliminates the `col_num` array via `col_num[k] = col_den[k+1] + latest_pseudo[m-1-k]`.
pub(crate) struct OdpBootstrapScratch {
    n: usize,
    all_observed_positive: bool,
    inv_phi: f64,
    packed_sqrt_mu: Vec<f64>,
    cell_deltas: Vec<f64>,
    base_col_den: Vec<f64>,
    base_latest: Vec<f64>,
    delta_col_den: Vec<f64>,
    latest_pseudo: Vec<f64>,
    pseudo_ldfs: Vec<f64>,
    origin_reserves: Vec<f64>,
}

impl OdpBootstrapScratch {
    pub(crate) fn for_fit(fit: &OdpFit) -> Self {
        let n = fit.n_origins;
        let m = n.saturating_sub(1);
        let upper_len = n * (n + 1) / 2;
        let mut packed_sqrt_mu = Vec::with_capacity(upper_len);
        let mut base_col_den = vec![0.0; m];
        let mut base_latest = vec![0.0; n];

        for origin in 0..n {
            let row_off = origin * n;
            let last_dev = n - 1 - origin;
            for dev in 0..=last_dev {
                let mu = fit.fitted_incremental[row_off + dev];
                packed_sqrt_mu.push(if mu > 0.0 { mu.sqrt() } else { 0.0 });
                if dev < last_dev {
                    base_col_den[dev] += fit.fitted_cumulative[row_off + dev];
                }
            }
            base_latest[origin] = fit.fitted_cumulative[row_off + last_dev];
        }

        let inv_phi = if fit.dispersion > 0.0 && fit.dispersion.is_finite() {
            1.0 / fit.dispersion
        } else {
            0.0
        };

        Self {
            n,
            all_observed_positive: fit.n_observed == upper_len,
            inv_phi,
            packed_sqrt_mu,
            cell_deltas: vec![0.0; upper_len],
            base_col_den,
            base_latest,
            delta_col_den: vec![0.0; m],
            latest_pseudo: vec![0.0; n],
            pseudo_ldfs: vec![0.0; m],
            origin_reserves: vec![0.0; n],
        }
    }
}

#[derive(Clone, Copy)]
struct RefitOutcome {
    valid: bool,
    all_ldfs_ge_one: bool,
}

/// Complete volume-weighted LDF computation from `delta_col_den` + `latest_pseudo`
/// in a single right-to-left `O(n)` pass using `col_num[k] = col_den[k+1] + latest_pseudo[m-1-k]`.
#[inline]
fn finalize_pseudo_ldfs(scratch: &mut OdpBootstrapScratch) -> RefitOutcome {
    let m = scratch.n.saturating_sub(1);
    let mut all_ge_one = true;
    let mut next_den = 0.0;
    for k in (0..m).rev() {
        let den = scratch.base_col_den[k] + scratch.delta_col_den[k];
        if !(den > 0.0) || !den.is_finite() {
            return RefitOutcome {
                valid: false,
                all_ldfs_ge_one: false,
            };
        }
        let num = next_den + scratch.latest_pseudo[m - 1 - k];
        let f = num / den;
        if !f.is_finite() || f <= 0.0 {
            return RefitOutcome {
                valid: false,
                all_ldfs_ge_one: false,
            };
        }
        if f < 1.0 {
            all_ge_one = false;
        }
        scratch.pseudo_ldfs[k] = f;
        next_den = den;
    }
    RefitOutcome {
        valid: true,
        all_ldfs_ge_one: all_ge_one,
    }
}

/// Accumulate zero-mean cell perturbations `cell_deltas[0..N]` into
/// `delta_col_den[0..m]` and `latest_pseudo[0..n]` using paired origin rows
/// (breaking the serial `running += d` FP latency chain into 2 parallel pipes
/// and halving `delta_den` store traffic), then finalize volume-weighted LDFs.
#[inline]
fn accumulate_cell_deltas_and_refit(scratch: &mut OdpBootstrapScratch) -> RefitOutcome {
    let n = scratch.n;
    let m = n.saturating_sub(1);
    let deltas = &scratch.cell_deltas;
    let delta_den = &mut scratch.delta_col_den;
    let latest_out = &mut scratch.latest_pseudo;
    let base_latest = &scratch.base_latest;

    // Pair origins (0, 1) to overwrite delta_den[0..m] directly (no fill(0.0)).
    // Note: fit_odp guarantees n >= 3 so m >= 2.
    let mut row_start = 0usize;
    let mut origin = 0usize;
    if m >= 2 {
        let r0 = &deltas[0..n];
        let r1 = &deltas[n..2 * n - 1];
        row_start = 2 * n - 1;
        let mut run0 = 0.0;
        let mut run1 = 0.0;
        for (den_slot, (&d0, &d1)) in delta_den[..m - 1]
            .iter_mut()
            .zip(r0[..m - 1].iter().zip(r1[..m - 1].iter()))
        {
            run0 += d0;
            run1 += d1;
            *den_slot = run0 + run1;
        }
        run0 += r0[m - 1];
        delta_den[m - 1] = run0;
        latest_out[0] = base_latest[0] + run0 + r0[m];
        latest_out[1] = base_latest[1] + run1 + r1[m - 1];
        origin = 2;
    } else if m == 1 {
        let d0 = deltas[0];
        delta_den[0] = d0;
        latest_out[0] = base_latest[0] + d0 + deltas[1];
        row_start = 2;
        origin = 1;
    }

    // Remaining origin pairs (origin, origin + 1) while origin + 1 < m.
    while origin + 1 < m {
        let len_a = n - origin; // last_dev_a + 1 = m - origin + 1
        let len_b = len_a - 1; // last_dev_b + 1 = m - origin
        let ra = &deltas[row_start..row_start + len_a];
        let rb = &deltas[row_start + len_a..row_start + len_a + len_b];
        row_start += len_a + len_b;

        let last_a = len_a - 1;
        let last_b = len_b - 1;
        let mut run_a = 0.0;
        let mut run_b = 0.0;
        for (den_slot, (&da, &db)) in delta_den[..last_b]
            .iter_mut()
            .zip(ra[..last_b].iter().zip(rb[..last_b].iter()))
        {
            run_a += da;
            run_b += db;
            *den_slot += run_a + run_b;
        }
        run_a += ra[last_b];
        delta_den[last_b] += run_a;
        latest_out[origin] = base_latest[origin] + run_a + ra[last_a];
        latest_out[origin + 1] = base_latest[origin + 1] + run_b + rb[last_b];
        origin += 2;
    }

    if origin < m {
        let last_dev = m - origin;
        let row = &deltas[row_start..row_start + last_dev + 1];
        row_start += last_dev + 1;
        let mut running = 0.0;
        for (den_slot, &d) in delta_den[..last_dev].iter_mut().zip(&row[..last_dev]) {
            running += d;
            *den_slot += running;
        }
        latest_out[origin] = base_latest[origin] + running + row[last_dev];
    }
    latest_out[m] = base_latest[m] + deltas[row_start];

    finalize_pseudo_ldfs(scratch)
}

/// Fused 4-wide batched resampling (`pool[idx] * sqrt_mu`) + bounds-check-free
/// delta accumulation + LDF refit.
#[inline]
fn refit_pseudo_ldfs_from_pool(
    pool: &[f64],
    sampler: &mut FastIndexSampler,
    rng: &mut DeterministicRng,
    scratch: &mut OdpBootstrapScratch,
) -> RefitOutcome {
    if scratch.all_observed_positive {
        sampler.fill_weighted_deltas(pool, &scratch.packed_sqrt_mu, rng, &mut scratch.cell_deltas);
        return accumulate_cell_deltas_and_refit(scratch);
    }

    // General path when some observed cells have zero fitted mass (`sm == 0.0`).
    for (dst, &sm) in scratch
        .cell_deltas
        .iter_mut()
        .zip(scratch.packed_sqrt_mu.iter())
    {
        *dst = if sm > 0.0 {
            pool[sampler.next(rng)] * sm
        } else {
            0.0
        };
    }
    accumulate_cell_deltas_and_refit(scratch)
}

/// Fused single-pass pseudo-triangle delta accumulation + LDF refit from an
/// externally supplied residual slice (used by synchronized multi-line bootstrap).
#[inline]
fn refit_pseudo_ldfs_from_slice(
    sampled_residuals: &[f64],
    scratch: &mut OdpBootstrapScratch,
) -> RefitOutcome {
    if scratch.all_observed_positive {
        for ((dst, &r), &sm) in scratch
            .cell_deltas
            .iter_mut()
            .zip(sampled_residuals.iter())
            .zip(scratch.packed_sqrt_mu.iter())
        {
            *dst = r * sm;
        }
        return accumulate_cell_deltas_and_refit(scratch);
    }

    let mut r_idx = 0usize;
    for (dst, &sm) in scratch
        .cell_deltas
        .iter_mut()
        .zip(scratch.packed_sqrt_mu.iter())
    {
        if sm > 0.0 {
            *dst = sampled_residuals[r_idx] * sm;
            r_idx += 1;
        } else {
            *dst = 0.0;
        }
    }
    accumulate_cell_deltas_and_refit(scratch)
}

#[inline]
fn sample_positive_process(
    mag: f64,
    phi: f64,
    inv_phi: f64,
    dist: ProcessDistribution,
    rng: &mut DeterministicRng,
) -> f64 {
    if !(phi > 0.0) || !(mag > 0.0) || !mag.is_finite() {
        return mag;
    }
    match dist {
        ProcessDistribution::Gamma => rng.sample_gamma(mag * inv_phi, phi),
        ProcessDistribution::OverDispersedPoisson => phi * rng.sample_poisson(mag * inv_phi),
        ProcessDistribution::None => mag,
    }
}

#[inline]
fn sample_process_increment_fast(
    future_mu: f64,
    phi: f64,
    inv_phi: f64,
    dist: ProcessDistribution,
    rng: &mut DeterministicRng,
) -> f64 {
    if !(phi > 0.0) || future_mu == 0.0 || !future_mu.is_finite() {
        return future_mu;
    }
    // Per Shapland (2016) §3.5.3, preserve process variance on negative pseudo-
    // incremental expectations by reflecting the draw: -Sample(|mu|, phi).
    let sign = if future_mu < 0.0 { -1.0 } else { 1.0 };
    let mag = future_mu.abs();
    let draw = match dist {
        ProcessDistribution::Gamma => rng.sample_gamma(mag * inv_phi, phi),
        ProcessDistribution::OverDispersedPoisson => phi * rng.sample_poisson(mag * inv_phi),
        ProcessDistribution::None => mag,
    };
    sign * draw
}

/// Project per-origin reserves and sample exact process variance in `O(n)`.
///
/// Exploits the reproductive additivity of Gamma(`shape_j`, `φ`) and
/// `φ · Poisson(λ_j)` under common dispersion `φ` so each open origin requires
/// 1 draw (2 when negative pseudo-LDFs create both positive and negative future
/// increments) instead of `O(n)` cell-level draws (`O(n^2)` total).
///
/// Writes `scratch.origin_reserves[0..n]` and returns `Some(total_reserve)`.
#[inline]
fn project_origin_reserves(
    tail: f64,
    phi: f64,
    all_ldfs_ge_one: bool,
    process_distribution: ProcessDistribution,
    rng: &mut DeterministicRng,
    scratch: &mut OdpBootstrapScratch,
) -> Option<f64> {
    let n = scratch.n;
    let inv_phi = scratch.inv_phi;
    let tail_inc = tail - 1.0;

    if all_ldfs_ge_one || process_distribution == ProcessDistribution::None {
        // Walking origin = 0..n traverses latest_dev = n-1 down to 0, so a single
        // register `cdf` compounds pseudo-LDFs right-to-left with zero arrays.
        let mut cdf = tail;
        let l0 = scratch.latest_pseudo[0];
        if !l0.is_finite() {
            return None;
        }
        let r0 = if tail_inc > 0.0 {
            sample_process_increment_fast(l0 * tail_inc, phi, inv_phi, process_distribution, rng)
        } else {
            0.0
        };
        if !r0.is_finite() {
            return None;
        }
        scratch.origin_reserves[0] = r0;
        let mut total = r0;

        match process_distribution {
            ProcessDistribution::Gamma if phi > 0.0 => {
                for origin in 1..n {
                    cdf *= scratch.pseudo_ldfs[n - 1 - origin];
                    let latest_i = scratch.latest_pseudo[origin];
                    let expected_res = latest_i * (cdf - 1.0);
                    let res_i = if expected_res > 0.0 && expected_res.is_finite() {
                        let shape = expected_res * inv_phi;
                        if shape >= 1.0 {
                            rng.sample_gamma_ge1(shape) * phi
                        } else {
                            rng.sample_gamma(shape, phi)
                        }
                    } else if expected_res < 0.0 && expected_res.is_finite() {
                        -rng.sample_gamma(-expected_res * inv_phi, phi)
                    } else if expected_res == 0.0 {
                        0.0
                    } else {
                        return None;
                    };
                    scratch.origin_reserves[origin] = res_i;
                    total += res_i;
                }
            }
            ProcessDistribution::OverDispersedPoisson if phi > 0.0 => {
                for origin in 1..n {
                    cdf *= scratch.pseudo_ldfs[n - 1 - origin];
                    let latest_i = scratch.latest_pseudo[origin];
                    if !latest_i.is_finite() {
                        return None;
                    }
                    let expected_res = latest_i * (cdf - 1.0);
                    let res_i = sample_process_increment_fast(
                        expected_res,
                        phi,
                        inv_phi,
                        ProcessDistribution::OverDispersedPoisson,
                        rng,
                    );
                    if !res_i.is_finite() {
                        return None;
                    }
                    scratch.origin_reserves[origin] = res_i;
                    total += res_i;
                }
            }
            _ => {
                for origin in 1..n {
                    cdf *= scratch.pseudo_ldfs[n - 1 - origin];
                    let latest_i = scratch.latest_pseudo[origin];
                    let expected_res = latest_i * (cdf - 1.0);
                    if !expected_res.is_finite() {
                        return None;
                    }
                    scratch.origin_reserves[origin] = expected_res;
                    total += expected_res;
                }
            }
        }
        return total.is_finite().then_some(total);
    }

    // General path when any pseudo-LDF < 1.0: track exact positive and negative
    // suffix increment factors in two registers (exact equivalence to Shapland
    // cell-by-cell reflection because every valid pseudo-LDF > 0 preserves signs).
    let mut pos_factor = tail_inc;
    let mut neg_factor = 0.0;

    let l0 = scratch.latest_pseudo[0];
    if !l0.is_finite() {
        return None;
    }
    let r0 = if tail_inc > 0.0 {
        sample_process_increment_fast(l0 * tail_inc, phi, inv_phi, process_distribution, rng)
    } else {
        0.0
    };
    if !r0.is_finite() {
        return None;
    }
    scratch.origin_reserves[0] = r0;
    let mut total = r0;

    for origin in 1..n {
        let f = scratch.pseudo_ldfs[n - 1 - origin];
        let step = f - 1.0;
        if step >= 0.0 {
            pos_factor = step + f * pos_factor;
            neg_factor *= f;
        } else {
            pos_factor *= f;
            neg_factor = (-step) + f * neg_factor;
        }

        let latest_i = scratch.latest_pseudo[origin];
        if !latest_i.is_finite() {
            return None;
        }
        let (pos_mu, neg_mu) = if latest_i >= 0.0 {
            (latest_i * pos_factor, latest_i * neg_factor)
        } else {
            (-latest_i * neg_factor, -latest_i * pos_factor)
        };
        let pos_draw = if pos_mu > 0.0 {
            sample_positive_process(pos_mu, phi, inv_phi, process_distribution, rng)
        } else {
            0.0
        };
        let neg_draw = if neg_mu > 0.0 {
            sample_positive_process(neg_mu, phi, inv_phi, process_distribution, rng)
        } else {
            0.0
        };
        let res_i = pos_draw - neg_draw;
        if !res_i.is_finite() {
            return None;
        }
        scratch.origin_reserves[origin] = res_i;
        total += res_i;
    }

    total.is_finite().then_some(total)
}

/// Execute one ODP bootstrap iteration into reusable `scratch` buffers using an
/// externally supplied residual slice (used by synchronized multi-line bootstrap).
///
/// Returns `Some(&scratch.origin_reserves)` on valid refit or `None` if the
/// pseudo-triangle has a degenerate non-positive column sum.
pub(crate) fn run_odp_iteration_with_scratch<'a>(
    fit: &OdpFit,
    sampled_residuals: &[f64],
    process_distribution: ProcessDistribution,
    rng: &mut DeterministicRng,
    scratch: &'a mut OdpBootstrapScratch,
) -> Option<&'a [f64]> {
    let outcome = refit_pseudo_ldfs_from_slice(sampled_residuals, scratch);
    if !outcome.valid {
        return None;
    }
    project_origin_reserves(
        fit.tail,
        fit.dispersion,
        outcome.all_ldfs_ge_one,
        process_distribution,
        rng,
        scratch,
    )?;
    Some(&scratch.origin_reserves)
}

/// Run the full England & Verrall (1999, 2002) / Shapland (2016) ODP bootstrap.
pub fn bootstrap_odp(
    triangle: &TriangleView<'_>,
    tail: f64,
    options: OdpBootstrapOptions,
) -> Result<OdpBootstrapResult, ValidationError> {
    if options.iterations == 0 {
        return Err(ValidationError::ShapeMismatch);
    }

    let fit = fit_odp(triangle, tail, options.monotonicity_policy)?;
    let (pool, pool_mean) =
        build_adjusted_residual_pool(&fit, options.residual_adjustment, options.center_residuals)?;

    let mut rng = DeterministicRng::from_seed(options.seed);
    let mut sampler = FastIndexSampler::new(pool.len());
    let n = fit.n_origins;
    let mut simulated_totals = Vec::with_capacity(options.iterations);
    let mut simulated_by_origin: Vec<Vec<f64>> = (0..n)
        .map(|_| Vec::with_capacity(options.iterations))
        .collect();
    let mut scratch = OdpBootstrapScratch::for_fit(&fit);
    let mut failed_iterations = 0usize;

    for _ in 0..options.iterations {
        let outcome = refit_pseudo_ldfs_from_pool(&pool, &mut sampler, &mut rng, &mut scratch);
        if !outcome.valid {
            failed_iterations += 1;
            continue;
        }
        if let Some(total) = project_origin_reserves(
            fit.tail,
            fit.dispersion,
            outcome.all_ldfs_ge_one,
            options.process_distribution,
            &mut rng,
            &mut scratch,
        ) {
            simulated_totals.push(total);
            for (origin_vec, &r) in simulated_by_origin
                .iter_mut()
                .zip(scratch.origin_reserves.iter())
            {
                origin_vec.push(r);
            }
        } else {
            failed_iterations += 1;
        }
    }

    if simulated_totals.is_empty() {
        return Err(ValidationError::NotFinite);
    }

    // Reuse a single scratch buffer across portfolio + all n origin summaries
    // and skip redundant finiteness scans (every pushed value is verified finite).
    let mut summary_buf = Vec::with_capacity(simulated_totals.len());
    let summary = summarize_with_scratch(&simulated_totals, &mut summary_buf, true)?;
    let mut origin_summaries = Vec::with_capacity(n);
    for series in &simulated_by_origin {
        origin_summaries.push(summarize_with_scratch(series, &mut summary_buf, true)?);
    }

    Ok(OdpBootstrapResult {
        base_total_reserve: fit.total_reserve,
        fit,
        options,
        simulated_totals,
        simulated_by_origin,
        origin_summaries,
        summary,
        failed_iterations,
        residual_pool_mean_before_centering: pool_mean,
    })
}

/// Run a deterministic ODP bootstrap using the classic England & Verrall
/// (1999, 2002) specification (`DegreesOfFreedom` scaling + Gamma process variance).
///
/// Because this compact entry point only returns portfolio totals, it skips
/// allocating per-origin simulation series (`O(B)` space instead of `O(n B)`),
/// skips `n` per-origin summaries, and collapses process variance to 1–2 exact
/// Gamma draws per iteration.
pub fn bootstrap(
    triangle: &TriangleView<'_>,
    tail: f64,
    config: BootstrapConfig,
) -> Result<BootstrapResult, ValidationError> {
    if config.iterations == 0 {
        return Err(ValidationError::ShapeMismatch);
    }

    let options = OdpBootstrapOptions::england_verrall(config.iterations, config.seed);
    let fit = fit_odp(triangle, tail, options.monotonicity_policy)?;
    let (pool, _) =
        build_adjusted_residual_pool(&fit, options.residual_adjustment, options.center_residuals)?;

    let mut rng = DeterministicRng::from_seed(options.seed);
    let mut sampler = FastIndexSampler::new(pool.len());
    let n = fit.n_origins;
    let phi = fit.dispersion;
    let mut scratch = OdpBootstrapScratch::for_fit(&fit);
    let inv_phi = scratch.inv_phi;
    let tail_inc = fit.tail - 1.0;
    let mut simulated_totals = Vec::with_capacity(config.iterations);

    for _ in 0..config.iterations {
        let outcome = refit_pseudo_ldfs_from_pool(&pool, &mut sampler, &mut rng, &mut scratch);
        if !outcome.valid {
            continue;
        }
        let mut ok = true;
        let total = if outcome.all_ldfs_ge_one {
            let mut cdf = fit.tail;
            let l0 = scratch.latest_pseudo[0];
            if !l0.is_finite() {
                ok = false;
            }
            let mut pos_sum = if tail_inc > 0.0 && l0 > 0.0 {
                l0 * tail_inc
            } else {
                0.0
            };
            let mut neg_sum = if tail_inc > 0.0 && l0 < 0.0 {
                -l0 * tail_inc
            } else {
                0.0
            };
            for origin in 1..n {
                cdf *= scratch.pseudo_ldfs[n - 1 - origin];
                let li = scratch.latest_pseudo[origin];
                if !li.is_finite() {
                    ok = false;
                    break;
                }
                let expected = li * (cdf - 1.0);
                if expected >= 0.0 {
                    pos_sum += expected;
                } else {
                    neg_sum -= expected;
                }
            }
            if ok {
                let p_draw = if pos_sum > 0.0 {
                    sample_positive_process(
                        pos_sum,
                        phi,
                        inv_phi,
                        options.process_distribution,
                        &mut rng,
                    )
                } else {
                    0.0
                };
                let n_draw = if neg_sum > 0.0 {
                    sample_positive_process(
                        neg_sum,
                        phi,
                        inv_phi,
                        options.process_distribution,
                        &mut rng,
                    )
                } else {
                    0.0
                };
                p_draw - n_draw
            } else {
                f64::NAN
            }
        } else {
            let mut pos_factor = tail_inc;
            let mut neg_factor = 0.0;
            let l0 = scratch.latest_pseudo[0];
            if !l0.is_finite() {
                ok = false;
            }
            let mut pos_sum = if tail_inc > 0.0 && l0 > 0.0 {
                l0 * tail_inc
            } else {
                0.0
            };
            let mut neg_sum = if tail_inc > 0.0 && l0 < 0.0 {
                -l0 * tail_inc
            } else {
                0.0
            };
            for origin in 1..n {
                let f = scratch.pseudo_ldfs[n - 1 - origin];
                let step = f - 1.0;
                if step >= 0.0 {
                    pos_factor = step + f * pos_factor;
                    neg_factor *= f;
                } else {
                    pos_factor *= f;
                    neg_factor = (-step) + f * neg_factor;
                }
                let li = scratch.latest_pseudo[origin];
                if !li.is_finite() {
                    ok = false;
                    break;
                }
                if li >= 0.0 {
                    pos_sum += li * pos_factor;
                    neg_sum += li * neg_factor;
                } else {
                    pos_sum += -li * neg_factor;
                    neg_sum += -li * pos_factor;
                }
            }
            if ok {
                let p_draw = if pos_sum > 0.0 {
                    sample_positive_process(
                        pos_sum,
                        phi,
                        inv_phi,
                        options.process_distribution,
                        &mut rng,
                    )
                } else {
                    0.0
                };
                let n_draw = if neg_sum > 0.0 {
                    sample_positive_process(
                        neg_sum,
                        phi,
                        inv_phi,
                        options.process_distribution,
                        &mut rng,
                    )
                } else {
                    0.0
                };
                p_draw - n_draw
            } else {
                f64::NAN
            }
        };
        if ok && total.is_finite() {
            simulated_totals.push(total);
        }
    }

    if simulated_totals.is_empty() {
        return Err(ValidationError::NotFinite);
    }

    let mut summary_buf = Vec::with_capacity(simulated_totals.len());
    let summary = summarize_with_scratch(&simulated_totals, &mut summary_buf, true)?;
    Ok(BootstrapResult {
        base_total_reserve: fit.total_reserve,
        simulated_totals,
        summary,
    })
}

/// Reserve-only convenience wrapper around [`bootstrap`].
pub fn bootstrap_summary(
    triangle: &TriangleView<'_>,
    tail: f64,
    config: BootstrapConfig,
) -> Result<BootstrapSummary, ValidationError> {
    Ok(bootstrap(triangle, tail, config)?.summary)
}

/// Output of the 1-year Solvency II Claims Development Result (CDR)
/// re-reserving ODP bootstrap (England & Verrall / Merz & Wüthrich;
/// mirrors `mages/ChainLadder`'s `CDR(BootChainLadder)`).
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct OdpCdrBootstrapResult {
    /// Initial deterministic reserve $R^{(0)}$ summed across origins.
    pub base_total_reserve: f64,
    /// Initial deterministic reserve $R_i^{(0)}$ per origin.
    pub base_origin_reserves: Vec<f64>,
    /// Simulated total 1-year CDR samples: $R^{(0)} - (Y_{\text{next}} + R^{(1)})$.
    /// Negative values represent adverse 1-year development.
    pub simulated_total_cdr: Vec<f64>,
    /// Simulated 1-year CDR samples per origin (`n_origins` vectors).
    pub simulated_cdr_by_origin: Vec<Vec<f64>>,
    /// Empirical summary statistics of total 1-year CDR.
    pub summary: BootstrapSummary,
    /// Empirical summary statistics of 1-year CDR per origin.
    pub origin_summaries: Vec<BootstrapSummary>,
    /// Count of skipped iterations due to degenerate pseudo-triangles.
    pub failed_iterations: usize,
}

/// Run a 1-year Solvency II Claims Development Result (CDR) re-reserving
/// bootstrap on top of the ODP bootstrap model.
///
/// For each simulation $b$:
/// 1. Resample scaled Pearson residuals and refit pseudo-LDFs $\hat{f}_k^{(b)}$.
/// 2. Simulate the next calendar-year incremental payments $Y_{i, n-i}^{(b)}$
///    (with process variance from `options.process_distribution`) for each open
///    origin $i \ge 1$.
/// 3. Append the simulated diagonal $C_{i, n-i}^{(b)} = C_{i, n-1-i} + Y_{i, n-i}^{(b)}$
///    to the observed triangle, re-estimate volume-weighted chain-ladder LDFs
///    on the extended 1-year-later triangle, and compute the updated reserve $R_i^{(1,b)}$.
/// 4. Record $\text{CDR}_i^{(b)}(1) = R_i^{(0)} - (Y_{i, n-i}^{(b)} + R_i^{(1,b)})$.
pub fn bootstrap_odp_cdr(
    triangle: &TriangleView<'_>,
    tail: f64,
    options: OdpBootstrapOptions,
) -> Result<OdpCdrBootstrapResult, ValidationError> {
    if options.iterations == 0 {
        return Err(ValidationError::ShapeMismatch);
    }

    let fit = fit_odp(triangle, tail, options.monotonicity_policy)?;
    let (pool, _) =
        build_adjusted_residual_pool(&fit, options.residual_adjustment, options.center_residuals)?;

    let n = fit.n_origins;
    let m = n.saturating_sub(1);
    let mut rng = DeterministicRng::from_seed(options.seed);
    let mut sampler = FastIndexSampler::new(pool.len());

    let mut simulated_total_cdr = Vec::with_capacity(options.iterations);
    let mut simulated_cdr_by_origin: Vec<Vec<f64>> = (0..n)
        .map(|_| Vec::with_capacity(options.iterations))
        .collect();
    let mut scratch = OdpBootstrapScratch::for_fit(&fit);
    let inv_phi = scratch.inv_phi;
    let mut ext_ldfs = vec![1.0; m];
    let mut ext_cdfs = vec![tail; n];
    let mut next_pay = vec![0.0; n];
    let mut next_cum_diag = vec![0.0; n];
    let mut failed_iterations = 0usize;

    // Precompute observed column sums once: each 1-year CDR draw only appends a
    // single simulated diagonal cell (i = n - 1 - k, j = k + 1) to link k.
    let mut obs_den = vec![0.0; m];
    let mut obs_num = vec![0.0; m];
    for i in 0..m {
        let row = &triangle.data[i * n..i * n + (n - i)];
        for k in 0..(n - 1 - i) {
            let c0 = row[k];
            let c1 = row[k + 1];
            if c0.is_finite() && c1.is_finite() {
                obs_den[k] += c0;
                obs_num[k] += c1;
            }
        }
    }
    let obs_diag: Vec<f64> = (0..n).map(|i| triangle.get(i, n - 1 - i)).collect();
    let r1_origin0 = obs_diag[0] * (tail - 1.0);

    for _ in 0..options.iterations {
        // 1. Fused single-pass resampling + delta accumulation + LDF refit.
        let outcome = refit_pseudo_ldfs_from_pool(&pool, &mut sampler, &mut rng, &mut scratch);
        if !outcome.valid {
            failed_iterations += 1;
            continue;
        }

        // 2. Simulate next calendar diagonal (i + j == n) and update extended LDFs in O(n).
        let mut valid_ext = true;
        for i in 1..n {
            let k = n - 1 - i; // link k -> k + 1 (j_next = k + 1 = n - i)
            let c_prev = obs_diag[i];
            let expected_inc = c_prev * (scratch.pseudo_ldfs[k] - 1.0);
            let sim_inc = sample_process_increment_fast(
                expected_inc,
                fit.dispersion,
                inv_phi,
                options.process_distribution,
                &mut rng,
            );
            let c_next = c_prev + sim_inc;
            next_pay[i] = sim_inc;
            next_cum_diag[i] = c_next;

            let den = obs_den[k] + c_prev;
            let num = obs_num[k] + c_next;
            if !den.is_finite() || den <= 0.0 || !num.is_finite() || num <= 0.0 {
                valid_ext = false;
                break;
            }
            ext_ldfs[k] = num / den;
        }
        if !valid_ext {
            failed_iterations += 1;
            continue;
        }

        // 3. Compound extended CDFs right-to-left in O(n).
        ext_cdfs[m] = tail;
        for k in (0..m).rev() {
            ext_cdfs[k] = ext_ldfs[k] * ext_cdfs[k + 1];
        }

        // 4. Compute updated reserve R^{(1)} and 1-year CDR = R^{(0)} - (Y_next + R^{(1)}) in O(n).
        let cdr0 = fit.origin_reserves[0] - r1_origin0;
        if !cdr0.is_finite() {
            failed_iterations += 1;
            continue;
        }
        scratch.origin_reserves[0] = cdr0;
        let mut total_cdr = cdr0;
        let mut ok = true;
        for i in 1..n {
            let j_next = n - i;
            let c_next = next_cum_diag[i];
            let r1 = c_next * (ext_cdfs[j_next] - 1.0);
            let cdr_i = fit.origin_reserves[i] - (next_pay[i] + r1);
            if !cdr_i.is_finite() {
                ok = false;
                break;
            }
            scratch.origin_reserves[i] = cdr_i;
            total_cdr += cdr_i;
        }
        if !ok || !total_cdr.is_finite() {
            failed_iterations += 1;
            continue;
        }
        simulated_total_cdr.push(total_cdr);
        for (origin_vec, &r) in simulated_cdr_by_origin
            .iter_mut()
            .zip(scratch.origin_reserves.iter())
        {
            origin_vec.push(r);
        }
    }

    if simulated_total_cdr.is_empty() {
        return Err(ValidationError::NotFinite);
    }

    let mut summary_buf = Vec::with_capacity(simulated_total_cdr.len());
    let summary = summarize_with_scratch(&simulated_total_cdr, &mut summary_buf, true)?;
    let mut origin_summaries = Vec::with_capacity(n);
    for series in &simulated_cdr_by_origin {
        origin_summaries.push(summarize_with_scratch(series, &mut summary_buf, true)?);
    }

    Ok(OdpCdrBootstrapResult {
        base_total_reserve: fit.total_reserve,
        base_origin_reserves: fit.origin_reserves,
        simulated_total_cdr,
        simulated_cdr_by_origin,
        summary,
        origin_summaries,
        failed_iterations,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::chain_ladder::ChainLadderEngine;
    use crate::datasets::genins;
    use crate::ldf::DevelopmentFactorEngine;
    use crate::types::Triangle;

    fn triangle_4x4() -> Triangle {
        #[rustfmt::skip]
        let data = vec![
            1000.0, 1500.0, 1700.0, 1800.0,
            1200.0, 1800.0, 2000.0, f64::NAN,
            1500.0, 2100.0, f64::NAN, f64::NAN,
            1800.0, f64::NAN, f64::NAN, f64::NAN,
        ];
        Triangle::new(4, 4, data)
    }

    #[test]
    fn bootstrap_is_deterministic_for_same_seed() {
        let triangle = triangle_4x4();
        let config = BootstrapConfig {
            iterations: 50,
            seed: 42,
        };

        let first = bootstrap(&triangle.view(), 1.0, config).expect("bootstrap");
        let second = bootstrap(&triangle.view(), 1.0, config).expect("bootstrap");

        assert_eq!(first.simulated_totals, second.simulated_totals);
        assert_eq!(first.summary, second.summary);
    }

    #[test]
    fn bootstrap_returns_ordered_quantiles() {
        let triangle = triangle_4x4();
        let summary = bootstrap_summary(
            &triangle.view(),
            1.0,
            BootstrapConfig {
                iterations: 100,
                seed: 7,
            },
        )
        .expect("summary");

        assert!(summary.mean >= 0.0);
        assert!(summary.std_dev >= 0.0);
        assert!(summary.p50 <= summary.p75);
        assert!(summary.p75 <= summary.p90);
        assert!(summary.p90 <= summary.p95);
        assert!(summary.p95 <= summary.p99);
    }

    #[test]
    fn bootstrap_rejects_zero_iterations() {
        let triangle = triangle_4x4();
        assert_eq!(
            bootstrap(
                &triangle.view(),
                1.0,
                BootstrapConfig {
                    iterations: 0,
                    seed: 1
                }
            ),
            Err(ValidationError::ShapeMismatch)
        );
    }

    #[test]
    fn bootstrap_base_reserve_matches_chain_ladder() {
        let triangle = triangle_4x4();
        let result = bootstrap(
            &triangle.view(),
            1.0,
            BootstrapConfig {
                iterations: 10,
                seed: 9,
            },
        )
        .expect("bootstrap");

        let factors =
            DevelopmentFactorEngine::volume_weighted(&triangle.view(), 1.0).expect("factors");
        let projection =
            ChainLadderEngine::project(&triangle.view(), &factors).expect("projection");
        assert!((result.base_total_reserve - projection.total_reserve).abs() < 1e-9);
    }

    #[test]
    fn odp_fit_leverages_sum_to_rank_and_saturate_corners() {
        let triangle = triangle_4x4();
        let fit = fit_odp(&triangle.view(), 1.0, MonotonicityPolicy::Strict).expect("fit");

        assert_eq!(fit.n_observed, 10);
        assert_eq!(fit.rank, 7);
        assert_eq!(fit.degrees_of_freedom, 3);
        let h_sum: f64 = fit.residuals.iter().map(|r| r.leverage).sum();
        assert!((h_sum - 7.0).abs() < 1e-10, "sum(h_ii)={h_sum}");
        let saturated_corners: Vec<(usize, usize)> = fit
            .residuals
            .iter()
            .filter(|r| r.saturated)
            .map(|r| (r.origin, r.dev))
            .collect();
        assert_eq!(saturated_corners, vec![(0, 3), (3, 0)]);
    }

    #[test]
    fn odp_fit_tridiagonal_matches_general_solver_to_machine_precision() {
        let tri = genins();
        let fit = fit_odp(&tri.view(), 1.05, MonotonicityPolicy::Strict).expect("fast fit");
        let general = solve_odp_general(
            fit.n_origins,
            fit.tail,
            &fit.fitted_cumulative,
            &fit.fitted_incremental,
            &fit.observed_incremental,
        )
        .expect("general fit");

        assert_eq!(fit.residuals.len(), general.residuals.len());
        for (a, b) in fit.residuals.iter().zip(general.residuals.iter()) {
            assert_eq!((a.origin, a.dev), (b.origin, b.dev));
            assert!((a.leverage - b.leverage).abs() < 1e-12);
            assert!((a.raw_pearson_residual - b.raw_pearson_residual).abs() < 1e-12);
        }
        for i in 0..fit.n_origins {
            let tol = 1e-10 * (1.0 + general.analytic_origin_prediction_error[i]);
            assert!(
                (fit.analytic_origin_param_se[i] - general.analytic_origin_param_se[i]).abs() < tol
            );
            assert!(
                (fit.analytic_origin_prediction_error[i]
                    - general.analytic_origin_prediction_error[i])
                    .abs()
                    < tol
            );
        }
        assert!(
            (fit.analytic_total_param_se - general.analytic_total_param_se).abs()
                < 1e-10 * general.analytic_total_param_se
        );
    }

    #[test]
    fn odp_fit_handles_flat_ldf_via_general_fallback() {
        // Last development link has LDF == 1.0 (zero incremental fitted mass in col 3).
        #[rustfmt::skip]
        let tri = Triangle::new(4, 4, vec![
            1000.0, 1500.0, 1700.0, 1700.0,
            1200.0, 1800.0, 2000.0, f64::NAN,
            1500.0, 2100.0, f64::NAN, f64::NAN,
            1800.0, f64::NAN, f64::NAN, f64::NAN,
        ]);
        let fit = fit_odp(&tri.view(), 1.0, MonotonicityPolicy::Strict).expect("fallback fit");
        assert_eq!(fit.n_observed, 9);
        assert_eq!(fit.rank, 6);
        assert_eq!(fit.degrees_of_freedom, 3);
        let h_sum: f64 = fit.residuals.iter().map(|r| r.leverage).sum();
        assert!((h_sum - 6.0).abs() < 1e-10, "sum(h_ii)={h_sum}");
    }

    #[test]
    fn ziggurat_normal_and_fast_index_samplers_match_theoretical_distributions() {
        let mut rng = DeterministicRng::from_seed(1234567);
        let n = 200_000usize;
        let mut sum = 0.0;
        let mut s2 = 0.0;
        let mut s3 = 0.0;
        let mut s4 = 0.0;
        let mut tail_hits = 0usize;
        for _ in 0..n {
            let z = rng.next_standard_normal();
            sum += z;
            let z2 = z * z;
            s2 += z2;
            s3 += z2 * z;
            s4 += z2 * z2;
            if z.abs() >= ZIG_NORM_R {
                tail_hits += 1;
            }
        }
        let nf = n as f64;
        let mean = sum / nf;
        let var = s2 / nf - mean * mean;
        let skew = s3 / nf;
        let kurt = s4 / nf;
        assert!(mean.abs() < 0.01, "mean={mean}");
        assert!((var - 1.0).abs() < 0.015, "var={var}");
        assert!(skew.abs() < 0.03, "skew={skew}");
        assert!((kurt - 3.0).abs() < 0.08, "kurt={kurt}");
        // P(|Z| >= 3.654153) ≈ 2.58e-4 => ~52 hits in 200k.
        assert!((20..=100).contains(&tail_hits), "tail_hits={tail_hits}");

        // FastIndexSampler uniformity on bound = 53 (16-bit Lemire) and 1500 (32-bit Lemire).
        let mut counts53 = [0usize; 53];
        let mut s53 = FastIndexSampler::new(53);
        for _ in 0..53_000 {
            counts53[s53.next(&mut rng)] += 1;
        }
        let chi2: f64 = counts53
            .iter()
            .map(|&c| {
                let d = c as f64 - 1000.0;
                d * d / 1000.0
            })
            .sum();
        assert!(chi2 < 100.0, "chi2(df=52)={chi2}");

        let mut s1500 = FastIndexSampler::new(1500);
        for _ in 0..5_000 {
            assert!(s1500.next(&mut rng) < 1500);
        }
    }

    #[test]
    fn gamma_and_poisson_samplers_match_theoretical_moments() {
        let mut rng = DeterministicRng::from_seed(42069);
        let n = 40_000usize;

        for &(shape, scale) in &[(0.4, 2.5), (3.5, 10.0), (25.0, 50.0)] {
            let samples: Vec<f64> = (0..n).map(|_| rng.sample_gamma(shape, scale)).collect();
            let mean = samples.iter().sum::<f64>() / n as f64;
            let var = samples.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (n - 1) as f64;
            let expected_mean = shape * scale;
            let expected_var = shape * scale * scale;
            assert!(
                (mean - expected_mean).abs() / expected_mean < 0.03,
                "gamma({shape},{scale}) mean={mean} expected={expected_mean}"
            );
            assert!(
                (var - expected_var).abs() / expected_var < 0.08,
                "gamma({shape},{scale}) var={var} expected={expected_var}"
            );
        }

        for &lambda in &[0.7, 9.5, 10.0, 12.0, 20.0, 45.0, 300.0] {
            let samples: Vec<f64> = (0..n).map(|_| rng.sample_poisson(lambda)).collect();
            let mean = samples.iter().sum::<f64>() / n as f64;
            let var = samples.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (n - 1) as f64;
            assert!(
                (mean - lambda).abs() / lambda < 0.03,
                "poisson({lambda}) mean={mean}"
            );
            assert!(
                (var - lambda).abs() / lambda < 0.06,
                "poisson({lambda}) var={var}"
            );
        }
    }
}