symplex 0.34.0

Exact symbolic mathematics for Rust: calculus, summation, solving, linear algebra, transforms, compile-time dimensional analysis, and Rust/C code generation
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
//! Analysis of variance — one-way, factorial (two-way) and
//! repeated-measures — and the post-hoc pairwise comparisons that follow a
//! significant `F`.
//!
//! **Rule:** every ANOVA and every post-hoc procedure lives here; the
//! one-way `F` test moved in from `hypothesis` in 0.18.
//!
//! As in [`super::hypothesis`], everything that is a rational function of the
//! observations is **exact** ([`Q`]): sums of squares, mean squares, `F`,
//! `η²` and partial `η²`, the Greenhouse–Geisser and Huynh–Feldt `ε`,
//! Mauchly's `W`.  p-values are exact **expressions** ([`Ex`]) — the `F`
//! tail `I_{d₂/(d₂ + d₁F)}(d₂/2, d₁/2)` through `betainc_regularized`, a χ²
//! tail through `uppergamma` — that the caller evaluates with
//! [`Ex::eval_f64`] (or the `p_value_f64` helpers).  The studentized range
//! distribution behind Tukey's HSD has no closed form and is integrated
//! numerically in `f64` — each tail directly, to a relative `1e-14` down to
//! underflow — so those p-values and limits are `f64` and say so.
//!
//! | Function | Design | Reference |
//! |---|---|---|
//! | [`anova_one_way`] | `k` independent groups | `scipy.stats.f_oneway` |
//! | [`anova_two_way`] | `A × B` factorial with interaction, balanced or unbalanced; Type I, II or III sums of squares ([`SsType`]) | `statsmodels.stats.anova.anova_lm(ols('y ~ C(A) * C(B)').fit(), typ)` |
//! | [`anova_repeated_measures`] | one within-subject factor, `n` subjects × `k` conditions | `statsmodels.stats.anova.AnovaRM`; `pingouin.rm_anova(correction=True)`, `pingouin.epsilon`, `pingouin.sphericity` |
//! | [`tukey_hsd`] | all pairwise differences of `k` independent groups | `scipy.stats.tukey_hsd` |
//! | [`pairwise_t_tests`] | pairwise Welch tests with Bonferroni / Holm adjustment | `scipy.stats.ttest_ind` + `statsmodels.stats.multitest.multipletests` |
//!
//! ```
//! use symplex::prelude::*;
//! use symplex::linprog::q;
//! use symplex::stats::anova::{anova_two_way, TwoWayData};
//!
//! let ctx = Context::new();
//! // A 2 × 3 design, three replicates per cell: cells[a][b] = observations.
//! let data = TwoWayData::from_i64(&[
//!     &[&[4, 5, 6], &[6, 7, 8], &[9, 10, 12]],
//!     &[&[5, 5, 7], &[8, 9, 11], &[13, 14, 16]],
//! ])?;
//! // statsmodels: anova_lm(ols('y ~ C(A) * C(B)', df).fit(), typ=2)
//! //   C(A)       sum_sq 24.5      df 1   F 14.225806451612754  PR(>F) 0.00266347768868363
//! //   C(B)       sum_sq 148.7777… df 2   F 43.19354838709679   PR(>F) 3.29199072704e-06
//! //   C(A):C(B)  sum_sq 8.333333… df 2   F 2.4193548387096655  PR(>F) 0.13098893805732365
//! //   Residual   sum_sq 20.66666… df 12
//! let r = anova_two_way(&ctx, &data)?;
//! assert_eq!(r.factor_a.ss, q(49, 2));
//! assert_eq!(r.factor_a.f, Some(q(441, 31)));
//! assert_eq!(r.factor_b.ss, q(1339, 9));
//! assert_eq!(r.interaction.ss, q(25, 3));
//! assert_eq!((r.residual.ss, r.residual.df), (q(62, 3), 12));
//! assert!((r.factor_a.p_value_f64()? - 0.002_663_477_688_683_533_4).abs() < 1e-12);
//! # Ok::<(), SymplexError>(())
//! ```

use std::f64::consts::{LN_2, PI, SQRT_2};

use num_traits::{One, Signed, Zero};

use super::common::{
    check_confidence, check_unit_open, chi_squared_sf, ex, f_sf, f_sf_rational, invalid, qi, qu,
};
use super::data::{self, Q};
use super::hypothesis::{self, Alternative, PValue, TestResult, p_value_accessors};
use super::numdist::norm::{pdf as norm_pdf, sf as norm_sf};
use super::regression::ols;
use crate::api::context::Context;
use crate::api::expr::Ex;
use crate::base::errors::SymplexError;
use crate::base::interval::{Bounds, Interval};
use crate::base::numeric::ratio_to_f64;
use crate::domains::exact_matrix::QMatrix;
use crate::domains::optimize::{RootOpts, brent_root, grow_bracket};
use crate::output::codegen::numeric_rt::{erf, erfc};

// ═══════════════════════════════════════════════════════════════════════════
// Small helpers
// ═══════════════════════════════════════════════════════════════════════════

fn failed(op: &'static str, reason: impl Into<String>) -> SymplexError {
    SymplexError::computation_failed(op, reason)
}

fn to_f64(op: &'static str, q: &Q) -> Result<f64, SymplexError> {
    ratio_to_f64(q).ok_or_else(|| failed(op, format!("{q} does not fit in an f64")))
}

/// `Σ (xᵢ − x̄)²` of a non-empty slice (the caller guarantees non-emptiness;
/// an empty slice contributes `0`, which is the right value for a sum).
fn centred_ss(x: &[Q]) -> Q {
    data::sum_of_squares(x).unwrap_or_else(|_| Q::zero())
}

// ═══════════════════════════════════════════════════════════════════════════
// One-way ANOVA
// ═══════════════════════════════════════════════════════════════════════════

/// The between- and within-group sums of squares of a collection of
/// groups, with the total count and the number of groups.
pub(crate) struct GroupSums {
    /// `Σ nᵢ (x̄ᵢ − x̄)²`.
    pub(crate) ss_between: Q,
    /// `Σᵢ Σⱼ (xᵢⱼ − x̄ᵢ)²`.
    pub(crate) ss_within: Q,
    /// `N`, the total number of observations.
    pub(crate) n: usize,
    /// `k`, the number of groups.
    pub(crate) k: usize,
}

/// The sums of squares of `k ≥ 2` non-empty groups (shared with
/// [`eta_squared`](super::hypothesis::eta_squared)).
pub(crate) fn sums_of_squares(
    op: &'static str,
    groups: &[Vec<Q>],
) -> Result<GroupSums, SymplexError> {
    let k = groups.len();
    if k < 2 {
        return Err(invalid(op, "at least two groups are needed"));
    }
    if groups.iter().any(Vec::is_empty) {
        return Err(invalid(op, "every group must be non-empty"));
    }
    let all: Vec<Q> = groups.iter().flatten().cloned().collect();
    let grand = data::mean(&all)?;
    let mut ss_between = Q::zero();
    let mut ss_within = Q::zero();
    for g in groups {
        let m = data::mean(g)?;
        let d = &m - &grand;
        ss_between += qu(g.len()) * &d * &d;
        ss_within += data::sum_of_squares(g)?;
    }
    Ok(GroupSums {
        ss_between,
        ss_within,
        n: all.len(),
        k,
    })
}

/// The outcome of a one-way analysis of variance.
#[derive(Clone, Debug, PartialEq)]
pub struct AnovaResult {
    /// The `F` statistic `(SS_between/df_between) / (SS_within/df_within)`,
    /// exact.
    pub f: Q,
    /// `k − 1`.
    pub df_between: usize,
    /// `N − k`.
    pub df_within: usize,
    /// `P(F_{df_between, df_within} ≥ f)` as an exact expression.
    pub p_value: Ex,
    /// `Σ nᵢ (x̄ᵢ − x̄)²`.
    pub ss_between: Q,
    /// `Σᵢ Σⱼ (xᵢⱼ − x̄ᵢ)²`.
    pub ss_within: Q,
    /// `SS_between / SS_total`, the proportion of variance explained.
    pub eta_squared: Q,
}

impl AnovaResult {
    /// The p-value as an `f64`.
    ///
    /// # Errors
    ///
    /// Propagates the evaluation error of the expression.
    pub fn p_value_f64(&self) -> Result<f64, SymplexError> {
        self.p_value.eval_f64()
    }
}

impl PValue for AnovaResult {
    fn p_value_ex(&self) -> &Ex {
        &self.p_value
    }
}
p_value_accessors!(AnovaResult);

/// One-way analysis of variance of `k` independent groups:
/// `F = (SS_between/(k−1)) / (SS_within/(N−k))` with the sums of squares and
/// `F` exact, `η² = SS_between/SS_total`, and `P(F_{k−1, N−k} ≥ F)` as an
/// exact expression.  `scipy.stats.f_oneway(*groups)`.
///
/// ```
/// use symplex::prelude::*;
/// use symplex::linprog::q;
/// use symplex::stats::anova::anova_one_way;
/// use symplex::stats::data::from_i64;
///
/// let ctx = Context::new();
/// let g = [from_i64(&[6, 8, 4, 5, 3, 4]), from_i64(&[8, 12, 9, 11, 6, 8]), from_i64(&[13, 9, 11, 8, 7, 12])];
/// // scipy: f_oneway(*g) → statistic 9.264705882352942 (= 315/34), pvalue 0.0023987773293929083
/// let r = anova_one_way(&ctx, &g)?;
/// assert_eq!(r.f, q(315, 34));
/// assert_eq!((r.df_between, r.df_within), (2, 15));
/// assert!((r.p_value_f64()? - 0.002_398_777_329_392_908_3).abs() < 1e-12);
/// # Ok::<(), SymplexError>(())
/// ```
///
/// # Errors
///
/// [`SymplexError::InvalidArgument`] for fewer than two groups, an empty
/// group, `N ≤ k`, or zero within-group variance.
pub fn anova_one_way(ctx: &Context, groups: &[Vec<Q>]) -> Result<AnovaResult, SymplexError> {
    const OP: &str = "anova_one_way";
    let GroupSums {
        ss_between,
        ss_within,
        n,
        k,
    } = sums_of_squares(OP, groups)?;
    if n <= k {
        return Err(invalid(
            OP,
            "at least one group needs more than one observation",
        ));
    }
    if ss_within.is_zero() {
        return Err(invalid(OP, "the within-group variance is zero"));
    }
    let (df_between, df_within) = (k - 1, n - k);
    let f = (&ss_between / qu(df_between)) / (&ss_within / qu(df_within));
    let total = &ss_between + &ss_within;
    let eta_squared = &ss_between / &total;
    Ok(AnovaResult {
        p_value: f_sf(ctx, df_between, df_within, &f),
        f,
        df_between,
        df_within,
        ss_between,
        ss_within,
        eta_squared,
    })
}

// ═══════════════════════════════════════════════════════════════════════════
// Rows of an ANOVA table
// ═══════════════════════════════════════════════════════════════════════════

/// Which line of an ANOVA table a row is.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Source {
    /// The main effect of factor `A` (two-way).
    FactorA,
    /// The main effect of factor `B` (two-way).
    FactorB,
    /// The `A × B` interaction (two-way).
    Interaction,
    /// The within-subject factor (repeated measures).
    Conditions,
    /// Between-subject variation (repeated measures; not tested).
    Subjects,
    /// The error term the effects are tested against.
    Residual,
    /// The corrected total, `Σ (y − ȳ)²` with `N − 1` degrees of freedom.
    Total,
}

/// One row of an ANOVA table.  Effects carry an `F` test and effect sizes;
/// the residual row carries only its mean square; the total row only `ss`
/// and `df`.
#[derive(Clone, Debug, PartialEq)]
pub struct AnovaRow {
    /// Which line this is.
    pub source: Source,
    /// The sum of squares, exact.
    pub ss: Q,
    /// Degrees of freedom.
    pub df: usize,
    /// `ss / df` (`None` for the total row).
    pub ms: Option<Q>,
    /// `ms / ms_residual`, exact (effects only).
    pub f: Option<Q>,
    /// `P(F_{df, df_residual} ≥ f)` as an exact expression (effects only).
    pub p_value: Option<Ex>,
    /// `ss / ss_total` (effects only).
    pub eta_squared: Option<Q>,
    /// `ss / (ss + ss_residual)` (effects only).
    pub partial_eta_squared: Option<Q>,
}

impl AnovaRow {
    /// An effect row tested against the residual `(ss_resid, df_resid)`.
    fn effect(
        ctx: &Context,
        source: Source,
        ss: Q,
        df: usize,
        ss_resid: &Q,
        df_resid: usize,
        ss_total: &Q,
    ) -> Self {
        let test = FTest::of(ctx, &ss, df, ss_resid, df_resid);
        Self::with_test(source, ss, df, test, ss_resid, ss_total)
    }

