fdars-core 0.38.0

Functional Data Analysis algorithms in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
//! Wavelet-domain scalar-on-function regression (`wcr`, WAV-03).
//!
//! `wcr` transforms each functional predictor curve into its multi-level DWT
//! coefficient pyramid (via the Phase 69 primitive), concatenates the bands into a
//! single per-curve coefficient vector, and fits a scalar-on-function regression
//! **in coefficient space** — either PCR (reusing [`crate::regression::fdata_to_pc_1d`])
//! or PLS (reusing [`crate::regression::fdata_to_pls_1d`]). The fitted
//! coefficient-space weights are then mapped back to the time-domain functional
//! coefficient β(t) by the inverse DWT ([`crate::wavelet::reconstruct`]).
//!
//! ## Why this recovers β(t) exactly
//!
//! The multi-level orthogonal DWT is a linear, orthonormal map `W`: the design row
//! for curve `i` is `c_i = W x_i` (wavelet coefficients). If the true relationship
//! is `y = α + C β_c` in coefficient space (with `C` the coefficient design), then
//! in the time domain `y = α + X (Wᵀ β_c)`, so the time-domain coefficient is
//! `β(t) = Wᵀ β_c` — exactly the inverse DWT of the coefficient-space weights.
//! [`coeff_weights_to_beta_t`] performs that inverse DWT.
//!
//! ## Shared seams (reused by the `wnet` regressor, Plan 70-02)
//!
//! - [`curves_to_coeff_design`] — the curves → concatenated-coefficient-design seam.
//! - [`coeff_weights_to_beta_t`] — the coefficient-weights → β(t) seam.
//!
//! ## End-to-end example (via the prelude, WAV-06)
//!
//! ```
//! use fdars_core::prelude::*;
//!
//! fn main() -> Result<(), fdars_core::FdarError> {
//!     // 6 curves of length 32 (a db4-decomposable grid), built deterministically.
//!     let (n, m) = (6usize, 32usize);
//!     let mut flat = vec![0.0_f64; n * m];
//!     for i in 0..n {
//!         for j in 0..m {
//!             // A smooth, per-curve-varying fill (no RNG → deterministic doctest).
//!             let t = j as f64 / m as f64;
//!             flat[i + j * n] = ((i as f64 + 1.0) * t).sin() + 0.5 * (i as f64) * t;
//!         }
//!     }
//!     let data = FdMatrix::from_column_major(flat, n, m)?;
//!     let y: Vec<f64> = (0..n).map(|i| 1.0 + 0.3 * i as f64).collect();
//!
//!     // Fit the wavelet-domain PCR regressor, then predict + read the coefficients.
//!     let fit = wcr(&data, &y, &WcrConfig::default())?;
//!     let preds = fit.predict(&data)?;
//!     let beta = fit.beta_t();
//!     let fitted = fit.fitted_values();
//!
//!     assert_eq!(preds.len(), fitted.len());
//!     assert_eq!(beta.len(), m);
//!
//!     // Self-consistency: predicting on the TRAINING curves reproduces the stored
//!     // fitted values exactly (the affine intercept folds in the centering offset).
//!     for (p, f) in preds.iter().zip(fitted) {
//!         assert!((p - f).abs() < 1e-7, "predict diverges from fitted: {p} vs {f}");
//!     }
//!     Ok(())
//! }
//! ```
//!
//! The full wavelet surface (DWT primitives + `wcr`/`wnet` + config/result types)
//! is re-exported at the crate root and via [`crate::prelude`] (Phase 71, WAV-06).

use crate::error::FdarError;
use crate::matrix::FdMatrix;
use crate::regression::{fdata_to_pc_1d, fdata_to_pls_1d};
use crate::wavelet::{decompose_matrix, reconstruct, BoundaryMode, WaveletCoeffs, WaveletFamily};

/// Which coefficient-space fit `wcr` uses.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum WcrMethod {
    /// Principal-component regression on the wavelet-coefficient design
    /// (reuses [`crate::regression::fdata_to_pc_1d`]).
    #[default]
    Pcr,
    /// Partial-least-squares regression on the wavelet-coefficient design
    /// (reuses [`crate::regression::fdata_to_pls_1d`]).
    Pls,
}

/// Configuration for [`wcr`].
///
/// The DWT parameters (`family`, `mode`, `level`) select the wavelet basis the
/// curves are transformed into; `ncomp` and `method` select the coefficient-space
/// fit. [`Default`] is db4 / periodic / auto-depth / PCR with `ncomp == 5`.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct WcrConfig {
    /// Wavelet family for the DWT of each curve (default [`WaveletFamily::Daubechies(4)`]).
    pub family: WaveletFamily,
    /// Boundary handling for the DWT (default [`BoundaryMode::Periodic`]).
    pub mode: BoundaryMode,
    /// Explicit decomposition depth; `None` (default) uses the maximum useful level.
    pub level: Option<usize>,
    /// Number of coefficient-space components (FPC or PLS) to fit.
    pub ncomp: usize,
    /// Which coefficient-space regressor to use (default [`WcrMethod::Pcr`]).
    pub method: WcrMethod,
}

impl Default for WcrConfig {
    fn default() -> Self {
        Self {
            family: WaveletFamily::Daubechies(4),
            mode: BoundaryMode::Periodic,
            level: None,
            ncomp: 5,
            method: WcrMethod::Pcr,
        }
    }
}

/// Result of a [`wcr`] fit.
///
/// Carries the time-domain functional coefficient β(t), the coefficient-space
/// weights it was reconstructed from, fitted values / residuals, and the DWT
/// configuration a future `predict` (Phase 71) needs to reproduce the transform.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct WcrResult {
    /// Affine intercept α such that
    /// `ŷ_i = intercept + Σ_j design[i,j] · coeff_weights[j]` reproduces the fitted
    /// values directly (matching the [`predict`](WcrResult::predict) formula and
    /// `wnet`'s intercept convention).
    ///
    /// This is NOT the raw OLS intercept from the score regression: the centering
    /// offset `Σ_j col_mean_j · coeff_weights[j]` has been folded in. Because of
    /// this, manual reconstruction from the public fields uses the stored intercept
    /// as-is (no re-centering needed).
    pub intercept: f64,
    /// Time-domain functional coefficient β(t) (length `m` = curve length).
    pub beta_t: Vec<f64>,
    /// Fitted response values (length `n`).
    pub fitted_values: Vec<f64>,
    /// Residuals `y - ŷ` (length `n`).
    pub residuals: Vec<f64>,
    /// Effective number of coefficient-space components used.
    pub ncomp: usize,
    /// The fitting method used.
    pub method: WcrMethod,
    /// Coefficient-space functional coefficient (length `P` = total wavelet coefficients).
    pub coeff_weights: Vec<f64>,
    /// Wavelet family used for the DWT (for reproducing the transform in prediction).
    pub family: WaveletFamily,
    /// Boundary mode used for the DWT.
    pub mode: BoundaryMode,
    /// Effective decomposition depth used.
    pub level: usize,
}

/// Band layout of a per-curve wavelet-coefficient vector — everything
/// [`reconstruct`] needs to rebuild a [`WaveletCoeffs`] shell from a flat vector.
///
/// Coefficients are concatenated **finest-first**: `[approx ++ details[0] ++
/// details[1] ++ ...]`, matching [`WaveletCoeffs`] band order.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct CoeffLayout {
    /// Length of the coarse approximation band (the first `approx_len` coefficients).
    pub(crate) approx_len: usize,
    /// Detail-band lengths, finest-first (matching [`WaveletCoeffs::details`] order).
    pub(crate) detail_lens: Vec<usize>,
    /// Original signal length (curve length `m`).
    pub(crate) signal_len: usize,
    /// Wavelet family used for the transform.
    pub(crate) family: WaveletFamily,
    /// Boundary mode used for the transform.
    pub(crate) mode: BoundaryMode,
    /// Per-level analysis-input lengths, finest-first (the [`WaveletCoeffs::level_lens`]).
    pub(crate) level_lens: Vec<usize>,
}

impl CoeffLayout {
    /// Total number of wavelet coefficients per curve (`P` = approx + all details).
    pub(crate) fn total_len(&self) -> usize {
        self.approx_len + self.detail_lens.iter().sum::<usize>()
    }

    /// Number of decomposition levels.
    pub(crate) fn levels(&self) -> usize {
        self.detail_lens.len()
    }
}

/// Flatten one curve's [`WaveletCoeffs`] into a finest-first coefficient vector.
fn coeffs_to_row(coeffs: &WaveletCoeffs) -> Vec<f64> {
    let mut row = Vec::with_capacity(
        coeffs.approx.len() + coeffs.details.iter().map(Vec::len).sum::<usize>(),
    );
    row.extend_from_slice(&coeffs.approx);
    for band in &coeffs.details {
        row.extend_from_slice(band);
    }
    row
}

/// SHARED SEAM. Transform every curve (row) of `data` into its concatenated
/// wavelet-coefficient vector, assembling an `n × P` design matrix.
///
/// Each row of the returned [`FdMatrix`] is one curve's `[approx ++ details...]`
/// (finest-first). All curves must share the same length (guaranteed by a common
/// evaluation grid), so every row has the same layout — the returned [`CoeffLayout`]
/// records that shared band structure so coefficient-space weights can be scattered
/// back to a [`WaveletCoeffs`] for the inverse DWT.
///
/// # Errors
/// - [`FdarError::InvalidDimension`] if `data` is empty (surfaced from
///   [`decompose_matrix`]), or if the per-curve coefficient layouts disagree
///   (should not happen for a common grid).
/// - [`FdarError::InvalidParameter`] if the family is unsupported or the level is
///   out of range (surfaced from [`decompose_matrix`]).
pub(crate) fn curves_to_coeff_design(
    data: &FdMatrix,
    family: WaveletFamily,
    mode: BoundaryMode,
    level: Option<usize>,
) -> Result<(FdMatrix, CoeffLayout), FdarError> {
    let per_curve = decompose_matrix(data, family.clone(), mode, level)?;
    // per_curve is non-empty: decompose_matrix rejects zero-row matrices.
    let first = &per_curve[0];
    let layout = CoeffLayout {
        approx_len: first.approx.len(),
        detail_lens: first.details.iter().map(Vec::len).collect(),
        signal_len: first.signal_len,
        family,
        mode,
        level_lens: first.level_lens.clone(),
    };
    let p = layout.total_len();
    let n = per_curve.len();

    // Assemble the n × P design in column-major order, validating that every curve
    // produced the same band structure as curve 0.
    let mut flat = vec![0.0_f64; n * p];
    for (i, coeffs) in per_curve.iter().enumerate() {
        if coeffs.approx.len() != layout.approx_len
            || coeffs.details.len() != layout.detail_lens.len()
            || coeffs
                .details
                .iter()
                .zip(&layout.detail_lens)
                .any(|(band, &len)| band.len() != len)
            || coeffs.signal_len != layout.signal_len
        {
            return Err(FdarError::InvalidDimension {
                parameter: "data",
                expected: format!("all curves share curve-0 coefficient layout (P = {p})"),
                actual: format!("curve {i} produced a different band structure"),
            });
        }
        let row = coeffs_to_row(coeffs);
        for (j, &v) in row.iter().enumerate() {
            flat[i + j * n] = v;
        }
    }

    let design = FdMatrix::from_column_major(flat, n, p)?;
    Ok((design, layout))
}

