fdars-core 0.13.0

Functional Data Analysis algorithms in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
//! Comprehensive tests for the SPM module.

#[cfg(test)]
mod spm_tests {
    use crate::matrix::FdMatrix;
    use crate::spm::amewma::{spm_amewma_monitor, AmewmaConfig};
    use crate::spm::chi_squared::{chi2_cdf, chi2_quantile};
    use crate::spm::contrib::{spe_contributions, t2_contributions, t2_contributions_mfpca};
    use crate::spm::control::spe_moment_match_diagnostic;
    use crate::spm::control::{spe_control_limit, t2_control_limit};
    use crate::spm::cusum::{spm_cusum_monitor, spm_cusum_monitor_with_restart, CusumConfig};
    use crate::spm::ewma::{ewma_scores, spm_ewma_monitor, EwmaConfig};
    use crate::spm::frcc::{frcc_monitor, frcc_phase1, FrccConfig};
    use crate::spm::iterative::{spm_phase1_iterative, IterativePhase1Config};
    use crate::spm::mfpca::{mfpca, MfpcaConfig};
    use crate::spm::phase::{mf_spm_monitor, mf_spm_phase1, spm_monitor, spm_phase1, SpmConfig};
    use crate::spm::stats::{hotelling_t2, spe_multivariate, spe_univariate};
    use std::f64::consts::PI;

    // ── Helpers ──────────────────────────────────────────────────────────────

    fn uniform_grid(n: usize) -> Vec<f64> {
        (0..n).map(|i| i as f64 / (n - 1) as f64).collect()
    }

    /// Generate in-control functional data: sine waves with random phase.
    fn generate_ic_data(n: usize, m: usize, seed: u64) -> FdMatrix {
        let t = uniform_grid(m);
        let mut data = FdMatrix::zeros(n, m);
        for i in 0..n {
            // Deterministic pseudo-random phase
            let phase = ((seed.wrapping_add(i as u64).wrapping_mul(2654435761)) as f64)
                / (u32::MAX as f64)
                * 0.5;
            let amplitude = 1.0
                + ((seed.wrapping_add(i as u64 * 7).wrapping_mul(1103515245)) as f64)
                    / (u32::MAX as f64)
                    * 0.2;
            for j in 0..m {
                data[(i, j)] = amplitude * (2.0 * PI * t[j] + phase).sin()
                    + 0.05
                        * ((seed
                            .wrapping_add(i as u64 * 13 + j as u64 * 7)
                            .wrapping_mul(48271)) as f64)
                        / (u32::MAX as f64);
            }
        }
        data
    }

    /// Generate out-of-control data (shifted mean).
    fn generate_oc_data(n: usize, m: usize, shift: f64, seed: u64) -> FdMatrix {
        let mut data = generate_ic_data(n, m, seed + 999);
        for i in 0..n {
            for j in 0..m {
                data[(i, j)] += shift;
            }
        }
        data
    }

    // ── chi_squared tests ────────────────────────────────────────────────────

    #[test]
    fn test_chi2_cdf_at_zero() {
        assert_eq!(chi2_cdf(0.0, 1), 0.0);
        assert_eq!(chi2_cdf(0.0, 5), 0.0);
    }

    #[test]
    fn test_chi2_quantile_median_df2() {
        let q = chi2_quantile(0.5, 2);
        assert!(
            (q - 1.3862943611198906).abs() < 0.02,
            "chi2_quantile(0.5, 2) = {q}, expected ~1.386"
        );
    }

    #[test]
    fn test_chi2_quantile_95th_df2() {
        let q = chi2_quantile(0.95, 2);
        assert!(
            (q - 5.991).abs() < 0.02,
            "chi2_quantile(0.95, 2) = {q}, expected ~5.991"
        );
    }

    #[test]
    fn test_chi2_roundtrip() {
        for k in &[1usize, 2, 5, 10] {
            for &x in &[1.0, 3.0, 7.0, 15.0] {
                let p = chi2_cdf(x, *k);
                if p > 0.01 && p < 0.99 {
                    let x_back = chi2_quantile(p, *k);
                    assert!(
                        (x_back - x).abs() < 0.1,
                        "Round-trip failed: k={k}, x={x}, p={p}, x_back={x_back}"
                    );
                }
            }
        }
    }

    // ── T-squared tests ──────────────────────────────────────────────────────

    #[test]
    fn test_t2_non_negative() {
        let scores =
            FdMatrix::from_column_major(vec![1.0, -2.0, 0.5, 3.0, 1.0, -1.0], 3, 2).unwrap();
        let eigenvalues = vec![2.0, 0.5];
        let t2 = hotelling_t2(&scores, &eigenvalues).unwrap();
        for &v in &t2 {
            assert!(v >= 0.0, "T2 must be non-negative, got {v}");
        }
    }

    #[test]
    fn test_t2_zero_scores() {
        let scores = FdMatrix::zeros(5, 3);
        let eigenvalues = vec![1.0, 1.0, 1.0];
        let t2 = hotelling_t2(&scores, &eigenvalues).unwrap();
        for &v in &t2 {
            assert!(v.abs() < 1e-15, "T2 of zero scores should be 0, got {v}");
        }
    }

    #[test]
    fn test_t2_dimension_mismatch() {
        let scores = FdMatrix::zeros(5, 3);
        let eigenvalues = vec![1.0, 1.0]; // wrong length
        assert!(hotelling_t2(&scores, &eigenvalues).is_err());
    }

    #[test]
    fn test_t2_negative_eigenvalue() {
        let scores = FdMatrix::zeros(5, 2);
        let eigenvalues = vec![1.0, -0.5];
        assert!(hotelling_t2(&scores, &eigenvalues).is_err());
    }

    // ── SPE tests ────────────────────────────────────────────────────────────

    #[test]
    fn test_spe_non_negative() {
        let m = 20;
        let argvals = uniform_grid(m);
        let centered = generate_ic_data(10, m, 42);
        let reconstructed = FdMatrix::zeros(10, m);
        let spe = spe_univariate(&centered, &reconstructed, &argvals).unwrap();
        for &v in &spe {
            assert!(v >= 0.0, "SPE must be non-negative, got {v}");
        }
    }

    #[test]
    fn test_spe_zero_error() {
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(5, m, 42);
        let spe = spe_univariate(&data, &data, &argvals).unwrap();
        for &v in &spe {
            assert!(
                v.abs() < 1e-10,
                "SPE should be 0 when centered==reconstructed, got {v}"
            );
        }
    }

    #[test]
    fn test_spe_dimension_mismatch() {
        let argvals = uniform_grid(20);
        let centered = FdMatrix::zeros(5, 20);
        let reconstructed = FdMatrix::zeros(5, 15); // wrong size
        assert!(spe_univariate(&centered, &reconstructed, &argvals).is_err());
    }

    #[test]
    fn test_spe_multivariate_matches_sum() {
        let m1 = 15;
        let m2 = 20;
        let argvals1 = uniform_grid(m1);
        let argvals2 = uniform_grid(m2);
        let n = 5;

        let var1_std = generate_ic_data(n, m1, 42);
        let var1_rec = FdMatrix::zeros(n, m1);
        let var2_std = generate_ic_data(n, m2, 99);
        let var2_rec = FdMatrix::zeros(n, m2);

        let spe_total = spe_multivariate(
            &[&var1_std, &var2_std],
            &[&var1_rec, &var2_rec],
            &[&argvals1, &argvals2],
        )
        .unwrap();

        let spe1 = spe_univariate(&var1_std, &var1_rec, &argvals1).unwrap();
        let spe2 = spe_univariate(&var2_std, &var2_rec, &argvals2).unwrap();

        for i in 0..n {
            let expected = spe1[i] + spe2[i];
            assert!(
                (spe_total[i] - expected).abs() < 1e-10,
                "Multivariate SPE should equal sum of univariate, obs {i}: {} vs {}",
                spe_total[i],
                expected
            );
        }
    }

    // ── Control limit tests ──────────────────────────────────────────────────

    #[test]
    fn test_t2_control_limit_chi2() {
        let limit = t2_control_limit(2, 0.05).unwrap();
        assert!(
            (limit.ucl - 5.991).abs() < 0.05,
            "T2 UCL for ncomp=2, alpha=0.05 should be ~5.991, got {}",
            limit.ucl
        );
    }

    #[test]
    fn test_t2_control_limit_invalid() {
        assert!(t2_control_limit(0, 0.05).is_err());
        assert!(t2_control_limit(5, 0.0).is_err());
        assert!(t2_control_limit(5, 1.0).is_err());
    }

    #[test]
    fn test_spe_control_limit_moment_matching() {
        // Known SPE values with mean ~2, var ~1
        let spe_values: Vec<f64> = (0..100)
            .map(|i| 2.0 + 0.5 * ((i as f64 * 0.1).sin()))
            .collect();
        let limit = spe_control_limit(&spe_values, 0.05).unwrap();
        assert!(limit.ucl > 0.0, "SPE UCL must be positive");
        // UCL should be above most values
        let n_above = spe_values.iter().filter(|&&v| v > limit.ucl).count();
        // With alpha=0.05, we expect roughly 5% or fewer above
        assert!(
            n_above <= 15,
            "Too many values ({n_above}/100) above SPE UCL"
        );
    }

    #[test]
    fn test_spe_control_limit_too_few_values() {
        assert!(spe_control_limit(&[1.0], 0.05).is_err());
    }

    // ── Phase I/II tests ─────────────────────────────────────────────────────

    #[test]
    fn test_phase1_basic() {
        let n = 40;
        let m = 30;
        let data = generate_ic_data(n, m, 42);
        let argvals = uniform_grid(m);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            tuning_fraction: 0.5,
            seed: 42,
        };

        let chart = spm_phase1(&data, &argvals, &config).unwrap();
        assert_eq!(chart.t2_phase1.len(), chart.spe_phase1.len()); // same length
        assert!(chart.t2_limit.ucl > 0.0);
        assert!(chart.spe_limit.ucl > 0.0);