    /// An effect row from an already computed `F` test.
    fn with_test(
        source: Source,
        ss: Q,
        df: usize,
        test: FTest,
        ss_resid: &Q,
        ss_total: &Q,
    ) -> Self {
        Self {
            source,
            eta_squared: Some(&ss / ss_total),
            partial_eta_squared: Some(&ss / (&ss + ss_resid)),
            ms: Some(&ss / qu(df)),
            ss,
            df,
            f: Some(test.f),
            p_value: Some(test.p_value),
        }
    }

    fn untested(source: Source, ss: Q, df: usize) -> Self {
        Self {
            source,
            ms: Some(&ss / qu(df)),
            ss,
            df,
            f: None,
            p_value: None,
            eta_squared: None,
            partial_eta_squared: None,
        }
    }

    fn total(ss: Q, df: usize) -> Self {
        Self {
            source: Source::Total,
            ss,
            df,
            ms: None,
            f: None,
            p_value: None,
            eta_squared: None,
            partial_eta_squared: None,
        }
    }

    /// The p-value as an `f64`.
    ///
    /// # Errors
    ///
    /// [`SymplexError::InvalidArgument`] if this row has no `F` test (the
    /// residual, subjects and total rows); otherwise the evaluation error of
    /// the expression (not expected).
    pub fn p_value_f64(&self) -> Result<f64, SymplexError> {
        self.tested_p_value("AnovaRow::p_value_f64")?.eval_f64()
    }

    /// `log10` of the p-value, finite even when
    /// [`p_value_f64`](Self::p_value_f64) underflows to `0.0`; see
    /// [`PValue`].  (`AnovaRow` cannot implement the trait itself: the
    /// residual and total rows have no p-value.)
    ///
    /// # Errors
    ///
    /// As [`p_value_f64`](Self::p_value_f64).
    pub fn p_value_log10(&self) -> Result<f64, SymplexError> {
        hypothesis::p_value_log10_of(self.tested_p_value("AnovaRow::p_value_log10")?)
    }

    /// `ln` of the p-value, evaluated as an expression; see [`PValue`].
    ///
    /// # Errors
    ///
    /// As [`p_value_f64`](Self::p_value_f64).
    pub fn p_value_ln(&self) -> Result<f64, SymplexError> {
        hypothesis::p_value_ln_of(self.tested_p_value("AnovaRow::p_value_ln")?)
    }

    /// The p-value to `digits` significant digits as a decimal string with
    /// exponent; see [`PValue`].
    ///
    /// # Errors
    ///
    /// As [`p_value_f64`](Self::p_value_f64).
    pub fn p_value_decimal(&self, digits: u32) -> Result<String, SymplexError> {
        self.tested_p_value("AnovaRow::p_value_decimal")?
            .eval_decimal(digits)
    }

    /// The p-value expression of an effect row, or the `InvalidArgument`
    /// error every accessor reports for an untested row.
    fn tested_p_value(&self, op: &'static str) -> Result<&Ex, SymplexError> {
        self.p_value
            .as_ref()
            .ok_or_else(|| invalid(op, format!("the {:?} row has no F test", self.source)))
    }
}

/// An `F` statistic with its tail probability (a private carrier so the two
/// travel together).
struct FTest {
    f: Q,
    p_value: Ex,
}

impl FTest {
    /// `F = (ss/df) / (ss_resid/df_resid)` and `P(F_{df, df_resid} ≥ F)`.
    fn of(ctx: &Context, ss: &Q, df: usize, ss_resid: &Q, df_resid: usize) -> Self {
        let f = (ss / qu(df)) / (ss_resid / qu(df_resid));
        let p_value = f_sf(ctx, df, df_resid, &f);
        Self { f, p_value }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// Two-way ANOVA
// ═══════════════════════════════════════════════════════════════════════════

/// One observation of a two-way layout in long form: the (0-based) levels of
/// the two factors and the response.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Observation {
    /// Level of factor `A`, `0 ≤ a < a_levels`.
    pub a: usize,
    /// Level of factor `B`, `0 ≤ b < b_levels`.
    pub b: usize,
    /// The response.
    pub y: Q,
}

/// The observations of an `A × B` factorial design, stored by cell:
/// `cells[a][b]` is the (non-empty) vector of replicates at level `a` of
/// `A` and level `b` of `B`.  Cell sizes may differ (an unbalanced design);
/// every cell must contain at least one observation.
///
/// Build it from nested vectors ([`from_cells`](Self::from_cells),
/// [`from_i64`](Self::from_i64)) or from long-form rows
/// ([`from_long`](Self::from_long)).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TwoWayData {
    a_levels: usize,
    b_levels: usize,
    cells: Vec<Vec<Vec<Q>>>,
}

impl TwoWayData {
    /// From `cells[a][b] = replicates`.
    ///
    /// # Errors
    ///
    /// [`SymplexError::InvalidArgument`] if either factor has fewer than two
    /// levels, the rows have different lengths, or a cell is empty.
    pub fn from_cells(cells: Vec<Vec<Vec<Q>>>) -> Result<Self, SymplexError> {
        const OP: &str = "TwoWayData::from_cells";
        let a_levels = cells.len();
        if a_levels < 2 {
            return Err(invalid(OP, "factor A needs at least two levels"));
        }
        let b_levels = cells.first().map_or(0, Vec::len);
        if b_levels < 2 {
            return Err(invalid(OP, "factor B needs at least two levels"));
        }
        for (a, row) in cells.iter().enumerate() {
            if row.len() != b_levels {
                return Err(invalid(
                    OP,
                    format!(
                        "level {a} of A has {} cells, expected {b_levels}",
                        row.len()
                    ),
                ));
            }
            if let Some((b, _)) = row.iter().enumerate().find(|(_, c)| c.is_empty()) {
                return Err(invalid(
                    OP,
                    format!("cell (A = {a}, B = {b}) has no observations"),
                ));
            }
        }
        Ok(Self {
            a_levels,
            b_levels,
            cells,
        })
    }

    /// From integer observations, `cells[a][b] = replicates`.
    ///
    /// # Errors
    ///
    /// As [`from_cells`](Self::from_cells).
    pub fn from_i64(cells: &[&[&[i64]]]) -> Result<Self, SymplexError> {
        Self::from_cells(
            cells
                .iter()
                .map(|row| row.iter().map(|c| data::from_i64(c)).collect())
                .collect(),
        )
    }

    /// From long-form rows (one [`Observation`] per response).  The numbers
    /// of levels are `max(a) + 1` and `max(b) + 1`; every combination of
    /// levels must occur.
    ///
    /// ```
    /// use symplex::prelude::*;
    /// use symplex::linprog::qi;
    /// use symplex::stats::anova::{Observation, TwoWayData};
    ///
    /// let rows: Vec<Observation> = [(0, 0, 4), (0, 0, 5), (0, 1, 6), (1, 0, 5), (1, 1, 8), (1, 1, 9)]
    ///     .iter()
    ///     .map(|&(a, b, y)| Observation { a, b, y: qi(y) })
    ///     .collect();
    /// let data = TwoWayData::from_long(&rows)?;
    /// assert_eq!((data.a_levels(), data.b_levels(), data.n_obs()), (2, 2, 6));
    /// assert_eq!(data.cell(1, 1), Some(&[qi(8), qi(9)][..]));
    /// assert!(!data.is_balanced());
    /// # Ok::<(), SymplexError>(())
    /// ```
    ///
    /// # Errors
    ///
    /// [`SymplexError::InvalidArgument`] for no rows, fewer than two levels
    /// of either factor, or a combination of levels with no observation.
    pub fn from_long(rows: &[Observation]) -> Result<Self, SymplexError> {
        const OP: &str = "TwoWayData::from_long";
        if rows.is_empty() {
            return Err(invalid(OP, "no observations"));
        }
        // Every combination of levels needs an observation, so there are at
        // most `rows.len()` of them: checked before `max + 1` (which
        // overflowed at `usize::MAX`) and before the table is allocated.
        let a_levels = rows.iter().map(|o| o.a).max().unwrap_or(0).checked_add(1);
        let b_levels = rows.iter().map(|o| o.b).max().unwrap_or(0).checked_add(1);
        let (a_levels, b_levels) = match (a_levels, b_levels) {
            (Some(a), Some(b)) if a.checked_mul(b).is_some_and(|ab| ab <= rows.len()) => (a, b),
            _ => {
                return Err(invalid(
                    OP,
                    "more combinations of levels than observations: some combination has none",
                ));
            }
        };
        let mut cells = vec![vec![Vec::new(); b_levels]; a_levels];
        for o in rows {
            // `o.a < a_levels` and `o.b < b_levels` by construction of the maxima.
            if let Some(cell) = cells.get_mut(o.a).and_then(|row| row.get_mut(o.b)) {
                cell.push(o.y.clone());
            }
        }
        Self::from_cells(cells).map_err(|e| match e {
            SymplexError::InvalidArgument { reason, .. } => invalid(OP, reason),
            other => other,
        })
    }

    /// Number of levels of factor `A`.
    #[must_use]
    pub fn a_levels(&self) -> usize {
        self.a_levels
    }

    /// Number of levels of factor `B`.
    #[must_use]
    pub fn b_levels(&self) -> usize {
        self.b_levels
    }

    /// Total number of observations `N`.
    #[must_use]
    pub fn n_obs(&self) -> usize {
        self.cells.iter().flatten().map(Vec::len).sum()
    }

    /// The replicates of cell `(a, b)`, or `None` when either index is out
    /// of range.
    #[must_use]
    pub fn cell(&self, a: usize, b: usize) -> Option<&[Q]> {
        self.cells.get(a)?.get(b).map(Vec::as_slice)
    }

    /// All cells, `cells[a][b] = replicates`.
    #[must_use]
    pub fn cells(&self) -> &[Vec<Vec<Q>>] {
        &self.cells
    }

    /// Whether every cell has the same number of replicates.
    #[must_use]
    pub fn is_balanced(&self) -> bool {
        let mut sizes = self.cells.iter().flatten().map(Vec::len);
        match sizes.next() {
            Some(first) => sizes.all(|s| s == first),
            None => true,
        }
    }

    /// The observations in `cells[a][b][r]` order.
    fn flatten(&self) -> Vec<Q> {
        self.cells.iter().flatten().flatten().cloned().collect()
    }

    /// The observations at level `a` of `A`, all `B` levels pooled.
    fn a_slice(&self, a: usize) -> Vec<Q> {
        self.cells
            .get(a)
            .map(|row| row.iter().flatten().cloned().collect())
            .unwrap_or_default()
    }

    /// The observations at level `b` of `B`, all `A` levels pooled.
    fn b_slice(&self, b: usize) -> Vec<Q> {
        self.cells
            .iter()
            .filter_map(|row| row.get(b))
            .flatten()
            .cloned()
            .collect()
    }
}

/// How the sums of squares of a factorial design are attributed to its
/// terms.  For a **balanced** design the three types coincide; they differ
/// only when cell sizes are unequal.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SsType {
    /// Sequential: `SS(A)`, `SS(B | A)`, `SS(AB | A, B)` — each term adjusted
    /// for the ones before it, in the order `A`, `B`, `A×B`.  R's `anova()`,
    /// `anova_lm(typ=1)`.
    TypeI,
    /// Hierarchical: `SS(A | B)`, `SS(B | A)`, `SS(AB | A, B)` — each main
    /// effect adjusted for the other, the interaction for both.
    /// `car::Anova(type=2)`, `anova_lm(typ=2)`.
    TypeII,
    /// Marginal: every term adjusted for all others, including the
    /// interaction, under **sum-to-zero (effects) contrasts** — the
    /// `contr.sum` / `C(A, Sum)` coding of SPSS and `car::Anova(type=3)`.
    /// `anova_lm(ols('y ~ C(A, Sum) * C(B, Sum)').fit(), typ=3)`.  (With the
    /// default treatment contrasts Type III main effects are not the
    /// classical ones; this crate always uses sum-to-zero coding here.)
    TypeIII,
}

/// The table of a two-way analysis of variance; see [`anova_two_way`].
#[derive(Clone, Debug, PartialEq)]
pub struct TwoWayAnova {
    /// The attribution used for the main effects.
    pub ss_type: SsType,
    /// Main effect of `A`, `df = a − 1`.
    pub factor_a: AnovaRow,
    /// Main effect of `B`, `df = b − 1`.
    pub factor_b: AnovaRow,
    /// The `A × B` interaction, `df = (a − 1)(b − 1)`.
    pub interaction: AnovaRow,
    /// Within-cell variation, `df = N − ab`.
    pub residual: AnovaRow,
    /// `Σ (y − ȳ)²`, `df = N − 1`.
    pub total: AnovaRow,
    /// `ȳ`, exact.
    pub grand_mean: Q,
    /// `cell_means[a][b]`, exact.
    pub cell_means: Vec<Vec<Q>>,
}