/// SHARED SEAM. Map a `P`-length coefficient-space weight vector back to the time
/// domain via the inverse DWT.
///
/// Splits `weights` into the approximation band and the finest-first detail bands per
/// `layout`, packs them into a [`WaveletCoeffs`] shell, and calls [`reconstruct`],
/// yielding β(t) of length `layout.signal_len`.
///
/// # Errors
/// - [`FdarError::InvalidDimension`] if `weights.len()` does not equal the total
///   coefficient count implied by `layout`.
/// - [`FdarError::InvalidParameter`] if the family is unsupported (surfaced from
///   [`reconstruct`]).
pub(crate) fn coeff_weights_to_beta_t(
    weights: &[f64],
    layout: &CoeffLayout,
) -> Result<Vec<f64>, FdarError> {
    let expected = layout.total_len();
    if weights.len() != expected {
        return Err(FdarError::InvalidDimension {
            parameter: "weights",
            expected: format!("{expected} coefficients (approx + all detail bands)"),
            actual: format!("{} coefficients", weights.len()),
        });
    }

    let approx = weights[..layout.approx_len].to_vec();
    let mut details: Vec<Vec<f64>> = Vec::with_capacity(layout.detail_lens.len());
    let mut offset = layout.approx_len;
    for &len in &layout.detail_lens {
        details.push(weights[offset..offset + len].to_vec());
        offset += len;
    }

    let coeffs = WaveletCoeffs {
        approx,
        details,
        levels: layout.levels(),
        signal_len: layout.signal_len,
        family: layout.family.clone(),
        mode: layout.mode,
        level_lens: layout.level_lens.clone(),
    };
    reconstruct(&coeffs)
}

// ---------------------------------------------------------------------------
// Local OLS helpers (mirrors scalar_on_function's private OLS path; those helpers
// are module-private there, so wcr carries a small self-contained normal-equations
// solver rather than widening their visibility).
// ---------------------------------------------------------------------------

/// Build the OLS design `[1, scores]` (n × (1 + ncomp)).
fn design_with_intercept(scores: &FdMatrix, ncomp: usize) -> FdMatrix {
    let n = scores.nrows();
    let mut design = FdMatrix::zeros(n, 1 + ncomp);
    for i in 0..n {
        design[(i, 0)] = 1.0;
        for k in 0..ncomp {
            design[(i, 1 + k)] = scores[(i, k)];
        }
    }
    design
}

/// Solve OLS `min ||Xb - y||²` via normal equations with Cholesky.
fn ols_solve(x: &FdMatrix, y: &[f64]) -> Result<Vec<f64>, FdarError> {
    let (n, p) = x.shape();
    if n < p || p == 0 {
        return Err(FdarError::InvalidDimension {
            parameter: "design matrix",
            expected: format!("n >= p and p > 0 (p={p})"),
            actual: format!("n={n}, p={p}"),
        });
    }
    // X'X (p × p) and X'y (p).
    let mut xtx = vec![0.0_f64; p * p];
    let mut xty = vec![0.0_f64; p];
    for a in 0..p {
        for b in 0..p {
            let mut s = 0.0;
            for i in 0..n {
                s += x[(i, a)] * x[(i, b)];
            }
            xtx[a + b * p] = s;
        }
        let mut sy = 0.0;
        for i in 0..n {
            sy += x[(i, a)] * y[i];
        }
        xty[a] = sy;
    }
    let l = cholesky_factor(&xtx, p)?;
    Ok(cholesky_solve(&l, &xty, p))
}

/// Cholesky factor `A = L Lᵀ` (column-major `p × p`, lower-triangular `L`).
fn cholesky_factor(a: &[f64], p: usize) -> Result<Vec<f64>, FdarError> {
    let mut l = vec![0.0_f64; p * p];
    for j in 0..p {
        let mut diag = a[j + j * p];
        for k in 0..j {
            diag -= l[j + k * p] * l[j + k * p];
        }
        if diag <= 0.0 {
            return Err(FdarError::ComputationFailed {
                operation: "Cholesky factorization (wcr OLS)",
                detail: "design matrix X'X is not positive definite; try reducing ncomp"
                    .to_string(),
            });
        }
        let ljj = diag.sqrt();
        l[j + j * p] = ljj;
        for i in (j + 1)..p {
            let mut s = a[i + j * p];
            for k in 0..j {
                s -= l[i + k * p] * l[j + k * p];
            }
            l[i + j * p] = s / ljj;
        }
    }
    Ok(l)
}

/// Solve `L Lᵀ b = rhs` by forward then back substitution.
fn cholesky_solve(l: &[f64], rhs: &[f64], p: usize) -> Vec<f64> {
    // Forward: L z = rhs.
    let mut z = vec![0.0_f64; p];
    for i in 0..p {
        let mut s = rhs[i];
        for k in 0..i {
            s -= l[i + k * p] * z[k];
        }
        z[i] = s / l[i + i * p];
    }
    // Back: Lᵀ b = z.
    let mut b = vec![0.0_f64; p];
    for i in (0..p).rev() {
        let mut s = z[i];
        for k in (i + 1)..p {
            s -= l[k + i * p] * b[k];
        }
        b[i] = s / l[i + i * p];
    }
    b
}

/// Recover the plain-dot coefficient-space functional coefficient β_coeff.
///
/// The fit's predictions satisfy `fitted_i = intercept + ⟨centered_row_i, β_coeff⟩`
/// (plain dot) for a unique `β_coeff` in the span of the (full-column-rank, `P ≤ n`)
/// coefficient design. This regresses the centered fitted contribution
/// `fitted_i - intercept` onto the column-centered design via the normal equations,
/// recovering that exact `β_coeff` independently of which reduced-rank method (PCR or
/// PLS) produced the fit or which internal integration weighting it used.
fn recover_coeff_weights(
    design: &FdMatrix,
    fitted: &[f64],
    intercept: f64,
) -> Result<Vec<f64>, FdarError> {
    let (n, p) = design.shape();
    // Column means (centering absorbs the intercept).
    let col_means: Vec<f64> = (0..p)
        .map(|j| design.column(j).iter().sum::<f64>() / n as f64)
        .collect();
    // Centered design X_c and centered target r = fitted - intercept.
    let mut xc = FdMatrix::zeros(n, p);
    for j in 0..p {
        for i in 0..n {
            xc[(i, j)] = design[(i, j)] - col_means[j];
        }
    }
    let r: Vec<f64> = fitted.iter().map(|&f| f - intercept).collect();
    // Normal equations X_c' X_c b = X_c' r.
    let mut xtx = vec![0.0_f64; p * p];
    let mut xtr = vec![0.0_f64; p];
    for a in 0..p {
        for b in 0..p {
            let mut s = 0.0;
            for i in 0..n {
                s += xc[(i, a)] * xc[(i, b)];
            }
            xtx[a + b * p] = s;
        }
        let mut sr = 0.0;
        for i in 0..n {
            sr += xc[(i, a)] * r[i];
        }
        xtr[a] = sr;
    }
    // Ridge-nudge the diagonal for numerical stability against rank-deficient bands
    // (near-zero-variance coefficient columns from short signals); tiny relative to
    // the trace, so it does not perturb a well-posed recovery.
    let trace: f64 = (0..p).map(|j| xtx[j + j * p]).sum();
    let eps = 1e-10 * (trace / p as f64).max(1e-12);
    for j in 0..p {
        xtx[j + j * p] += eps;
    }
    let l = cholesky_factor(&xtx, p)?;
    Ok(cholesky_solve(&l, &xtr, p))
}

/// Compute fitted values `ŷ = X b`.
fn compute_fitted(design: &FdMatrix, coeffs: &[f64]) -> Vec<f64> {
    let (n, p) = design.shape();
    (0..n)
        .map(|i| {
            let mut yhat = 0.0;
            for j in 0..p {
                yhat += design[(i, j)] * coeffs[j];
            }
            yhat
        })
        .collect()
}

// ---------------------------------------------------------------------------
// wcr entry point
// ---------------------------------------------------------------------------