        // T2 should be non-negative
        for &v in &chart.t2_phase1 {
            assert!(v >= 0.0);
        }
    }

    #[test]
    fn test_phase1_ic_few_alarms() {
        let n = 60;
        let m = 30;
        let data = generate_ic_data(n, m, 42);
        let argvals = uniform_grid(m);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            tuning_fraction: 0.5,
            seed: 42,
        };

        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        // Monitor the same type of data
        let new_data = generate_ic_data(20, m, 123);
        let result = spm_monitor(&chart, &new_data, &argvals).unwrap();

        // In-control data should have few alarms (allow some due to random variation)
        let t2_alarm_count = result.t2_alarm.iter().filter(|&&a| a).count();
        let spe_alarm_count = result.spe_alarm.iter().filter(|&&a| a).count();
        // With 20 observations at alpha=0.05, expect ~1 alarm
        // Allow up to 10 for robustness
        assert!(
            t2_alarm_count <= 15,
            "Too many T2 alarms for IC data: {t2_alarm_count}/20"
        );
        assert!(
            spe_alarm_count <= 15,
            "Too many SPE alarms for IC data: {spe_alarm_count}/20"
        );
    }

    #[test]
    fn test_phase2_oc_many_alarms() {
        let n = 60;
        let m = 30;
        let data = generate_ic_data(n, m, 42);
        let argvals = uniform_grid(m);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            tuning_fraction: 0.5,
            seed: 42,
        };

        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        // Monitor shifted data (large shift)
        let oc_data = generate_oc_data(20, m, 5.0, 456);
        let result = spm_monitor(&chart, &oc_data, &argvals).unwrap();

        // Out-of-control data should trigger many alarms
        let t2_alarm_count = result.t2_alarm.iter().filter(|&&a| a).count();
        let spe_alarm_count = result.spe_alarm.iter().filter(|&&a| a).count();
        let total_alarms = t2_alarm_count + spe_alarm_count;
        assert!(
            total_alarms >= 5,
            "Expected many alarms for shifted data, got T2={t2_alarm_count}, SPE={spe_alarm_count}"
        );
    }

    #[test]
    fn test_phase1_too_few_observations() {
        let data = generate_ic_data(3, 20, 42);
        let argvals = uniform_grid(20);
        let config = SpmConfig::default();
        assert!(spm_phase1(&data, &argvals, &config).is_err());
    }

    // ── EWMA tests ───────────────────────────────────────────────────────────

    #[test]
    fn test_ewma_lambda_one_equals_raw() {
        let scores = FdMatrix::from_column_major(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2).unwrap();

        let smoothed = ewma_scores(&scores, 1.0).unwrap();

        for i in 0..3 {
            for k in 0..2 {
                assert!(
                    (smoothed[(i, k)] - scores[(i, k)]).abs() < 1e-12,
                    "EWMA with lambda=1 should equal raw scores at ({i},{k})"
                );
            }
        }
    }

    #[test]
    fn test_ewma_lambda_small_nearly_constant() {
        let n = 10;
        let scores =
            FdMatrix::from_column_major((0..n * 2).map(|i| i as f64).collect(), n, 2).unwrap();

        let smoothed = ewma_scores(&scores, 0.01).unwrap();

        // With very small lambda, smoothed scores should be much smaller than raw
        // (they build up very slowly from 0)
        for k in 0..2 {
            assert!(
                smoothed[(n - 1, k)].abs() < scores[(n - 1, k)].abs(),
                "EWMA with small lambda should dampen scores"
            );
        }
    }

    #[test]
    fn test_ewma_invalid_lambda() {
        let scores = FdMatrix::zeros(5, 2);
        assert!(ewma_scores(&scores, 0.0).is_err());
        assert!(ewma_scores(&scores, 1.5).is_err());
        assert!(ewma_scores(&scores, -0.1).is_err());
    }

    #[test]
    fn test_ewma_monitor_basic() {
        let n = 60;
        let m = 30;
        let data = generate_ic_data(n, m, 42);
        let argvals = uniform_grid(m);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            tuning_fraction: 0.5,
            seed: 42,
        };

        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let ewma_config = EwmaConfig {
            lambda: 0.2,
            ncomp: 3,
            alpha: 0.05,
            ..EwmaConfig::default()
        };

        let new_data = generate_ic_data(15, m, 789);
        let result = spm_ewma_monitor(&chart, &new_data, &argvals, &ewma_config).unwrap();

        assert_eq!(result.t2.len(), 15);
        assert_eq!(result.spe.len(), 15);
        assert_eq!(result.smoothed_scores.shape(), (15, 3));
        assert!(result.t2_limit > 0.0);
    }

    // ── MFPCA tests ──────────────────────────────────────────────────────────

    #[test]
    fn test_mfpca_basic() {
        let n = 30;
        let m1 = 20;
        let m2 = 15;
        let var1 = generate_ic_data(n, m1, 42);
        let var2 = generate_ic_data(n, m2, 99);

        let config = MfpcaConfig {
            ncomp: 3,
            weighted: true,
        };

        let result = mfpca(&[&var1, &var2], &config).unwrap();
        assert_eq!(result.scores.shape(), (n, 3));
        assert_eq!(result.eigenfunctions.len(), 2);
        assert_eq!(result.eigenfunctions[0].nrows(), m1);
        assert_eq!(result.eigenfunctions[1].nrows(), m2);
        assert_eq!(result.eigenvalues.len(), 3);
        assert_eq!(result.means.len(), 2);
        assert_eq!(result.scales.len(), 2);
    }

    #[test]
    fn test_mfpca_project_reconstruct_roundtrip() {
        let n = 20;
        let m1 = 15;
        let m2 = 10;
        let var1 = generate_ic_data(n, m1, 42);
        let var2 = generate_ic_data(n, m2, 99);

        let ncomp = n.min(m1 + m2);
        let config = MfpcaConfig {
            ncomp,
            weighted: false,
        };

        let result = mfpca(&[&var1, &var2], &config).unwrap();
        let actual_ncomp = result.eigenvalues.len();

        // Project original data
        let scores = result.project(&[&var1, &var2]).unwrap();

        // Reconstruct
        let recon = result.reconstruct(&scores, actual_ncomp).unwrap();

        // Check approximate reconstruction
        for i in 0..n {
            for j in 0..m1 {
                assert!(
                    (recon[0][(i, j)] - var1[(i, j)]).abs() < 1.0,
                    "Reconstruction error too large for var1 at ({i},{j}): {} vs {}",
                    recon[0][(i, j)],
                    var1[(i, j)]
                );
            }
        }
    }

    #[test]
    fn test_mfpca_empty_variables() {
        let config = MfpcaConfig::default();
        assert!(mfpca(&[], &config).is_err());
    }

    #[test]
    fn test_mfpca_inconsistent_rows() {
        let var1 = FdMatrix::zeros(10, 5);
        let var2 = FdMatrix::zeros(8, 5); // different n
        let config = MfpcaConfig::default();
        assert!(mfpca(&[&var1, &var2], &config).is_err());
    }

    // ── Multivariate Phase I/II tests ────────────────────────────────────────

    #[test]
    fn test_mf_phase1_basic() {
        let n = 40;
        let m1 = 20;
        let m2 = 15;
        let var1 = generate_ic_data(n, m1, 42);
        let var2 = generate_ic_data(n, m2, 99);
        let argvals1 = uniform_grid(m1);
        let argvals2 = uniform_grid(m2);

        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            tuning_fraction: 0.5,
            seed: 42,
        };

        let chart =
            mf_spm_phase1(&[&var1, &var2], &[&argvals1[..], &argvals2[..]], &config).unwrap();

        assert!(chart.t2_limit.ucl > 0.0);
        assert!(chart.spe_limit.ucl > 0.0);

        // Monitor new data
        let new_var1 = generate_ic_data(10, m1, 200);
        let new_var2 = generate_ic_data(10, m2, 300);
        let result = mf_spm_monitor(
            &chart,
            &[&new_var1, &new_var2],
            &[&argvals1[..], &argvals2[..]],
        )
        .unwrap();
        assert_eq!(result.t2.len(), 10);
        assert_eq!(result.spe.len(), 10);
    }

    // ── FRCC tests ───────────────────────────────────────────────────────────

    #[test]
    fn test_frcc_basic() {
        let n = 40;
        let m = 30;
        let p = 2;
        let argvals = uniform_grid(m);

        // Generate predictors
        let mut predictors = FdMatrix::zeros(n, p);
        for i in 0..n {
            predictors[(i, 0)] = i as f64 / n as f64;
            predictors[(i, 1)] = if i % 2 == 0 { 1.0 } else { 0.0 };
        }

        // Generate functional response dependent on predictors
        let mut y_curves = FdMatrix::zeros(n, m);
        for i in 0..n {
            for j in 0..m {
                let t = argvals[j];
                let mu = (2.0 * PI * t).sin();
                let beta1 = t * predictors[(i, 0)];
                let beta2 = 0.5 * (4.0 * PI * t).cos() * predictors[(i, 1)];
                y_curves[(i, j)] =
                    mu + beta1 + beta2 + 0.1 * ((i * 13 + j * 7) % 100) as f64 / 100.0;
            }
        }

        let config = FrccConfig {
            ncomp: 3,
            fosr_lambda: 1e-4,
            alpha: 0.05,
            tuning_fraction: 0.5,
            seed: 42,
            ..FrccConfig::default()
        };

        let chart = frcc_phase1(&y_curves, &predictors, &argvals, &config).unwrap();
        assert!(chart.t2_limit.ucl > 0.0);
        assert!(chart.spe_limit.ucl > 0.0);

        // Monitor new data
        let n_new = 10;
        let mut new_x = FdMatrix::zeros(n_new, p);
        let mut new_y = FdMatrix::zeros(n_new, m);
        for i in 0..n_new {
            new_x[(i, 0)] = (i as f64 + 0.5) / n_new as f64;
            new_x[(i, 1)] = if i % 2 == 0 { 1.0 } else { 0.0 };
            for j in 0..m {
                let t = argvals[j];
                let mu = (2.0 * PI * t).sin();
                let beta1 = t * new_x[(i, 0)];
                let beta2 = 0.5 * (4.0 * PI * t).cos() * new_x[(i, 1)];
                new_y[(i, j)] = mu + beta1 + beta2 + 0.1 * ((i * 17 + j * 3) % 100) as f64 / 100.0;
            }
        }

        let result = frcc_monitor(&chart, &new_y, &new_x, &argvals).unwrap();
        assert_eq!(result.t2.len(), n_new);
        assert_eq!(result.spe.len(), n_new);
    }

    #[test]
    fn test_frcc_too_few_observations() {
        let y = FdMatrix::zeros(4, 20);
        let x = FdMatrix::zeros(4, 2);
        let argvals = uniform_grid(20);
        let config = FrccConfig::default();
        assert!(frcc_phase1(&y, &x, &argvals, &config).is_err());
    }

    #[test]
    fn test_frcc_negative_fosr_lambda_rejected() {
        let y = generate_ic_data(40, 20, 42);
        let x = FdMatrix::from_column_major((0..80).map(|i| (i as f64) * 0.1).collect(), 40, 2)
            .unwrap();
        let argvals = uniform_grid(20);
        let config = FrccConfig {
            fosr_lambda: -1.0,
            ..FrccConfig::default()
        };
        assert!(frcc_phase1(&y, &x, &argvals, &config).is_err());
    }

    // ── Contribution tests ───────────────────────────────────────────────────

    #[test]
    fn test_t2_contributions_sum_approx_total() {
        let n = 5;
        let ncomp = 3;
        let scores = FdMatrix::from_column_major(
            vec![
                1.0, 2.0, 3.0, -1.0, 0.5, 0.5, -1.0, 2.0, 1.0, -0.5, -0.3, 0.7, 1.0, -2.0, 0.1,
            ],
            n,
            ncomp,
        )
        .unwrap();
        let eigenvalues = vec![2.0, 1.0, 0.5];
        let grid_sizes = vec![10, 15]; // 2 variables

        let t2_total = hotelling_t2(&scores, &eigenvalues).unwrap();
        let contrib = t2_contributions(&scores, &eigenvalues, &grid_sizes).unwrap();

        assert_eq!(contrib.shape(), (n, 2));

        // Sum of contributions should approximate total T2
        for i in 0..n {
            let sum: f64 = (0..2).map(|v| contrib[(i, v)]).sum();
            assert!(
                (sum - t2_total[i]).abs() < 1e-8,
                "T2 contribution sum ({sum}) should equal total ({}) for obs {i}",
                t2_total[i]
            );
        }
    }

    #[test]
    fn test_spe_contributions_sum_equals_total() {
        let n = 5;
        let m1 = 10;
        let m2 = 8;
        let argvals1 = uniform_grid(m1);
        let argvals2 = uniform_grid(m2);

        let var1_std = generate_ic_data(n, m1, 42);
        let var1_rec = FdMatrix::zeros(n, m1);
        let var2_std = generate_ic_data(n, m2, 99);
        let var2_rec = FdMatrix::zeros(n, m2);

        let contrib = spe_contributions(
            &[&var1_std, &var2_std],
            &[&var1_rec, &var2_rec],
            &[&argvals1, &argvals2],
        )
        .unwrap();

        let spe_total = spe_multivariate(
            &[&var1_std, &var2_std],
            &[&var1_rec, &var2_rec],
            &[&argvals1, &argvals2],
        )
        .unwrap();

        assert_eq!(contrib.shape(), (n, 2));

        for i in 0..n {
            let sum: f64 = (0..2).map(|v| contrib[(i, v)]).sum();
            assert!(
                (sum - spe_total[i]).abs() < 1e-10,
                "SPE contribution sum ({sum}) should equal total ({}) for obs {i}",
                spe_total[i]
            );
        }
    }

    #[test]
    fn test_t2_contributions_dimension_mismatch() {
        let scores = FdMatrix::zeros(5, 3);
        let eigenvalues = vec![1.0, 1.0]; // wrong length
        let grid_sizes = vec![10, 10];
        assert!(t2_contributions(&scores, &eigenvalues, &grid_sizes).is_err());
    }

    // ── Input validation tests ───────────────────────────────────────────────

    #[test]
    fn test_monitor_dimension_mismatch() {
        let n = 40;
        let m = 30;
        let data = generate_ic_data(n, m, 42);
        let argvals = uniform_grid(m);
        let config = SpmConfig {
            ncomp: 3,
            ..SpmConfig::default()
        };

        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let wrong_m = FdMatrix::zeros(5, 20); // wrong number of columns
        assert!(spm_monitor(&chart, &wrong_m, &argvals).is_err());
    }

    #[test]
    fn test_mfpca_project_wrong_variables() {
        let var1 = generate_ic_data(20, 10, 42);
        let var2 = generate_ic_data(20, 8, 99);
        let config = MfpcaConfig {
            ncomp: 3,
            weighted: true,
        };
        let result = mfpca(&[&var1, &var2], &config).unwrap();

        // Wrong number of variables
        let new_var1 = generate_ic_data(5, 10, 200);
        assert!(result.project(&[&new_var1]).is_err());
    }

    #[test]
    fn test_mfpca_project_wrong_grid_size() {
        let var1 = generate_ic_data(20, 10, 42);
        let var2 = generate_ic_data(20, 8, 99);
        let config = MfpcaConfig {
            ncomp: 3,
            weighted: true,
        };
        let result = mfpca(&[&var1, &var2], &config).unwrap();

        // Wrong grid size for variable 2
        let new_var1 = generate_ic_data(5, 10, 200);
        let new_var2 = generate_ic_data(5, 12, 300); // should be 8
        assert!(result.project(&[&new_var1, &new_var2]).is_err());
    }

    // ── Gap 1: SPE control limit formula verification ───────────────────────

    #[test]
    fn test_spe_control_limit_formula_verification() {
        let spe_values = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let alpha = 0.05;
        let n = spe_values.len();

        // Manually compute mean and variance
        let mean: f64 = spe_values.iter().sum::<f64>() / n as f64;
        let var: f64 =
            spe_values.iter().map(|&s| (s - mean).powi(2)).sum::<f64>() / (n as f64 - 1.0);

        // Moment-matching parameters
        let a = var / (2.0 * mean);
        let b = 2.0 * mean * mean / var;
        let b_int = b.round().max(1.0) as usize;

        // Expected UCL
        let expected_ucl = a * chi2_quantile(1.0 - alpha, b_int);

        let limit = spe_control_limit(&spe_values, alpha).unwrap();

        assert!(
            (limit.ucl - expected_ucl).abs() < 1e-10,
            "SPE UCL should match moment-matching formula: got {}, expected {}",
            limit.ucl,
            expected_ucl
        );

        // Verify intermediate values: mean = 5.5, var = 9.1667
        assert!((mean - 5.5).abs() < 1e-10, "Mean should be 5.5, got {mean}");
        assert!(
            (var - 9.166666666666666).abs() < 1e-6,
            "Variance should be ~9.1667, got {var}"
        );
    }

    // ── Gap 2: EWMA asymptotic behavior ─────────────────────────────────────

    #[test]
    fn test_ewma_asymptotic_behavior() {
        // Create a long sequence (100 obs, 3 components) with known mean and variance
        let n = 100;
        let ncomp = 3;
        let mut raw_data = Vec::with_capacity(n * ncomp);
        for i in 0..n {
            for k in 0..ncomp {
                // Deterministic pseudo-random values centered around 0
                let val = ((i * 13 + k * 7 + 1) as f64).sin() * (k as f64 + 1.0);
                raw_data.push(val);
            }
        }
        let scores = FdMatrix::from_column_major(raw_data, n, ncomp).unwrap();
        let smoothed = ewma_scores(&scores, 0.2).unwrap();

        // Verify smoothed scores at the end are closer to the column mean than raw scores
        for k in 0..ncomp {
            // EWMA smoothed scores should generally be less extreme than raw
            // (closer to the running average). For a long sequence, the last
            // smoothed value should be within a reasonable range.
            assert!(
                smoothed[(n - 1, k)].abs()
                    < scores
                        .column(k)
                        .iter()
                        .map(|v| v.abs())
                        .fold(0.0_f64, f64::max)
                        + 0.1,
                "Smoothed score at end should not exceed max absolute raw score for component {k}"
            );
        }

        // Verify EWMA variance ratio: var(smoothed) < var(raw) for each component
        for k in 0..ncomp {
            let raw_mean: f64 = (0..n).map(|i| scores[(i, k)]).sum::<f64>() / n as f64;
            let raw_var: f64 = (0..n)
                .map(|i| (scores[(i, k)] - raw_mean).powi(2))
                .sum::<f64>()
                / (n as f64 - 1.0);

            let sm_mean: f64 = (0..n).map(|i| smoothed[(i, k)]).sum::<f64>() / n as f64;
            let sm_var: f64 = (0..n)
                .map(|i| (smoothed[(i, k)] - sm_mean).powi(2))
                .sum::<f64>()
                / (n as f64 - 1.0);

            assert!(
                sm_var < raw_var,
                "EWMA smoothed variance ({sm_var}) should be less than raw variance ({raw_var}) for component {k}"
            );
        }
    }

    // ── Gap 3: MFPCA with tighter tolerance ─────────────────────────────────

    #[test]
    fn test_mfpca_tight_reconstruction_and_decreasing_eigenvalues() {
        let n = 40;
        let m1 = 30;
        let m2 = 25;
        let t1 = uniform_grid(m1);
        let t2_grid = uniform_grid(m2);

        // Variable 1: sin waves with varying amplitude (structured, low-rank)
        let mut var1 = FdMatrix::zeros(n, m1);
        for i in 0..n {
            let phase = i as f64 / n as f64 * PI;
            let amp = 1.0 + 0.5 * (i as f64 / n as f64);
            for j in 0..m1 {
                var1[(i, j)] = amp * (2.0 * PI * t1[j] + phase).sin();
            }
        }

        // Variable 2: polynomial curves
        let mut var2 = FdMatrix::zeros(n, m2);
        for i in 0..n {
            let a = i as f64 / n as f64;
            let b = 0.5 - i as f64 / (2.0 * n as f64);
            for j in 0..m2 {
                let t = t2_grid[j];
                var2[(i, j)] = a * t + b * t * t;
            }
        }

        // Use enough components to capture most variance
        let config = MfpcaConfig {
            ncomp: 10,
            weighted: true,
        };

        let result = mfpca(&[&var1, &var2], &config).unwrap();
        let actual_ncomp = result.eigenvalues.len();

        // Project and reconstruct
        let scores = result.project(&[&var1, &var2]).unwrap();
        let recon = result.reconstruct(&scores, actual_ncomp).unwrap();

        // Verify reconstruction error is < 0.1 for each element
        let mut max_error_v1 = 0.0_f64;
        for i in 0..n {
            for j in 0..m1 {
                let err = (recon[0][(i, j)] - var1[(i, j)]).abs();
                max_error_v1 = max_error_v1.max(err);
            }
        }
        assert!(
            max_error_v1 < 0.1,
            "Variable 1 max reconstruction error = {max_error_v1}, expected < 0.1"
        );

        let mut max_error_v2 = 0.0_f64;
        for i in 0..n {
            for j in 0..m2 {
                let err = (recon[1][(i, j)] - var2[(i, j)]).abs();
                max_error_v2 = max_error_v2.max(err);
            }
        }
        assert!(
            max_error_v2 < 0.1,
            "Variable 2 max reconstruction error = {max_error_v2}, expected < 0.1"
        );

        // Verify eigenvalues are decreasing (non-increasing)
        for w in result.eigenvalues.windows(2) {
            assert!(
                w[0] >= w[1] - 1e-12,
                "Eigenvalues should be decreasing: {} followed by {}",
                w[0],
                w[1]
            );
        }
    }

    // ── Gap 4: Phase I alarm rate tightening ────────────────────────────────

    #[test]
    fn test_phase1_alarm_rate_tighter() {
        let n = 100;
        let m = 30;
        let data = generate_ic_data(n, m, 42);
        let argvals = uniform_grid(m);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            tuning_fraction: 0.5,
            seed: 42,
        };

        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        // Calibration set has ~50 observations
        let n_cal = chart.t2_phase1.len();

        // At alpha=0.05, expect at most ~5% alarms. Allow 15% tolerance
        // to account for finite-sample calibration effects.
        let max_allowed = (n_cal as f64 * 0.15).ceil() as usize;

        let t2_alarms = chart
            .t2_phase1
            .iter()
            .filter(|&&v| v > chart.t2_limit.ucl)
            .count();
        let spe_alarms = chart
            .spe_phase1
            .iter()
            .filter(|&&v| v > chart.spe_limit.ucl)
            .count();

        assert!(
            t2_alarms <= max_allowed,
            "T2 alarm count ({t2_alarms}/{n_cal}) exceeds 10% tolerance ({max_allowed})"
        );
        assert!(
            spe_alarms <= max_allowed,
            "SPE alarm count ({spe_alarms}/{n_cal}) exceeds 10% tolerance ({max_allowed})"
        );
    }

    // ── Gap 5: MFPCA constant/degenerate data ──────────────────────────────

    #[test]
    fn test_mfpca_constant_data_zero_eigenvalues() {
        let n = 10;
        let m = 15;

        // All curves identical
        let mut data = FdMatrix::zeros(n, m);
        for i in 0..n {
            for j in 0..m {
                data[(i, j)] = (j as f64 * 0.1).sin();
            }
        }

        let config = MfpcaConfig {
            ncomp: 3,
            weighted: false,
        };

        let result = mfpca(&[&data], &config).unwrap();

        // All eigenvalues should be ~0 since there is no variation
        for (l, &ev) in result.eigenvalues.iter().enumerate() {
            assert!(
                ev < 1e-10,
                "Eigenvalue[{l}] = {ev} should be ~0 for constant data"
            );
        }
    }

    #[test]
    fn test_mfpca_single_variable_matches_structure() {
        // Single variable MFPCA should produce valid results consistent with
        // univariate structure: ncomp scores, ncomp eigenfunctions, etc.
        let n = 20;
        let m = 15;
        let var1 = generate_ic_data(n, m, 42);

        let config = MfpcaConfig {
            ncomp: 3,
            weighted: false,
        };

        let result = mfpca(&[&var1], &config).unwrap();

        assert_eq!(result.scores.shape(), (n, 3));
        assert_eq!(result.eigenfunctions.len(), 1);
        assert_eq!(result.eigenfunctions[0].nrows(), m);
        assert_eq!(result.eigenfunctions[0].ncols(), 3);
        assert_eq!(result.eigenvalues.len(), 3);
        assert_eq!(result.means.len(), 1);
        assert_eq!(result.means[0].len(), m);
        assert_eq!(result.scales.len(), 1);
        assert_eq!(result.grid_sizes, vec![m]);

        // Eigenvalues should be non-negative and decreasing
        for &ev in &result.eigenvalues {
            assert!(ev >= 0.0, "Eigenvalue should be non-negative, got {ev}");
        }
        for w in result.eigenvalues.windows(2) {
            assert!(
                w[0] >= w[1] - 1e-12,
                "Eigenvalues should be decreasing: {} followed by {}",
                w[0],
                w[1]
            );
        }
    }

    // ── Gap 6: Phase I sample size boundary ─────────────────────────────────

    #[test]
    fn test_phase1_minimal_sample_size() {
        // n=8, ncomp=2, tuning_fraction=0.5 → 4 tuning, 4 calibration
        let n = 8;
        let m = 15;
        let data = generate_ic_data(n, m, 42);
        let argvals = uniform_grid(m);
        let config = SpmConfig {
            ncomp: 2,
            alpha: 0.05,
            tuning_fraction: 0.5,
            seed: 42,
        };

        let chart = spm_phase1(&data, &argvals, &config);
        assert!(
            chart.is_ok(),
            "Phase I with n=8 should succeed, got: {:?}",
            chart.err()
        );

        let chart = chart.unwrap();
        assert!(chart.t2_limit.ucl > 0.0);
        assert!(chart.spe_limit.ucl > 0.0);
        // Should have calibration observations
        assert!(
            !chart.t2_phase1.is_empty(),
            "Should have calibration T2 values"
        );
        assert!(
            !chart.spe_phase1.is_empty(),
            "Should have calibration SPE values"
        );
    }

    // ── Gap 7: EWMA extreme lambda ──────────────────────────────────────────

    #[test]
    fn test_ewma_extreme_lambda_very_small() {
        // lambda=0.001: smoothed scores should be nearly constant (close to 0)
        let n = 50;
        let ncomp = 2;
        let mut raw_data = Vec::with_capacity(n * ncomp);
        for i in 0..n {
            for k in 0..ncomp {
                raw_data.push(((i * 7 + k * 3 + 1) as f64).sin() * 5.0);
            }
        }
        let scores = FdMatrix::from_column_major(raw_data, n, ncomp).unwrap();
        let smoothed = ewma_scores(&scores, 0.001).unwrap();

        // With lambda=0.001, the smoothed values build up extremely slowly from 0
        // The max absolute smoothed value should be much smaller than the max raw value
        for k in 0..ncomp {
            let max_raw: f64 = (0..n).map(|i| scores[(i, k)].abs()).fold(0.0_f64, f64::max);
            let max_smoothed: f64 = (0..n)
                .map(|i| smoothed[(i, k)].abs())
                .fold(0.0_f64, f64::max);

            assert!(
                max_smoothed < max_raw * 0.1,
                "lambda=0.001: max smoothed ({max_smoothed}) should be << max raw ({max_raw}) for component {k}"
            );
        }

        // All smoothed values should be close to 0
        for i in 0..n {
            for k in 0..ncomp {
                assert!(
                    smoothed[(i, k)].abs() < 1.0,
                    "lambda=0.001: smoothed[({i},{k})] = {} should be close to 0",
                    smoothed[(i, k)]
                );
            }
        }
    }

    #[test]
    fn test_ewma_extreme_lambda_near_one() {
        // lambda=0.999: smoothed scores should be nearly equal to raw scores
        let n = 20;
        let ncomp = 2;
        let mut raw_data = Vec::with_capacity(n * ncomp);
        for i in 0..n {
            for k in 0..ncomp {
                raw_data.push(((i * 7 + k * 3 + 1) as f64).sin() * 5.0);
            }
        }
        let scores = FdMatrix::from_column_major(raw_data, n, ncomp).unwrap();
        let smoothed = ewma_scores(&scores, 0.999).unwrap();

        // After a few observations, the smoothed values should closely track raw values
        for i in 5..n {
            for k in 0..ncomp {
                let diff = (smoothed[(i, k)] - scores[(i, k)]).abs();
                let scale = scores[(i, k)].abs().max(1.0);
                assert!(
                    diff / scale < 0.05,
                    "lambda=0.999: smoothed[({i},{k})] = {} should be close to raw {} (relative diff: {})",
                    smoothed[(i, k)],
                    scores[(i, k)],
                    diff / scale
                );
            }
        }
    }

    // ── Gap 8: Contribution decomposition with non-trivial eigenvalues ──────

    #[test]
    fn test_t2_contributions_nontrivial_eigenvalues_sum_to_total() {
        let n = 5;
        let ncomp = 3;
        // Scores with real structure
        let scores = FdMatrix::from_column_major(
            vec![
                2.0, -1.5, 3.0, 0.5, -2.0, // component 0
                1.0, 0.5, -1.0, 2.0, -0.5, // component 1
                0.3, -0.2, 0.8, -0.4, 0.1, // component 2
            ],
            n,
            ncomp,
        )
        .unwrap();

        // Non-trivial eigenvalues: not all 1.0
        let eigenvalues = vec![4.0, 1.0, 0.25];
        let grid_sizes = vec![10, 15, 5]; // 3 variables

        let t2_total = hotelling_t2(&scores, &eigenvalues).unwrap();
        let contrib = t2_contributions(&scores, &eigenvalues, &grid_sizes).unwrap();

        assert_eq!(contrib.shape(), (n, 3));

        // Sum of contributions should equal total T2
        for i in 0..n {
            let sum: f64 = (0..3).map(|v| contrib[(i, v)]).sum();
            assert!(
                (sum - t2_total[i]).abs() < 1e-8,
                "T2 contribution sum ({sum}) should equal total ({}) for obs {i}",
                t2_total[i]
            );
        }

        // Verify T2 values are what we expect manually for the first observation:
        // T2 = 2.0^2/4.0 + 1.0^2/1.0 + 0.3^2/0.25 = 1.0 + 1.0 + 0.36 = 2.36
        let expected_t2_0 = 2.0_f64.powi(2) / 4.0 + 1.0_f64.powi(2) / 1.0 + 0.3_f64.powi(2) / 0.25;
        assert!(
            (t2_total[0] - expected_t2_0).abs() < 1e-10,
            "T2[0] should be {expected_t2_0}, got {}",
            t2_total[0]
        );

        // With non-trivial eigenvalues, contributions should reflect the weighting
        // Variable with largest grid_size should get the largest share
        // grid_sizes = [10, 15, 5], so variable 1 (15/30 = 0.5) should get 50%
        for i in 0..n {
            let total = t2_total[i];
            if total > 1e-12 {
                let frac_v1 = contrib[(i, 1)] / total;
                assert!(
                    (frac_v1 - 0.5).abs() < 1e-10,
                    "Variable 1 should contribute 50% of T2 (got {frac_v1}) for obs {i}"
                );
            }
        }
    }

    // ── ncomp selection tests ─────────────────────────────────────────────

    use crate::spm::ncomp::{select_ncomp, NcompMethod};

    #[test]
    fn test_ncomp_cumvar_known_spectrum() {
        let eigenvalues = [10.0, 5.0, 1.0, 0.1, 0.01];
        // Total = 16.11; cumvar at 2 = 15/16.11 = 0.931; at 3 = 16/16.11 = 0.993
        let ncomp = select_ncomp(&eigenvalues, &NcompMethod::CumulativeVariance(0.95)).unwrap();
        assert_eq!(ncomp, 3, "Should need 3 PCs for 95% variance");
    }

    #[test]
    fn test_ncomp_elbow_known_spectrum() {
        let eigenvalues = [10.0, 5.0, 1.0, 0.1, 0.01];
        let ncomp = select_ncomp(&eigenvalues, &NcompMethod::Elbow).unwrap();
        // Second finite diffs: d2[1]=10-10+1=1, d2[2]=5-2+0.1=3.1, d2[3]=1-0.2+0.01=0.81
        // Wait: d2[k] = λ[k-1] - 2λ[k] + λ[k+1]
        // d2[1] = 10 - 2*5 + 1 = 1
        // d2[2] = 5 - 2*1 + 0.1 = 3.1
        // d2[3] = 1 - 2*0.1 + 0.01 = 0.81
        // max at k=2 → ncomp = 3
        assert!(
            (2..=4).contains(&ncomp),
            "Elbow should be around 3, got {ncomp}"
        );
    }

    #[test]
    fn test_ncomp_fixed_clamp() {
        let eigenvalues = [10.0, 5.0, 1.0];
        let ncomp = select_ncomp(&eigenvalues, &NcompMethod::Fixed(10)).unwrap();
        assert_eq!(ncomp, 3, "Fixed(10) should be clamped to 3");
        let ncomp = select_ncomp(&eigenvalues, &NcompMethod::Fixed(0)).unwrap();
        assert_eq!(ncomp, 1, "Fixed(0) should be clamped to 1");
    }

    #[test]
    fn test_ncomp_flat_spectrum() {
        let eigenvalues = [1.0, 1.0, 1.0, 1.0, 1.0];
        let ncomp = select_ncomp(&eigenvalues, &NcompMethod::CumulativeVariance(0.80)).unwrap();
        assert_eq!(ncomp, 4, "Flat spectrum: need 4/5 for 80%");
    }

    #[test]
    fn test_ncomp_single_eigenvalue() {
        let eigenvalues = [5.0];
        let ncomp = select_ncomp(&eigenvalues, &NcompMethod::CumulativeVariance(0.95)).unwrap();
        assert_eq!(ncomp, 1);
        // Elbow fallback to CV(0.95) for < 3 values
        let ncomp = select_ncomp(&eigenvalues, &NcompMethod::Elbow).unwrap();
        assert_eq!(ncomp, 1);
    }

    #[test]
    fn test_ncomp_integration_with_phase1() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 10,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();
        // Use eigenvalues from chart to select ncomp
        let ncomp =
            select_ncomp(&chart.eigenvalues, &NcompMethod::CumulativeVariance(0.95)).unwrap();
        assert!(ncomp >= 1 && ncomp <= chart.eigenvalues.len());
    }

    // ── t2_pc_contributions tests ─────────────────────────────────────────

    use crate::spm::contrib::t2_pc_contributions;

    #[test]
    fn test_t2_pc_contributions_sum_matches_t2() {
        let data = generate_ic_data(20, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();
        let new_data = generate_ic_data(10, 30, 100);
        let result = spm_monitor(&chart, &new_data, &argvals).unwrap();

        let contrib = t2_pc_contributions(&result.scores, &chart.eigenvalues).unwrap();
        let (n, ncomp) = contrib.shape();
        assert_eq!(n, 10);
        assert_eq!(ncomp, chart.eigenvalues.len());

        // Row sums should match T² values
        for i in 0..n {
            let row_sum: f64 = (0..ncomp).map(|l| contrib[(i, l)]).sum();
            assert!(
                (row_sum - result.t2[i]).abs() < 1e-8,
                "Row {i} sum {row_sum} != T2 {}",
                result.t2[i]
            );
        }
    }

    #[test]
    fn test_t2_pc_contributions_manual_3x2() {
        // Manual test: 3 obs, 2 components
        let scores = FdMatrix::from_column_major(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2).unwrap();
        let eigenvalues = [2.0, 3.0];
        let contrib = t2_pc_contributions(&scores, &eigenvalues).unwrap();

        // contrib[(0,0)] = 1²/2 = 0.5, contrib[(0,1)] = 4²/3 = 5.333
        assert!((contrib[(0, 0)] - 0.5).abs() < 1e-10);
        assert!((contrib[(0, 1)] - 16.0 / 3.0).abs() < 1e-10);
        // contrib[(1,0)] = 2²/2 = 2.0, contrib[(1,1)] = 5²/3 = 8.333
        assert!((contrib[(1, 0)] - 2.0).abs() < 1e-10);
        assert!((contrib[(1, 1)] - 25.0 / 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_t2_pc_contributions_shifted_attribution() {
        // Large shift should make overall T² large, and some PC should dominate
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let oc_data = generate_oc_data(5, 30, 5.0, 42);
        let result = spm_monitor(&chart, &oc_data, &argvals).unwrap();
        let contrib = t2_pc_contributions(&result.scores, &chart.eigenvalues).unwrap();

        // For shifted data, total T² should be large and at least one PC
        // should dominate the contribution
        for i in 0..5 {
            let total: f64 = (0..chart.eigenvalues.len()).map(|l| contrib[(i, l)]).sum();
            let max_frac = (0..chart.eigenvalues.len())
                .map(|l| contrib[(i, l)] / total)
                .fold(0.0_f64, f64::max);
            // At least one PC should contribute > 30% of the total
            assert!(
                max_frac > 0.3,
                "Shifted obs {i}: dominant PC should contribute > 30%, got {max_frac}"
            );
        }
    }

    // ── rules tests ───────────────────────────────────────────────────────

    use crate::spm::rules::{evaluate_rules, western_electric_rules, ChartRule};

    #[test]
    fn test_we1_single_outlier() {
        let mut values = vec![0.0; 20];
        values[10] = 4.0; // Beyond 3σ
        let violations = western_electric_rules(&values, 0.0, 1.0).unwrap();
        let we1_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule == ChartRule::WE1)
            .collect();
        assert!(
            !we1_violations.is_empty(),
            "Should detect WE1 violation at index 10"
        );
        assert!(we1_violations.iter().any(|v| v.indices.contains(&10)));
    }

    #[test]
    fn test_we4_eight_in_a_row() {
        // 8 points all above center
        let values: Vec<f64> = (0..15)
            .map(|i| if (3..11).contains(&i) { 0.5 } else { -0.5 })
            .collect();
        let violations = evaluate_rules(&values, 0.0, 1.0, &[ChartRule::WE4]).unwrap();
        assert!(
            !violations.is_empty(),
            "Should detect WE4: 8 consecutive same side"
        );
    }

    #[test]
    fn test_nelson5_monotone() {
        // 6 strictly increasing points
        let values: Vec<f64> = (0..10).map(|i| i as f64 * 0.1).collect();
        let violations = evaluate_rules(&values, 0.0, 1.0, &[ChartRule::Nelson5]).unwrap();
        assert!(
            !violations.is_empty(),
            "Should detect Nelson5: 6 monotone increasing"
        );
    }

    #[test]
    fn test_no_false_positives_centered() {
        // Data centered at 0 with small variation (within 1σ range, but not 15 consecutive)
        let values: Vec<f64> = (0..10)
            .map(|i| if i % 2 == 0 { 0.5 } else { -0.5 })
            .collect();
        let violations = western_electric_rules(&values, 0.0, 1.0).unwrap();
        // WE1 should not fire (all within 3σ)
        let we1: Vec<_> = violations
            .iter()
            .filter(|v| v.rule == ChartRule::WE1)
            .collect();
        assert!(we1.is_empty(), "No WE1 violations expected");
    }

    #[test]
    fn test_rules_empty_input() {
        let violations = evaluate_rules(&[], 0.0, 1.0, &[ChartRule::WE1, ChartRule::WE4]).unwrap();
        assert!(
            violations.is_empty(),
            "Empty input should yield no violations"
        );
    }

    // ── bootstrap tests ───────────────────────────────────────────────────

    use crate::spm::bootstrap::{spe_limit_robust, t2_limit_robust, ControlLimitMethod};

    #[test]
    fn test_bootstrap_parametric_matches_existing() {
        let limit_param =
            t2_limit_robust(&[1.0; 50], 3, 0.05, &ControlLimitMethod::Parametric).unwrap();
        let limit_orig = t2_control_limit(3, 0.05).unwrap();
        assert!(
            (limit_param.ucl - limit_orig.ucl).abs() < 1e-10,
            "Parametric should match: {} vs {}",
            limit_param.ucl,
            limit_orig.ucl
        );
    }

    #[test]
    fn test_bootstrap_empirical_on_sequence() {
        // Values 1..100, empirical 95th percentile ≈ 95
        let values: Vec<f64> = (1..=100).map(|i| i as f64).collect();
        let limit = t2_limit_robust(&values, 5, 0.05, &ControlLimitMethod::Empirical).unwrap();
        assert!(
            (limit.ucl - 95.0).abs() < 1.5,
            "Empirical 95th percentile of 1..100 should be ~95, got {}",
            limit.ucl
        );
    }

    #[test]
    fn test_bootstrap_convergence() {
        // Bootstrap on uniform data should give stable result
        let values: Vec<f64> = (1..=100).map(|i| i as f64).collect();
        let limit = t2_limit_robust(
            &values,
            5,
            0.05,
            &ControlLimitMethod::Bootstrap {
                n_bootstrap: 1000,
                seed: 42,
            },
        )
        .unwrap();
        // Should be close to the empirical quantile
        assert!(
            limit.ucl > 85.0 && limit.ucl < 100.0,
            "Bootstrap UCL should be near 95, got {}",
            limit.ucl
        );
    }

    #[test]
    fn test_kde_convergence() {
        let values: Vec<f64> = (1..=100).map(|i| i as f64).collect();
        let limit = t2_limit_robust(
            &values,
            5,
            0.05,
            &ControlLimitMethod::KernelDensity { bandwidth: None },
        )
        .unwrap();
        assert!(
            limit.ucl > 80.0 && limit.ucl < 105.0,
            "KDE UCL should be near 95, got {}",
            limit.ucl
        );
    }

    #[test]
    fn test_spe_limit_robust_empirical() {
        let values: Vec<f64> = (1..=50).map(|i| i as f64).collect();
        let limit = spe_limit_robust(&values, 0.05, &ControlLimitMethod::Empirical).unwrap();
        assert!(limit.ucl > 45.0 && limit.ucl <= 50.0);
    }

    // ── ARL tests ─────────────────────────────────────────────────────────

    use crate::spm::arl::{arl0_ewma_t2, arl0_spe, arl0_t2, arl1_t2, ArlConfig};

    #[test]
    fn test_arl0_approximately_1_over_alpha() {
        // ARL0 ≈ 1/α for T² chart
        let eigenvalues = [1.0, 0.5, 0.25];
        let ucl = chi2_quantile(0.95, 3); // α = 0.05
        let config = ArlConfig {
            n_simulations: 5000,
            max_run_length: 2000,
            seed: 42,
        };
        let result = arl0_t2(&eigenvalues, ucl, &config).unwrap();
        // Theory: ARL0 = 1/0.05 = 20
        assert!(
            result.arl > 10.0 && result.arl < 40.0,
            "ARL0 should be near 20, got {}",
            result.arl
        );
    }

    #[test]
    fn test_arl1_less_than_arl0() {
        let eigenvalues = [1.0, 0.5];
        let ucl = chi2_quantile(0.95, 2);
        let config = ArlConfig {
            n_simulations: 2000,
            max_run_length: 1000,
            seed: 42,
        };
        let arl0 = arl0_t2(&eigenvalues, ucl, &config).unwrap();
        let shift = [2.0, 0.0]; // Shift in PC1
        let arl1 = arl1_t2(&eigenvalues, ucl, &shift, &config).unwrap();
        assert!(
            arl1.arl < arl0.arl,
            "ARL1 ({}) should be less than ARL0 ({})",
            arl1.arl,
            arl0.arl
        );
    }

    #[test]
    fn test_ewma_lambda1_matches_raw() {
        // EWMA with lambda=1 should match raw T²
        let eigenvalues = [1.0, 0.5];
        let ucl = chi2_quantile(0.95, 2);
        let config = ArlConfig {
            n_simulations: 1000,
            max_run_length: 500,
            seed: 42,
        };
        let raw = arl0_t2(&eigenvalues, ucl, &config).unwrap();
        let ewma = arl0_ewma_t2(&eigenvalues, ucl, 1.0, &config).unwrap();
        // With lambda=1, EWMA reduces to raw scores, adjusted eigenvalues = eigenvalues
        assert!(
            (ewma.arl - raw.arl).abs() / raw.arl < 0.3,
            "EWMA(lambda=1) ARL {} should be close to raw ARL {}",
            ewma.arl,
            raw.arl
        );
    }

    #[test]
    fn test_arl_monotonicity_shift() {
        let eigenvalues = [1.0];
        let ucl = chi2_quantile(0.95, 1);
        let config = ArlConfig {
            n_simulations: 1000,
            max_run_length: 500,
            seed: 42,
        };
        let arl_small = arl1_t2(&eigenvalues, ucl, &[1.0], &config).unwrap();
        let arl_large = arl1_t2(&eigenvalues, ucl, &[3.0], &config).unwrap();
        assert!(
            arl_large.arl < arl_small.arl,
            "Larger shift should give smaller ARL1: {} vs {}",
            arl_large.arl,
            arl_small.arl
        );
    }

    #[test]
    fn test_arl0_spe_basic() {
        let config = ArlConfig {
            n_simulations: 1000,
            max_run_length: 500,
            seed: 42,
        };
        // df=10, scale=1.0, ucl at 95th percentile ≈ 18.31
        let ucl = chi2_quantile(0.95, 10);
        let result = arl0_spe(10.0, 1.0, ucl, &config).unwrap();
        // ARL0 should be roughly 1/0.05 = 20
        assert!(
            result.arl > 8.0 && result.arl < 50.0,
            "SPE ARL0 should be near 20, got {}",
            result.arl
        );
    }

    // ── MEWMA tests ───────────────────────────────────────────────────────

    use crate::spm::mewma::{spm_mewma_monitor, MewmaConfig};

    #[test]
    fn test_mewma_asymptotic_ic_data() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let new_data = generate_ic_data(20, 30, 100);
        let mewma_config = MewmaConfig {
            lambda: 0.2,
            ncomp: 3,
            alpha: 0.05,
            asymptotic: true,
        };
        let result = spm_mewma_monitor(&chart, &new_data, &argvals, &mewma_config).unwrap();
        assert_eq!(result.mewma_statistic.len(), 20);
        assert_eq!(result.alarm.len(), 20);
        assert!(result.ucl > 0.0);
    }

    #[test]
    fn test_mewma_exact_converges_to_asymptotic() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        // Generate enough data that exact should converge
        let new_data = generate_ic_data(100, 30, 100);
        let mewma_asym = spm_mewma_monitor(
            &chart,
            &new_data,
            &argvals,
            &MewmaConfig {
                asymptotic: true,
                ..MewmaConfig::default()
            },
        )
        .unwrap();
        let mewma_exact = spm_mewma_monitor(
            &chart,
            &new_data,
            &argvals,
            &MewmaConfig {
                asymptotic: false,
                ..MewmaConfig::default()
            },
        )
        .unwrap();

        // At the end of the sequence, exact should be close to asymptotic
        let last = mewma_asym.mewma_statistic.len() - 1;
        let ratio = mewma_exact.mewma_statistic[last] / mewma_asym.mewma_statistic[last];
        assert!(
            (ratio - 1.0).abs() < 0.15,
            "Exact and asymptotic should converge, ratio = {ratio}"
        );
    }

    #[test]
    fn test_mewma_lambda1_matches_raw_t2() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let new_data = generate_ic_data(10, 30, 100);
        let mewma_result = spm_mewma_monitor(
            &chart,
            &new_data,
            &argvals,
            &MewmaConfig {
                lambda: 1.0,
                ncomp: 3,
                alpha: 0.05,
                asymptotic: true,
            },
        )
        .unwrap();
        let raw_result = spm_monitor(&chart, &new_data, &argvals).unwrap();

        // With lambda=1, MEWMA T² should equal raw T²
        for i in 0..10 {
            assert!(
                (mewma_result.mewma_statistic[i] - raw_result.t2[i]).abs() < 1e-6,
                "Lambda=1 MEWMA[{i}]={} should equal T2[{i}]={}",
                mewma_result.mewma_statistic[i],
                raw_result.t2[i]
            );
        }
    }

    #[test]
    fn test_mewma_sensitive_to_small_shifts() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        // Small shift (0.5) - MEWMA should accumulate it
        let oc_data = generate_oc_data(30, 30, 0.5, 42);
        let mewma_config = MewmaConfig {
            lambda: 0.1,
            ncomp: 3,
            alpha: 0.05,
            asymptotic: true,
        };
        let result = spm_mewma_monitor(&chart, &oc_data, &argvals, &mewma_config).unwrap();
        // MEWMA statistic should generally increase over time due to accumulation
        let first_half_mean: f64 = result.mewma_statistic[..15].iter().sum::<f64>() / 15.0;
        let second_half_mean: f64 = result.mewma_statistic[15..].iter().sum::<f64>() / 15.0;
        assert!(
            second_half_mean >= first_half_mean * 0.5,
            "MEWMA should accumulate shift: first_half={first_half_mean}, second_half={second_half_mean}"
        );
    }

    // ── Profile monitoring tests ──────────────────────────────────────────

    use crate::spm::profile::{profile_monitor, profile_phase1, ProfileMonitorConfig};

    /// Generate y_curves and predictors for profile testing.
    fn generate_profile_data(n: usize, m: usize, p: usize, seed: u64) -> (FdMatrix, FdMatrix) {
        let t = uniform_grid(m);
        let mut y = FdMatrix::zeros(n, m);
        let mut pred = FdMatrix::zeros(n, p);
        for i in 0..n {
            // Deterministic pseudo-random predictor values
            for k in 0..p {
                pred[(i, k)] = ((seed
                    .wrapping_add(i as u64 * 3 + k as u64)
                    .wrapping_mul(48271)) as f64)
                    / (u32::MAX as f64)
                    * 2.0
                    - 1.0;
            }
            for j in 0..m {
                // y = beta0(t) + x * beta1(t) + noise
                let beta0 = (2.0 * PI * t[j]).sin();
                let beta1 = t[j] * (1.0 - t[j]);
                let noise = 0.05
                    * ((seed
                        .wrapping_add(i as u64 * 13 + j as u64 * 7)
                        .wrapping_mul(48271)) as f64)
                    / (u32::MAX as f64);
                y[(i, j)] = beta0 + pred[(i, 0)] * beta1 + noise;
            }
        }
        (y, pred)
    }

    #[test]
    fn test_profile_phase1_basic() {
        let (y, pred) = generate_profile_data(60, 20, 1, 42);
        let argvals = uniform_grid(20);
        let config = ProfileMonitorConfig {
            window_size: 15,
            ncomp: 2,
            ..ProfileMonitorConfig::default()
        };
        let chart = profile_phase1(&y, &pred, &argvals, &config).unwrap();
        assert!(chart.t2_limit.ucl > 0.0);
        assert!(!chart.eigenvalues.is_empty());
    }

    #[test]
    fn test_profile_monitor_stable() {
        let (y_train, pred_train) = generate_profile_data(60, 20, 1, 42);
        let (y_test, pred_test) = generate_profile_data(40, 20, 1, 100);
        let argvals = uniform_grid(20);
        let config = ProfileMonitorConfig {
            window_size: 15,
            ncomp: 2,
            ..ProfileMonitorConfig::default()
        };
        let chart = profile_phase1(&y_train, &pred_train, &argvals, &config).unwrap();
        let result = profile_monitor(&chart, &y_test, &pred_test, &argvals, &config).unwrap();
        assert!(!result.t2.is_empty());
        // Stable profile should have few alarms
        let alarm_rate =
            result.t2_alarm.iter().filter(|&&a| a).count() as f64 / result.t2_alarm.len() as f64;
        assert!(
            alarm_rate < 0.5,
            "Stable profile alarm rate should be low, got {alarm_rate}"
        );
    }

    #[test]
    fn test_profile_shifted_triggers_alarms() {
        let (y_train, pred_train) = generate_profile_data(60, 20, 1, 42);
        let argvals = uniform_grid(20);
        let config = ProfileMonitorConfig {
            window_size: 15,
            ncomp: 2,
            ..ProfileMonitorConfig::default()
        };
        let chart = profile_phase1(&y_train, &pred_train, &argvals, &config).unwrap();

        // Shifted data
        let (mut y_test, pred_test) = generate_profile_data(40, 20, 1, 200);
        for i in 0..y_test.nrows() {
            for j in 0..y_test.ncols() {
                y_test[(i, j)] += 3.0; // Large shift
            }
        }
        let result = profile_monitor(&chart, &y_test, &pred_test, &argvals, &config).unwrap();
        let alarm_count = result.t2_alarm.iter().filter(|&&a| a).count();
        assert!(
            alarm_count > 0,
            "Shifted profile should trigger some alarms"
        );
    }

    #[test]
    fn test_profile_small_window() {
        let (y, pred) = generate_profile_data(30, 15, 1, 42);
        let argvals = uniform_grid(15);
        let config = ProfileMonitorConfig {
            window_size: 5,
            step_size: 2,
            ncomp: 2,
            ..ProfileMonitorConfig::default()
        };
        let chart = profile_phase1(&y, &pred, &argvals, &config).unwrap();
        assert!(chart.eigenvalues.len() <= 2);
    }

    #[test]
    fn test_profile_step_size_zero_rejected() {
        let (y, pred) = generate_profile_data(30, 15, 1, 42);
        let argvals = uniform_grid(15);
        let config = ProfileMonitorConfig {
            step_size: 0,
            ..ProfileMonitorConfig::default()
        };
        assert!(profile_phase1(&y, &pred, &argvals, &config).is_err());
    }

    // ── Partial-domain monitoring tests ───────────────────────────────────

    use crate::spm::partial::{
        spm_monitor_partial, spm_monitor_partial_batch, DomainCompletion, PartialDomainConfig,
    };

    #[test]
    fn test_partial_full_domain_matches_monitor() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let new_data = generate_ic_data(1, 30, 200);
        let full_values: Vec<f64> = (0..30).map(|j| new_data[(0, j)]).collect();

        // Full domain with ZeroPad should match spm_monitor
        let partial_config = PartialDomainConfig {
            ncomp: 3,
            completion: DomainCompletion::ZeroPad,
            ..PartialDomainConfig::default()
        };
        let partial_result =
            spm_monitor_partial(&chart, &full_values, &argvals, 30, &partial_config).unwrap();
        let full_result = spm_monitor(&chart, &new_data, &argvals).unwrap();

        assert!(
            (partial_result.t2 - full_result.t2[0]).abs() < 1e-4,
            "Full domain partial T2 ({}) should match full T2 ({})",
            partial_result.t2,
            full_result.t2[0]
        );
    }

    #[test]
    fn test_partial_decreasing_domain_noisier() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let new_data = generate_ic_data(1, 30, 300);
        let values: Vec<f64> = (0..30).map(|j| new_data[(0, j)]).collect();

        let partial_config = PartialDomainConfig {
            ncomp: 3,
            completion: DomainCompletion::PartialProjection,
            ..PartialDomainConfig::default()
        };

        // More observed points → scores should stabilize
        let result_10 =
            spm_monitor_partial(&chart, &values, &argvals, 10, &partial_config).unwrap();
        let result_30 =
            spm_monitor_partial(&chart, &values, &argvals, 30, &partial_config).unwrap();

        assert!(
            result_10.domain_fraction < result_30.domain_fraction,
            "10 points should have smaller domain fraction"
        );
    }

    #[test]
    fn test_partial_ce_accuracy() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 2,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let new_data = generate_ic_data(1, 30, 400);
        let values: Vec<f64> = (0..30).map(|j| new_data[(0, j)]).collect();

        let partial_config = PartialDomainConfig {
            ncomp: 2,
            completion: DomainCompletion::ConditionalExpectation,
            ..PartialDomainConfig::default()
        };
        let result = spm_monitor_partial(&chart, &values, &argvals, 20, &partial_config).unwrap();
        assert!(result.completed_curve.is_some());
        assert_eq!(result.scores.len(), 2);
        assert!(result.domain_fraction > 0.0 && result.domain_fraction <= 1.0);
    }

    #[test]
    fn test_partial_zeropad_baseline() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let new_data = generate_ic_data(1, 30, 500);
        let values: Vec<f64> = (0..30).map(|j| new_data[(0, j)]).collect();

        let partial_config = PartialDomainConfig {
            ncomp: 3,
            completion: DomainCompletion::ZeroPad,
            ..PartialDomainConfig::default()
        };
        let result = spm_monitor_partial(&chart, &values, &argvals, 15, &partial_config).unwrap();
        assert!(result.completed_curve.is_some());
        let curve = result.completed_curve.unwrap();
        // Unobserved portion should equal the mean
        for j in 15..30 {
            assert!(
                (curve[j] - chart.fpca.mean[j]).abs() < 1e-10,
                "ZeroPad: unobserved point {j} should equal mean"
            );
        }
    }

    #[test]
    fn test_partial_n_observed_1() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 2,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let values = vec![1.0; 30];
        let partial_config = PartialDomainConfig {
            ncomp: 2,
            completion: DomainCompletion::ConditionalExpectation,
            ..PartialDomainConfig::default()
        };
        let result = spm_monitor_partial(&chart, &values, &argvals, 1, &partial_config).unwrap();
        assert_eq!(result.scores.len(), 2);
    }

    #[test]
    fn test_partial_n_observed_0_error() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 2,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let values = vec![1.0; 30];
        let partial_config = PartialDomainConfig::default();
        let result = spm_monitor_partial(&chart, &values, &argvals, 0, &partial_config);
        assert!(result.is_err(), "n_observed=0 should return error");
    }

    #[test]
    fn test_partial_batch() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 2,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let values1 = vec![0.5; 30];
        let values2 = vec![1.0; 30];
        let batch: Vec<(&[f64], usize)> = vec![(&values1, 15), (&values2, 20)];
        let partial_config = PartialDomainConfig::default();
        let results = spm_monitor_partial_batch(&chart, &batch, &argvals, &partial_config).unwrap();
        assert_eq!(results.len(), 2);
    }

    // ── Elastic SPM tests ─────────────────────────────────────────────────

    use crate::spm::elastic_spm::{elastic_spm_monitor, elastic_spm_phase1, ElasticSpmConfig};

    #[test]
    fn test_elastic_spm_amplitude_only() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = ElasticSpmConfig {
            spm: SpmConfig {
                ncomp: 3,
                ..SpmConfig::default()
            },
            monitor_phase: false,
            ..ElasticSpmConfig::default()
        };
        let chart = elastic_spm_phase1(&data, &argvals, &config).unwrap();
        assert!(chart.phase_chart.is_none());

        let new_data = generate_ic_data(10, 30, 100);
        let result = elastic_spm_monitor(&chart, &new_data, &argvals).unwrap();
        assert!(result.phase.is_none());
        assert_eq!(result.amplitude.t2.len(), 10);
    }

    #[test]
    fn test_elastic_spm_with_phase() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = ElasticSpmConfig {
            spm: SpmConfig {
                ncomp: 3,
                ..SpmConfig::default()
            },
            monitor_phase: true,
            warp_ncomp: 2,
            ..ElasticSpmConfig::default()
        };
        let chart = elastic_spm_phase1(&data, &argvals, &config).unwrap();
        assert!(chart.phase_chart.is_some());

        let new_data = generate_ic_data(10, 30, 100);
        let result = elastic_spm_monitor(&chart, &new_data, &argvals).unwrap();
        assert!(result.phase.is_some());
        let phase = result.phase.unwrap();
        assert_eq!(phase.t2.len(), 10);
    }

    #[test]
    fn test_elastic_spm_roundtrip() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = ElasticSpmConfig {
            spm: SpmConfig {
                ncomp: 3,
                ..SpmConfig::default()
            },
            ..ElasticSpmConfig::default()
        };
        let chart = elastic_spm_phase1(&data, &argvals, &config).unwrap();
        assert!(!chart.karcher_mean.is_empty());
        assert_eq!(chart.karcher_mean.len(), 30);

        // Monitor same data — should have few alarms
        let result = elastic_spm_monitor(&chart, &data, &argvals).unwrap();
        assert_eq!(result.aligned_data.nrows(), 40);
        assert_eq!(result.warping_functions.nrows(), 40);
    }

    #[test]
    fn test_elastic_spm_shifted_detection() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = ElasticSpmConfig {
            spm: SpmConfig {
                ncomp: 3,
                ..SpmConfig::default()
            },
            ..ElasticSpmConfig::default()
        };
        let chart = elastic_spm_phase1(&data, &argvals, &config).unwrap();

        let oc_data = generate_oc_data(10, 30, 5.0, 42);
        let result = elastic_spm_monitor(&chart, &oc_data, &argvals).unwrap();
        let amp_alarms = result.amplitude.t2_alarm.iter().filter(|&&a| a).count();
        // Large shift should trigger alarms
        assert!(
            amp_alarms > 0,
            "Amplitude shift should trigger alarms, got {amp_alarms}"
        );
    }

    // ── CUSUM tests ──────────────────────────────────────────────────────────

    #[test]
    fn test_cusum_multivariate_ic_data() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let ic_data = generate_ic_data(20, 30, 200);
        let cusum_config = CusumConfig {
            ncomp: 3,
            ..CusumConfig::default()
        };
        let result = spm_cusum_monitor(&chart, &ic_data, &argvals, &cusum_config).unwrap();

        assert_eq!(result.cusum_statistic.len(), 20);
        assert_eq!(result.alarm.len(), 20);
        assert!(result.ucl > 0.0);
        assert!(result.cusum_plus.is_none()); // multivariate mode
        assert!(result.cusum_minus.is_none());
        for &s in &result.cusum_statistic {
            assert!(s >= 0.0, "CUSUM statistic must be non-negative");
        }
    }

    #[test]
    fn test_cusum_univariate_mode() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let ic_data = generate_ic_data(15, 30, 300);
        let cusum_config = CusumConfig {
            ncomp: 3,
            multivariate: false,
            ..CusumConfig::default()
        };
        let result = spm_cusum_monitor(&chart, &ic_data, &argvals, &cusum_config).unwrap();

        assert_eq!(result.cusum_statistic.len(), 15);
        assert!(result.cusum_plus.is_some());
        assert!(result.cusum_minus.is_some());
        let cp = result.cusum_plus.as_ref().unwrap();
        let cm = result.cusum_minus.as_ref().unwrap();
        assert_eq!(cp.shape(), (15, 3));
        assert_eq!(cm.shape(), (15, 3));
    }

    #[test]
    fn test_cusum_detects_shift() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let oc_data = generate_oc_data(20, 30, 3.0, 42);
        let cusum_config = CusumConfig {
            ncomp: 3,
            k: 0.5,
            h: 4.0,
            ..CusumConfig::default()
        };
        let result = spm_cusum_monitor(&chart, &oc_data, &argvals, &cusum_config).unwrap();
        let alarm_count = result.alarm.iter().filter(|&&a| a).count();
        assert!(
            alarm_count > 0,
            "CUSUM should detect shifted data, got 0 alarms"
        );
    }

    #[test]
    fn test_cusum_restart_fewer_alarms() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let oc_data = generate_oc_data(30, 30, 3.0, 42);
        let cusum_config = CusumConfig {
            ncomp: 3,
            k: 0.5,
            h: 4.0,
            ..CusumConfig::default()
        };

        let result_no_restart =
            spm_cusum_monitor(&chart, &oc_data, &argvals, &cusum_config).unwrap();
        let result_restart =
            spm_cusum_monitor_with_restart(&chart, &oc_data, &argvals, &cusum_config).unwrap();

        // With restart, statistics reset after alarm, so cumulative stats should differ
        assert_eq!(result_no_restart.cusum_statistic.len(), 30);
        assert_eq!(result_restart.cusum_statistic.len(), 30);
        // After first alarm, restart version should have lower statistics
        let no_restart_alarms = result_no_restart.alarm.iter().filter(|&&a| a).count();
        let restart_alarms = result_restart.alarm.iter().filter(|&&a| a).count();
        // Both should detect the shift
        assert!(no_restart_alarms > 0);
        assert!(restart_alarms > 0);
    }

    #[test]
    fn test_cusum_invalid_k() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig::default();
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let ic_data = generate_ic_data(5, 30, 100);
        let cusum_config = CusumConfig {
            k: -1.0,
            ..CusumConfig::default()
        };
        assert!(spm_cusum_monitor(&chart, &ic_data, &argvals, &cusum_config).is_err());
    }

    #[test]
    fn test_cusum_invalid_h() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig::default();
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let ic_data = generate_ic_data(5, 30, 100);
        let cusum_config = CusumConfig {
            h: 0.0,
            ..CusumConfig::default()
        };
        assert!(spm_cusum_monitor(&chart, &ic_data, &argvals, &cusum_config).is_err());
    }

    // ── Adaptive EWMA (AMFEWMA) tests ────────────────────────────────────────

    #[test]
    fn test_amewma_ic_data() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let ic_data = generate_ic_data(20, 30, 200);
        let amewma_config = AmewmaConfig {
            ncomp: 3,
            ..AmewmaConfig::default()
        };
        let result = spm_amewma_monitor(&chart, &ic_data, &argvals, &amewma_config).unwrap();

        assert_eq!(result.t2_statistic.len(), 20);
        assert_eq!(result.lambda_t.len(), 20);
        assert_eq!(result.alarm.len(), 20);
        assert!(result.ucl > 0.0);
        assert_eq!(result.smoothed_scores.shape(), (20, 3));

        // Lambda should stay within bounds
        for &lam in &result.lambda_t {
            assert!(
                lam >= amewma_config.lambda_min && lam <= amewma_config.lambda_max,
                "lambda_t={lam} out of bounds [{}, {}]",
                amewma_config.lambda_min,
                amewma_config.lambda_max
            );
        }
    }

    #[test]
    fn test_amewma_detects_shift() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let oc_data = generate_oc_data(20, 30, 5.0, 42);
        let amewma_config = AmewmaConfig {
            ncomp: 3,
            ..AmewmaConfig::default()
        };
        let result = spm_amewma_monitor(&chart, &oc_data, &argvals, &amewma_config).unwrap();
        let alarm_count = result.alarm.iter().filter(|&&a| a).count();
        assert!(
            alarm_count > 0,
            "AMFEWMA should detect shifted data, got 0 alarms"
        );
    }

    #[test]
    fn test_amewma_lambda_increases_on_shift() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        // Monitor shifted data — lambda should increase due to larger errors
        let oc_data = generate_oc_data(20, 30, 5.0, 42);
        let amewma_config = AmewmaConfig {
            ncomp: 3,
            lambda_init: 0.2,
            ..AmewmaConfig::default()
        };
        let result = spm_amewma_monitor(&chart, &oc_data, &argvals, &amewma_config).unwrap();

        // Later lambdas should be larger than initial as the adaptive weight reacts
        let avg_lambda: f64 = result.lambda_t.iter().sum::<f64>() / result.lambda_t.len() as f64;
        assert!(
            avg_lambda > amewma_config.lambda_init,
            "Average lambda ({avg_lambda}) should exceed lambda_init ({}) under shift",
            amewma_config.lambda_init
        );
    }

    #[test]
    fn test_amewma_invalid_lambda_range() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = SpmConfig::default();
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let ic_data = generate_ic_data(5, 30, 100);

        // lambda_min > lambda_max
        let bad_config = AmewmaConfig {
            lambda_min: 0.8,
            lambda_max: 0.2,
            lambda_init: 0.2,
            ..AmewmaConfig::default()
        };
        assert!(spm_amewma_monitor(&chart, &ic_data, &argvals, &bad_config).is_err());

        // lambda_init out of range
        let bad_config2 = AmewmaConfig {
            lambda_min: 0.1,
            lambda_max: 0.5,
            lambda_init: 0.8,
            ..AmewmaConfig::default()
        };
        assert!(spm_amewma_monitor(&chart, &ic_data, &argvals, &bad_config2).is_err());

        // eta out of range
        let bad_config3 = AmewmaConfig {
            eta: 0.0,
            ..AmewmaConfig::default()
        };
        assert!(spm_amewma_monitor(&chart, &ic_data, &argvals, &bad_config3).is_err());
    }

    // ── Iterative Phase I tests ──────────────────────────────────────────────

    #[test]
    fn test_iterative_phase1_clean_data() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = IterativePhase1Config {
            spm: SpmConfig {
                ncomp: 3,
                alpha: 0.05,
                ..SpmConfig::default()
            },
            max_removal_fraction: 0.3,
            ..IterativePhase1Config::default()
        };
        let result = spm_phase1_iterative(&data, &argvals, &config).unwrap();

        // Removal fraction should not exceed the limit
        let frac = result.removed_indices.len() as f64 / 40.0;
        assert!(
            frac <= 0.3 + 0.01,
            "Should not exceed max_removal_fraction, got {frac}"
        );
        assert_eq!(result.n_remaining + result.removed_indices.len(), 40);
        // Chart should be valid
        assert!(result.chart.t2_limit.ucl > 0.0);
    }

    #[test]
    fn test_iterative_phase1_removes_outliers() {
        // Build data with extreme outliers — large enough to be clearly OC
        let mut data = generate_ic_data(50, 30, 42);
        // Add 5 extreme outliers
        for i in 45..50 {
            for j in 0..30 {
                data[(i, j)] += 50.0;
            }
        }
        let argvals = uniform_grid(30);
        let config = IterativePhase1Config {
            spm: SpmConfig {
                ncomp: 3,
                alpha: 0.01,
                ..SpmConfig::default()
            },
            max_removal_fraction: 0.5,
            ..IterativePhase1Config::default()
        };
        let result = spm_phase1_iterative(&data, &argvals, &config).unwrap();

        // Should perform at least one iteration
        assert!(
            result.n_iterations > 0 || result.removed_indices.is_empty(),
            "iterations={}, removed={}",
            result.n_iterations,
            result.removed_indices.len()
        );
        // The chart should be valid regardless
        assert!(result.chart.t2_limit.ucl > 0.0);
        assert!(result.n_remaining >= 4);
    }

    #[test]
    fn test_iterative_phase1_max_removal_fraction() {
        // All data is "extreme" — should hit removal fraction limit
        let data = generate_oc_data(40, 30, 10.0, 42);
        let argvals = uniform_grid(30);
        let config = IterativePhase1Config {
            spm: SpmConfig {
                ncomp: 3,
                alpha: 0.05,
                ..SpmConfig::default()
            },
            max_removal_fraction: 0.1,
            ..IterativePhase1Config::default()
        };
        let result = spm_phase1_iterative(&data, &argvals, &config).unwrap();

        // Should not remove more than 10%
        assert!(
            result.removed_indices.len() as f64 / 40.0 <= 0.1 + 0.01,
            "Should not exceed max_removal_fraction"
        );
    }

    #[test]
    fn test_iterative_phase1_invalid_params() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);

        // max_iterations = 0
        let config = IterativePhase1Config {
            max_iterations: 0,
            ..IterativePhase1Config::default()
        };
        assert!(spm_phase1_iterative(&data, &argvals, &config).is_err());

        // max_removal_fraction = 0
        let config2 = IterativePhase1Config {
            max_removal_fraction: 0.0,
            ..IterativePhase1Config::default()
        };
        assert!(spm_phase1_iterative(&data, &argvals, &config2).is_err());
    }

    #[test]
    fn test_iterative_phase1_convergence() {
        let data = generate_ic_data(40, 30, 42);
        let argvals = uniform_grid(30);
        let config = IterativePhase1Config {
            spm: SpmConfig {
                ncomp: 3,
                alpha: 0.05,
                ..SpmConfig::default()
            },
            max_iterations: 20,
            ..IterativePhase1Config::default()
        };
        let result = spm_phase1_iterative(&data, &argvals, &config).unwrap();

        // Should converge before max_iterations on clean data
        assert!(
            result.n_iterations < 20,
            "Should converge before max iterations on clean data, took {}",
            result.n_iterations
        );
    }

    // ── Upgrade tests: Kaiser ncomp ─────────────────────────────────────────

    #[test]
    fn test_kaiser_ncomp_known_spectrum() {
        use crate::spm::ncomp::{select_ncomp, NcompMethod};
        // Eigenvalues: [10, 5, 1, 0.1, 0.01]  mean = 3.222
        // Components > mean: 10, 5 → should retain 2
        let eigenvalues = vec![10.0, 5.0, 1.0, 0.1, 0.01];
        let k = select_ncomp(&eigenvalues, &NcompMethod::Kaiser).unwrap();
        assert_eq!(k, 2, "Kaiser should retain 2 components for this spectrum");
    }

    #[test]
    fn test_kaiser_ncomp_flat_spectrum() {
        use crate::spm::ncomp::{select_ncomp, NcompMethod};
        // All equal → all equal to mean → none strictly above → fallback to 1
        let eigenvalues = vec![1.0, 1.0, 1.0, 1.0];
        let k = select_ncomp(&eigenvalues, &NcompMethod::Kaiser).unwrap();
        assert_eq!(k, 1, "Kaiser with flat spectrum should return 1");
    }

    #[test]
    fn test_kaiser_ncomp_single() {
        use crate::spm::ncomp::{select_ncomp, NcompMethod};
        let eigenvalues = vec![5.0];
        let k = select_ncomp(&eigenvalues, &NcompMethod::Kaiser).unwrap();
        assert_eq!(k, 1, "Kaiser with single eigenvalue should return 1");
    }

    // ── Upgrade tests: MFPCA relative threshold ─────────────────────────────

    #[test]
    fn test_mfpca_relative_threshold() {
        // Test that MFPCA works correctly with variables of very different scales
        let n = 20;
        let m1 = 10;
        let m2 = 10;
        let t1 = uniform_grid(m1);
        let t2 = uniform_grid(m2);

        // Variable 1: large scale
        let mut var1 = FdMatrix::zeros(n, m1);
        for i in 0..n {
            let phase = i as f64 * 0.3;
            for j in 0..m1 {
                var1[(i, j)] = 1000.0 * (2.0 * PI * t1[j] + phase).sin() + 5000.0;
            }
        }

        // Variable 2: tiny scale (but not zero)
        let mut var2 = FdMatrix::zeros(n, m2);
        for i in 0..n {
            let phase = i as f64 * 0.2;
            for j in 0..m2 {
                var2[(i, j)] = 0.001 * (2.0 * PI * t2[j] + phase).cos() + 0.005;
            }
        }

        let config = MfpcaConfig {
            ncomp: 3,
            weighted: true,
        };
        let result = mfpca(&[&var1, &var2], &config).unwrap();
        // Should have 3 or fewer components
        assert!(result.eigenvalues.len() <= 3);
        // Scale threshold should be relative to max scale
        assert!(result.scales[0] > result.scales[1]);
    }

    // ── Upgrade tests: SPE moment-match diagnostic ──────────────────────────

    #[test]
    fn test_spe_moment_match_diagnostic_adequate() {
        // Generate chi-squared-like data (exponential-ish)
        let n = 200;
        let spe: Vec<f64> = (0..n)
            .map(|i| {
                let x = (i as f64 * 2654435761.0) % 100.0;
                (x / 10.0).max(0.1)
            })
            .collect();
        let result = spe_moment_match_diagnostic(&spe);
        assert!(result.is_ok());
        let (kurtosis, theoretical, _adequate) = result.unwrap();
        // Just check it returns finite values
        assert!(kurtosis.is_finite());
        assert!(theoretical.is_finite());
    }

    #[test]
    fn test_spe_moment_match_diagnostic_too_few() {
        let spe = vec![1.0, 2.0, 3.0]; // < 4
        assert!(spe_moment_match_diagnostic(&spe).is_err());
    }

    // ── Upgrade tests: Bootstrap median quantile ────────────────────────────

    #[test]
    fn test_bootstrap_quantile_convergence() {
        use crate::spm::bootstrap::{t2_limit_robust, ControlLimitMethod};
        // For a large sample from chi2(5), bootstrap should converge near parametric
        let n = 500;
        let values: Vec<f64> = (0..n)
            .map(|i| {
                // Pseudo chi2(5): sum of 5 squared normals (approximated)
                let mut sum = 0.0;
                for k in 0..5 {
                    let z =
                        ((i as u64 * 48271 + k as u64 * 2654435761) % 10000) as f64 / 5000.0 - 1.0;
                    sum += z * z;
                }
                sum
            })
            .collect();

        let parametric =
            t2_limit_robust(&values, 5, 0.05, &ControlLimitMethod::Parametric).unwrap();
        let bootstrap = t2_limit_robust(
            &values,
            5,
            0.05,
            &ControlLimitMethod::Bootstrap {
                n_bootstrap: 1000,
                seed: 42,
            },
        )
        .unwrap();

        // Bootstrap should be in a reasonable range (not identical, but same ballpark)
        assert!(bootstrap.ucl > 0.0, "Bootstrap UCL should be positive");
        assert!(parametric.ucl > 0.0, "Parametric UCL should be positive");
    }

    // ── Upgrade tests: Partial domain condition number ──────────────────────

    #[test]
    fn test_partial_monitoring_well_conditioned() {
        use crate::spm::partial::{spm_monitor_partial, DomainCompletion, PartialDomainConfig};
        let n = 40;
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(n, m, 42);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        // Monitor with half-domain observation (should not crash even with regularization)
        let partial_vals: Vec<f64> = (0..m).map(|j| data[(0, j)]).collect();
        let partial_config = PartialDomainConfig {
            ncomp: 3,
            alpha: 0.05,
            completion: DomainCompletion::ConditionalExpectation,
            ..PartialDomainConfig::default()
        };
        let result = spm_monitor_partial(&chart, &partial_vals, &argvals, m / 2, &partial_config);
        assert!(result.is_ok(), "Partial monitoring should succeed");
        let r = result.unwrap();
        assert!(r.domain_fraction > 0.0 && r.domain_fraction < 1.0);
    }

    // ── Upgrade tests: MEWMA eigenvalue guard ───────────────────────────────

    #[test]
    fn test_mewma_eigenvalue_guard() {
        use crate::spm::mewma::{spm_mewma_monitor, MewmaConfig};
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(40, m, 42);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        // Normal usage should succeed
        let new_data = generate_ic_data(10, m, 99);
        let mewma_config = MewmaConfig {
            lambda: 0.2,
            ncomp: 3,
            alpha: 0.05,
            asymptotic: true,
        };
        let result = spm_mewma_monitor(&chart, &new_data, &argvals, &mewma_config);
        assert!(result.is_ok());
    }

    // ── Upgrade tests: FRCC R-squared diagnostic ────────────────────────────

    #[test]
    fn test_frcc_low_r_squared_rejected() {
        // Predictors that are pure noise → R² ≈ 0 → should fail
        let n = 30;
        let m = 15;
        let argvals = uniform_grid(m);
        let y = generate_ic_data(n, m, 42);

        // Random predictors with no relationship to y
        let mut predictors = FdMatrix::zeros(n, 2);
        for i in 0..n {
            predictors[(i, 0)] = ((i as u64).wrapping_mul(48271) % 10000) as f64 / 10000.0;
            predictors[(i, 1)] = ((i as u64).wrapping_mul(2654435761) % 10000) as f64 / 10000.0;
        }

        let config = FrccConfig {
            ncomp: 2,
            fosr_lambda: 1e-4,
            alpha: 0.05,
            tuning_fraction: 0.5,
            seed: 42,
            ..FrccConfig::default()
        };
        let result = frcc_phase1(&y, &predictors, &argvals, &config);
        // Should either fail with low R² or produce a result with low R²
        // The exact behavior depends on the data, but the check is in place
        if let Ok(chart) = &result {
            assert!(
                chart.fosr_r_squared >= 0.1,
                "If chart was built, R² should be >= 0.1"
            );
        }
    }

    // ── Upgrade tests: MFPCA T² contributions ───────────────────────────────

    #[test]
    fn test_t2_contributions_mfpca_sums() {
        let n = 20;
        let m1 = 10;
        let m2 = 10;
        let t1 = uniform_grid(m1);
        let t2_grid = uniform_grid(m2);

        let mut var1 = FdMatrix::zeros(n, m1);
        let mut var2 = FdMatrix::zeros(n, m2);
        for i in 0..n {
            let phase = i as f64 * 0.3;
            for j in 0..m1 {
                var1[(i, j)] = (2.0 * PI * t1[j] + phase).sin();
            }
            for j in 0..m2 {
                var2[(i, j)] = (2.0 * PI * t2_grid[j] + phase * 0.5).cos();
            }
        }

        let mfpca_config = MfpcaConfig {
            ncomp: 3,
            weighted: true,
        };
        let mfpca_result = mfpca(&[&var1, &var2], &mfpca_config).unwrap();
        let _ncomp = mfpca_result.eigenvalues.len();

        // Compute T² and contributions
        let t2_vals = hotelling_t2(&mfpca_result.scores, &mfpca_result.eigenvalues).unwrap();
        let contrib = t2_contributions_mfpca(&mfpca_result.scores, &mfpca_result).unwrap();

        // Per-variable contributions should sum to T² for each observation
        for i in 0..n {
            let row_sum: f64 = (0..2).map(|v| contrib[(i, v)]).sum();
            assert!(
                (row_sum - t2_vals[i]).abs() < 1e-8 * (1.0 + t2_vals[i].abs()),
                "MFPCA T² contribution row sum ({}) should match T² ({})",
                row_sum,
                t2_vals[i]
            );
        }
    }

    // ── Upgrade tests: AMEWMA time-dependent covariance ─────────────────────

    #[test]
    fn test_amewma_time_dependent_covariance() {
        use crate::spm::amewma::{spm_amewma_monitor, AmewmaConfig};
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(40, m, 42);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let new_data = generate_ic_data(20, m, 99);
        let amewma_config = AmewmaConfig {
            lambda_min: 0.05,
            lambda_max: 0.95,
            lambda_init: 0.2,
            eta: 0.1,
            ncomp: 3,
            alpha: 0.05,
            error_clip: None,
        };
        let result = spm_amewma_monitor(&chart, &new_data, &argvals, &amewma_config).unwrap();

        // T² should be finite and non-negative
        for &t2 in &result.t2_statistic {
            assert!(
                t2.is_finite() && t2 >= 0.0,
                "T² should be finite and non-negative, got {t2}"
            );
        }
        // Lambda values should be within bounds
        for &lam in &result.lambda_t {
            assert!(
                lam >= amewma_config.lambda_min && lam <= amewma_config.lambda_max,
                "Lambda {lam} should be in [{}, {}]",
                amewma_config.lambda_min,
                amewma_config.lambda_max
            );
        }
    }

    // ── Upgrade tests: Profile effective sample size ────────────────────────

    #[test]
    fn test_profile_effective_n_windows() {
        use crate::spm::profile::{profile_phase1, ProfileMonitorConfig};
        let n = 60;
        let m = 10;
        let p = 2;
        let argvals = uniform_grid(m);

        let y = generate_ic_data(n, m, 42);
        let mut predictors = FdMatrix::zeros(n, p);
        for i in 0..n {
            for j in 0..p {
                predictors[(i, j)] =
                    ((i as u64 * (j as u64 + 1)).wrapping_mul(48271) % 10000) as f64 / 10000.0;
            }
        }

        // Non-overlapping windows
        let config_no_overlap = ProfileMonitorConfig {
            fosr_lambda: 1e-4,
            ncomp: 2,
            alpha: 0.05,
            window_size: 10,
            step_size: 10,
        };
        let chart1 = profile_phase1(&y, &predictors, &argvals, &config_no_overlap).unwrap();

        // Overlapping windows (step=1)
        let config_overlap = ProfileMonitorConfig {
            fosr_lambda: 1e-4,
            ncomp: 2,
            alpha: 0.05,
            window_size: 10,
            step_size: 1,
        };
        let chart2 = profile_phase1(&y, &predictors, &argvals, &config_overlap).unwrap();

        // Non-overlapping: effective should be positive and bounded
        assert!(
            chart1.effective_n_windows >= 2.0,
            "Non-overlapping: effective={} should be >= 2",
            chart1.effective_n_windows
        );

        // Overlapping: should also be positive and bounded
        assert!(
            chart2.effective_n_windows >= 2.0,
            "Overlapping: effective={} should be >= 2",
            chart2.effective_n_windows
        );

        // Lag-1 autocorrelation should be in [-1, 1]
        assert!(
            chart1.lag1_autocorrelation >= -1.0 && chart1.lag1_autocorrelation <= 1.0,
            "lag1_autocorrelation={} should be in [-1, 1]",
            chart1.lag1_autocorrelation
        );
        assert!(
            chart2.lag1_autocorrelation >= -1.0 && chart2.lag1_autocorrelation <= 1.0,
            "lag1_autocorrelation={} should be in [-1, 1]",
            chart2.lag1_autocorrelation
        );
    }

    // ── Round 2 upgrade tests ───────────────────────────────────────────────

    #[test]
    fn test_ncomp_elbow_smoothed() {
        use crate::spm::ncomp::{select_ncomp, NcompMethod};
        // Noisy spectrum where smoothing helps: [100, 50, 48, 2, 1, 0.5]
        // Without smoothing, elbow could be at index 1 or 2 (noise)
        // With smoothing, the elbow at the 48→2 drop is clearer
        let eigenvalues = vec![100.0, 50.0, 48.0, 2.0, 1.0, 0.5];
        let k = select_ncomp(&eigenvalues, &NcompMethod::Elbow).unwrap();
        assert!(
            (2..=4).contains(&k),
            "Smoothed elbow should find ~3, got {k}"
        );
    }

    #[test]
    fn test_hotelling_t2_regularized() {
        use crate::spm::stats::hotelling_t2_regularized;
        let scores = FdMatrix::from_column_major(vec![1.0, 2.0, 3.0, 4.0], 2, 2).unwrap();
        // Very small eigenvalue that would cause numerical issues
        let eigenvalues = vec![1.0, 1e-20];
        // Without regularization, T² would be huge for the second component
        let t2_reg = hotelling_t2_regularized(&scores, &eigenvalues, 1e-6).unwrap();
        assert!(t2_reg[0].is_finite(), "Regularized T² should be finite");
        assert!(t2_reg[1].is_finite(), "Regularized T² should be finite");
        // With floor=1e-6, second component contributes score²/1e-6
        let expected_0 = 1.0 / 1.0 + 3.0 * 3.0 / 1e-6;
        assert!(
            (t2_reg[0] - expected_0).abs() < 1e-3,
            "Expected {expected_0}, got {}",
            t2_reg[0]
        );
    }

    #[test]
    fn test_ewma_exact_covariance() {
        use crate::spm::ewma::{spm_ewma_monitor, EwmaConfig};
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(40, m, 42);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();
        let new_data = generate_ic_data(10, m, 99);

        let config_asymptotic = EwmaConfig {
            lambda: 0.2,
            ncomp: 3,
            alpha: 0.05,
            exact_covariance: false,
        };
        let config_exact = EwmaConfig {
            lambda: 0.2,
            ncomp: 3,
            alpha: 0.05,
            exact_covariance: true,
        };

        let r_asym = spm_ewma_monitor(&chart, &new_data, &argvals, &config_asymptotic).unwrap();
        let r_exact = spm_ewma_monitor(&chart, &new_data, &argvals, &config_exact).unwrap();

        // Exact T² should be >= asymptotic T² (startup correction inflates early)
        assert!(
            r_exact.t2[0] >= r_asym.t2[0] - 1e-10,
            "Exact T²[0]={} should be >= asymptotic T²[0]={}",
            r_exact.t2[0],
            r_asym.t2[0]
        );

        // Later values should converge
        let last = r_asym.t2.len() - 1;
        let ratio = r_exact.t2[last] / r_asym.t2[last].max(1e-15);
        assert!(
            (ratio - 1.0).abs() < 0.1,
            "Late T² should converge: exact={}, asymptotic={}",
            r_exact.t2[last],
            r_asym.t2[last]
        );
    }

    #[test]
    fn test_cusum_restart_config() {
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(40, m, 42);
        let chart_config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &chart_config).unwrap();

        let shifted = generate_oc_data(20, m, 3.0, 99);

        // Without restart
        let config_no_restart = CusumConfig {
            restart: false,
            ..CusumConfig::default()
        };
        let r1 = spm_cusum_monitor(&chart, &shifted, &argvals, &config_no_restart).unwrap();

        // With restart via config
        let config_restart = CusumConfig {
            restart: true,
            ..CusumConfig::default()
        };
        let r2 = spm_cusum_monitor(&chart, &shifted, &argvals, &config_restart).unwrap();

        // Both should produce results; restart may have different alarm patterns
        assert_eq!(r1.cusum_statistic.len(), r2.cusum_statistic.len());
    }

    #[test]
    fn test_phase1_sample_size_adequate() {
        let m = 20;
        let argvals = uniform_grid(m);

        // Small dataset: n=8, ncomp=5 → 8 < 10*5 → not adequate
        let small_data = generate_ic_data(8, m, 42);
        let config = SpmConfig {
            ncomp: 5,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&small_data, &argvals, &config).unwrap();
        assert!(
            !chart.sample_size_adequate,
            "n=8, ncomp=5 should be inadequate"
        );

        // Large dataset: n=60, ncomp=3 → 60 >= 30 → adequate
        let large_data = generate_ic_data(60, m, 42);
        let config2 = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart2 = spm_phase1(&large_data, &argvals, &config2).unwrap();
        assert!(
            chart2.sample_size_adequate,
            "n=60, ncomp=3 should be adequate"
        );
    }

    #[test]
    fn test_iterative_removal_rates() {
        let n = 50;
        let m = 20;
        let argvals = uniform_grid(m);

        let mut data = generate_ic_data(n, m, 42);
        // Add strong outliers
        for j in 0..m {
            data[(0, j)] += 50.0;
            data[(1, j)] += 50.0;
        }

        let config = IterativePhase1Config {
            spm: SpmConfig {
                ncomp: 3,
                alpha: 0.01,
                ..SpmConfig::default()
            },
            ..IterativePhase1Config::default()
        };
        let result = spm_phase1_iterative(&data, &argvals, &config).unwrap();
        // Should have removal_rates recorded
        assert_eq!(
            result.removal_rates.len(),
            result.n_iterations,
            "Should have one removal rate per iteration"
        );
        // All rates should be in [0, 1]
        for &rate in &result.removal_rates {
            assert!(
                (0.0..=1.0).contains(&rate),
                "Removal rate {rate} out of range"
            );
        }
    }

    #[test]
    fn test_amewma_error_clipping() {
        use crate::spm::amewma::{spm_amewma_monitor, AmewmaConfig};
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(40, m, 42);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        // Data with an outlier
        let mut new_data = generate_ic_data(10, m, 99);
        for j in 0..m {
            new_data[(5, j)] += 100.0; // extreme outlier
        }

        // Without clipping
        let config_no_clip = AmewmaConfig {
            error_clip: None,
            ..AmewmaConfig::default()
        };
        let r1 = spm_amewma_monitor(&chart, &new_data, &argvals, &config_no_clip).unwrap();

        // With clipping at 9.0 (3σ)
        let config_clip = AmewmaConfig {
            error_clip: Some(9.0),
            ..AmewmaConfig::default()
        };
        let r2 = spm_amewma_monitor(&chart, &new_data, &argvals, &config_clip).unwrap();

        // Clipped lambda should change less dramatically
        let max_lambda_no_clip = r1.lambda_t.iter().cloned().fold(0.0_f64, f64::max);
        let max_lambda_clip = r2.lambda_t.iter().cloned().fold(0.0_f64, f64::max);
        // With clipping, max lambda should be constrained
        assert!(
            max_lambda_clip <= max_lambda_no_clip + 1e-10,
            "Clipped max lambda ({max_lambda_clip}) should be <= unclipped ({max_lambda_no_clip})"
        );
    }

    #[test]
    fn test_frcc_configurable_r_squared_threshold() {
        // With min_r_squared=0.0, even noise predictors should pass
        let n = 30;
        let m = 15;
        let argvals = uniform_grid(m);
        let y = generate_ic_data(n, m, 42);

        let mut predictors = FdMatrix::zeros(n, 2);
        for i in 0..n {
            predictors[(i, 0)] = ((i as u64).wrapping_mul(48271) % 10000) as f64 / 10000.0;
            predictors[(i, 1)] = ((i as u64).wrapping_mul(2654435761) % 10000) as f64 / 10000.0;
        }

        let config_strict = FrccConfig {
            min_r_squared: 0.5, // Very strict
            ..FrccConfig::default()
        };
        let result_strict = frcc_phase1(&y, &predictors, &argvals, &config_strict);
        // Noise predictors should fail with strict threshold
        assert!(
            result_strict.is_err(),
            "Strict R² threshold should reject noise predictors"
        );

        let config_lax = FrccConfig {
            min_r_squared: 0.0, // Accept anything
            ..FrccConfig::default()
        };
        let result_lax = frcc_phase1(&y, &predictors, &argvals, &config_lax);
        // Should pass with lax threshold
        assert!(
            result_lax.is_ok(),
            "Zero R² threshold should accept any predictors"
        );
    }

    #[test]
    fn test_elastic_spm_alignment_residual() {
        use crate::spm::elastic_spm::{elastic_spm_phase1, ElasticSpmConfig};
        let n = 30;
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(n, m, 42);

        let config = ElasticSpmConfig::default();
        let chart = elastic_spm_phase1(&data, &argvals, &config).unwrap();

        // Alignment residual should be non-negative and finite
        assert!(
            chart.mean_alignment_residual >= 0.0 && chart.mean_alignment_residual.is_finite(),
            "mean_alignment_residual={} should be non-negative and finite",
            chart.mean_alignment_residual
        );
    }

    #[test]
    fn test_partial_configurable_regularization() {
        use crate::spm::partial::{spm_monitor_partial, PartialDomainConfig};
        let n = 40;
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(n, m, 42);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();
        let partial_vals: Vec<f64> = (0..m).map(|j| data[(0, j)]).collect();

        // Default regularization
        let config1 = PartialDomainConfig::default();
        let r1 = spm_monitor_partial(&chart, &partial_vals, &argvals, m / 2, &config1).unwrap();

        // Stronger regularization
        let config2 = PartialDomainConfig {
            regularization_eps: 1e-6,
            ..PartialDomainConfig::default()
        };
        let r2 = spm_monitor_partial(&chart, &partial_vals, &argvals, m / 2, &config2).unwrap();

        // Both should produce finite results
        assert!(r1.t2.is_finite(), "Default reg T² should be finite");
        assert!(r2.t2.is_finite(), "Strong reg T² should be finite");
    }

    // ── Round 3 upgrade tests ────────────────────────────────────────────

    #[test]
    fn test_chi2_quantile_high_df() {
        // Verify chi2_quantile doesn't hang or produce NaN for large df
        // (blocker: 1e-300 tolerance in Lentz algorithm could cause infinite loops)
        use crate::spm::chi_squared::{chi2_cdf, chi2_quantile};
        for &k in &[50, 100, 200] {
            let q = chi2_quantile(0.95, k);
            assert!(
                q.is_finite(),
                "chi2_quantile(0.95, {k}) should be finite, got {q}"
            );
            assert!(q > 0.0, "chi2_quantile(0.95, {k}) should be positive");
            // Round-trip: CDF of the quantile should be close to 0.95
            let p = chi2_cdf(q, k);
            assert!(
                (p - 0.95).abs() < 0.01,
                "Round-trip failed for k={k}: q={q}, cdf(q)={p}"
            );
        }
    }

    #[test]
    fn test_mewma_spe_present() {
        // Verify MEWMA now returns SPE statistics
        use crate::spm::mewma::{spm_mewma_monitor, MewmaConfig};
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(40, m, 42);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let monitor_data = generate_ic_data(15, m, 99);
        let mewma_config = MewmaConfig {
            lambda: 0.2,
            ncomp: 3,
            alpha: 0.05,
            asymptotic: true,
        };
        let result = spm_mewma_monitor(&chart, &monitor_data, &argvals, &mewma_config).unwrap();

        // SPE fields should be populated and consistent
        assert_eq!(
            result.spe.len(),
            15,
            "SPE should have one value per observation"
        );
        assert!(result.spe_limit > 0.0, "SPE limit should be positive");
        assert_eq!(
            result.spe_alarm.len(),
            15,
            "SPE alarm should match observations"
        );
        // In-control data: most SPE values should be below the limit
        let n_spe_alarm: usize = result.spe_alarm.iter().filter(|&&a| a).count();
        assert!(
            n_spe_alarm < 10,
            "In-control data should have few SPE alarms"
        );
    }

    #[test]
    fn test_cusum_spe_present() {
        // Verify CUSUM now returns SPE statistics
        use crate::spm::cusum::{spm_cusum_monitor, CusumConfig};
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(40, m, 42);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let monitor_data = generate_ic_data(15, m, 99);
        let cusum_config = CusumConfig {
            ncomp: 3,
            ..CusumConfig::default()
        };
        let result = spm_cusum_monitor(&chart, &monitor_data, &argvals, &cusum_config).unwrap();

        assert_eq!(
            result.spe.len(),
            15,
            "SPE should have one value per observation"
        );
        assert!(result.spe_limit > 0.0, "SPE limit should be positive");
        assert_eq!(
            result.spe_alarm.len(),
            15,
            "SPE alarm should match observations"
        );
    }

    #[test]
    fn test_t2_pc_significance_bonferroni() {
        use crate::spm::contrib::{t2_pc_contributions, t2_pc_significance};
        // 5 obs, 3 PCs; PC1 has huge contribution, others small
        let scores = FdMatrix::from_column_major(
            vec![
                10.0, 0.1, 0.1, 0.1, 0.1, // PC1: one outlier
                0.1, 0.1, 0.1, 0.1, 0.1, // PC2: all small
                0.1, 0.1, 0.1, 0.1, 0.1, // PC3: all small
            ],
            5,
            3,
        )
        .unwrap();
        let eigenvalues = vec![1.0, 1.0, 1.0];
        let contribs = t2_pc_contributions(&scores, &eigenvalues).unwrap();
        let sig = t2_pc_significance(&contribs, 0.05).unwrap();

        // Obs 0, PC1 contribution = 100.0 >> chi2(1, 0.983) ≈ 5.73
        assert_eq!(
            sig[(0, 0)],
            1.0,
            "Large PC1 contribution should be significant"
        );
        // Small contributions should not be significant
        assert_eq!(
            sig[(1, 0)],
            0.0,
            "Small contribution should not be significant"
        );
        assert_eq!(
            sig[(0, 1)],
            0.0,
            "Small PC2 contribution should not be significant"
        );
    }

    #[test]
    fn test_partial_cholesky_retry() {
        // Test that partial monitoring handles near-singular systems gracefully
        use crate::spm::partial::{spm_monitor_partial, PartialDomainConfig};
        let n = 40;
        let m = 30;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(n, m, 42);
        let config = SpmConfig {
            ncomp: 5,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        // Observe only 3 grid points — very small observed domain
        let partial_vals: Vec<f64> = (0..m).map(|j| data[(0, j)]).collect();
        let partial_config = PartialDomainConfig {
            ncomp: 5,
            ..PartialDomainConfig::default()
        };
        let result = spm_monitor_partial(&chart, &partial_vals, &argvals, 3, &partial_config);
        // Should succeed (Cholesky retry handles ill-conditioning)
        assert!(
            result.is_ok(),
            "Partial monitoring with 3 obs should succeed: {:?}",
            result.err()
        );
        let r = result.unwrap();
        assert!(r.t2.is_finite(), "T² should be finite");
    }

    #[test]
    fn test_frcc_uses_shared_split_indices() {
        // Verify FRCC produces valid results (using shared split_indices from phase.rs)
        use crate::spm::frcc::{frcc_phase1, FrccConfig};
        let n = 60;
        let m = 20;
        let argvals = uniform_grid(m);
        let y_curves = generate_ic_data(n, m, 42);

        // Create simple predictors
        let mut pred_data = vec![0.0; n];
        for i in 0..n {
            pred_data[i] = (i as f64) / n as f64;
        }
        let predictors = FdMatrix::from_column_major(pred_data, n, 1).unwrap();

        let config = FrccConfig {
            min_r_squared: 0.0, // Allow any R² for this test
            ..FrccConfig::default()
        };
        let result = frcc_phase1(&y_curves, &predictors, &argvals, &config);
        // Should produce a valid chart (not crash from missing functions)
        assert!(result.is_ok(), "FRCC should succeed: {:?}", result.err());
    }

    #[test]
    fn test_pcg_rng_determinism() {
        // Verify the PCG-based split is deterministic (same seed → same split)
        let (tune1, cal1) = crate::spm::phase::split_indices(100, 0.5, 42);
        let (tune2, cal2) = crate::spm::phase::split_indices(100, 0.5, 42);
        assert_eq!(tune1, tune2, "Same seed should produce same tuning set");
        assert_eq!(cal1, cal2, "Same seed should produce same calibration set");

        // Different seeds should produce different splits
        let (tune3, _) = crate::spm::phase::split_indices(100, 0.5, 99);
        assert_ne!(
            tune1, tune3,
            "Different seeds should produce different splits"
        );
    }

    // ── Round 4 validation tests ─────────────────────────────────────────────

    #[test]
    fn test_amewma_error_clip_positive_validation() {
        // error_clip must be positive when set
        let n = 60;
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(n, m, 42);
        let config = SpmConfig::default();
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        let new_data = generate_ic_data(10, m, 99);
        let amewma_config = AmewmaConfig {
            error_clip: Some(-1.0),
            ..AmewmaConfig::default()
        };
        let result = spm_amewma_monitor(&chart, &new_data, &argvals, &amewma_config);
        assert!(result.is_err(), "Negative error_clip should be rejected");
        let err_msg = format!("{:?}", result.unwrap_err());
        assert!(
            err_msg.contains("error_clip") && err_msg.contains("positive"),
            "Error should mention error_clip must be positive: {err_msg}"
        );

        // Zero should also be rejected
        let amewma_config_zero = AmewmaConfig {
            error_clip: Some(0.0),
            ..AmewmaConfig::default()
        };
        let result2 = spm_amewma_monitor(&chart, &new_data, &argvals, &amewma_config_zero);
        assert!(result2.is_err(), "Zero error_clip should be rejected");
    }

    #[test]
    fn test_amewma_q_prev_clamping_stability() {
        // Test that the adaptive weight doesn't diverge even with extreme data
        let n = 60;
        let m = 20;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(n, m, 42);
        let config = SpmConfig::default();
        let chart = spm_phase1(&data, &argvals, &config).unwrap();

        // Create data with a massive spike to stress the adaptive weight
        let mut new_data = generate_ic_data(20, m, 99);
        for j in 0..m {
            new_data[(5, j)] *= 100.0; // huge spike at observation 5
        }

        let amewma_config = AmewmaConfig {
            eta: 0.5, // fast adaptation to amplify the effect
            ..AmewmaConfig::default()
        };
        let result = spm_amewma_monitor(&chart, &new_data, &argvals, &amewma_config);
        assert!(
            result.is_ok(),
            "AMEWMA should handle extreme data: {:?}",
            result.err()
        );
        let res = result.unwrap();
        // All lambda_t must be within [lambda_min, lambda_max]
        for (t, &lam) in res.lambda_t.iter().enumerate() {
            assert!(
                lam >= amewma_config.lambda_min && lam <= amewma_config.lambda_max,
                "lambda_t[{t}] = {lam} out of range [{}, {}]",
                amewma_config.lambda_min,
                amewma_config.lambda_max
            );
        }
        // T² values should all be finite
        for (t, &t2) in res.t2_statistic.iter().enumerate() {
            assert!(t2.is_finite(), "t2_statistic[{t}] is not finite: {t2}");
        }
    }

    #[test]
    fn test_nelson7_closed_interval() {
        use crate::spm::rules::{evaluate_rules, ChartRule};
        // 15 points exactly at center ± 1σ boundary should trigger Nelson7
        // (closed interval: <= sigma, not < sigma)
        let center = 10.0;
        let sigma = 2.0;
        // All values exactly at center + sigma
        let values: Vec<f64> = vec![center + sigma; 15];
        let violations = evaluate_rules(&values, center, sigma, &[ChartRule::Nelson7]).unwrap();
        assert!(
            !violations.is_empty(),
            "Nelson7 should trigger for points exactly at 1σ boundary (closed interval)"
        );
        assert_eq!(violations[0].rule, ChartRule::Nelson7);
    }

    #[test]
    fn test_nelson7_just_outside_no_trigger() {
        use crate::spm::rules::{evaluate_rules, ChartRule};
        // 15 points just outside 1σ should NOT trigger Nelson7
        let center = 10.0;
        let sigma = 2.0;
        let values: Vec<f64> = vec![center + sigma + 0.001; 15];
        let violations = evaluate_rules(&values, center, sigma, &[ChartRule::Nelson7]).unwrap();
        assert!(
            violations.is_empty(),
            "Nelson7 should NOT trigger for points just outside 1σ boundary"
        );
    }

    #[test]
    fn test_phase1_small_tuning_set_error() {
        // Very small data with high tuning_fraction should fail gracefully
        let m = 10;
        let argvals = uniform_grid(m);
        let data = generate_ic_data(4, m, 42); // only 4 observations
        let config = SpmConfig {
            tuning_fraction: 0.25, // ~1 obs for tuning, too few
            ..SpmConfig::default()
        };
        let result = spm_phase1(&data, &argvals, &config);
        assert!(result.is_err(), "Should fail with tiny tuning set");
        let err_msg = format!("{:?}", result.unwrap_err());
        assert!(
            err_msg.contains("tuning") || err_msg.contains("3"),
            "Error should mention tuning set size: {err_msg}"
        );
    }

    #[test]
    fn test_profile_window_size_vs_predictors() {
        use crate::spm::profile::{profile_phase1, ProfileMonitorConfig};
        let n = 100;
        let m = 15;
        let argvals = uniform_grid(m);
        let y_curves = generate_ic_data(n, m, 42);

        // 3 predictors: window_size must be >= 5 (p + 2)
        let mut pred_data = vec![0.0; n * 3];
        for i in 0..n {
            pred_data[i] = (i as f64) / n as f64;
            pred_data[n + i] = ((i as f64) / n as f64).powi(2);
            pred_data[2 * n + i] = ((i * 7 + 3) as f64 % 10.0) / 10.0;
        }
        let predictors = FdMatrix::from_column_major(pred_data, n, 3).unwrap();

        // window_size = 4 < p + 2 = 5 should fail
        let config = ProfileMonitorConfig {
            window_size: 4,
            step_size: 4,
            ncomp: 2,
            ..ProfileMonitorConfig::default()
        };
        let result = profile_phase1(&y_curves, &predictors, &argvals, &config);
        assert!(result.is_err(), "window_size < p+2 should fail");
        let err_msg = format!("{:?}", result.unwrap_err());
        assert!(
            err_msg.contains("window_size") && err_msg.contains("p + 2"),
            "Error should mention window_size and p+2: {err_msg}"
        );
    }

    #[test]
    fn test_t2_pc_significance_extreme_scores() {
        use crate::spm::contrib::{t2_pc_contributions, t2_pc_significance};
        // Build scores where PC1 has a huge value but PC2 is small
        let scores = FdMatrix::from_column_major(
            vec![
                100.0, 0.5, 0.3, // col 0 (PC1): obs0=100, obs1=0.5, obs2=0.3
                0.1, 0.2, 0.1, // col 1 (PC2): all small
            ],
            3,
            2,
        )
        .unwrap();
        let eigenvalues = [1.0, 1.0];
        let contribs = t2_pc_contributions(&scores, &eigenvalues).unwrap();

        // Significance at α = 0.05: Bonferroni → test each PC at α/2 = 0.025
        let sig = t2_pc_significance(&contribs, 0.05).unwrap();
        let (n, ncomp) = sig.shape();
        assert_eq!(n, 3);
        assert_eq!(ncomp, 2);

        // Observation 0, PC1 (score²=10000) should be significant
        assert_eq!(sig[(0, 0)], 1.0, "Large PC1 score should be significant");
        // Observation 0, PC2 (score²=0.01) should not be significant
        assert_eq!(
            sig[(0, 1)],
            0.0,
            "Small PC2 score should not be significant"
        );
        // Observations 1,2 PC1 (score²=0.25, 0.09) should not be significant
        assert_eq!(sig[(1, 0)], 0.0);
        assert_eq!(sig[(2, 0)], 0.0);
    }

    #[test]
    fn test_t2_pc_significance_alpha_bounds() {
        use crate::spm::contrib::t2_pc_significance;
        let contribs = FdMatrix::from_column_major(vec![1.0, 2.0], 2, 1).unwrap();
        assert!(t2_pc_significance(&contribs, 0.0).is_err());
        assert!(t2_pc_significance(&contribs, 1.0).is_err());
        assert!(t2_pc_significance(&contribs, -0.1).is_err());
        assert!(t2_pc_significance(&contribs, 0.05).is_ok());
    }

    // ── spm_monitor_from_fields tests ───────────────────────────────────────

    #[test]
    fn test_monitor_from_fields_matches_spm_monitor() {
        use crate::spm::phase::spm_monitor_from_fields;

        let m = 30;
        let argvals = uniform_grid(m);
        let ic_data = generate_ic_data(40, m, 42);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&ic_data, &argvals, &config).unwrap();

        let new_data = generate_ic_data(10, m, 99);

        let r1 = spm_monitor(&chart, &new_data, &argvals).unwrap();
        let r2 = spm_monitor_from_fields(
            &chart.fpca.mean,
            &chart.fpca.rotation,
            &chart.fpca.weights,
            &chart.eigenvalues,
            chart.t2_limit.ucl,
            chart.spe_limit.ucl,
            &new_data,
            &argvals,
        )
        .unwrap();

        assert_eq!(r1.t2.len(), r2.t2.len());
        assert_eq!(r1.spe.len(), r2.spe.len());
        for (a, b) in r1.t2.iter().zip(&r2.t2) {
            assert!((a - b).abs() < 1e-10, "T2 mismatch: {a} vs {b}");
        }
        for (a, b) in r1.spe.iter().zip(&r2.spe) {
            assert!((a - b).abs() < 1e-10, "SPE mismatch: {a} vs {b}");
        }
        assert_eq!(r1.t2_alarm, r2.t2_alarm);
        assert_eq!(r1.spe_alarm, r2.spe_alarm);
        assert_eq!(r1.scores.shape(), r2.scores.shape());
        let (n, nc) = r1.scores.shape();
        for i in 0..n {
            for k in 0..nc {
                assert!(
                    (r1.scores[(i, k)] - r2.scores[(i, k)]).abs() < 1e-10,
                    "Scores mismatch at ({i},{k})"
                );
            }
        }
    }

    #[test]
    fn test_monitor_from_fields_oc_data() {
        use crate::spm::phase::spm_monitor_from_fields;

        let m = 30;
        let argvals = uniform_grid(m);
        let ic_data = generate_ic_data(40, m, 42);
        let config = SpmConfig {
            ncomp: 3,
            alpha: 0.05,
            ..SpmConfig::default()
        };
        let chart = spm_phase1(&ic_data, &argvals, &config).unwrap();

        // Out-of-control data with large mean shift should trigger alarms
        let oc_data = generate_oc_data(5, m, 5.0, 123);
        let r1 = spm_monitor(&chart, &oc_data, &argvals).unwrap();
        let r2 = spm_monitor_from_fields(
            &chart.fpca.mean,
            &chart.fpca.rotation,
            &chart.fpca.weights,
            &chart.eigenvalues,
            chart.t2_limit.ucl,
            chart.spe_limit.ucl,
            &oc_data,
            &argvals,
        )
        .unwrap();

        // Results should match exactly
        assert_eq!(r1.t2_alarm, r2.t2_alarm);
        assert_eq!(r1.spe_alarm, r2.spe_alarm);
        // OC data should trigger at least one alarm
        let any_alarm = r2.t2_alarm.iter().any(|&a| a) || r2.spe_alarm.iter().any(|&a| a);
        assert!(any_alarm, "OC data with shift=5.0 should trigger alarms");
    }

    #[test]
    fn test_monitor_from_fields_dimension_errors() {
        use crate::spm::phase::spm_monitor_from_fields;

        let m = 10;
        let argvals = uniform_grid(m);
        let mean = vec![0.0; m];
        let rotation = FdMatrix::zeros(m, 2);
        let weights = vec![1.0; m];
        let eigenvalues = vec![1.0, 0.5];

        // Wrong number of columns
        let bad_data = FdMatrix::zeros(5, m + 1);
        assert!(spm_monitor_from_fields(
            &mean,
            &rotation,
            &weights,
            &eigenvalues,
            10.0,
            5.0,
            &bad_data,
            &argvals,
        )
        .is_err());

        // Wrong argvals length
        let data = FdMatrix::zeros(5, m);
        let bad_argvals = vec![0.0; m + 1];
        assert!(spm_monitor_from_fields(
            &mean,
            &rotation,
            &weights,
            &eigenvalues,
            10.0,
            5.0,
            &data,
            &bad_argvals,
        )
        .is_err());

        // Wrong rotation rows
        let bad_rotation = FdMatrix::zeros(m + 1, 2);
        assert!(spm_monitor_from_fields(
            &mean,
            &bad_rotation,
            &weights,
            &eigenvalues,
            10.0,
            5.0,
            &data,
            &argvals,
        )
        .is_err());

        // Wrong rotation cols
        let bad_rotation2 = FdMatrix::zeros(m, 3);
        assert!(spm_monitor_from_fields(
            &mean,
            &bad_rotation2,
            &weights,
            &eigenvalues,
            10.0,
            5.0,
            &data,
            &argvals,
        )
        .is_err());

        // Wrong weights length
        let bad_weights = vec![1.0; m + 1];
        assert!(spm_monitor_from_fields(
            &mean,
            &rotation,
            &bad_weights,
            &eigenvalues,
            10.0,
            5.0,
            &data,
            &argvals,
        )
        .is_err());
    }
}