impl TwoWayAnova {
    /// The rows in table order: `A`, `B`, `A × B`, residual, total.
    #[must_use]
    pub fn rows(&self) -> Vec<&AnovaRow> {
        vec![
            &self.factor_a,
            &self.factor_b,
            &self.interaction,
            &self.residual,
            &self.total,
        ]
    }
}

/// Which terms a reduced model contains (the intercept is always present).
#[derive(Clone, Copy)]
struct Terms {
    a: bool,
    b: bool,
    ab: bool,
}

/// The contrast coding of a factor's dummy columns.  Residual sums of
/// squares do not depend on it; Type III sums of squares do.
#[derive(Clone, Copy)]
enum Coding {
    /// Indicators of levels `1..n` (level `0` is the reference).
    Treatment,
    /// Sum-to-zero: indicators of levels `1..n`, with level `0` coded `−1`
    /// in every column.  Which level is omitted does not change the span.
    Sum,
}

/// The `n_levels − 1` codes of `level`.
fn codes(level: usize, n_levels: usize, coding: Coding) -> Vec<Q> {
    (1..n_levels)
        .map(|i| match coding {
            Coding::Treatment => {
                if level == i {
                    Q::one()
                } else {
                    Q::zero()
                }
            }
            Coding::Sum => {
                if level == i {
                    Q::one()
                } else if level == 0 {
                    -Q::one()
                } else {
                    Q::zero()
                }
            }
        })
        .collect()
}

/// The residual sum of squares of the model with the given terms, by exact
/// least squares on the dummy-coded design.
fn reduced_rss(
    op: &'static str,
    data: &TwoWayData,
    y: &[Q],
    terms: Terms,
    coding: Coding,
) -> Result<Q, SymplexError> {
    let mut rows: Vec<Vec<Q>> = Vec::with_capacity(y.len());
    for (a, row) in data.cells.iter().enumerate() {
        let ca = codes(a, data.a_levels, coding);
        for (b, cell) in row.iter().enumerate() {
            let cb = codes(b, data.b_levels, coding);
            let mut r = Vec::new();
            if terms.a {
                r.extend(ca.iter().cloned());
            }
            if terms.b {
                r.extend(cb.iter().cloned());
            }
            if terms.ab {
                for x in &ca {
                    for z in &cb {
                        r.push(x * z);
                    }
                }
            }
            rows.extend(std::iter::repeat_n(r, cell.len()));
        }
    }
    ols(y, &rows, true).map(|fit| fit.ssr).map_err(|e| {
        failed(
            op,
            format!("least-squares fit of a reduced model failed: {e}"),
        )
    })
}

/// Two-way analysis of variance with **Type II** sums of squares — the
/// attribution of `statsmodels`' `anova_lm(model, typ=2)` and
/// `car::Anova(type=2)`.  Equivalent to [`anova_two_way_with`] with
/// [`SsType::TypeII`]; for a balanced design every type gives the same
/// table.
///
/// See the [module documentation](self) for an example.
///
/// # Errors
///
/// As [`anova_two_way_with`].
pub fn anova_two_way(ctx: &Context, data: &TwoWayData) -> Result<TwoWayAnova, SymplexError> {
    anova_two_way_with(ctx, data, SsType::TypeII)
}

/// Two-way analysis of variance of the `A × B` design in `data`, with the
/// interaction, using the sums of squares of `ss_type`.
///
/// The model is `y = μ + αₐ + βᵦ + (αβ)ₐᵦ + e`; every sum of squares is the
/// exact difference of the residual sums of squares of two nested
/// least-squares fits on the dummy-coded design, the residual is the pooled
/// within-cell `Σ (y − ȳₐᵦ)²` with `N − ab` degrees of freedom, and each
/// effect is tested by `F = MS_effect / MS_residual` with
/// `P(F_{df, N−ab} ≥ F)` as an exact expression.  Effect sizes are
/// `η² = SS/SS_total` and partial `η² = SS/(SS + SS_residual)`.
///
/// **Balanced designs**: with equal cell sizes the factors are orthogonal
/// and the Type I, II and III sums of squares coincide.  **Unbalanced
/// designs**: they differ for the main effects (never for the interaction);
/// see [`SsType`].
///
/// ```
/// use symplex::prelude::*;
/// use symplex::linprog::{q, qi};
/// use symplex::stats::anova::{anova_two_way_with, SsType, TwoWayData};
///
/// let ctx = Context::new();
/// // Unbalanced 2 × 3 design (cell sizes 4, 2, 3 / 2, 4, 3).
/// let data = TwoWayData::from_i64(&[
///     &[&[4, 5, 6, 7], &[6, 8], &[9, 10, 12]],
///     &[&[5, 7], &[8, 9, 11, 10], &[13, 14, 16]],
/// ])?;
/// // statsmodels: anova_lm(ols('y ~ C(A) * C(B)', df).fit(), typ=1): C(A) sum_sq 37.555555555555636
/// //              anova_lm(..., typ=2):                             C(A) sum_sq 24.000000000000096
/// //              anova_lm(ols('y ~ C(A, Sum) * C(B, Sum)', df).fit(), typ=3): C(A, Sum) sum_sq 22.61538461538463
/// let t1 = anova_two_way_with(&ctx, &data, SsType::TypeI)?;
/// let t2 = anova_two_way_with(&ctx, &data, SsType::TypeII)?;
/// let t3 = anova_two_way_with(&ctx, &data, SsType::TypeIII)?;
/// assert_eq!(t1.factor_a.ss, q(338, 9));
/// assert_eq!(t2.factor_a.ss, qi(24));
/// assert_eq!(t3.factor_a.ss, q(294, 13));
/// // The interaction is the same under every type: sum_sq 8.666666666666684, F 2.2285714285714326.
/// assert_eq!(t1.interaction.ss, q(26, 3));
/// assert_eq!(t3.interaction.f, Some(q(78, 35)));
/// # Ok::<(), SymplexError>(())
/// ```
///
/// # Errors
///
/// [`SymplexError::InvalidArgument`] if every cell has a single observation
/// (no residual degrees of freedom), the response is constant, or the
/// within-cell variance is zero.
pub fn anova_two_way_with(
    ctx: &Context,
    data: &TwoWayData,
    ss_type: SsType,
) -> Result<TwoWayAnova, SymplexError> {
    const OP: &str = "anova_two_way";
    let (a, b) = (data.a_levels, data.b_levels);
    let n = data.n_obs();
    if n <= a * b {
        return Err(invalid(
            OP,
            "every cell has a single observation: no residual degrees of freedom",
        ));
    }
    let df_resid = n - a * b;
    let y = data.flatten();
    let grand_mean = data::mean(&y)?;
    let ss_total = centred_ss(&y);
    if ss_total.is_zero() {
        return Err(invalid(OP, "the response is constant"));
    }
    let cell_means = data
        .cells
        .iter()
        .map(|row| {
            row.iter()
                .map(|c| data::mean(c))
                .collect::<Result<Vec<_>, _>>()
        })
        .collect::<Result<Vec<_>, _>>()?;
    // Residual sums of squares of the nested models that do not need a
    // least-squares solve: `∅` (the total), `A`, `B` and the full model.
    let rss_full = data
        .cells
        .iter()
        .flatten()
        .fold(Q::zero(), |acc, c| acc + centred_ss(c));
    if rss_full.is_zero() {
        return Err(invalid(
            OP,
            "the within-cell variance is zero: F is undefined",
        ));
    }
    let rss_a = (0..a).fold(Q::zero(), |acc, i| acc + centred_ss(&data.a_slice(i)));
    let rss_b = (0..b).fold(Q::zero(), |acc, j| acc + centred_ss(&data.b_slice(j)));
    let additive = Terms {
        a: true,
        b: true,
        ab: false,
    };
    let rss_ab = reduced_rss(OP, data, &y, additive, Coding::Treatment)?;
    let (ss_a, ss_b) = match ss_type {
        SsType::TypeI => (&ss_total - &rss_a, &rss_a - &rss_ab),
        SsType::TypeII => (&rss_b - &rss_ab, &rss_a - &rss_ab),
        SsType::TypeIII => {
            let without_a = Terms {
                a: false,
                b: true,
                ab: true,
            };
            let without_b = Terms {
                a: true,
                b: false,
                ab: true,
            };
            let rss_no_a = reduced_rss(OP, data, &y, without_a, Coding::Sum)?;
            let rss_no_b = reduced_rss(OP, data, &y, without_b, Coding::Sum)?;
            (rss_no_a - &rss_full, rss_no_b - &rss_full)
        }
    };
    let ss_ab = &rss_ab - &rss_full;
    Ok(TwoWayAnova {
        ss_type,
        factor_a: AnovaRow::effect(
            ctx,
            Source::FactorA,
            ss_a,
            a - 1,
            &rss_full,
            df_resid,
            &ss_total,
        ),
        factor_b: AnovaRow::effect(
            ctx,
            Source::FactorB,
            ss_b,
            b - 1,
            &rss_full,
            df_resid,
            &ss_total,
        ),
        interaction: AnovaRow::effect(
            ctx,
            Source::Interaction,
            ss_ab,
            (a - 1) * (b - 1),
            &rss_full,
            df_resid,
            &ss_total,
        ),
        residual: AnovaRow::untested(Source::Residual, rss_full, df_resid),
        total: AnovaRow::total(ss_total, n - 1),
        grand_mean,
        cell_means,
    })
}

// ═══════════════════════════════════════════════════════════════════════════
// Repeated measures
// ═══════════════════════════════════════════════════════════════════════════

/// Mauchly's test of sphericity; see [`RepeatedMeasuresAnova::mauchly`].
#[derive(Clone, Debug, PartialEq)]
pub struct Mauchly {
    /// `W = Π λᵢ / (Σ λᵢ / (k−1))^{k−1}` over the `k − 1` non-zero eigenvalues
    /// of the double-centred covariance matrix, exact (`1` under sphericity).
    pub w: Q,
    /// `χ² = −(n − 1) ρ ln W` with Box's `ρ = 1 − (2d² + d + 2)/(6d(n−1))`,
    /// `d = k − 1`, as an exact expression.
    pub chi_squared: Ex,
    /// `k(k − 1)/2 − 1`.
    pub df: usize,
    /// The p-value with Box's (1949) second-order correction
    /// `min(1, P₁ + ω₂ (P₂ − P₁))`, `Pᵢ` the χ² tails at `df` and `df + 4`,
    /// `ω₂ = (d+2)(d−1)(d−2)(2d³ + 6d² + 3d + 2) / (288 (n−1)² d² ρ²)`, as
    /// an exact expression (for `k = 3` the correction term vanishes).
    /// This `ω₂` is the second coefficient of Box's expansion of the
    /// moments `E[Wʰ]` (the `B₃` Bernoulli-polynomial term; checked
    /// against that expansion in mpmath); `pingouin.sphericity` writes
    /// `3k` for its `3d`, so its p-value differs for `k ≥ 4`.  The
    /// truncated series can exceed `1` when `ω₂ > 1` (few subjects for
    /// many conditions); it is capped there.
    pub p_value: Ex,
}

impl Mauchly {
    /// The statistic as an `f64`.
    ///
    /// # Errors
    ///
    /// Propagates the evaluation error of the expression.
    pub fn chi_squared_f64(&self) -> Result<f64, SymplexError> {
        self.chi_squared.eval_f64()
    }

    /// The p-value as an `f64`.
    ///
    /// # Errors
    ///
    /// Propagates the evaluation error of the expression.
    pub fn p_value_f64(&self) -> Result<f64, SymplexError> {
        self.p_value.eval_f64()
    }
}

impl PValue for Mauchly {
    fn p_value_ex(&self) -> &Ex {
        &self.p_value
    }
}
p_value_accessors!(Mauchly);