/// Fit the wavelet-domain scalar-on-function regressor `wcr` (WAV-03).
///
/// Transforms every curve into its wavelet-coefficient vector, fits PCR or PLS in
/// coefficient space (per `config.method`), and reconstructs the time-domain
/// functional coefficient β(t) via the inverse DWT.
///
/// The coefficient index is treated as an abstract basis: the PCR/PLS calls receive
/// a uniform grid `0..P` as their `argvals` (Simpson integration weights over that
/// grid), since wavelet coefficients carry no intrinsic spacing.
///
/// # Arguments
/// * `data` — functional predictor matrix (n × m), one curve per row.
/// * `y` — scalar response (length n).
/// * `config` — DWT + coefficient-space fit configuration.
///
/// # Errors
/// - [`FdarError::InvalidDimension`] if `data` has fewer than 3 rows, zero columns,
///   or `y.len() != n`.
/// - [`FdarError::InvalidParameter`] if `config.ncomp == 0`, or if the DWT rejects
///   the family/level (surfaced from [`decompose_matrix`]).
/// - [`FdarError::ComputationFailed`] if the underlying PCA/PLS or OLS fails.
#[must_use = "expensive computation whose result should not be discarded"]
pub fn wcr(data: &FdMatrix, y: &[f64], config: &WcrConfig) -> Result<WcrResult, FdarError> {
    let (n, m) = data.shape();
    if n < 3 {
        return Err(FdarError::InvalidDimension {
            parameter: "data",
            expected: "at least 3 rows (observations)".to_string(),
            actual: format!("{n} rows"),
        });
    }
    if m == 0 {
        return Err(FdarError::InvalidDimension {
            parameter: "data",
            expected: "at least 1 column (evaluation point)".to_string(),
            actual: format!("{m} columns"),
        });
    }
    if y.len() != n {
        return Err(FdarError::InvalidDimension {
            parameter: "y",
            expected: format!("{n} elements (== data rows)"),
            actual: format!("{} elements", y.len()),
        });
    }
    if config.ncomp == 0 {
        return Err(FdarError::InvalidParameter {
            parameter: "ncomp",
            message: "ncomp must be >= 1".to_string(),
        });
    }

    // Curves -> coefficient design (shared seam). Surfaces DWT errors unchanged.
    let (design, layout) =
        curves_to_coeff_design(data, config.family.clone(), config.mode, config.level)?;
    let p = design.ncols();

    // Coefficients form an abstract basis: use a uniform 0..P grid for integration.
    let argvals: Vec<f64> = (0..p).map(|j| j as f64).collect();

    // Clamp effective ncomp to the fittable rank. The OLS design is `[1, scores]`
    // (n × (ncomp + 1)), so `ols_solve` needs ncomp + 1 <= n, i.e. ncomp <= n - 1;
    // otherwise a valid small-n call (e.g. default ncomp = 5 with n <= 5) would be
    // rejected by ols_solve's `n < p` guard. `n >= 3` is enforced above, so
    // `n.saturating_sub(1) >= 2`.
    let ncomp = config.ncomp.min(n.saturating_sub(1)).min(p);

    // Fit in coefficient space: PCR or PLS yields reduced-rank scores, then OLS on
    // [1, scores] gives the intercept and fitted values.
    let (scores, ncomp) = match config.method {
        WcrMethod::Pcr => {
            let fpca = fdata_to_pc_1d(&design, ncomp, &argvals)?;
            let k = fpca.scores.ncols();
            (fpca.scores, k)
        }
        WcrMethod::Pls => {
            let pls = fdata_to_pls_1d(&design, y, ncomp, &argvals)?;
            let k = pls.scores.ncols();
            (pls.scores, k)
        }
    };
    let ols_design = design_with_intercept(&scores, ncomp);
    let coeffs = ols_solve(&ols_design, y)?;
    let intercept = coeffs[0];
    let fitted_values = compute_fitted(&ols_design, &coeffs);

    // Recover the coefficient-space functional coefficient β_coeff directly in the raw
    // wavelet-coefficient basis, so β(t) acts on a curve by the plain functional inner
    // product ⟨coeffs(curve), β_coeff⟩ (matching decompose → concatenate → dot).
    //
    // The score-projection recovery (Σ_k γ_k · rotation/weight_k) instead yields β_coeff
    // in each method's *internal* integration-weighted inner product (sqrt-weighted for
    // PCR's SVD, int-weighted for PLS's NIPALS), which does not match a plain dot. Since
    // the reduced-rank fitted values lie exactly in the span of the coefficient design
    // (full column rank here, P ≤ n), regressing the centered fitted contribution back
    // onto the centered design recovers the exact, method-agnostic plain-dot β_coeff.
    let coeff_weights = recover_coeff_weights(&design, &fitted_values, intercept)?;

    // Re-express the intercept in the affine coefficient-space convention so that
    // `fitted_i == intercept + Σ_j design[i,j]·coeff_weights[j]` holds directly
    // (matching `compute_fitted_affine`, and mirroring `wnet`'s intercept). The
    // centered recovery above satisfies `fitted_i = intercept +
    // Σ_j (design[i,j] − col_mean_j)·w_j`, so the affine intercept folds in the
    // constant centering offset `Σ_j col_mean_j·w_j`. This makes `predict` (WAV-05)
    // reproduce the stored `fitted_values` exactly. β(t) (the slope) is unchanged.
    let (n_rows, p_cols) = design.shape();
    let intercept = {
        let offset: f64 = (0..p_cols)
            .map(|j| {
                let col_mean = design.column(j).iter().sum::<f64>() / n_rows as f64;
                col_mean * coeff_weights[j]
            })
            .sum();
        intercept - offset
    };

    // β(t) via inverse DWT of the coefficient-space weights (shared seam).
    let beta_t = coeff_weights_to_beta_t(&coeff_weights, &layout)?;

    let residuals: Vec<f64> = y
        .iter()
        .zip(&fitted_values)
        .map(|(&yi, &yh)| yi - yh)
        .collect();

    Ok(WcrResult {
        intercept,
        beta_t,
        fitted_values,
        residuals,
        ncomp,
        method: config.method,
        coeff_weights,
        family: config.family.clone(),
        mode: config.mode,
        level: layout.levels(),
    })
}

impl WcrResult {
    /// Predict the scalar response for new functional curves (WAV-05).
    ///
    /// Re-transforms each new curve into the wavelet-coefficient design using the
    /// STORED fitted DWT configuration (`family` / `mode` / effective `level`), then
    /// applies the affine coefficient-space map `ŷ = intercept + Σ_j design[i,j] ·
    /// coeff_weights[j]`. Re-passing the training curves reproduces the stored
    /// [`fitted_values`](WcrResult::fitted_values) exactly (up to float rounding).
    ///
    /// # Arguments
    /// * `new` — functional predictor matrix (rows = curves) on the SAME evaluation
    ///   grid as the training data (`new.ncols()` must equal the training grid length).
    ///
    /// # Errors
    /// - [`FdarError::InvalidDimension`] with `parameter: "new"` if `new` has zero
    ///   rows (no curves to predict), if `new.ncols()` differs from the training
    ///   grid length, or (defensively) if the re-transformed design width disagrees
    ///   with the stored coefficient-space width.
    /// - [`FdarError::InvalidParameter`] if the DWT rejects the stored family/level
    ///   (surfaced from [`decompose_matrix`]).
    pub fn predict(&self, new: &FdMatrix) -> Result<Vec<f64>, FdarError> {
        let train_m = self.beta_t.len();
        if new.nrows() == 0 {
            return Err(FdarError::InvalidDimension {
                parameter: "new",
                expected: "at least 1 row (curve)".to_string(),
                actual: "0 rows".to_string(),
            });
        }
        if new.ncols() != train_m {
            return Err(FdarError::InvalidDimension {
                parameter: "new",
                expected: format!("{train_m} columns (== training grid length)"),
                actual: format!("{} columns", new.ncols()),
            });
        }
        // Re-transform with the STORED fitted DWT config so the new-curve design
        // matches the fit-time design exactly.
        let (design, _layout) =
            curves_to_coeff_design(new, self.family.clone(), self.mode, Some(self.level))?;
        if design.ncols() != self.coeff_weights.len() {
            return Err(FdarError::InvalidDimension {
                parameter: "new",
                expected: format!(
                    "coefficient-space width {} (== stored coeff_weights)",
                    self.coeff_weights.len()
                ),
                actual: format!("{} coefficients", design.ncols()),
            });
        }
        Ok(compute_fitted_affine(
            &design,
            &self.coeff_weights,
            self.intercept,
        ))
    }

    /// The time-domain functional coefficient β(t) (length `m` = curve length).
    #[must_use]
    pub fn beta_t(&self) -> &[f64] {
        &self.beta_t
    }

    /// The functional coefficient β(t) (crate-convention alias of [`beta_t`](WcrResult::beta_t)).
    #[must_use]
    pub fn coefficient_function(&self) -> &[f64] {
        &self.beta_t
    }

    /// The fitted response values (length `n`).
    #[must_use]
    pub fn fitted_values(&self) -> &[f64] {
        &self.fitted_values
    }
}

// ===========================================================================
// wnet — wavelet-domain elastic-net scalar-on-function regressor (WAV-04)
// ===========================================================================
//
// `wnet` is the sparse/elastic-net half of the wavelet-domain regressor pair.
// It reuses the shared `curves_to_coeff_design` / `coeff_weights_to_beta_t`
// seams above, but fits an **elastic-net** (L1 lasso + L2 ridge) directly on
// the wavelet-coefficient design via a NEW thin per-coefficient coordinate-
// descent adapter ([`elastic_net_cd`]), with a deterministic cross-validated λ
// ([`wnet_cv_lambda`]). A sparse wavelet basis is exactly where L1 shrinkage
// shines: localized signal concentrates in a few coefficients, and the L1
// penalty drives the rest to exactly zero.
//
// The per-coefficient CD is modeled on the group-lasso soft-threshold PATTERN
// in `scalar_on_function::additive` (partial-residual → coordinate update →
// shrink) but is scalar-per-coefficient (elastic-net), not group-lasso.

/// Configuration for [`wnet`].
///
/// The DWT parameters (`family`, `mode`, `level`) select the wavelet basis the
/// curves are transformed into (same defaults as [`WcrConfig`]: db4 / periodic /
/// auto-depth). `alpha` mixes L1 vs L2 (`alpha == 1` is pure lasso, `alpha == 0`
/// is pure ridge), and the remaining fields drive the deterministic K-fold
/// cross-validated λ search.
///
/// [`Default`] is db4 / periodic / auto-depth, `alpha == 0.5`, an auto geometric
/// λ grid of 50 values, 5 folds, fixed seed 0, `max_iter == 1000`, `tol == 1e-6`.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct WnetConfig {
    /// Wavelet family for the DWT of each curve (default [`WaveletFamily::Daubechies(4)`]).
    pub family: WaveletFamily,
    /// Boundary handling for the DWT (default [`BoundaryMode::Periodic`]).
    pub mode: BoundaryMode,
    /// Explicit decomposition depth; `None` (default) uses the maximum useful level.
    pub level: Option<usize>,
    /// Elastic-net mixing parameter ∈ [0, 1]: `1.0` is pure L1 (lasso),
    /// `0.0` is pure L2 (ridge). Default `0.5`.
    pub alpha: f64,
    /// Explicit λ grid to search. `None` (default) auto-builds a geometric grid.
    pub lambda_grid: Option<Vec<f64>>,
    /// Number of λ values in the auto geometric grid (used when `lambda_grid` is
    /// `None`). Default `50`.
    pub n_lambda: usize,
    /// Number of cross-validation folds. Default `5`.
    pub n_folds: usize,
    /// Fixed RNG seed for the (deterministic) fold partition. Default `0`.
    pub seed: u64,
    /// Maximum coordinate-descent sweeps. Default `1000`.
    pub max_iter: usize,
    /// Coordinate-descent convergence tolerance (max |Δβ| per sweep). Default `1e-6`.
    pub tol: f64,
}

impl Default for WnetConfig {
    fn default() -> Self {
        Self {
            family: WaveletFamily::Daubechies(4),
            mode: BoundaryMode::Periodic,
            level: None,
            alpha: 0.5,
            lambda_grid: None,
            n_lambda: 50,
            n_folds: 5,
            seed: 0,
            max_iter: 1000,
            tol: 1e-6,
        }
    }
}