/// The outcome of a one-way repeated-measures analysis of variance; see
/// [`anova_repeated_measures`].
#[derive(Clone, Debug, PartialEq)]
pub struct RepeatedMeasuresAnova {
    /// `n`, the number of subjects.
    pub n_subjects: usize,
    /// `k`, the number of conditions.
    pub n_conditions: usize,
    /// The within-subject factor, `df = k − 1`, tested against the error.
    pub conditions: AnovaRow,
    /// Between-subject variation, `df = n − 1` (removed, not tested).
    pub subjects: AnovaRow,
    /// The subject × condition residual, `df = (k − 1)(n − 1)`.
    pub error: AnovaRow,
    /// `Σ (y − ȳ)²`, `df = nk − 1`.
    pub total: AnovaRow,
    /// `F = MS_conditions / MS_error`, exact (also `conditions.f`).
    pub f: Q,
    /// `P(F_{k−1, (k−1)(n−1)} ≥ F)` under sphericity, exact expression.
    pub p_value: Ex,
    /// Greenhouse–Geisser `ε̂ = (tr S̃)² / ((k − 1) tr S̃²)` of the
    /// double-centred sample covariance `S̃`, exact; `1/(k−1) ≤ ε̂ ≤ 1`.
    pub epsilon_gg: Q,
    /// Huynh–Feldt `ε̃ = (n(k−1)ε̂ − 2) / ((k−1)(n − 1 − (k−1)ε̂))`, exact and
    /// **uncapped** (it can exceed `1`); `None` when its denominator is not
    /// positive (too few subjects for the correction to be defined).
    pub epsilon_hf: Option<Q>,
    /// The Greenhouse–Geisser corrected p-value: the `F` tail with both
    /// degrees of freedom multiplied by `ε̂`.
    pub p_value_gg: Ex,
    /// The Huynh–Feldt corrected p-value, with `min(ε̃, 1)`; `None` when
    /// `epsilon_hf` is.
    pub p_value_hf: Option<Ex>,
    /// Mauchly's sphericity test; `None` for `k = 2` (sphericity holds
    /// trivially) or a singular covariance matrix (`n − 1 < k − 1`, or a
    /// degenerate sample), where `W` is `0` and the test is undefined.
    pub mauchly: Option<Mauchly>,
    /// `ȳ`, exact.
    pub grand_mean: Q,
    /// The mean of each condition over subjects, exact.
    pub condition_means: Vec<Q>,
    /// The mean of each subject over conditions, exact.
    pub subject_means: Vec<Q>,
}

impl RepeatedMeasuresAnova {
    /// The uncorrected p-value as an `f64`.
    ///
    /// # Errors
    ///
    /// Propagates the evaluation error of the expression.
    pub fn p_value_f64(&self) -> Result<f64, SymplexError> {
        self.p_value.eval_f64()
    }

    /// The Greenhouse–Geisser corrected p-value as an `f64`.
    ///
    /// # Errors
    ///
    /// Propagates the evaluation error of the expression.
    pub fn p_value_gg_f64(&self) -> Result<f64, SymplexError> {
        self.p_value_gg.eval_f64()
    }

    /// The Huynh–Feldt corrected p-value as an `f64`, if defined.
    ///
    /// # Errors
    ///
    /// Propagates the evaluation error of the expression.
    pub fn p_value_hf_f64(&self) -> Result<Option<f64>, SymplexError> {
        self.p_value_hf.as_ref().map(Ex::eval_f64).transpose()
    }

    /// `log10` of the Greenhouse–Geisser corrected p-value, finite even
    /// when [`p_value_gg_f64`](Self::p_value_gg_f64) underflows to `0.0`;
    /// see [`PValue`].
    ///
    /// # Errors
    ///
    /// Propagates the evaluation error of the expression.
    pub fn p_value_gg_log10(&self) -> Result<f64, SymplexError> {
        hypothesis::p_value_log10_of(&self.p_value_gg)
    }

    /// `log10` of the Huynh–Feldt corrected p-value, if defined; see
    /// [`p_value_gg_log10`](Self::p_value_gg_log10).
    ///
    /// # Errors
    ///
    /// Propagates the evaluation error of the expression.
    pub fn p_value_hf_log10(&self) -> Result<Option<f64>, SymplexError> {
        self.p_value_hf
            .as_ref()
            .map(hypothesis::p_value_log10_of)
            .transpose()
    }

    /// The rows in table order: conditions, subjects, error, total.
    #[must_use]
    pub fn rows(&self) -> Vec<&AnovaRow> {
        vec![&self.conditions, &self.subjects, &self.error, &self.total]
    }
}

/// The uncorrected p-value (the `F` tail under sphericity).
impl PValue for RepeatedMeasuresAnova {
    fn p_value_ex(&self) -> &Ex {
        &self.p_value
    }
}
p_value_accessors!(RepeatedMeasuresAnova);

/// Mauchly's `W` and its χ² approximation from the `k × k` sample covariance
/// `s` and the trace of its double-centred form `S̃ = C S C`.
fn mauchly(
    ctx: &Context,
    s: &[Vec<Q>],
    trace: &Q,
    n: usize,
    k: usize,
) -> Result<Option<Mauchly>, SymplexError> {
    const OP: &str = "anova_repeated_measures";
    let d = k - 1;
    // The product of the d non-zero eigenvalues of S̃ = C S C equals
    // det(MᵀSM)/det(MᵀM) for any k × d matrix M whose columns span the
    // contrast space; successive differences do.
    let m = QMatrix::new(
        (0..k)
            .map(|row| {
                (0..d)
                    .map(|j| {
                        if row == j {
                            Q::one()
                        } else if row == j + 1 {
                            -Q::one()
                        } else {
                            Q::zero()
                        }
                    })
                    .collect()
            })
            .collect(),
    )
    .map_err(|e| failed(OP, e.to_string()))?;
    let s_mat = QMatrix::new(s.to_vec()).map_err(|e| failed(OP, e.to_string()))?;
    let mt = m.transpose();
    let mtsm = mt.matmul(&s_mat)?.matmul(&m)?;
    let mtm = mt.matmul(&m)?;
    let product = mtsm.det()? / mtm.det()?;
    if !product.is_positive() {
        return Ok(None);
    }
    let mean_eigenvalue = trace / qu(d);
    let denominator = (0..d).fold(Q::one(), |acc, _| acc * &mean_eigenvalue);
    let w = product / denominator;
    let (dq, n1) = (qu(d), qu(n - 1));
    let rho = Q::one() - (qi(2) * &dq * &dq + &dq + qi(2)) / (qi(6) * &dq * &n1);
    if !rho.is_positive() {
        return Ok(None);
    }
    let scale = &n1 * &dq * &rho;
    let omega2 = (&dq + qi(2))
        * (&dq - qi(1))
        * (&dq - qi(2))
        * (qi(2) * &dq * &dq * &dq + qi(6) * &dq * &dq + qi(3) * &dq + qi(2))
        / (qi(288) * &scale * &scale);
    let df = d * (d + 1) / 2 - 1;
    let chi_squared = ex(ctx, &(-(&n1 * &rho))) * ex(ctx, &w).ln();
    let p1 = chi_squared_sf(ctx, df, &chi_squared);
    let p2 = chi_squared_sf(ctx, df + 4, &chi_squared);
    // `p₂ > p₁` and `ω₂ ≥ 0`, so the expansion is at least `p₁ > 0`; but
    // `ω₂` grows like `d²/n²` and past `ω₂ ≈ 1` the truncated series
    // exceeds `1` near the mode of `χ²` (`d = 10`, `n = 11`: `1.0063`).
    let p_value = (&p1 + ex(ctx, &omega2) * (p2 - &p1)).min_with(&ctx.one());
    Ok(Some(Mauchly {
        w,
        chi_squared,
        df,
        p_value,
    }))
}

/// One-way repeated-measures analysis of variance: `subjects_by_condition[i]`
/// is the row of subject `i` (`n` rows) over the `k` conditions (columns).
///
/// The additive model `y_ij = μ + πᵢ + τⱼ + e_ij` gives
/// `SS_total = SS_subjects + SS_conditions + SS_error` with
/// `SS_conditions = n Σⱼ (ȳ_·ⱼ − ȳ)²`, `SS_subjects = k Σᵢ (ȳ_ᵢ· − ȳ)²`, and
/// `F = (SS_conditions/(k−1)) / (SS_error/((k−1)(n−1)))`, all exact
/// (`statsmodels.stats.anova.AnovaRM`).  Partial `η² = SS_conditions /
/// (SS_conditions + SS_error)` (pingouin's `ng2` is `SS_conditions /
/// SS_total` here, the row's `eta_squared`).
///
/// Sphericity: the Greenhouse–Geisser `ε̂` is computed exactly from the
/// double-centred sample covariance without eigenvalues, `(tr S̃)² / ((k−1)
/// tr S̃²)`, the Huynh–Feldt `ε̃` from it, and the corrected p-values are the
/// `F` tails with degrees of freedom `(k−1)ε` and `(k−1)(n−1)ε`
/// (`pingouin.rm_anova(correction=True)`, `pingouin.epsilon`).  Mauchly's
/// `W` is exact and its χ² approximation is Box's (1949) second-order
/// expansion (`pingouin.sphericity` for `k = 3`; see [`Mauchly`]).
///
/// ```
/// use symplex::prelude::*;
/// use symplex::linprog::q;
/// use symplex::stats::anova::anova_repeated_measures;
/// use symplex::stats::data::from_i64;
///
/// let ctx = Context::new();
/// // Five subjects measured under three conditions.
/// let y = [
///     from_i64(&[5, 7, 9]),
///     from_i64(&[4, 5, 8]),
///     from_i64(&[6, 8, 10]),
///     from_i64(&[3, 6, 4]),
///     from_i64(&[7, 9, 13]),
/// ];
/// // statsmodels: AnovaRM(long, 'y', 'subject', within=['cond']).fit()
/// //   → F Value 12.179775280898882, Num DF 2, Den DF 8, Pr > F 0.0037355110334743136
/// // pingouin: epsilon(wide, correction='gg') = 0.5426457491265326, ('hf') = 0.5877873360597936
/// //           rm_anova(..., correction=True).p_GG_corr = 0.02126436585826144
/// let r = anova_repeated_measures(&ctx, &y)?;
/// assert_eq!(r.f, q(1084, 89));
/// assert_eq!((r.conditions.df, r.error.df), (2, 8));
/// assert!((r.p_value_f64()? - 0.003_735_511_033_474_317).abs() < 1e-12);
/// assert_eq!(r.epsilon_gg, q(7921, 14597));
/// assert_eq!(r.epsilon_hf, Some(q(4168, 7091)));
/// assert!((r.p_value_gg_f64()? - 0.021_264_365_858_261_566).abs() < 1e-12);
/// # Ok::<(), SymplexError>(())
/// ```
///
/// # Errors
///
/// [`SymplexError::InvalidArgument`] for fewer than two subjects or
/// conditions, ragged rows, or zero error variance (every subject's profile
/// is a shift of the condition means).
pub fn anova_repeated_measures(
    ctx: &Context,
    subjects_by_condition: &[Vec<Q>],
) -> Result<RepeatedMeasuresAnova, SymplexError> {
    const OP: &str = "anova_repeated_measures";
    let n = subjects_by_condition.len();
    if n < 2 {
        return Err(invalid(OP, "at least two subjects are needed"));
    }
    let k = subjects_by_condition.first().map_or(0, Vec::len);
    if k < 2 {
        return Err(invalid(OP, "at least two conditions are needed"));
    }
    if let Some((i, row)) = subjects_by_condition
        .iter()
        .enumerate()
        .find(|(_, r)| r.len() != k)
    {
        return Err(invalid(
            OP,
            format!(
                "subject {i} has {} observations, expected {k} (one per condition)",
                row.len()
            ),
        ));
    }
    let all: Vec<Q> = subjects_by_condition.iter().flatten().cloned().collect();
    let grand_mean = data::mean(&all)?;
    let ss_total = centred_ss(&all);
    let condition_means: Vec<Q> = (0..k)
        .map(|j| {
            subjects_by_condition
                .iter()
                .fold(Q::zero(), |acc, r| acc + &r[j])
                / qu(n)
        })
        .collect();
    let subject_means: Vec<Q> = subjects_by_condition
        .iter()
        .map(|r| data::sum(r) / qu(k))
        .collect();
    let sq = |x: &Q| x * x;
    let ss_conditions = qu(n)
        * condition_means
            .iter()
            .fold(Q::zero(), |acc, m| acc + sq(&(m - &grand_mean)));
    let ss_subjects = qu(k)
        * subject_means
            .iter()
            .fold(Q::zero(), |acc, m| acc + sq(&(m - &grand_mean)));
    let ss_error = &ss_total - &ss_conditions - &ss_subjects;
    if !ss_error.is_positive() {
        return Err(invalid(
            OP,
            "the error variance is zero (every subject's profile is a shift of the condition means): F is undefined",
        ));
    }
    let (df_c, df_s, df_e) = (k - 1, n - 1, (k - 1) * (n - 1));
    let test = FTest::of(ctx, &ss_conditions, df_c, &ss_error, df_e);
    let (f, p_value) = (test.f.clone(), test.p_value.clone());
    let conditions = AnovaRow::with_test(
        Source::Conditions,
        ss_conditions,
        df_c,
        test,
        &ss_error,
        &ss_total,
    );

    // Sample covariance of the conditions across subjects and its
    // double-centred form S̃ = C S C, C = I − 11ᵀ/k.
    let s: Vec<Vec<Q>> = (0..k)
        .map(|a| {
            (0..k)
                .map(|b| {
                    subjects_by_condition.iter().fold(Q::zero(), |acc, r| {
                        acc + (&r[a] - &condition_means[a]) * (&r[b] - &condition_means[b])
                    }) / qu(n - 1)
                })
                .collect()
        })
        .collect();
    let row_means: Vec<Q> = s.iter().map(|r| data::sum(r) / qu(k)).collect();
    let all_mean = data::sum(&row_means) / qu(k);
    let s_tilde: Vec<Vec<Q>> = (0..k)
        .map(|a| {
            (0..k)
                .map(|b| &s[a][b] - &row_means[a] - &row_means[b] + &all_mean)
                .collect()
        })
        .collect();
    let trace = (0..k).fold(Q::zero(), |acc, a| acc + &s_tilde[a][a]);
    let trace_sq = s_tilde
        .iter()
        .flatten()
        .fold(Q::zero(), |acc, v| acc + sq(v));
    // SS_error > 0 ⇒ S̃ ≠ 0 ⇒ tr S̃² > 0.
    if !trace_sq.is_positive() {
        return Err(failed(
            OP,
            "the double-centred covariance vanished although SS_error > 0",
        ));
    }
    let epsilon_gg = sq(&trace) / (qu(df_c) * &trace_sq);
    let hf_denominator = qu(df_c) * (qu(df_s) - qu(df_c) * &epsilon_gg);
    let epsilon_hf = hf_denominator
        .is_positive()
        .then(|| (qu(n) * qu(df_c) * &epsilon_gg - qi(2)) / hf_denominator);
    let corrected = |eps: &Q| f_sf_rational(ctx, &(qu(df_c) * eps), &(qu(df_e) * eps), &f);
    let p_value_gg = corrected(&epsilon_gg);
    let one = Q::one();
    let p_value_hf = epsilon_hf
        .as_ref()
        .map(|e| corrected(if *e > one { &one } else { e }));
    let mauchly = if k >= 3 {
        mauchly(ctx, &s, &trace, n, k)?
    } else {
        None
    };
    Ok(RepeatedMeasuresAnova {
        n_subjects: n,
        n_conditions: k,
        conditions,
        subjects: AnovaRow::untested(Source::Subjects, ss_subjects, df_s),
        error: AnovaRow::untested(Source::Residual, ss_error, df_e),
        total: AnovaRow::total(ss_total, n * k - 1),
        f,
        p_value,
        epsilon_gg,
        epsilon_hf,
        p_value_gg,
        p_value_hf,
        mauchly,
        grand_mean,
        condition_means,
        subject_means,
    })
}

// ═══════════════════════════════════════════════════════════════════════════
// The studentized range distribution
// ═══════════════════════════════════════════════════════════════════════════

/// An `m`-point Gauss–Legendre rule on `[−1, 1]`, applied panel-wise.
struct GaussLegendre {
    nodes: Vec<f64>,
    weights: Vec<f64>,
}

impl GaussLegendre {
    /// Nodes by Newton's method on `P_m` from the Chebyshev-like initial
    /// guesses `cos(π(i + ¾)/(m + ½))`; weights `2 / ((1 − x²) P_m′(x)²)`.
    fn new(m: usize) -> Self {
        let mut nodes = Vec::with_capacity(m);
        let mut weights = Vec::with_capacity(m);
        for i in 0..m {
            let mut x = (PI * (i as f64 + 0.75) / (m as f64 + 0.5)).cos();
            for _ in 0..100 {
                let (p, d) = legendre(m, x);
                let step = p / d;
                x -= step;
                if step.abs() < 1e-15 {
                    break;
                }
            }
            // `P_m′` at the final node: the derivative of the previous iterate
            // is off by `P_m″ · step`, a relative `10⁻¹⁴` in the weight.
            let (_, dp) = legendre(m, x);
            nodes.push(x);
            weights.push(2.0 / ((1.0 - x * x) * dp * dp));
        }
        Self { nodes, weights }
    }

    /// `∫ₐᵇ f` with `[a, b]` split into `panels` equal panels.
    fn integrate(&self, f: &dyn Fn(f64) -> f64, a: f64, b: f64, panels: usize) -> f64 {
        let panels = panels.max(1);
        let h = (b - a) / panels as f64;
        let half = 0.5 * h;
        let mut total = 0.0;
        for p in 0..panels {
            let mid = a + (p as f64 + 0.5) * h;
            let mut acc = 0.0;
            for (x, w) in self.nodes.iter().zip(&self.weights) {
                acc += w * f(mid + half * x);
            }
            total += half * acc;
        }
        total
    }
}

/// `(P_m(x), P_m′(x))` by the three-term recurrence.
fn legendre(m: usize, x: f64) -> (f64, f64) {
    let mut p0 = 1.0;
    let mut p1 = x;
    for j in 2..=m {
        let jf = j as f64;
        let p2 = ((2.0 * jf - 1.0) * x * p1 - (jf - 1.0) * p0) / jf;
        p0 = p1;
        p1 = p2;
    }
    let dp = m as f64 * (x * p1 - p0) / (x * x - 1.0);
    (p1, dp)
}

/// Which tail of a distribution on `[0, ∞)` a quadrature computes.  Each
/// tail is integrated directly, never as `1 −` the other: past `10⁻¹⁶` the
/// complement of a CDF is rounding noise.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Tail {
    /// `P(X ≤ x)`.
    Lower,
    /// `P(X > x)`.
    Upper,
}

impl Tail {
    /// The tail at `x = 0` (`Lower`: `0`, `Upper`: `1`); swapped at `x = ∞`.
    fn at_zero(self) -> f64 {
        match self {
            Tail::Lower => 0.0,
            Tail::Upper => 1.0,
        }
    }

    fn at_infinity(self) -> f64 {
        1.0 - self.at_zero()
    }
}

/// `P(a < Z ≤ a + w)` for a standard normal `Z` and `w ≥ 0`, without
/// cancellation.  The interval is passed by its width: `a + w` rounds `w`
/// away when `w ≪ |a|`.  A short interval (`h(|m| + 1) ≤ 0.1` with midpoint
/// `m = a + h` and half-width `h = w/2`) by the series of `∫_{−h}^{h} e^{−my − y²/2} dy`
/// through the Hermite generating function `e^{xt − t²/2} = Σ Heₙ(x) tⁿ/n!`:
///
/// `P = φ(m) · 2h · Σ_{j≥0} He_{2j}(m) h^{2j} / ((2j + 1)(2j)!)`,
///
/// whose terms fall by at least `(h(|m|+1))²/2` each; otherwise a
/// difference of two tails on the same side of `0` (their ratio is then at
/// most about `e^{−0.1}`, so only a few ulps are lost) or a sum of two
/// `erf` values across `0`.
fn norm_interval(a: f64, w: f64) -> f64 {
    let h = 0.5 * w;
    let m = a + h;
    if h * (m.abs() + 1.0) <= 0.1 {
        // He_{n+1} = m He_n − n He_{n−1}, two steps per term.
        let (mut even, mut odd) = (1.0, m); // He_{2j−2}, He_{2j−1}
        let (mut sum, mut hpow, mut fact) = (1.0, 1.0, 1.0);
        for j in 1..=12 {
            let n = (2 * j) as f64;
            even = m * odd - (n - 1.0) * even; // He_{2j}
            odd = m * even - n * odd; // He_{2j+1}
            hpow *= h * h;
            fact *= (n - 1.0) * n; // (2j)!
            let term = even * hpow / ((n + 1.0) * fact);
            sum += term;
            if term.abs() <= 1e-17 * sum.abs() {
                break;
            }
        }
        return norm_pdf(m) * w * sum;
    }
    let b = a + w;
    if a >= 0.0 {
        0.5 * (erfc(a / SQRT_2) - erfc(b / SQRT_2))
    } else if b <= 0.0 {
        0.5 * (erfc(-b / SQRT_2) - erfc(-a / SQRT_2))
    } else {
        0.5 * (erf(b / SQRT_2) + erf(-a / SQRT_2))
    }
}

/// `c = √(2 (ln k + 39.2))`: the minimum of `k` standard normals falls
/// below `−c` with probability `k Φ(−c) ≤ k φ(c)/c < 10⁻¹⁸`.
fn min_normal_cut(k: usize) -> f64 {
    (2.0 * ((k as f64).ln() + 39.2)).sqrt()
}

/// One tail of the range `W` of `k ≥ 2` independent standard normals at
/// `w ≥ 0`, by composite 16-point Gauss–Legendre quadrature over the
/// position `z` of the minimum:
///
/// * `P(W ≤ w) = k ∫ φ(z) P(z < Z ≤ z + w)^{k−1} dz` ([`norm_interval`]);
/// * `P(W > w) = k ∫ φ(z) Φ̄(z)^{k−1} [1 − (1 − r)^{k−1}] dz` with
///   `r = Φ̄(z + w)/Φ̄(z)`: the density of the minimum times the chance that
///   one of the other `k − 1` exceeds `z + w`, and `1 − (1 − r)^{k−1}` is
///   `−expm1((k − 1) ln1p(−r))` — no difference of nearly equal numbers
///   anywhere, so the upper tail keeps its relative accuracy down to
///   underflow.
///
/// Windows: the upper-tail integrand is at most `k(k−1) φ(z) Φ̄(z + w)`,
/// which peaks at `z = −w/2` and falls by `e^{−d²}` at distance `d`, and at
/// most the density of the minimum, below `−c` ([`min_normal_cut`]) at
/// total mass `10⁻¹⁸`: `[min(−w/2 − 7.5, −c), −w/2 + 7.5]`.  The lower-tail
/// integrand is at most the density of the minimum and, for small `w`,
/// `k w^{k−1} φ(z)^k`: `[−c, 8.5]`.  Panels are at most `2.8` widths of the
/// integrand's features wide (`1/√k` for the lower tail at small `w`,
/// `1/√(2 ln k)` — the spread of the minimum — for the upper tail).
fn normal_range_tail(w: f64, k: usize, tail: Tail, rule: &GaussLegendre) -> f64 {
    if w.is_nan() || w <= 0.0 {
        return tail.at_zero();
    }
    if w == f64::INFINITY {
        return tail.at_infinity();
    }
    let kf = k as f64;
    let km1 = kf - 1.0;
    let cut = min_normal_cut(k);
    let integral = match tail {
        Tail::Lower => {
            let f = |z: f64| norm_pdf(z) * norm_interval(z, w).powf(km1);
            let (a, b, width) = (-cut, 8.5, (2.8 / kf.sqrt()).min(2.0));
            rule.integrate(&f, a, b, ((b - a) / width).ceil() as usize)
        }
        Tail::Upper => {
            let f = |z: f64| {
                let above = norm_sf(z);
                if above <= 0.0 {
                    return 0.0;
                }
                // `r ≤ 1`, but two rounded tails a hair apart may give
                // `1 + ε` (`ln1p(−1 − ε)` is NaN: 0.28's `P(Q > 1 | k = 12,
                // df = 1)` was NaN).
                let r = (norm_sf(z + w) / above).min(1.0);
                let one_exceeds = -(km1 * (-r).ln_1p()).exp_m1();
                norm_pdf(z) * above.powf(km1) * one_exceeds
            };
            let centre = -0.5 * w;
            let (a, b) = ((centre - 7.5).min(-cut), centre + 7.5);
            let width = (2.8 / (2.0 * kf.ln()).max(1.0).sqrt()).min(2.0);
            rule.integrate(&f, a, b, ((b - a) / width).ceil() as usize)
        }
    };
    (kf * integral).clamp(0.0, 1.0)
}

/// Degree of the interpolant on each piece of a [`RangeUpperTable`]:
/// `TABLE_DEGREE + 1` Chebyshev points of the second kind.
const TABLE_DEGREE: usize = 20;

/// Width of a piece of a [`RangeUpperTable`], in units of `1/√(2 ln k)`.
const TABLE_PIECE: f64 = 1.5;

/// `w²/4` above which `e^{w²/4}` is not formed (it overflows past 709.78):
/// there `R` is below `10⁻³⁰⁰` and a subnormal or nearly so, and `ln R`
/// is rounded as it stands.
const TABLE_EXP_CAP: f64 = 700.0;