/// Result of a [`wnet`] fit.
///
/// Carries the time-domain functional coefficient β(t), the sparse coefficient-
/// space weights it was reconstructed from, the indices of the nonzero
/// (selected) coefficients, the CV-selected λ, the elastic-net mixing `alpha`,
/// fitted values / residuals, and the DWT configuration a future `predict`
/// (Phase 71) needs to reproduce the transform.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct WnetResult {
    /// Affine intercept α such that
    /// `ŷ_i = intercept + Σ_j design[i,j] · coeff_weights[j]` reproduces the fitted
    /// values directly (the elastic-net affine intercept, matching the
    /// [`predict`](WnetResult::predict) formula).
    pub intercept: f64,
    /// Time-domain functional coefficient β(t) (length `m` = curve length).
    pub beta_t: Vec<f64>,
    /// Fitted response values (length `n`).
    pub fitted_values: Vec<f64>,
    /// Residuals `y - ŷ` (length `n`).
    pub residuals: Vec<f64>,
    /// Coefficient-space functional coefficient (length `P` = total wavelet
    /// coefficients) — sparse (many exact zeros).
    pub coeff_weights: Vec<f64>,
    /// Indices (into `coeff_weights`) of the nonzero/selected coefficients.
    pub selected: Vec<usize>,
    /// Cross-validation-selected λ.
    pub lambda: f64,
    /// Elastic-net mixing parameter used (`config.alpha`).
    pub alpha: f64,
    /// Wavelet family used for the DWT (for reproducing the transform in prediction).
    pub family: WaveletFamily,
    /// Boundary mode used for the DWT.
    pub mode: BoundaryMode,
    /// Effective decomposition depth used.
    pub level: usize,
}

/// Soft-threshold operator `sign(z)·max(|z| - γ, 0)` (the L1 proximal step).
#[inline]
fn soft_threshold(z: f64, gamma: f64) -> f64 {
    if z > gamma {
        z - gamma
    } else if z < -gamma {
        z + gamma
    } else {
        0.0
    }
}

/// SHARED CD ENGINE. Per-coefficient elastic-net coordinate descent on the raw
/// wavelet-coefficient design.
///
/// Fits `min_β (1/2n)‖y - α - Xβ‖² + λ[α_mix‖β‖₁ + ½(1-α_mix)‖β‖²]` by cyclic
/// coordinate descent. For coordinate `j`, the update uses the partial residual
/// `r = y_centered - Σ_{k≠j} βₖ X_c,ₖ` (maintained via a running fitted vector for
/// O(nP)/sweep), the coordinate gradient `z_j = (X_c,ⱼ · r)/n`, then applies the
/// L1 soft-threshold with the L2-ridge denominator:
/// `βⱼ = soft(z_j, λ·α_mix) / (‖X_c,ⱼ‖²/n + λ(1-α_mix))`.
///
/// Columns are centered internally (so the penalty is scale-consistent across
/// coefficients only up to their own norm — we do NOT rescale to unit variance,
/// keeping the coefficient-space geometry faithful to the DWT). The intercept is
/// recovered as `mean(y) - Σ βⱼ·mean(Xⱼ)` on the un-centered column means.
///
/// Returns `(intercept, coeff_weights)` where `coeff_weights` has length `P`.
///
/// # Errors
/// - [`FdarError::InvalidDimension`] if `y.len()` does not equal `design.nrows()`.
/// - [`FdarError::InvalidParameter`] if `alpha` is outside `[0, 1]`, `lambda` is
///   negative or non-finite, `tol` is negative or non-finite, or `max_iter == 0`.
pub(crate) fn elastic_net_cd(
    design: &FdMatrix,
    y: &[f64],
    lambda: f64,
    alpha: f64,
    max_iter: usize,
    tol: f64,
) -> Result<(f64, Vec<f64>), FdarError> {
    let (n, p) = design.shape();
    if y.len() != n {
        return Err(FdarError::InvalidDimension {
            parameter: "y",
            expected: format!("{n} elements (== design rows)"),
            actual: format!("{} elements", y.len()),
        });
    }
    if !(0.0..=1.0).contains(&alpha) {
        return Err(FdarError::InvalidParameter {
            parameter: "alpha",
            message: format!("alpha must be in [0, 1], got {alpha}"),
        });
    }
    if lambda < 0.0 || !lambda.is_finite() {
        return Err(FdarError::InvalidParameter {
            parameter: "lambda",
            message: format!("lambda must be finite and >= 0, got {lambda}"),
        });
    }
    if !tol.is_finite() || tol < 0.0 {
        return Err(FdarError::InvalidParameter {
            parameter: "tol",
            message: format!("tol must be finite and >= 0, got {tol}"),
        });
    }
    if max_iter == 0 {
        return Err(FdarError::InvalidParameter {
            parameter: "max_iter",
            message: "max_iter must be >= 1".to_string(),
        });
    }

    let n_f = n as f64;
    let mu_y = y.iter().sum::<f64>() / n_f;
    let y_centered: Vec<f64> = y.iter().map(|&v| v - mu_y).collect();

    // Per-column means and centered columns; precompute ‖X_c,ⱼ‖²/n.
    let col_means: Vec<f64> = (0..p)
        .map(|j| design.column(j).iter().sum::<f64>() / n_f)
        .collect();
    let mut xc = vec![0.0_f64; n * p]; // column-major, n × p
    let mut col_norm_sq_over_n = vec![0.0_f64; p];
    for j in 0..p {
        let mu = col_means[j];
        let mut norm_sq = 0.0;
        let col = design.column(j);
        for i in 0..n {
            let v = col[i] - mu;
            xc[i + j * n] = v;
            norm_sq += v * v;
        }
        col_norm_sq_over_n[j] = norm_sq / n_f;
    }

    // Coefficients start at zero; the running fit tracks Σⱼ βⱼ X_c,ⱼ so a
    // coordinate's partial residual is (y_centered - fit + βⱼ X_c,ⱼ) in O(n).
    let mut beta = vec![0.0_f64; p];
    let mut fit = vec![0.0_f64; n]; // Σⱼ βⱼ X_c,ⱼ
    let l1 = lambda * alpha;
    let l2 = lambda * (1.0 - alpha);

    for _sweep in 0..max_iter {
        let mut max_delta = 0.0_f64;
        for j in 0..p {
            let denom = col_norm_sq_over_n[j] + l2;
            if denom <= 0.0 {
                // Dead column (zero-variance) with no ridge: leave at zero.
                if beta[j] != 0.0 {
                    let old = beta[j];
                    for i in 0..n {
                        fit[i] -= old * xc[i + j * n];
                    }
                    max_delta = max_delta.max(old.abs());
                    beta[j] = 0.0;
                }
                continue;
            }
            // z_j = (X_c,ⱼ · partial_residual)/n where
            // partial_residual = y_centered - (fit - βⱼ X_c,ⱼ).
            let old = beta[j];
            let mut dot = 0.0;
            for i in 0..n {
                let r = y_centered[i] - fit[i] + old * xc[i + j * n];
                dot += xc[i + j * n] * r;
            }
            let z = dot / n_f;
            let new = soft_threshold(z, l1) / denom;
            if new != old {
                let diff = new - old;
                for i in 0..n {
                    fit[i] += diff * xc[i + j * n];
                }
                max_delta = max_delta.max(diff.abs());
                beta[j] = new;
            }
        }
        if max_delta < tol {
            break;
        }
    }

    // Intercept on un-centered column means: mu_y - Σ βⱼ·mean(Xⱼ).
    let intercept = mu_y - (0..p).map(|j| beta[j] * col_means[j]).sum::<f64>();
    Ok((intercept, beta))
}

/// Build the geometric λ grid used by [`wnet_cv_lambda`].
///
/// If `config.lambda_grid` is `Some`, that grid is returned verbatim (validated
/// non-empty by the caller). Otherwise a log-spaced grid of `config.n_lambda`
/// values from `λ_max` down to `λ_max · ε` (ε = 1e-3) is built, where `λ_max` is
/// the smallest λ that zeroes every coefficient:
/// `λ_max = max_j |X_c,ⱼ · y_centered| / (n · max(α, tiny))`.
///
/// The grid is returned in descending order (largest/sparsest λ first) so ties in
/// CV-MSE naturally resolve toward the larger λ when scanned.
fn build_lambda_grid(design: &FdMatrix, y: &[f64], config: &WnetConfig) -> Vec<f64> {
    if let Some(grid) = &config.lambda_grid {
        return grid.clone();
    }
    let (n, p) = design.shape();
    let n_f = n as f64;
    let mu_y = y.iter().sum::<f64>() / n_f;
    let y_centered: Vec<f64> = y.iter().map(|&v| v - mu_y).collect();

    // λ_max = max_j |X_c,ⱼ · y_centered| / (n·α_eff).
    let alpha_eff = config.alpha.max(1e-3);
    let mut max_corr = 0.0_f64;
    for j in 0..p {
        let mu = design.column(j).iter().sum::<f64>() / n_f;
        let col = design.column(j);
        let dot: f64 = (0..n).map(|i| (col[i] - mu) * y_centered[i]).sum();
        max_corr = max_corr.max(dot.abs());
    }
    let lambda_max = (max_corr / (n_f * alpha_eff)).max(1e-8);

    let n_lambda = config.n_lambda.max(1);
    if n_lambda == 1 {
        return vec![lambda_max];
    }
    let eps = 1e-3_f64;
    let log_max = lambda_max.ln();
    let log_min = (lambda_max * eps).ln();
    let step = (log_max - log_min) / (n_lambda as f64 - 1.0);
    (0..n_lambda)
        .map(|k| (log_max - step * k as f64).exp())
        .collect()
}