/// The upper tail `R(w) = P(W > w)` of the range of `k` standard normals
/// ([`normal_range_tail`]), tabulated for repeated use at one `k`: the
/// studentized range tail of every pair of a [`tukey_hsd`] shares it, and
/// only the χ weight of the outer integral depends on the statistic.
///
/// On `(0, w₀)` (see below) it interpolates `F(w) = ln(R(w) e^{w²/4})` — smooth
/// and of moderate size: `0` at `w = 0`, and far out, where `R ≈
/// k(k−1)Φ̄(w/√2)`, about `ln(k(k−1)/(w√π))` — by the second ("true")
/// barycentric formula on `TABLE_DEGREE + 1` Chebyshev points of the
/// second kind in each piece (Berrut & Trefethen, SIAM Review 46, 2004:
/// weights `(−1)ʲ`, halved at the ends; forward stable there), pieces
/// `TABLE_PIECE/√(2 ln k)` wide (the spread of the maximum of `k` normals
/// sets the scale of `R`'s features), each built on first use from the
/// direct quadrature.  `w²/4` is split exactly (`w² =
/// hi + lo` by a fused multiply-add) and `e^{−hi/4}` applied as a separate
/// factor, so no logarithm near `−600` (whose ulp is `10⁻¹³`) is ever
/// rounded.  From `w₀ = 2√(ln(k(k−1)/2) + 1075 ln 2)` on it is `0`: by the
/// union bound over the pairs, `R ≤ k(k−1)Φ̄(w/√2) ≤ ½ k(k−1) e^{−w²/4}
/// < 2⁻¹⁰⁷⁵` (`w₀ = 54.8` for `k = 40`).  On a piece whose samples are
/// not all finite (`R` underflowed just short of `w₀`) the direct
/// quadrature answers.
///
/// Accuracy: with exact samples (`k = 2`, `R = erfc(w/2)`) the interpolant
/// is within `5·10⁻¹⁵` relative of `R` over `(0, 50)` at any degree from
/// 12 up; against the quadrature it differs by the quadrature's own noise
/// (a few `10⁻¹⁵` in the body, up to `5·10⁻¹⁴` for `w > 30` or
/// `k = 1000`), as much as a table of degree 40 on pieces a third as wide.
/// (Past `w²/4 = TABLE_EXP_CAP`, `R < 10⁻³⁰⁰`, only to the precision of
/// the quadrature's own near-subnormal values.)
struct RangeUpperTable<'r> {
    k: usize,
    rule: &'r GaussLegendre,
    width: f64,
    /// `w₀`, from which `R` rounds to `0`.
    zero_from: f64,
    /// `cos(jπ/n)`, `j = 0..=n`.
    points: Vec<f64>,
    /// The barycentric weights `(−1)ʲ δⱼ` (`δ = ½` at the ends).
    weights: Vec<f64>,
    /// `F` at the points of each piece, once built (`None` inside: not all
    /// finite, use the quadrature).
    pieces: std::cell::RefCell<Vec<Option<Option<Vec<f64>>>>>,
}

impl<'r> RangeUpperTable<'r> {
    fn new(k: usize, rule: &'r GaussLegendre) -> Self {
        let n = TABLE_DEGREE;
        let kf = k as f64;
        let width = TABLE_PIECE / (2.0 * kf.ln()).max(1.0).sqrt();
        let zero_from = 2.0 * ((0.5 * kf * (kf - 1.0)).ln() + 1075.0 * LN_2).sqrt();
        let points = (0..=n).map(|j| (PI * j as f64 / n as f64).cos()).collect();
        let weights = (0..=n)
            .map(|j| {
                let sign = if j % 2 == 0 { 1.0 } else { -1.0 };
                if j == 0 || j == n { 0.5 * sign } else { sign }
            })
            .collect();
        let count = (zero_from / width).ceil() as usize;
        Self {
            k,
            rule,
            width,
            zero_from,
            points,
            weights,
            pieces: std::cell::RefCell::new(vec![None; count]),
        }
    }

    /// `(hi, lo)` with `hi + lo = w²` exactly.
    fn square(w: f64) -> (f64, f64) {
        let hi = w * w;
        (hi, w.mul_add(w, -hi))
    }

    /// `F(v) = ln(R(v) e^{v²/4})` from the quadrature.
    fn sample(&self, v: f64) -> f64 {
        let (hi, lo) = Self::square(v);
        let r = self.direct(v);
        if 0.25 * hi < TABLE_EXP_CAP {
            (r * (0.25 * hi).exp()).ln() + 0.25 * lo
        } else {
            r.ln() + 0.25 * hi + 0.25 * lo
        }
    }

    fn direct(&self, w: f64) -> f64 {
        normal_range_tail(w, self.k, Tail::Upper, self.rule)
    }

    /// `P(W > w)`.
    fn upper(&self, w: f64) -> f64 {
        if w >= self.zero_from {
            return 0.0;
        }
        if w.is_nan() || w <= 0.0 {
            return self.direct(w);
        }
        let pieces_len = self.pieces.borrow().len();
        let index = ((w / self.width) as usize).min(pieces_len - 1);
        let a = index as f64 * self.width;
        if self.pieces.borrow()[index].is_none() {
            let values: Vec<f64> = self
                .points
                .iter()
                .map(|&x| self.sample(a + 0.5 * self.width * (1.0 + x)))
                .collect();
            let usable = values.iter().all(|f| f.is_finite());
            self.pieces.borrow_mut()[index] = Some(usable.then_some(values));
        }
        let pieces = self.pieces.borrow();
        let Some(Some(values)) = &pieces[index] else {
            return self.direct(w);
        };
        let x = 2.0 * (w - a) / self.width - 1.0;
        let (mut num, mut den) = (0.0, 0.0);
        for ((&p, &c), &f) in self.points.iter().zip(&self.weights).zip(values) {
            let d = x - p;
            if d == 0.0 {
                (num, den) = (f, 1.0);
                break;
            }
            num += c * f / d;
            den += c / d;
        }
        let (hi, lo) = Self::square(w);
        let f = num / den - 0.25 * lo;
        let r = if 0.25 * hi < TABLE_EXP_CAP {
            f.exp() * (-0.25 * hi).exp()
        } else {
            (f - 0.25 * hi).exp()
        };
        r.clamp(0.0, 1.0)
    }
}

/// `R(x) = ln Γ(x) − [(x − ½) ln x − x + ½ ln 2π]`, the remainder of
/// Stirling's formula, for `x ≥ ½`, to a few ulps.  From `x = 10` the
/// asymptotic series `Σ B₂ₖ/(2k(2k−1) x^{2k−1})` through `k = 7` (DLMF
/// 5.11.1; the next term is below `3·10⁻¹⁷` there); below, the shift
/// `R(x) = R(x + 1) + (x + ½) ln(1 + 1/x) − 1` with the difference summed
/// as the positive series `Σ_{j≥1} v^{2j}/(2j + 1)`, `v = 1/(2x + 1)` (from
/// `ln(1 + 1/x) = ln((1 + v)/(1 − v))`), so nothing cancels.  (`ln Γ` in
/// `f64` and a subtraction lose about `10⁻¹⁵` absolutely, which the
/// density prefactor would carry as a relative error.)
fn stirling_remainder(x: f64) -> f64 {
    const B: [f64; 7] = [
        1.0 / 12.0,
        -1.0 / 360.0,
        1.0 / 1260.0,
        -1.0 / 1680.0,
        1.0 / 1188.0,
        -691.0 / 360_360.0,
        1.0 / 156.0,
    ];
    let mut x = x;
    let mut shift = 0.0;
    while x < 10.0 {
        let v2 = (1.0 / (2.0 * x + 1.0)).powi(2);
        let (mut pow, mut sum) = (v2, 0.0);
        for j in 1..=40 {
            let term = pow / (2 * j + 1) as f64;
            sum += term;
            if term <= 1e-17 * sum {
                break;
            }
            pow *= v2;
        }
        shift += sum;
        x += 1.0;
    }
    let inv2 = 1.0 / (x * x);
    shift + B.iter().rev().fold(0.0, |acc, &b| acc * inv2 + b) / x
}

/// `ln(1 + u) − u`, without the cancellation of the two terms for small `u`
/// (the alternating series `−u²/2 + u³/3 − …`).
fn ln1p_minus_u(u: f64) -> f64 {
    if u.abs() >= 0.25 {
        return u.ln_1p() - u;
    }
    let mut term = u * u;
    let mut sum = 0.0;
    for n in 2..=40 {
        let contribution = term / n as f64;
        sum += if n % 2 == 0 {
            -contribution
        } else {
            contribution
        };
        term *= u;
        if contribution.abs() <= 1e-18 * sum.abs() {
            break;
        }
    }
    sum
}

/// Above this many degrees of freedom `S = √(χ²_ν/ν)` is `1` for every
/// purpose of a double: the studentized range is the range of `k` normals
/// up to a relative `O(q⁴/ν)` (the upper tail is `0` in `f64` past
/// `q ≈ 55`).
const NU_NORMAL_LIMIT: f64 = 1e30;

/// The outer window ends where the log-integrand has fallen this far below
/// its maximum; by log-concavity (see [`studentized_range_tail`]) the mass
/// beyond is below `e^{−45} ≈ 3·10⁻²⁰` of the total.
const OUTER_LOG_DROP: f64 = 45.0;

/// The largest change of the log-integrand across one outer panel: a
/// 16-point Gauss–Legendre panel integrates `e^{−12x}` on `[0, 1]` to a
/// relative `10⁻²⁰`.
const OUTER_PANEL_LOG_CHANGE: f64 = 12.0;

/// A log-integrand maximum below which the outer integral underflows (see
/// [`studentized_range_tail`]): `e^{−760} · 3000 < 4.9·10⁻³²⁴`.
const UNDERFLOW_LOG_MODE: f64 = -760.0;

/// Cap on the evaluations of each search loop of [`studentized_range_tail`]
/// (each loop at least doubles a step, so a few hundred reach any double).
const OUTER_SEARCH_CAP: usize = 4096;

/// One tail of the studentized range `Q = W/S` for `k` groups and `ν`
/// degrees of freedom (`W` the range of `k` standard normals, `S =
/// √(χ²_ν/ν)` independent), computed directly — the upper tail is never
/// `1 − P(Q ≤ q)`:
///
/// `P(Q ≤ q) = ∫₀^∞ f_ν(s) P(W ≤ qs) ds`, `P(Q > q) = ∫₀^∞ f_ν(s) P(W > qs) ds`
///
/// with the range tails of [`normal_range_tail`].  Arguments are validated
/// by the caller (`k ≥ 2`, `ν ≥ 1`, `q` not NaN).
///
/// **Variable.**  The outer integral runs over `t = ln s`, with integrand
/// `s f_ν(s) P(W ≶ qs)`: near `s = 1` the abscissae stay resolved when
/// `1/√(2ν)` is below the spacing of doubles, and near `s = 0` (a far upper
/// tail at small `ν`, where the mass sits at `s ≈ 1/q`) they keep their
/// relative precision.  `ln(s f_ν(s))` is evaluated with the `O(ν)` terms
/// cancelled analytically (`x = ν/2`, `u = e^t − 1`):
///
/// `ln(s f_ν(s)) = ln 2 + ½ ln(x/2π) − R(x) + 2x·(t − u) − x u²`
///
/// with `R` the Stirling remainder of `ln Γ(x)` and `t − u = ln(1+u) − u`
/// by its series for small `u` ([`ln1p_minus_u`]).
///
/// **Window.**  `f_ν` is log-concave for `ν ≥ 1`, and so are both tails of
/// `W` (the joint density of the minimum and maximum of normals is
/// log-concave, and so are its linear images and their integrals —
/// Prékopa), hence the integrand is log-concave in `s` and unimodal in
/// `t`.  Its mode is bracketed from the estimate `s² ≈ ν/(ν + q²/2)`
/// (upper tail; `P(W > w) ≈ e^{−w²/4}`) or `(ν + k − 1)/ν` (lower tail;
/// `P(W ≤ w) ∝ w^{k−1}`) by doubling steps and refined by golden-section
/// search; from there 16-point panels march outwards on each side, each
/// twice the previous but halved until the log-integrand changes by at
/// most [`OUTER_PANEL_LOG_CHANGE`] across it, until it is
/// [`OUTER_LOG_DROP`] below the mode.
///
/// **Accuracy.**  Against mpmath (the same double integral at 25–45
/// digits, with the range tail both in this non-cancelling form and as
/// `1 − P(W ≤ w)` at high precision) and the exact `k = 2` case
/// `P(Q > q) = 2 P(T_ν > q/√2)`: a relative `10⁻¹⁵` in the body, a few
/// `10⁻¹⁴` in a far upper tail, where the complementary error function
/// itself is that accurate.
///
/// Past [`NU_NORMAL_LIMIT`] the range tail itself is returned.
fn studentized_range_tail(q: f64, k: usize, nu: f64, tail: Tail) -> Result<f64, SymplexError> {
    let rule = GaussLegendre::new(16);
    studentized_range_tail_by(q, k, nu, tail, &|w| normal_range_tail(w, k, tail, &rule))
}

/// [`studentized_range_tail`] with the range tail `P(W ≶ w)` supplied:
/// the direct quadrature, or a [`RangeUpperTable`] shared by many
/// statistics at one `k` (the outer integral is otherwise the same).
fn studentized_range_tail_by(
    q: f64,
    k: usize,
    nu: f64,
    tail: Tail,
    range_tail_at: &dyn Fn(f64) -> f64,
) -> Result<f64, SymplexError> {
    const OP: &str = "studentized_range";
    if q.is_nan() || q <= 0.0 {
        return Ok(tail.at_zero());
    }
    if q == f64::INFINITY {
        return Ok(tail.at_infinity());
    }
    let rule = GaussLegendre::new(16);
    if nu >= NU_NORMAL_LIMIT {
        return Ok(range_tail_at(q));
    }
    let x = 0.5 * nu;
    let log_prefactor = LN_2 + 0.5 * (x / (2.0 * PI)).ln() - stirling_remainder(x);
    let log_weight = |t: f64| {
        let u = t.exp_m1();
        let t_minus_u = if u.abs() < 0.25 {
            ln1p_minus_u(u)
        } else {
            t - u
        };
        log_prefactor + 2.0 * x * t_minus_u - x * u * u
    };
    let range_tail = |t: f64| range_tail_at(q * t.exp());
    let log_h = |t: f64| log_weight(t) + range_tail(t).ln();
    let not_found = || {
        failed(
            OP,
            format!(
                "the studentized range quadrature found no window (q = {q}, k = {k}, df = {nu})"
            ),
        )
    };

    // 1. Bracket the mode, walking uphill with doubling steps from the
    //    estimate.  Where the integrand is 0 (the range tail underflowed)
    //    the mode lies towards smaller s for the upper tail, larger s for
    //    the lower.
    let scale = 1.0 / (2.0 * nu + 1.0).sqrt();
    let (t0, default_dir) = match tail {
        Tail::Upper => {
            let r = q / (2.0 * nu).sqrt();
            let t0 = if r > 1e100 {
                -r.ln()
            } else {
                -0.5 * (r * r).ln_1p()
            };
            (t0, -1.0)
        }
        Tail::Lower => (0.5 * ((k - 1) as f64 / nu).ln_1p(), 1.0),
    };
    let l0 = log_h(t0);
    let (l_right, l_left) = (log_h(t0 + scale), log_h(t0 - scale));
    let dir = if l_right > l0 && l_right >= l_left {
        1.0
    } else if l_left > l0 {
        -1.0
    } else if l0 == f64::NEG_INFINITY {
        default_dir
    } else {
        0.0
    };
    let (mut lo, mut mode, mut hi, mut l_mode) = (t0 - scale, t0, t0 + scale, l0);
    if dir != 0.0 {
        let l_first = if dir > 0.0 { l_right } else { l_left };
        let (mut prev, mut cur, mut l_cur) = (t0, t0 + dir * scale, l_first);
        let mut step = scale;
        let mut bracketed = false;
        for _ in 0..OUTER_SEARCH_CAP {
            step *= 2.0;
            let next = cur + dir * step;
            if next.is_nan() || next.abs() >= 1500.0 {
                // `e^t` is 0 or ∞ in `f64` from here on.  An integrand that
                // underflowed everywhere on the way is a tail below the
                // smallest double (the lower tail at a tiny `q`).
                if l_cur == f64::NEG_INFINITY {
                    return Ok(0.0);
                }
                break;
            }
            let l_next = log_h(next);
            if l_next > l_cur || (l_next == f64::NEG_INFINITY && l_cur == f64::NEG_INFINITY) {
                (prev, cur, l_cur) = (cur, next, l_next);
            } else {
                (lo, hi) = if dir > 0.0 {
                    (prev, next)
                } else {
                    (next, prev)
                };
                (mode, l_mode) = (cur, l_cur);
                bracketed = true;
                break;
            }
        }
        if !bracketed || !l_mode.is_finite() {
            return Err(not_found());
        }
    }
    // The integrand is unimodal and the window lies inside |t| < 1500, so
    // the integral is below `3000 e^{l_mode}`: under `e^{−760}` that is
    // below the smallest subnormal.  (Where the range tail underflows at
    // the estimate — the lower tail at a tiny `q` — the walk can stop at a
    // point where the density is `e^{−10¹²⁰}` and the march below could
    // not resolve the integrand.)
    if l_mode < UNDERFLOW_LOG_MODE {
        return Ok(0.0);
    }

    // 2. Golden-section refinement (the mode need only be known to a small
    //    fraction of the integrand's width, which is at least about
    //    `scale/√k`).
    let tol = 0.05 * scale / (k as f64).sqrt();
    for _ in 0..OUTER_SEARCH_CAP {
        if hi - lo <= tol {
            break;
        }
        const GOLDEN: f64 = 0.381_966_011_250_105_1;
        let left = mode - lo > hi - mode;
        let probe = if left {
            mode - GOLDEN * (mode - lo)
        } else {
            mode + GOLDEN * (hi - mode)
        };
        if probe == mode {
            break;
        }
        let l_probe = log_h(probe);
        match (l_probe > l_mode, left) {
            (true, true) => (hi, mode, l_mode) = (mode, probe, l_probe),
            (true, false) => (lo, mode, l_mode) = (mode, probe, l_probe),
            (false, true) => lo = probe,
            (false, false) => hi = probe,
        }
    }

    // 3. March outwards from the mode, one 16-point panel at a time, until
    //    the log-integrand is `OUTER_LOG_DROP` below the mode.  A panel
    //    doubles the previous one but is halved until the log-integrand
    //    changes by at most `OUTER_PANEL_LOG_CHANGE` across it: fine at the
    //    peak, wide in an exponential tail (small ν), narrow where the
    //    decay is super-exponential (large s).
    let h = |t: f64| log_weight(t).exp() * range_tail(t);
    let floor = l_mode - OUTER_LOG_DROP;
    let first = 0.25 * scale / (k as f64).sqrt();
    let mut total = 0.0;
    for dir in [-1.0, 1.0] {
        let (mut pos, mut l_pos, mut step) = (mode, l_mode, first);
        let mut reached = false;
        'march: for _ in 0..OUTER_SEARCH_CAP {
            let (mut next, mut l_next) = (pos + dir * step, f64::NAN);
            for _ in 0..OUTER_SEARCH_CAP {
                next = pos + dir * step;
                if !next.is_finite() || next == pos {
                    break 'march;
                }
                l_next = log_h(next);
                // An endpoint where the integrand underflowed ends the march
                // (it is below any floor above the underflow threshold).
                if (l_next - l_pos).abs() <= OUTER_PANEL_LOG_CHANGE || l_next == f64::NEG_INFINITY {
                    break;
                }
                step *= 0.5;
            }
            total += rule.integrate(&h, pos.min(next), pos.max(next), 1);
            if l_next < floor {
                reached = true;
                break;
            }
            (pos, l_pos, step) = (next, l_next, 2.0 * step);
        }
        if !reached {
            return Err(not_found());
        }
    }
    Ok(total.clamp(0.0, 1.0))
}

/// `k ≥ 2` and `df ≥ 1`, `df = +∞` included (the range of `k` standard
/// normals, as `scipy.stats.studentized_range` takes `np.inf`); `NaN` is
/// refused.
fn check_studentized_range_args(op: &'static str, k: usize, df: f64) -> Result<(), SymplexError> {
    if k < 2 {
        return Err(invalid(op, format!("k must be at least 2, got {k}")));
    }
    if df.is_nan() || df < 1.0 {
        return Err(invalid(
            op,
            format!("the degrees of freedom must be at least 1 (or +inf), got {df}"),
        ));
    }
    Ok(())
}

/// `P(Q ≤ q)` for the studentized range distribution of `k` groups with `df`
/// degrees of freedom, by composite Gauss–Legendre quadrature of the double
/// integral (relative accuracy about `1e-14`, also in the lower tail as
/// `q → 0`).  `scipy.stats.studentized_range.cdf(q, k, df)`.
///
/// `df = f64::INFINITY` is the limit `P(Q ≤ q) = k∫φ(z)[Φ(z) − Φ(z − q)]^{k−1}dz`,
/// the range of `k` standard normals (`scipy.stats.studentized_range.cdf(q,
/// k, np.inf)`); from `df = 1e30` on the finite-`df` result equals it to
/// double precision.
///
/// ```
/// use symplex::stats::anova::studentized_range_cdf;
///
/// // scipy: studentized_range.cdf(3.0, 3, 12) = 0.8729674086442558
/// assert!((studentized_range_cdf(3.0, 3, 12.0)? - 0.872_967_408_644_255_8).abs() < 1e-8);
/// // scipy: studentized_range.cdf(3.0, 3, np.inf) = 0.9144574283450421
/// let c = studentized_range_cdf(3.0, 3, f64::INFINITY)?;
/// assert!((c - 0.914_457_428_345_042_1).abs() < 1e-14);
/// # Ok::<(), symplex::prelude::SymplexError>(())
/// ```
///
/// # Errors
///
/// [`SymplexError::InvalidArgument`] for `k < 2`, `df < 1`, a NaN `df` or
/// a NaN `q`; [`SymplexError::ComputationFailed`] if the quadrature finds
/// no window (not expected).
pub fn studentized_range_cdf(q: f64, k: usize, df: f64) -> Result<f64, SymplexError> {
    const OP: &str = "studentized_range_cdf";
    check_studentized_range_args(OP, k, df)?;
    if q.is_nan() {
        return Err(invalid(OP, "q must not be NaN"));
    }
    studentized_range_tail(q, k, df, Tail::Lower)
}

/// `P(Q > q)` of the studentized range distribution, integrated directly
/// from the upper tail of the range of `k` normals (never as `1 − cdf`), so
/// it keeps its relative accuracy (about `1e-14`) down to underflow.
/// `scipy.stats.studentized_range.sf(q, k, df)` — which *is* `1 − cdf`
/// and is rounding noise below `10⁻¹⁵`.  `df = f64::INFINITY` is the
/// range of `k` standard normals (see [`studentized_range_cdf`]).
///
/// ```
/// use symplex::stats::anova::studentized_range_sf;
///
/// // mpmath (dps 25, the double integral with the non-cancelling range tail):
/// //   P(Q > 40 | k = 4, df = 100) = 9.8157947218104339959e-49
/// // scipy: studentized_range.sf(40, 4, 100) = 7.771561172376096e-16 (1 − cdf)
/// let p = studentized_range_sf(40.0, 4, 100.0)?;
/// assert!((p / 9.815_794_721_810_433_995_9e-49 - 1.0).abs() < 1e-13);
/// # Ok::<(), symplex::prelude::SymplexError>(())
/// ```
///
/// # Errors
///
/// As [`studentized_range_cdf`].
pub fn studentized_range_sf(q: f64, k: usize, df: f64) -> Result<f64, SymplexError> {
    const OP: &str = "studentized_range_sf";
    check_studentized_range_args(OP, k, df)?;
    if q.is_nan() {
        return Err(invalid(OP, "q must not be NaN"));
    }
    studentized_range_tail(q, k, df, Tail::Upper)
}

/// The quantile `q_p` with `P(Q ≤ q_p) = p` of the studentized range
/// distribution (the Tukey critical value for `p = 1 − α`), by Brent's
/// method on the logarithm of the *smaller* tail in `ln q`: `ln P(Q ≤ q) =
/// ln p` for `p ≤ ½`, `ln P(Q > q) = ln(1 − p)` above (`1 − p` is exact
/// there), so levels near `0` and near `1` keep their relative accuracy.
/// `scipy.stats.studentized_range.ppf(p, k, df)`.
///
/// ```
/// use symplex::stats::anova::studentized_range_quantile;
///
/// // scipy: studentized_range.ppf(0.95, 3, 15) = 3.6733776588970977
/// assert!((studentized_range_quantile(0.95, 3, 15.0)? - 3.673_377_658_897_097_7).abs() < 1e-6);
/// # Ok::<(), symplex::prelude::SymplexError>(())
/// ```
///
/// `df = f64::INFINITY` inverts the range of `k` standard normals (see
/// [`studentized_range_cdf`]; scipy: `studentized_range.ppf(0.95, 3,
/// np.inf) = 3.314493155398121`, the classic `q_{0.05; 3, ∞} = 3.314`).
///
/// # Errors
///
/// [`SymplexError::InvalidArgument`] for `k < 2`, `df < 1`, a NaN `df` or
/// `p ∉ (0, 1)`; [`SymplexError::ComputationFailed`] if the root search
/// fails.
pub fn studentized_range_quantile(p: f64, k: usize, df: f64) -> Result<f64, SymplexError> {
    check_studentized_range_args("studentized_range_quantile", k, df)?;
    check_unit_open("studentized_range_quantile", "p", p)?;
    studentized_range_root(p, k, df, None)
}