/// SHARED CV HELPER. Deterministic K-fold cross-validated λ selection for `wnet`.
///
/// Builds the geometric λ grid (or uses `config.lambda_grid`), partitions the `n`
/// observations into `config.n_folds` folds via [`crate::cv::create_folds`] with
/// the FIXED `config.seed` (so the partition — and therefore the selected λ — is
/// identical across runs), computes CV-MSE per λ (fit [`elastic_net_cd`] on each
/// training set, score on the held-out fold), and returns the λ minimizing
/// CV-MSE. Ties (within a small epsilon) resolve toward the LARGER λ (sparser).
///
/// # Errors
/// - [`FdarError::InvalidParameter`] if `config.n_folds < 2`,
///   `config.n_folds > n`, `config.alpha` is outside `[0, 1]`, or an explicit
///   `lambda_grid` is empty.
/// - [`FdarError::InvalidDimension`] if `y.len()` does not equal `design.nrows()`.
pub(crate) fn wnet_cv_lambda(
    design: &FdMatrix,
    y: &[f64],
    config: &WnetConfig,
) -> Result<f64, FdarError> {
    let (n, _p) = design.shape();
    if y.len() != n {
        return Err(FdarError::InvalidDimension {
            parameter: "y",
            expected: format!("{n} elements (== design rows)"),
            actual: format!("{} elements", y.len()),
        });
    }
    if config.n_folds < 2 {
        return Err(FdarError::InvalidParameter {
            parameter: "n_folds",
            message: format!("n_folds must be >= 2, got {}", config.n_folds),
        });
    }
    if config.n_folds > n {
        return Err(FdarError::InvalidParameter {
            parameter: "n_folds",
            message: format!(
                "n_folds ({}) must not exceed the number of observations ({n})",
                config.n_folds
            ),
        });
    }
    if !(0.0..=1.0).contains(&config.alpha) {
        return Err(FdarError::InvalidParameter {
            parameter: "alpha",
            message: format!("alpha must be in [0, 1], got {}", config.alpha),
        });
    }
    if let Some(grid) = &config.lambda_grid {
        if grid.is_empty() {
            return Err(FdarError::InvalidParameter {
                parameter: "lambda_grid",
                message: "explicit lambda_grid must be non-empty".to_string(),
            });
        }
    }

    let grid = build_lambda_grid(design, y, config);
    let folds = crate::cv::create_folds(n, config.n_folds, config.seed);

    // Precompute per-fold train/test index sets (shared across all λ).
    let fold_sets: Vec<(Vec<usize>, Vec<usize>)> = (0..config.n_folds)
        .map(|f| crate::cv::fold_indices(&folds, f))
        .collect();

    let mut best_lambda = grid[0];
    let mut best_mse = f64::INFINITY;
    let tie_eps = 1e-12;

    for &lam in &grid {
        let mut total_sse = 0.0_f64;
        let mut scored = 0usize;
        for (train_idx, test_idx) in &fold_sets {
            if train_idx.is_empty() || test_idx.is_empty() {
                continue;
            }
            let train_data = crate::cv::subset_rows(design, train_idx);
            let train_y = crate::cv::subset_vec(y, train_idx);
            let (intercept, beta) = elastic_net_cd(
                &train_data,
                &train_y,
                lam,
                config.alpha,
                config.max_iter,
                config.tol,
            )?;
            for &oi in test_idx {
                let mut yhat = intercept;
                for j in 0..design.ncols() {
                    yhat += design[(oi, j)] * beta[j];
                }
                let e = y[oi] - yhat;
                total_sse += e * e;
                scored += 1;
            }
        }
        if scored == 0 {
            continue;
        }
        let mse = total_sse / scored as f64;
        // Grid is descending (largest λ first). Strictly-less keeps the FIRST
        // (larger) λ on a tie; the epsilon guards float noise so a marginally
        // smaller MSE at a smaller λ does not override a near-equal larger λ.
        if mse < best_mse - tie_eps {
            best_mse = mse;
            best_lambda = lam;
        }
    }

    Ok(best_lambda)
}

// ---------------------------------------------------------------------------
// wnet entry point
// ---------------------------------------------------------------------------

/// Fit the wavelet-domain elastic-net scalar-on-function regressor `wnet` (WAV-04).
///
/// Transforms every curve into its wavelet-coefficient vector (shared seam
/// [`curves_to_coeff_design`]), selects a deterministic cross-validated λ
/// ([`wnet_cv_lambda`]), refits the per-coefficient elastic-net
/// ([`elastic_net_cd`]) at that λ on the full data, and reconstructs the
/// time-domain functional coefficient β(t) via the inverse DWT (shared seam
/// [`coeff_weights_to_beta_t`]).
///
/// Because a sparse wavelet basis concentrates localized signal in a few
/// coefficients, the L1 penalty drives the rest to exactly zero — the nonzero
/// indices are reported in [`WnetResult::selected`].
///
/// # Arguments
/// * `data` — functional predictor matrix (n × m), one curve per row.
/// * `y` — scalar response (length n).
/// * `config` — DWT + elastic-net + CV configuration.
///
/// # Errors
/// - [`FdarError::InvalidDimension`] if `data` has fewer than 3 rows, zero
///   columns, or `y.len() != n`.
/// - [`FdarError::InvalidParameter`] if `config.alpha ∉ [0, 1]`,
///   `config.n_folds < 2`, `config.n_folds > n`, `config.max_iter == 0`,
///   `config.tol` is negative or non-finite, an explicit `config.lambda_grid`
///   is empty, or the DWT rejects the family/level (surfaced from
///   [`decompose_matrix`]).
/// - [`FdarError::ComputationFailed`] if the underlying transform fails.
#[must_use = "expensive computation whose result should not be discarded"]
pub fn wnet(data: &FdMatrix, y: &[f64], config: &WnetConfig) -> Result<WnetResult, FdarError> {
    let (n, m) = data.shape();
    if n < 3 {
        return Err(FdarError::InvalidDimension {
            parameter: "data",
            expected: "at least 3 rows (observations)".to_string(),
            actual: format!("{n} rows"),
        });
    }
    if m == 0 {
        return Err(FdarError::InvalidDimension {
            parameter: "data",
            expected: "at least 1 column (evaluation point)".to_string(),
            actual: format!("{m} columns"),
        });
    }
    if y.len() != n {
        return Err(FdarError::InvalidDimension {
            parameter: "y",
            expected: format!("{n} elements (== data rows)"),
            actual: format!("{} elements", y.len()),
        });
    }
    if !(0.0..=1.0).contains(&config.alpha) {
        return Err(FdarError::InvalidParameter {
            parameter: "alpha",
            message: format!("alpha must be in [0, 1], got {}", config.alpha),
        });
    }
    if config.n_folds < 2 {
        return Err(FdarError::InvalidParameter {
            parameter: "n_folds",
            message: format!("n_folds must be >= 2, got {}", config.n_folds),
        });
    }
    if config.n_folds > n {
        return Err(FdarError::InvalidParameter {
            parameter: "n_folds",
            message: format!(
                "n_folds ({}) must not exceed the number of observations ({n})",
                config.n_folds
            ),
        });
    }
    if config.max_iter == 0 {
        return Err(FdarError::InvalidParameter {
            parameter: "max_iter",
            message: "max_iter must be >= 1".to_string(),
        });
    }
    if !config.tol.is_finite() || config.tol < 0.0 {
        return Err(FdarError::InvalidParameter {
            parameter: "tol",
            message: format!("tol must be finite and >= 0, got {}", config.tol),
        });
    }
    if let Some(grid) = &config.lambda_grid {
        if grid.is_empty() {
            return Err(FdarError::InvalidParameter {
                parameter: "lambda_grid",
                message: "explicit lambda_grid must be non-empty".to_string(),
            });
        }
    }

    // Curves -> coefficient design (shared seam). Surfaces DWT errors unchanged.
    let (design, layout) =
        curves_to_coeff_design(data, config.family.clone(), config.mode, config.level)?;

    // Deterministic CV-selected λ, then refit on the full data at that λ.
    let lambda = wnet_cv_lambda(&design, y, config)?;
    let (intercept, coeff_weights) = elastic_net_cd(
        &design,
        y,
        lambda,
        config.alpha,
        config.max_iter,
        config.tol,
    )?;

    // Selected (nonzero) coefficients.
    let selected: Vec<usize> = coeff_weights
        .iter()
        .enumerate()
        .filter(|(_, &b)| b != 0.0)
        .map(|(j, _)| j)
        .collect();

    // Fitted values via the plain coefficient-space dot: ŷ = intercept + X·β.
    let fitted_values = compute_fitted_affine(&design, &coeff_weights, intercept);
    let residuals: Vec<f64> = y
        .iter()
        .zip(&fitted_values)
        .map(|(&yi, &yh)| yi - yh)
        .collect();

    // β(t) via inverse DWT of the coefficient-space weights (shared seam).
    let beta_t = coeff_weights_to_beta_t(&coeff_weights, &layout)?;

    Ok(WnetResult {
        intercept,
        beta_t,
        fitted_values,
        residuals,
        coeff_weights,
        selected,
        lambda,
        alpha: config.alpha,
        family: config.family.clone(),
        mode: config.mode,
        level: layout.levels(),
    })
}

impl WnetResult {
    /// Predict the scalar response for new functional curves (WAV-05).
    ///
    /// Re-transforms each new curve into the wavelet-coefficient design using the
    /// STORED fitted DWT configuration (`family` / `mode` / effective `level`), then
    /// applies the affine coefficient-space map `ŷ = intercept + Σ_j design[i,j] ·
    /// coeff_weights[j]`. Re-passing the training curves reproduces the stored
    /// [`fitted_values`](WnetResult::fitted_values) exactly (up to float rounding).
    ///
    /// # Arguments
    /// * `new` — functional predictor matrix (rows = curves) on the SAME evaluation
    ///   grid as the training data (`new.ncols()` must equal the training grid length).
    ///
    /// # Errors
    /// - [`FdarError::InvalidDimension`] with `parameter: "new"` if `new` has zero
    ///   rows (no curves to predict), if `new.ncols()` differs from the training
    ///   grid length, or (defensively) if the re-transformed design width disagrees
    ///   with the stored coefficient-space width.
    /// - [`FdarError::InvalidParameter`] if the DWT rejects the stored family/level
    ///   (surfaced from [`decompose_matrix`]).
    pub fn predict(&self, new: &FdMatrix) -> Result<Vec<f64>, FdarError> {
        let train_m = self.beta_t.len();
        if new.nrows() == 0 {
            return Err(FdarError::InvalidDimension {
                parameter: "new",
                expected: "at least 1 row (curve)".to_string(),
                actual: "0 rows".to_string(),
            });
        }
        if new.ncols() != train_m {
            return Err(FdarError::InvalidDimension {
                parameter: "new",
                expected: format!("{train_m} columns (== training grid length)"),
                actual: format!("{} columns", new.ncols()),
            });
        }
        // Re-transform with the STORED fitted DWT config so the new-curve design
        // matches the fit-time design exactly.
        let (design, _layout) =
            curves_to_coeff_design(new, self.family.clone(), self.mode, Some(self.level))?;
        if design.ncols() != self.coeff_weights.len() {
            return Err(FdarError::InvalidDimension {
                parameter: "new",
                expected: format!(
                    "coefficient-space width {} (== stored coeff_weights)",
                    self.coeff_weights.len()
                ),
                actual: format!("{} coefficients", design.ncols()),
            });
        }
        Ok(compute_fitted_affine(
            &design,
            &self.coeff_weights,
            self.intercept,
        ))
    }

    /// The time-domain functional coefficient β(t) (length `m` = curve length).
    #[must_use]
    pub fn beta_t(&self) -> &[f64] {
        &self.beta_t
    }

    /// The functional coefficient β(t) (crate-convention alias of [`beta_t`](WnetResult::beta_t)).
    #[must_use]
    pub fn coefficient_function(&self) -> &[f64] {
        &self.beta_t
    }

    /// The fitted response values (length `n`).
    #[must_use]
    pub fn fitted_values(&self) -> &[f64] {
        &self.fitted_values
    }
}

/// Compute fitted values `ŷ = intercept + X β` (affine coefficient-space dot).
fn compute_fitted_affine(design: &FdMatrix, coeffs: &[f64], intercept: f64) -> Vec<f64> {
    let (n, p) = design.shape();
    (0..n)
        .map(|i| {
            let mut yhat = intercept;
            for j in 0..p {
                yhat += design[(i, j)] * coeffs[j];
            }
            yhat
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::matrix::FdMatrix;

    /// Deterministic pseudo-random value stream (LCG) — spans full rank, no dep.
    /// Mirrors the DWT module's own test helper so the design is truly full-rank.
    fn pseudo_random(n: usize, seed: u64) -> Vec<f64> {
        let mut state = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
        (0..n)
            .map(|_| {
                state = state
                    .wrapping_mul(6_364_136_223_846_793_005)
                    .wrapping_add(1_442_695_040_888_963_407);
                let u = (state >> 11) as f64 / (1u64 << 53) as f64;
                2.0 * u - 1.0
            })
            .collect()
    }

    /// Build a spanning, full-rank n×m predicate design from independent
    /// pseudo-random rows (n ≫ m). Returns the FdMatrix.
    fn spanning_design(n: usize, m: usize, seed0: u64) -> FdMatrix {
        let mut flat = vec![0.0_f64; n * m];
        for i in 0..n {
            let row = pseudo_random(m, seed0 + i as u64);
            for j in 0..m {
                flat[i + j * n] = row[j];
            }
        }
        FdMatrix::from_column_major(flat, n, m).unwrap()
    }

    fn rel_l2(recovered: &[f64], truth: &[f64]) -> f64 {
        let num: f64 = recovered
            .iter()
            .zip(truth)
            .map(|(a, b)| (a - b) * (a - b))
            .sum::<f64>()
            .sqrt();
        let den: f64 = truth.iter().map(|b| b * b).sum::<f64>().sqrt().max(1e-300);
        num / den
    }

    /// Fit `wcr` with a given method on a spanning design where y is generated
    /// from a known coefficient-space β, and assert β(t) recovers the inverse-DWT
    /// of that β within a tight relative L2 tolerance.
    fn recovery_for_method(method: WcrMethod) {
        let (n, m) = (120usize, 32usize);
        let data = spanning_design(n, m, 1000);
        let family = WaveletFamily::Daubechies(4);
        let mode = BoundaryMode::Periodic;

        // Coefficient design + layout (the exact seam wcr uses internally).
        let (design, layout) = curves_to_coeff_design(&data, family.clone(), mode, None).unwrap();
        let p = design.ncols();

        // Known coefficient-space β and intercept; y is exact (no noise) so a
        // full-rank fit must recover β_coeff exactly.
        let beta_coeff = pseudo_random(p, 77);
        let true_intercept = 0.37_f64;
        let y: Vec<f64> = (0..n)
            .map(|i| {
                let mut acc = true_intercept;
                for j in 0..p {
                    acc += design[(i, j)] * beta_coeff[j];
                }
                acc
            })
            .collect();

        // The true time-domain coefficient is the inverse DWT of β_coeff.
        let beta_t_true = coeff_weights_to_beta_t(&beta_coeff, &layout).unwrap();

        // Fit with enough components to span the coefficient design (min(n, P)).
        let config = WcrConfig {
            family,
            mode,
            level: None,
            ncomp: p.min(n),
            method,
            ..Default::default()
        };
        let fit = wcr(&data, &y, &config).unwrap();

        assert_eq!(fit.method, method);
        assert_eq!(fit.beta_t.len(), m);
        assert_eq!(fit.coeff_weights.len(), p);

        let e = rel_l2(&fit.beta_t, &beta_t_true);
        assert!(
            e < 1e-6,
            "{method:?}: beta_t recovery rel L2 err {e} exceeds tolerance on spanning full-rank design"
        );

        // Finite outputs.
        assert!(fit.beta_t.iter().all(|x| x.is_finite()));
        assert!(fit.fitted_values.iter().all(|x| x.is_finite()));
        assert!(fit.residuals.iter().all(|x| x.is_finite()));
        // With an exact (noiseless) full-rank fit, residuals ≈ 0.
        let max_resid = fit
            .residuals
            .iter()
            .fold(0.0_f64, |acc, &r| acc.max(r.abs()));
        assert!(
            max_resid < 1e-6,
            "{method:?}: residuals not ~0 ({max_resid})"
        );
    }

    #[test]
    fn wcr_pcr_recovers_known_beta_t_on_spanning_design() {
        recovery_for_method(WcrMethod::Pcr);
    }

    #[test]
    fn wcr_pls_recovers_known_beta_t_on_spanning_design() {
        recovery_for_method(WcrMethod::Pls);
    }

    #[test]
    fn wcr_default_config_is_db4_periodic_auto_pcr() {
        let c = WcrConfig::default();
        assert_eq!(c.family, WaveletFamily::Daubechies(4));
        assert_eq!(c.mode, BoundaryMode::Periodic);
        assert_eq!(c.level, None);
        assert_eq!(c.method, WcrMethod::Pcr);
        assert_eq!(WcrMethod::default(), WcrMethod::Pcr);
    }

    #[test]
    fn curves_to_coeff_design_layout_and_shape() {
        let (n, m) = (10usize, 48usize);
        let data = spanning_design(n, m, 500);
        let (design, layout) = curves_to_coeff_design(
            &data,
            WaveletFamily::Daubechies(4),
            BoundaryMode::Periodic,
            None,
        )
        .unwrap();
        assert_eq!(design.nrows(), n);
        assert_eq!(design.ncols(), layout.total_len());
        assert_eq!(layout.signal_len, m);
        assert_eq!(layout.levels(), layout.detail_lens.len());
    }

    #[test]
    fn coeff_weights_to_beta_t_inverts_decompose() {
        // A round-trip sanity: reconstruct of a curve's own coefficients == curve.
        let (n, m) = (4usize, 48usize);
        let data = spanning_design(n, m, 900);
        let (design, layout) = curves_to_coeff_design(
            &data,
            WaveletFamily::Daubechies(6),
            BoundaryMode::Periodic,
            None,
        )
        .unwrap();
        let row0: Vec<f64> = (0..design.ncols()).map(|j| design[(0, j)]).collect();
        let recon = coeff_weights_to_beta_t(&row0, &layout).unwrap();
        let orig = data.row(0);
        assert!(rel_l2(&recon, &orig) < 1e-10);
    }

    #[test]
    fn coeff_weights_to_beta_t_rejects_wrong_length() {
        let (n, m) = (4usize, 48usize);
        let data = spanning_design(n, m, 901);
        let (_design, layout) =
            curves_to_coeff_design(&data, WaveletFamily::Haar, BoundaryMode::Periodic, None)
                .unwrap();
        let wrong = vec![0.0; layout.total_len() + 1];
        assert!(matches!(
            coeff_weights_to_beta_t(&wrong, &layout),
            Err(FdarError::InvalidDimension { .. })
        ));
    }

    // --- Validation gate (SC4) ---

    fn base_config() -> WcrConfig {
        WcrConfig {
            ncomp: 3,
            ..Default::default()
        }
    }

    #[test]
    fn wcr_rejects_too_few_rows() {
        let data = spanning_design(2, 48, 1);
        let y = vec![0.0, 1.0];
        assert!(matches!(
            wcr(&data, &y, &base_config()),
            Err(FdarError::InvalidDimension { .. })
        ));
    }

    #[test]
    fn wcr_rejects_mismatched_y_len() {
        let data = spanning_design(10, 48, 2);
        let y = vec![0.0; 9];
        assert!(matches!(
            wcr(&data, &y, &base_config()),
            Err(FdarError::InvalidDimension { .. })
        ));
    }

    #[test]
    fn wcr_rejects_zero_ncomp() {
        let data = spanning_design(10, 48, 3);
        let y = vec![0.0; 10];
        let config = WcrConfig {
            ncomp: 0,
            ..Default::default()
        };
        assert!(matches!(
            wcr(&data, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn wcr_surfaces_unsupported_family() {
        let data = spanning_design(10, 48, 4);
        let y = vec![0.0; 10];
        let config = WcrConfig {
            family: WaveletFamily::Daubechies(11),
            ..base_config()
        };
        assert!(matches!(
            wcr(&data, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn wcr_surfaces_level_out_of_range() {
        let data = spanning_design(10, 48, 5);
        let y = vec![0.0; 10];
        let config = WcrConfig {
            level: Some(999),
            ..base_config()
        };
        assert!(matches!(
            wcr(&data, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn wcr_finite_outputs_both_methods() {
        let (n, m) = (100usize, 40usize);
        let data = spanning_design(n, m, 4242);
        let y = pseudo_random(n, 8080);
        for method in [WcrMethod::Pcr, WcrMethod::Pls] {
            let config = WcrConfig {
                ncomp: 8,
                method,
                ..Default::default()
            };
            let fit = wcr(&data, &y, &config).unwrap();
            assert!(fit.intercept.is_finite());
            assert!(fit.beta_t.iter().all(|x| x.is_finite()));
            assert!(fit.fitted_values.iter().all(|x| x.is_finite()));
            assert!(fit.residuals.iter().all(|x| x.is_finite()));
        }
    }

    #[test]
    fn wcr_small_n_default_config_succeeds() {
        // CR-01 regression: default WcrConfig has ncomp = 5. With a small sample
        // (n = 4), the old clamp `ncomp.min(n).min(p)` gave ncomp = 4, making the
        // OLS design n × (n + 1) = 4 × 5 which ols_solve rejected (n < p). The fix
        // clamps to `n - 1`, so the design stays overdetermined and the fit succeeds.
        let (n, m) = (4usize, 32usize);
        let data = spanning_design(n, m, 2468);
        let y = pseudo_random(n, 1357);
        let config = WcrConfig::default(); // ncomp = 5 > n
        let fit = wcr(&data, &y, &config).unwrap();
        // Effective ncomp clamped to n - 1 (= 3), never n.
        assert!(fit.ncomp < n, "ncomp {} exceeds n - 1", fit.ncomp);
        assert_eq!(fit.beta_t.len(), m);
        assert!(fit.intercept.is_finite());
        assert!(fit.beta_t.iter().all(|x| x.is_finite()));
        assert!(fit.fitted_values.iter().all(|x| x.is_finite()));
        assert!(fit.residuals.iter().all(|x| x.is_finite()));

        // Also confirm the documented minimum n = 3 works under the default config.
        let data3 = spanning_design(3, m, 2469);
        let y3 = pseudo_random(3, 1358);
        let fit3 = wcr(&data3, &y3, &WcrConfig::default()).unwrap();
        assert!(fit3.ncomp <= 2);
        assert!(fit3.beta_t.iter().all(|x| x.is_finite()));
    }

    // --- wcr::predict + accessors (WAV-05) ---

    #[test]
    fn wcr_predict_reproduces_training_fitted() {
        let (n, m) = (120usize, 32usize);
        let data = spanning_design(n, m, 6100);
        let y = pseudo_random(n, 6101);
        let config = WcrConfig {
            ncomp: 8,
            ..Default::default()
        };
        let fit = wcr(&data, &y, &config).unwrap();
        let preds = fit.predict(&data).unwrap();
        assert_eq!(preds.len(), fit.fitted_values.len());
        for (i, (&p, &f)) in preds.iter().zip(&fit.fitted_values).enumerate() {
            assert!(
                (p - f).abs() <= 1e-8,
                "wcr predict[{i}] {p} != fitted {f} (|Δ| {})",
                (p - f).abs()
            );
        }
    }

    #[test]
    fn wcr_predict_on_new_curves_is_finite_and_rejects_grid_mismatch() {
        let (n, m) = (100usize, 32usize);
        let data = spanning_design(n, m, 6200);
        let y = pseudo_random(n, 6201);
        let fit = wcr(
            &data,
            &y,
            &WcrConfig {
                ncomp: 6,
                ..Default::default()
            },
        )
        .unwrap();

        // Fresh same-m curves → finite predictions.
        let fresh = spanning_design(40, m, 6202);
        let preds = fit.predict(&fresh).unwrap();
        assert_eq!(preds.len(), 40);
        assert!(preds.iter().all(|x| x.is_finite()));

        // Different ncols → InvalidDimension, never a panic.
        let wrong = spanning_design(10, m + 8, 6203);
        assert!(matches!(
            fit.predict(&wrong),
            Err(FdarError::InvalidDimension { .. })
        ));
    }

    #[test]
    fn wcr_predict_on_zero_row_input_errors_naming_new_no_panic() {
        let (n, m) = (100usize, 32usize);
        let data = spanning_design(n, m, 6400);
        let y = pseudo_random(n, 6401);
        let fit = wcr(
            &data,
            &y,
            &WcrConfig {
                ncomp: 6,
                ..Default::default()
            },
        )
        .unwrap();

        // Zero-row input (correct ncols) → InvalidDimension naming "new", never a panic
        // and never the internal "data" parameter surfaced from decompose_matrix.
        let empty = FdMatrix::zeros(0, m);
        match fit.predict(&empty) {
            Err(FdarError::InvalidDimension { parameter, .. }) => {
                assert_eq!(parameter, "new");
            }
            other => panic!("expected InvalidDimension naming \"new\", got {other:?}"),
        }
    }

    // ===================================================================
    // wnet — wavelet-domain elastic-net regressor (WAV-04)
    // ===================================================================

    /// Build a synthetic sparse coefficient-space β (a handful of nonzero
    /// coefficients, the rest exactly zero) plus the spanning full-rank design
    /// and layout. Returns `(data, design, layout, beta_coeff, support)`.
    fn sparse_wnet_problem(
        n: usize,
        m: usize,
        seed0: u64,
    ) -> (FdMatrix, FdMatrix, CoeffLayout, Vec<f64>, Vec<usize>) {
        let data = spanning_design(n, m, seed0);
        let family = WaveletFamily::Daubechies(4);
        let mode = BoundaryMode::Periodic;
        let (design, layout) = curves_to_coeff_design(&data, family, mode, None).unwrap();
        let p = design.ncols();

        // Localize β in a few coefficients spread across the bands.
        let support: Vec<usize> = vec![0, 2, p / 2, p - 3]
            .into_iter()
            .filter(|&j| j < p)
            .collect();
        let mut beta_coeff = vec![0.0_f64; p];
        // Give the true-support coefficients large, well-separated magnitudes so
        // they clearly dominate the elastic-net solution.
        let mags = [4.0, -3.5, 5.0, -4.5];
        for (k, &j) in support.iter().enumerate() {
            beta_coeff[j] = mags[k % mags.len()];
        }
        (data, design, layout, beta_coeff, support)
    }

    #[test]
    fn wnet_elastic_net_cd_recovers_sparse_support() {
        // Fixed-λ path (Task 1): a moderate λ produces a sparse solution whose
        // nonzero coefficients concentrate on the true support.
        let (n, m) = (256usize, 32usize);
        let (_data, design, _layout, beta_coeff, support) = sparse_wnet_problem(n, m, 3000);
        let p = design.ncols();

        // y = intercept + X β (noiseless) — the localized signal.
        let intercept_true = 0.5_f64;
        let y: Vec<f64> = (0..n)
            .map(|i| {
                let mut acc = intercept_true;
                for j in 0..p {
                    acc += design[(i, j)] * beta_coeff[j];
                }
                acc
            })
            .collect();

        // Moderate λ, alpha=0.9 (strongly L1) → sparse.
        let (intercept, beta) = elastic_net_cd(&design, &y, 0.05, 0.9, 2000, 1e-8).unwrap();

        assert!(intercept.is_finite());
        assert!(beta.iter().all(|b| b.is_finite()));

        let selected: Vec<usize> = beta
            .iter()
            .enumerate()
            .filter(|(_, &b)| b.abs() > 1e-8)
            .map(|(j, _)| j)
            .collect();

        // True support is among the selected set.
        for &j in &support {
            assert!(
                selected.contains(&j),
                "true-support coeff {j} not selected (selected={selected:?})"
            );
        }
        // The selected set is meaningfully sparse relative to P.
        assert!(
            selected.len() < p / 2,
            "selection not sparse: |selected|={} of P={p}",
            selected.len()
        );
    }

    #[test]
    fn wnet_fixed_lambda_end_to_end_finite() {
        // Task 1 tracer: full wnet path (with CV under the hood) yields finite
        // β(t)/fitted/coeff outputs on the localized-signal problem.
        let (n, m) = (200usize, 32usize);
        let (data, design, _layout, beta_coeff, _support) = sparse_wnet_problem(n, m, 3100);
        let p = design.ncols();
        let y: Vec<f64> = (0..n)
            .map(|i| {
                let mut acc = 0.25;
                for j in 0..p {
                    acc += design[(i, j)] * beta_coeff[j];
                }
                acc
            })
            .collect();

        let config = WnetConfig {
            n_lambda: 15,
            n_folds: 4,
            ..Default::default()
        };
        let fit = wnet(&data, &y, &config).unwrap();
        assert_eq!(fit.beta_t.len(), m);
        assert_eq!(fit.coeff_weights.len(), p);
        assert!(fit.intercept.is_finite());
        assert!(fit.beta_t.iter().all(|x| x.is_finite()));
        assert!(fit.fitted_values.iter().all(|x| x.is_finite()));
        assert!(fit.residuals.iter().all(|x| x.is_finite()));
        assert!(fit.coeff_weights.iter().all(|x| x.is_finite()));
        // selected indices match the nonzero coeff_weights.
        for &j in &fit.selected {
            assert!(fit.coeff_weights[j] != 0.0);
        }
    }

    #[test]
    fn wnet_default_config_is_db4_periodic_auto() {
        let c = WnetConfig::default();
        assert_eq!(c.family, WaveletFamily::Daubechies(4));
        assert_eq!(c.mode, BoundaryMode::Periodic);
        assert_eq!(c.level, None);
        assert!((c.alpha - 0.5).abs() < 1e-15);
        assert_eq!(c.lambda_grid, None);
        assert_eq!(c.n_lambda, 50);
        assert_eq!(c.n_folds, 5);
        assert_eq!(c.seed, 0);
    }

    // --- Deterministic CV-λ (SC3) ---

    #[test]
    fn wnet_cv_lambda_is_deterministic_across_runs() {
        let (n, m) = (200usize, 32usize);
        let (data, design, _layout, beta_coeff, _support) = sparse_wnet_problem(n, m, 3200);
        let p = design.ncols();
        // Add mild noise so CV-MSE is non-degenerate but λ still well-defined.
        let noise = pseudo_random(n, 9999);
        let y: Vec<f64> = (0..n)
            .map(|i| {
                let mut acc = 0.1;
                for j in 0..p {
                    acc += design[(i, j)] * beta_coeff[j];
                }
                acc + 0.05 * noise[i]
            })
            .collect();

        let config = WnetConfig {
            alpha: 0.8,
            n_lambda: 20,
            n_folds: 5,
            seed: 0,
            ..Default::default()
        };
        let fit1 = wnet(&data, &y, &config).unwrap();
        let fit2 = wnet(&data, &y, &config).unwrap();
        assert_eq!(
            fit1.lambda, fit2.lambda,
            "CV-selected lambda differs across runs: {} vs {}",
            fit1.lambda, fit2.lambda
        );
        // Also exercise the helper directly.
        let l1 = wnet_cv_lambda(&design, &y, &config).unwrap();
        let l2 = wnet_cv_lambda(&design, &y, &config).unwrap();
        assert_eq!(l1, l2);
    }

    // --- β(t) recovery on SNR data (SC3) ---

    #[test]
    fn wnet_recovers_beta_t_on_snr_data() {
        // Spanning full-rank design, moderate SNR: the fit at the CV λ must be
        // non-degenerate and β(t) must track the injected β(t).
        let (n, m) = (300usize, 32usize);
        let (data, design, layout, beta_coeff, _support) = sparse_wnet_problem(n, m, 3300);
        let p = design.ncols();
        let beta_t_true = coeff_weights_to_beta_t(&beta_coeff, &layout).unwrap();

        // Signal variance vs noise: pick noise small relative to signal spread.
        let signal: Vec<f64> = (0..n)
            .map(|i| {
                let mut acc = 0.0;
                for j in 0..p {
                    acc += design[(i, j)] * beta_coeff[j];
                }
                acc
            })
            .collect();
        let sig_sd = {
            let mean = signal.iter().sum::<f64>() / n as f64;
            (signal.iter().map(|s| (s - mean).powi(2)).sum::<f64>() / n as f64).sqrt()
        };
        let noise = pseudo_random(n, 4141);
        let noise_scale = 0.05 * sig_sd; // ~20:1 SNR
        let y: Vec<f64> = (0..n)
            .map(|i| 0.3 + signal[i] + noise_scale * noise[i])
            .collect();

        let config = WnetConfig {
            alpha: 0.7,
            n_lambda: 30,
            n_folds: 5,
            ..Default::default()
        };
        let fit = wnet(&data, &y, &config).unwrap();

        // Non-degenerate: not all-zero.
        let nonzero = fit.coeff_weights.iter().filter(|&&b| b != 0.0).count();
        assert!(nonzero > 0, "degenerate all-zero fit at CV lambda");

        // β(t) tracks the injected β(t) within tolerance.
        let e = rel_l2(&fit.beta_t, &beta_t_true);
        assert!(
            e < 0.35,
            "wnet beta_t recovery rel L2 err {e} exceeds tolerance on SNR data"
        );
        assert!(fit.beta_t.iter().all(|x| x.is_finite()));
        assert!(fit.fitted_values.iter().all(|x| x.is_finite()));
    }

    // --- Validation gate (SC4) ---

    fn base_wnet_config() -> WnetConfig {
        WnetConfig {
            n_lambda: 10,
            n_folds: 3,
            ..Default::default()
        }
    }

    #[test]
    fn wnet_rejects_too_few_rows() {
        let data = spanning_design(2, 32, 10);
        let y = vec![0.0, 1.0];
        assert!(matches!(
            wnet(&data, &y, &base_wnet_config()),
            Err(FdarError::InvalidDimension { .. })
        ));
    }

    #[test]
    fn wnet_rejects_zero_cols() {
        // An empty-column matrix is rejected before any DWT.
        let data = FdMatrix::zeros(5, 0);
        let y = vec![0.0; 5];
        assert!(matches!(
            wnet(&data, &y, &base_wnet_config()),
            Err(FdarError::InvalidDimension { .. })
        ));
    }

    #[test]
    fn wnet_rejects_mismatched_y_len() {
        let data = spanning_design(10, 32, 11);
        let y = vec![0.0; 9];
        assert!(matches!(
            wnet(&data, &y, &base_wnet_config()),
            Err(FdarError::InvalidDimension { .. })
        ));
    }

    #[test]
    fn wnet_rejects_alpha_out_of_range() {
        let data = spanning_design(10, 32, 12);
        let y = vec![0.0; 10];
        let config = WnetConfig {
            alpha: 1.5,
            ..base_wnet_config()
        };
        assert!(matches!(
            wnet(&data, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
        let config = WnetConfig {
            alpha: -0.1,
            ..base_wnet_config()
        };
        assert!(matches!(
            wnet(&data, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn wnet_rejects_too_few_folds() {
        let data = spanning_design(10, 32, 13);
        let y = vec![0.0; 10];
        let config = WnetConfig {
            n_folds: 1,
            ..base_wnet_config()
        };
        assert!(matches!(
            wnet(&data, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn wnet_rejects_too_many_folds() {
        // WR-01: n_folds > n must be rejected rather than silently running fewer folds.
        let data = spanning_design(10, 32, 130);
        let y = vec![0.0; 10];
        let config = WnetConfig {
            n_folds: 11,
            ..base_wnet_config()
        };
        assert!(matches!(
            wnet(&data, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
        // The shared CV helper also rejects it directly.
        let (design, _layout) = curves_to_coeff_design(
            &data,
            WaveletFamily::Daubechies(4),
            BoundaryMode::Periodic,
            None,
        )
        .unwrap();
        assert!(matches!(
            wnet_cv_lambda(&design, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn wnet_rejects_negative_or_nan_tol() {
        // WR-02: negative or NaN tol is rejected (both via the entry and the CD engine).
        let data = spanning_design(10, 32, 131);
        let y = pseudo_random(10, 5);
        for bad in [-1e-6_f64, f64::NAN] {
            let config = WnetConfig {
                tol: bad,
                ..base_wnet_config()
            };
            assert!(matches!(
                wnet(&data, &y, &config),
                Err(FdarError::InvalidParameter { .. })
            ));
        }
        // elastic_net_cd rejects it directly too.
        let (design, _layout) = curves_to_coeff_design(
            &data,
            WaveletFamily::Daubechies(4),
            BoundaryMode::Periodic,
            None,
        )
        .unwrap();
        assert!(matches!(
            elastic_net_cd(&design, &y, 0.1, 0.5, 100, -1.0),
            Err(FdarError::InvalidParameter { .. })
        ));
        assert!(matches!(
            elastic_net_cd(&design, &y, 0.1, 0.5, 100, f64::NAN),
            Err(FdarError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn wnet_rejects_zero_max_iter() {
        // WR-03: max_iter == 0 would silently return an all-zero-coefficient model.
        let data = spanning_design(10, 32, 132);
        let y = pseudo_random(10, 6);
        let config = WnetConfig {
            max_iter: 0,
            ..base_wnet_config()
        };
        assert!(matches!(
            wnet(&data, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
        // elastic_net_cd rejects it directly too.
        let (design, _layout) = curves_to_coeff_design(
            &data,
            WaveletFamily::Daubechies(4),
            BoundaryMode::Periodic,
            None,
        )
        .unwrap();
        assert!(matches!(
            elastic_net_cd(&design, &y, 0.1, 0.5, 0, 1e-6),
            Err(FdarError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn wnet_rejects_empty_lambda_grid() {
        let data = spanning_design(10, 32, 14);
        let y = vec![0.0; 10];
        let config = WnetConfig {
            lambda_grid: Some(vec![]),
            ..base_wnet_config()
        };
        assert!(matches!(
            wnet(&data, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn wnet_surfaces_unsupported_family() {
        let data = spanning_design(10, 32, 15);
        let y = vec![0.0; 10];
        let config = WnetConfig {
            family: WaveletFamily::Daubechies(11),
            ..base_wnet_config()
        };
        assert!(matches!(
            wnet(&data, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn wnet_surfaces_level_out_of_range() {
        let data = spanning_design(10, 32, 16);
        let y = vec![0.0; 10];
        let config = WnetConfig {
            level: Some(999),
            ..base_wnet_config()
        };
        assert!(matches!(
            wnet(&data, &y, &config),
            Err(FdarError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn wnet_finite_outputs_on_larger_snr_design() {
        let (n, m) = (256usize, 48usize);
        let (data, design, _layout, beta_coeff, _support) = sparse_wnet_problem(n, m, 3400);
        let p = design.ncols();
        let noise = pseudo_random(n, 2727);
        let y: Vec<f64> = (0..n)
            .map(|i| {
                let mut acc = 0.2;
                for j in 0..p {
                    acc += design[(i, j)] * beta_coeff[j];
                }
                acc + 0.1 * noise[i]
            })
            .collect();

        let config = WnetConfig {
            alpha: 0.6,
            n_lambda: 25,
            n_folds: 5,
            ..Default::default()
        };
        let fit = wnet(&data, &y, &config).unwrap();
        assert!(fit.intercept.is_finite());
        assert!(fit.lambda.is_finite());
        assert!(fit.beta_t.iter().all(|x| x.is_finite()));
        assert!(fit.fitted_values.iter().all(|x| x.is_finite()));
        assert!(fit.residuals.iter().all(|x| x.is_finite()));
        assert!(fit.coeff_weights.iter().all(|x| x.is_finite()));
    }

    #[test]
    fn wnet_explicit_lambda_grid_is_used() {
        // With a single-λ explicit grid, the CV selection must return that λ.
        let (n, m) = (120usize, 32usize);
        let (data, design, _layout, beta_coeff, _support) = sparse_wnet_problem(n, m, 3500);
        let p = design.ncols();
        let y: Vec<f64> = (0..n)
            .map(|i| {
                let mut acc = 0.0;
                for j in 0..p {
                    acc += design[(i, j)] * beta_coeff[j];
                }
                acc
            })
            .collect();
        let config = WnetConfig {
            lambda_grid: Some(vec![0.123]),
            ..base_wnet_config()
        };
        let fit = wnet(&data, &y, &config).unwrap();
        assert!((fit.lambda - 0.123).abs() < 1e-15);
    }

    // --- wnet::predict + accessors (WAV-05) ---

    #[test]
    fn wnet_predict_reproduces_training_fitted() {
        let (n, m) = (200usize, 32usize);
        let (data, design, _layout, beta_coeff, _support) = sparse_wnet_problem(n, m, 6300);
        let p = design.ncols();
        let noise = pseudo_random(n, 6301);
        let y: Vec<f64> = (0..n)
            .map(|i| {
                let mut acc = 0.4;
                for j in 0..p {
                    acc += design[(i, j)] * beta_coeff[j];
                }
                acc + 0.05 * noise[i]
            })
            .collect();
        let config = WnetConfig {
            alpha: 0.7,
            n_lambda: 20,
            n_folds: 5,
            ..Default::default()
        };
        let fit = wnet(&data, &y, &config).unwrap();
        let preds = fit.predict(&data).unwrap();
        assert_eq!(preds.len(), fit.fitted_values.len());
        for (i, (&pv, &f)) in preds.iter().zip(&fit.fitted_values).enumerate() {
            assert!(
                (pv - f).abs() <= 1e-8,
                "wnet predict[{i}] {pv} != fitted {f} (|Δ| {})",
                (pv - f).abs()
            );
        }
    }

    #[test]
    fn wnet_predict_on_new_curves_is_finite_and_rejects_grid_mismatch() {
        let (n, m) = (150usize, 32usize);
        let (data, design, _layout, beta_coeff, _support) = sparse_wnet_problem(n, m, 6400);
        let p = design.ncols();
        let y: Vec<f64> = (0..n)
            .map(|i| {
                let mut acc = 0.2;
                for j in 0..p {
                    acc += design[(i, j)] * beta_coeff[j];
                }
                acc
            })
            .collect();
        let fit = wnet(
            &data,
            &y,
            &WnetConfig {
                n_lambda: 12,
                n_folds: 4,
                ..Default::default()
            },
        )
        .unwrap();

        // Fresh same-m curves → finite predictions.
        let fresh = spanning_design(30, m, 6401);
        let preds = fit.predict(&fresh).unwrap();
        assert_eq!(preds.len(), 30);
        assert!(preds.iter().all(|x| x.is_finite()));

        // Different ncols → InvalidDimension, never a panic.
        let wrong = spanning_design(10, m + 16, 6402);
        assert!(matches!(
            fit.predict(&wrong),
            Err(FdarError::InvalidDimension { .. })
        ));

        // Zero-row input (correct ncols) → InvalidDimension naming "new", no panic.
        let empty = FdMatrix::zeros(0, m);
        match fit.predict(&empty) {
            Err(FdarError::InvalidDimension { parameter, .. }) => {
                assert_eq!(parameter, "new");
            }
            other => panic!("expected InvalidDimension naming \"new\", got {other:?}"),
        }
    }

    #[test]
    fn accessors_return_stored_slices() {
        let (n, m) = (100usize, 32usize);
        let data = spanning_design(n, m, 6500);
        let y = pseudo_random(n, 6501);

        let wcr_fit = wcr(
            &data,
            &y,
            &WcrConfig {
                ncomp: 5,
                ..Default::default()
            },
        )
        .unwrap();
        assert_eq!(wcr_fit.beta_t(), wcr_fit.beta_t.as_slice());
        assert_eq!(wcr_fit.coefficient_function(), wcr_fit.beta_t.as_slice());
        assert_eq!(wcr_fit.beta_t().len(), m);
        assert_eq!(wcr_fit.fitted_values(), wcr_fit.fitted_values.as_slice());
        assert_eq!(wcr_fit.fitted_values().len(), n);

        let wnet_fit = wnet(
            &data,
            &y,
            &WnetConfig {
                n_lambda: 10,
                n_folds: 4,
                ..Default::default()
            },
        )
        .unwrap();
        assert_eq!(wnet_fit.beta_t(), wnet_fit.beta_t.as_slice());
        assert_eq!(wnet_fit.coefficient_function(), wnet_fit.beta_t.as_slice());
        assert_eq!(wnet_fit.beta_t().len(), m);
        assert_eq!(wnet_fit.fitted_values(), wnet_fit.fitted_values.as_slice());
        assert_eq!(wnet_fit.fitted_values().len(), n);
    }
}