/// [`studentized_range_quantile`] for checked arguments, the upper tail of
/// the range taken from `table` when one is given.
fn studentized_range_root(
    p: f64,
    k: usize,
    df: f64,
    table: Option<&RangeUpperTable<'_>>,
) -> Result<f64, SymplexError> {
    const OP: &str = "studentized_range_quantile";
    let (tail, level) = if p <= 0.5 {
        (Tail::Lower, p)
    } else {
        (Tail::Upper, 1.0 - p)
    };
    let rule = GaussLegendre::new(16);
    let range_tail_at = |w: f64| match table {
        Some(t) if tail == Tail::Upper => t.upper(w),
        _ => normal_range_tail(w, k, tail, &rule),
    };
    let ln_level = level.ln();
    // In `x = ln q`; an underflowed tail is floored so `g` stays finite and
    // monotone (`ln level ≥ ln 2⁻¹⁰⁷⁴ > −1e4`).
    let g = |x: f64| match studentized_range_tail_by(x.exp(), k, df, tail, &range_tail_at) {
        Ok(v) => v.ln().max(-1e4) - ln_level,
        Err(_) => f64::NAN,
    };
    let bracket = grow_bracket(g, 0.0, 1.0, Bounds::free(), 64).map_err(|e| {
        failed(
            OP,
            format!("no bracket for the studentized range quantile: {e}"),
        )
    })?;
    let opts = RootOpts {
        xtol: 1e-14,
        ..RootOpts::default()
    };
    let root = brent_root(g, bracket.lower, bracket.upper, &opts)
        .map_err(|e| failed(OP, e.to_string()))?
        .exp();
    if root.is_finite() && root > 0.0 {
        Ok(root)
    } else {
        Err(failed(
            OP,
            "the studentized range quantile did not converge",
        ))
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// Post-hoc comparisons
// ═══════════════════════════════════════════════════════════════════════════

/// One pairwise comparison of Tukey's HSD; see [`tukey_hsd`].
#[derive(Clone, Debug, PartialEq)]
pub struct PairwiseComparison {
    /// Index of the first group.
    pub i: usize,
    /// Index of the second group (`i < j`).
    pub j: usize,
    /// `x̄ᵢ − x̄ⱼ`, exact.
    pub diff: Q,
    /// `√(MSE/2 · (1/nᵢ + 1/nⱼ))`, exact (a square root of a rational).
    pub se: Ex,
    /// `|diff| / se`, exact: the studentized range statistic.
    pub statistic: Ex,
    /// `P(Q_{k, N−k} ≥ statistic)`, the family-wise adjusted p-value
    /// ([`studentized_range_sf`]; `f64`: the studentized range distribution
    /// is integrated numerically, to a relative `1e-14` also in the far
    /// tail).
    pub p_adj: f64,
    /// `diff ± q_{confidence, k, N−k} · se`.
    pub ci: Interval<f64>,
}

/// Tukey's honestly-significant-difference test of all `k(k−1)/2` pairwise
/// differences of means of `k` independent groups, with the Tukey–Kramer
/// standard error for unequal sizes: `se = √(MSE/2 · (1/nᵢ + 1/nⱼ))`,
/// `MSE = SS_within/(N − k)`, statistic `|x̄ᵢ − x̄ⱼ|/se` referred to the
/// studentized range distribution with `k` groups and `N − k` degrees of
/// freedom, and simultaneous `confidence` intervals `x̄ᵢ − x̄ⱼ ± q · se`.
/// `scipy.stats.tukey_hsd(*groups)` and `.confidence_interval(confidence)`.
///
/// Differences, standard errors and statistics are exact; the p-values and
/// interval limits are `f64`.  The p-values are the studentized range
/// upper tail integrated directly ([`studentized_range_sf`]), so a wide
/// separation gets its true tiny p-value (`scipy.stats.tukey_hsd` reports
/// `1 − cdf`, rounding noise below `10⁻¹⁵`).  All pairs and the critical
/// value share one tabulation of the inner integral, the range tail
/// `P(W > w)` of `k` normals, which depends on `k` alone (only the χ
/// weight of the outer integral depends on the pair): 30 groups take
/// about 30 ms (0.28 integrated each of the 435 pairs afresh, 1.8 s), and
/// the p-values agree with [`studentized_range_sf`] of each statistic to
/// its own accuracy (a relative `10⁻¹⁵`, `10⁻¹⁴` in a far tail).
///
/// ```
/// use symplex::prelude::*;
/// use symplex::linprog::{q, qi};
/// use symplex::stats::anova::tukey_hsd;
/// use symplex::stats::data::from_i64;
///
/// let ctx = Context::new();
/// let g = [from_i64(&[6, 8, 4, 5, 3, 4]), from_i64(&[8, 12, 9, 11, 6, 8]), from_i64(&[13, 9, 11, 8, 7, 12])];
/// // scipy: r = tukey_hsd(*g); r.statistic[0, 1] = -4.0, r.pvalue[0, 1] = 0.013913287267276697,
/// //        r.confidence_interval(0.95).low[0, 1] = -7.192999, .high[0, 1] = -0.807001
/// let pairs = tukey_hsd(&ctx, &g, 0.95)?;
/// assert_eq!((pairs[0].i, pairs[0].j), (0, 1));
/// assert_eq!(pairs[0].diff, qi(-4));
/// assert_eq!(pairs[0].se, ctx.from_ratio(q(34, 45)).sqrt());
/// assert!((pairs[0].p_adj - 0.013_913_287_267_276_697).abs() < 1e-7);
/// assert!((pairs[0].ci.lower - -7.192_999).abs() < 1e-5);
/// # Ok::<(), SymplexError>(())
/// ```
///
/// # Errors
///
/// [`SymplexError::InvalidArgument`] for fewer than two groups, an empty
/// group, `N ≤ k`, zero within-group variance, or `confidence ∉ (0, 1)`;
/// [`SymplexError::ComputationFailed`] if a difference, standard error or
/// statistic does not fit in an `f64` (the limits and p-values are `f64`).
pub fn tukey_hsd(
    ctx: &Context,
    groups: &[Vec<Q>],
    confidence: f64,
) -> Result<Vec<PairwiseComparison>, SymplexError> {
    const OP: &str = "tukey_hsd";
    check_confidence(OP, confidence)?;
    let k = groups.len();
    if k < 2 {
        return Err(invalid(OP, "at least two groups are needed"));
    }
    if groups.iter().any(Vec::is_empty) {
        return Err(invalid(OP, "every group must be non-empty"));
    }
    let n: usize = groups.iter().map(Vec::len).sum();
    if n <= k {
        return Err(invalid(
            OP,
            "at least one group needs more than one observation",
        ));
    }
    let df = n - k;
    let means = groups
        .iter()
        .map(|g| data::mean(g))
        .collect::<Result<Vec<_>, _>>()?;
    let ss_within = groups.iter().fold(Q::zero(), |acc, g| acc + centred_ss(g));
    if ss_within.is_zero() {
        return Err(invalid(OP, "the within-group variance is zero"));
    }
    let mse = ss_within / qu(df);
    let df_f = df as f64;
    // Every pair (and the critical value) integrates the same range tail
    // `P(W > w)` of k normals against its own χ weight: tabulate it once.
    let rule = GaussLegendre::new(16);
    let table = RangeUpperTable::new(k, &rule);
    let upper = |w: f64| table.upper(w);
    let q_crit = studentized_range_root(confidence, k, df_f, Some(&table))?;
    let mut out = Vec::with_capacity(k * (k - 1) / 2);
    for i in 0..k {
        for j in i + 1..k {
            let diff = &means[i] - &means[j];
            let var = &mse / qi(2) * (qu(groups[i].len()).recip() + qu(groups[j].len()).recip());
            let se = ex(ctx, &var).sqrt();
            let statistic = ex(ctx, &diff.abs()) / &se;
            let (diff_f, se_f) = (to_f64(OP, &diff)?, to_f64(OP, &var)?.sqrt());
            let stat_f = to_f64(OP, &(&diff * &diff / &var))?.sqrt();
            let p_adj = studentized_range_tail_by(stat_f, k, df_f, Tail::Upper, &upper)?;
            out.push(PairwiseComparison {
                i,
                j,
                diff,
                se,
                statistic,
                p_adj,
                ci: Interval::closed(diff_f - q_crit * se_f, diff_f + q_crit * se_f),
            });
        }
    }
    Ok(out)
}

/// A family-wise adjustment of pairwise p-values.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Adjustment {
    /// `p̃ = min(1, m p)` ([`hypothesis::bonferroni`]).
    Bonferroni,
    /// Holm's step-down procedure ([`hypothesis::holm`]).
    Holm,
}

/// One pairwise Welch t-test with its adjusted p-value; see
/// [`pairwise_t_tests`].
#[derive(Clone, Debug, PartialEq)]
pub struct PairwiseTTest {
    /// Index of the first group.
    pub i: usize,
    /// Index of the second group (`i < j`).
    pub j: usize,
    /// `x̄ᵢ − x̄ⱼ`, exact.
    pub diff: Q,
    /// The two-sided Welch test of the pair (exact statistic, exact p-value
    /// expression, rational Welch–Satterthwaite `df`).
    pub test: TestResult,
    /// The adjusted p-value.
    pub p_adj: f64,
    /// Whether the pair differs at family-wise level `alpha`.
    pub reject: bool,
}

/// All `k(k−1)/2` pairwise two-sided Welch t-tests
/// ([`hypothesis::t_test_two_sample`] with `equal_var = false`) with the
/// p-values adjusted by `adjustment` at family-wise level `alpha` — the
/// post-hoc procedure to use when variances differ between groups.
/// `scipy.stats.ttest_ind(gᵢ, gⱼ, equal_var=False)` per pair, then
/// `statsmodels.stats.multitest.multipletests(p, alpha, method)`.
///
/// ```
/// use symplex::prelude::*;
/// use symplex::linprog::qi;
/// use symplex::stats::anova::{pairwise_t_tests, Adjustment};
/// use symplex::stats::data::from_i64;
///
/// let ctx = Context::new();
/// let g = [from_i64(&[6, 8, 4, 5, 3, 4]), from_i64(&[8, 12, 9, 11, 6, 8]), from_i64(&[13, 9, 11, 8, 7, 12])];
/// // scipy: ttest_ind(g[0], g[1], equal_var=False).pvalue = 0.00644386616395533, (g[0], g[2]) 0.002386749612426776,
/// //        (g[1], g[2]) 0.46515103975349575
/// // statsmodels: multipletests(p, method='holm')[1] = [0.01288773232791066, 0.007160248837280328, 0.46515103975349575]
/// let t = pairwise_t_tests(&ctx, &g, Adjustment::Holm, 0.05)?;
/// assert_eq!(t.len(), 3);
/// assert_eq!(t[1].diff, qi(-5));
/// assert!((t[0].p_adj - 0.012_887_732_327_910_66).abs() < 1e-12);
/// assert_eq!(t.iter().map(|p| p.reject).collect::<Vec<_>>(), vec![true, true, false]);
/// # Ok::<(), SymplexError>(())
/// ```
///
/// # Errors
///
/// [`SymplexError::InvalidArgument`] for fewer than two groups, a group
/// with fewer than two observations, a pair of constant groups, or
/// `alpha ∉ (0, 1)`.
pub fn pairwise_t_tests(
    ctx: &Context,
    groups: &[Vec<Q>],
    adjustment: Adjustment,
    alpha: f64,
) -> Result<Vec<PairwiseTTest>, SymplexError> {
    const OP: &str = "pairwise_t_tests";
    let k = groups.len();
    if k < 2 {
        return Err(invalid(OP, "at least two groups are needed"));
    }
    let mut pairs = Vec::with_capacity(k * (k - 1) / 2);
    let mut p_values = Vec::with_capacity(k * (k - 1) / 2);
    for i in 0..k {
        for j in i + 1..k {
            let test = hypothesis::t_test_two_sample(
                ctx,
                &groups[i],
                &groups[j],
                false,
                Alternative::TwoSided,
            )?;
            p_values.push(test.p_value_f64()?);
            let diff = data::mean(&groups[i])? - data::mean(&groups[j])?;
            pairs.push((i, j, diff, test));
        }
    }
    let adjusted = match adjustment {
        Adjustment::Bonferroni => hypothesis::bonferroni(&p_values, alpha)?,
        Adjustment::Holm => hypothesis::holm(&p_values, alpha)?,
    };
    Ok(pairs
        .into_iter()
        .zip(adjusted.p_adjusted)
        .zip(adjusted.reject)
        .map(|(((i, j, diff, test), p_adj), reject)| PairwiseTTest {
            i,
            j,
            diff,
            test,
            p_adj,
            reject,
        })
        .collect())
}