scirs2-transform 0.4.0

Data transformation module for SciRS2 (scirs2-transform)
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
//! Kernel Approximation via Random Features
//!
//! This module provides scalable kernel methods using random feature maps.
//! The key idea (Bochner's theorem) is that shift-invariant kernels have
//! a spectral representation: `k(x,y) = E_w[φ_w(x) φ_w(y)]`
//! where φ_w(x) = sqrt(2) cos(w^T x + b) for random w, b.
//!
//! ## Methods
//!
//! - **Random Fourier Features (RFF)**: Approximate shift-invariant kernels
//!   (RBF, Laplacian, Cauchy, Matérn) via Bochner's theorem
//! - **Nyström Approximation**: Low-rank approximation using landmark points
//! - **Tensor Sketch**: Polynomial kernel approximation via count sketching
//! - **Kernel Ridge Regression with RFF**: Scalable KRR using random features
//! - **MMD Test**: Maximum Mean Discrepancy hypothesis testing
//!
//! ## References
//!
//! - Rahimi & Recht (2007): Random Features for Large-Scale Kernel Machines
//! - Williams & Seeger (2001): Using the Nyström Method to Speed Up Kernel Machines
//! - Pham & Pagh (2013): Fast and Scalable Polynomial Kernels via Explicit Feature Maps

use std::f64::consts::PI;

use scirs2_core::ndarray::{Array1, Array2, ArrayView2};
use scirs2_core::random::{
    seeded_rng, Cauchy, Distribution, Normal, SeedableRng, StandardNormal, StudentT, Uniform,
};
use scirs2_linalg::{eigh, solve};

use crate::error::{Result, TransformError};

// ============================================================================
// Shift-Invariant Kernels
// ============================================================================

/// Shift-invariant kernel types whose spectral density is known analytically.
///
/// For each kernel k(x,y) = k(x - y), Bochner's theorem guarantees a spectral
/// representation via a probability distribution p(ω) such that
/// k(Δ) = ∫ p(ω) e^{iω^T Δ} dω.
#[derive(Debug, Clone, PartialEq)]
pub enum ShiftInvariantKernel {
    /// Gaussian RBF: k(x,y) = exp(-γ ‖x-y‖²)
    /// Spectral density: N(0, 2γ I)
    RBF {
        /// Bandwidth parameter γ controlling the kernel width
        gamma: f64,
    },

    /// Laplacian: k(x,y) = exp(-γ ‖x-y‖₁)
    /// Spectral density (per dimension): Cauchy(0, 1/γ)
    Laplacian {
        /// Bandwidth parameter γ controlling the kernel width
        gamma: f64,
    },

    /// Cauchy: k(x,y) = ∏_d 1/(1 + γ(x_d - y_d)²)
    /// Spectral density (per dimension): Laplacian / double-exponential
    Cauchy {
        /// Bandwidth parameter γ controlling the kernel width
        gamma: f64,
    },

    /// Matérn 3/2: k(x,y) = (1 + √3 γ ‖x-y‖) exp(-√3 γ ‖x-y‖)
    /// Spectral density: Student-t with ν=3, scale=√(2·3) γ
    Matern32 {
        /// Length scale parameter controlling the smoothness
        length_scale: f64,
    },

    /// Matérn 5/2: k(x,y) = (1 + √5 γ ‖x-y‖ + 5γ²‖x-y‖²/3) exp(-√5 γ ‖x-y‖)
    /// Spectral density: Student-t with ν=5, scale=√(2·5) γ
    Matern52 {
        /// Length scale parameter controlling the smoothness
        length_scale: f64,
    },
}

impl ShiftInvariantKernel {
    /// Sample random frequency vectors ω from the spectral distribution p(ω).
    ///
    /// Returns a matrix of shape `(input_dim, n_samples)`.
    ///
    /// The spectral densities follow from Bochner's theorem:
    /// - RBF: ω ~ N(0, 2γ I_d)
    /// - Laplacian: each ω_i ~ Cauchy(0, γ) independently
    /// - Cauchy: each ω_i ~ Laplace(0, 1/γ) = double-exponential
    /// - Matérn 3/2: ω ~ StudentT(ν=3) scaled by √(6) / length_scale per dim
    /// - Matérn 5/2: ω ~ StudentT(ν=5) scaled by √(10) / length_scale per dim
    fn sample_frequencies(
        &self,
        n_samples: usize,
        input_dim: usize,
        seed: u64,
    ) -> std::result::Result<Array2<f64>, TransformError> {
        let mut rng = seeded_rng(seed);
        let total = n_samples * input_dim;
        let mut data = Vec::with_capacity(total);

        match self {
            ShiftInvariantKernel::RBF { gamma } => {
                // ω ~ N(0, 2γ) per component
                let std_dev = (2.0 * gamma).sqrt();
                let dist = Normal::new(0.0_f64, std_dev)
                    .map_err(|e| TransformError::ComputationError(e.to_string()))?;
                for _ in 0..total {
                    data.push(dist.sample(&mut rng));
                }
            }

            ShiftInvariantKernel::Laplacian { gamma } => {
                // ω_i ~ Cauchy(0, γ) per component (Laplacian spectral density)
                let dist = Cauchy::new(0.0_f64, *gamma)
                    .map_err(|e| TransformError::ComputationError(e.to_string()))?;
                for _ in 0..total {
                    let s = dist.sample(&mut rng);
                    // Clamp extreme Cauchy samples to avoid numerical issues
                    data.push(s.max(-1e6).min(1e6));
                }
            }

            ShiftInvariantKernel::Cauchy { gamma } => {
                // Cauchy kernel spectral density is Laplacian (double-exponential)
                // Laplace(0, b) can be sampled: U ~ Uniform(-0.5, 0.5), x = -b*sign(U)*ln(1-2|U|)
                let b = 1.0 / gamma;
                let udist = Uniform::new(-0.5_f64, 0.5_f64)
                    .map_err(|e| TransformError::ComputationError(e.to_string()))?;
                for _ in 0..total {
                    let u: f64 = udist.sample(&mut rng);
                    let sign = if u >= 0.0 { 1.0 } else { -1.0 };
                    let val = -b * sign * (1.0 - 2.0 * u.abs()).ln();
                    data.push(val);
                }
            }

            ShiftInvariantKernel::Matern32 { length_scale } => {
                // Matérn 3/2 spectral density: StudentT(ν=3) scaled by sqrt(6)/length_scale
                // (isotropic: sample each dimension independently)
                let scale = (6.0_f64).sqrt() / length_scale;
                let dist = StudentT::new(3.0_f64)
                    .map_err(|e| TransformError::ComputationError(e.to_string()))?;
                for _ in 0..total {
                    data.push(dist.sample(&mut rng) * scale);
                }
            }

            ShiftInvariantKernel::Matern52 { length_scale } => {
                // Matérn 5/2 spectral density: StudentT(ν=5) scaled by sqrt(10)/length_scale
                let scale = (10.0_f64).sqrt() / length_scale;
                let dist = StudentT::new(5.0_f64)
                    .map_err(|e| TransformError::ComputationError(e.to_string()))?;
                for _ in 0..total {
                    data.push(dist.sample(&mut rng) * scale);
                }
            }
        }

        // Shape: (input_dim, n_samples) — each column is one ω vector
        Array2::from_shape_vec((input_dim, n_samples), data)
            .map_err(|e| TransformError::ComputationError(e.to_string()))
    }

    /// Compute exact kernel value between two vectors.
    pub fn compute_exact(&self, x: &[f64], y: &[f64]) -> f64 {
        debug_assert_eq!(x.len(), y.len(), "Input dimensions must match");
        match self {
            ShiftInvariantKernel::RBF { gamma } => {
                let sq_dist: f64 = x
                    .iter()
                    .zip(y.iter())
                    .map(|(a, b)| (a - b).powi(2))
                    .sum();
                (-gamma * sq_dist).exp()
            }

            ShiftInvariantKernel::Laplacian { gamma } => {
                let l1_dist: f64 = x
                    .iter()
                    .zip(y.iter())
                    .map(|(a, b)| (a - b).abs())
                    .sum();
                (-gamma * l1_dist).exp()
            }

            ShiftInvariantKernel::Cauchy { gamma } => {
                // Product form: ∏_d 1/(1 + γ(x_d - y_d)²)
                x.iter()
                    .zip(y.iter())
                    .map(|(a, b)| 1.0 / (1.0 + gamma * (a - b).powi(2)))
                    .product()
            }

            ShiftInvariantKernel::Matern32 { length_scale } => {
                let dist: f64 = x
                    .iter()
                    .zip(y.iter())
                    .map(|(a, b)| (a - b).powi(2))
                    .sum::<f64>()
                    .sqrt();
                let r = 3.0_f64.sqrt() * dist / length_scale;
                (1.0 + r) * (-r).exp()
            }

            ShiftInvariantKernel::Matern52 { length_scale } => {
                let dist: f64 = x
                    .iter()
                    .zip(y.iter())
                    .map(|(a, b)| (a - b).powi(2))
                    .sum::<f64>()
                    .sqrt();
                let r = 5.0_f64.sqrt() * dist / length_scale;
                (1.0 + r + r.powi(2) / 3.0) * (-r).exp()
            }
        }
    }
}

// ============================================================================
// Random Fourier Features
// ============================================================================

/// Random Fourier Features (RFF) for scalable shift-invariant kernel approximation.
///
/// Based on Bochner's theorem: for a shift-invariant kernel k(x,y) = k(x-y),
/// the feature map z(x) = sqrt(2/D) [cos(w_1^T x + b_1), ..., cos(w_D^T x + b_D)]
/// satisfies k(x,y) ≈ z(x)^T z(y) with high probability.
///
/// # Example
///
/// ```rust,no_run
/// use scirs2_transform::random_features::{RandomFourierFeatures, ShiftInvariantKernel};
/// use scirs2_core::ndarray::Array2;
///
/// let x = Array2::<f64>::zeros((100, 10));
/// let kernel = ShiftInvariantKernel::RBF { gamma: 0.5 };
/// let mut rff = RandomFourierFeatures::new(200, kernel);
/// let z = rff.fit_transform(x.view(), 42).expect("should succeed");
/// assert_eq!(z.shape(), &[100, 200]);
/// ```
#[derive(Debug, Clone)]
pub struct RandomFourierFeatures {
    /// D: number of random features
    pub n_components: usize,
    /// The kernel being approximated
    pub kernel: ShiftInvariantKernel,
    /// Weight matrix ω: shape (input_dim, n_components)
    weights: Option<Array2<f64>>,
    /// Bias vector b: shape (n_components,), drawn from Uniform[0, 2π]
    biases: Option<Array1<f64>>,
}

impl RandomFourierFeatures {
    /// Create a new RFF transformer.
    pub fn new(n_components: usize, kernel: ShiftInvariantKernel) -> Self {
        RandomFourierFeatures {
            n_components,
            kernel,
            weights: None,
            biases: None,
        }
    }

    /// Fit: sample random frequencies and biases.
    pub fn fit(&mut self, input_dim: usize, seed: u64) -> Result<()> {
        if input_dim == 0 {
            return Err(TransformError::InvalidInput(
                "input_dim must be positive".to_string(),
            ));
        }
        if self.n_components == 0 {
            return Err(TransformError::InvalidInput(
                "n_components must be positive".to_string(),
            ));
        }

        // Sample frequency vectors from spectral distribution
        let weights = self
            .kernel
            .sample_frequencies(self.n_components, input_dim, seed)?;
        // weights shape: (input_dim, n_components)

        // Sample biases b_j ~ Uniform[0, 2π]
        let mut rng = seeded_rng(seed.wrapping_add(1));
        let bias_dist = Uniform::new(0.0_f64, 2.0 * PI)
            .map_err(|e| TransformError::ComputationError(e.to_string()))?;
        let biases: Vec<f64> = (0..self.n_components)
            .map(|_| bias_dist.sample(&mut rng))
            .collect();

        self.weights = Some(weights);
        self.biases = Some(
            Array1::from_vec(biases),
        );
        Ok(())
    }

    /// Transform: compute feature map z(X).
    ///
    /// Input shape: (n_samples, n_features)
    /// Output shape: (n_samples, n_components)
    pub fn transform(&self, x: ArrayView2<f64>) -> Result<Array2<f64>> {
        let weights = self.weights.as_ref().ok_or_else(|| {
            TransformError::NotFitted("RandomFourierFeatures not fitted".to_string())
        })?;
        let biases = self.biases.as_ref().ok_or_else(|| {
            TransformError::NotFitted("RandomFourierFeatures not fitted".to_string())
        })?;

        let (n_samples, n_features) = (x.nrows(), x.ncols());
        let expected_dim = weights.nrows();
        if n_features != expected_dim {
            return Err(TransformError::InvalidInput(format!(
                "Expected {} features, got {}",
                expected_dim, n_features
            )));
        }

        let n_components = self.n_components;
        let scale = (2.0_f64 / n_components as f64).sqrt();

        // Compute X @ W: shape (n_samples, n_components)
        // x: (n_samples, n_features), weights: (n_features, n_components)
        let mut z = Array2::<f64>::zeros((n_samples, n_components));
        for i in 0..n_samples {
            for j in 0..n_components {
                let dot: f64 = x
                    .row(i)
                    .iter()
                    .zip(weights.column(j).iter())
                    .map(|(a, b)| a * b)
                    .sum();
                z[[i, j]] = scale * (dot + biases[j]).cos();
            }
        }

        Ok(z)
    }

    /// Fit and transform in one step.
    pub fn fit_transform(
        &mut self,
        x: ArrayView2<f64>,
        seed: u64,
    ) -> Result<Array2<f64>> {
        let input_dim = x.ncols();
        self.fit(input_dim, seed)?;
        self.transform(x)
    }

    /// Compute approximate kernel matrix K[i,j] ≈ z(x_i)^T z(x_j).
    ///
    /// Output shape: (n_samples, n_samples)
    pub fn approximate_kernel(&self, x: ArrayView2<f64>) -> Result<Array2<f64>> {
        let z = self.transform(x)?;
        // K ≈ Z Z^T
        let n = z.nrows();
        let d = z.ncols();
        let mut k = Array2::<f64>::zeros((n, n));
        for i in 0..n {
            for j in i..n {
                let dot: f64 = (0..d).map(|l| z[[i, l]] * z[[j, l]]).sum();
                k[[i, j]] = dot;
                k[[j, i]] = dot;
            }
        }
        Ok(k)
    }

    /// Compute approximate cross-kernel matrix K[i,j] = k(X_i, Y_j).
    ///
    /// Output shape: (n_x, n_y)
    pub fn approximate_cross_kernel(
        &self,
        x: ArrayView2<f64>,
        y: ArrayView2<f64>,
    ) -> Result<Array2<f64>> {
        let zx = self.transform(x)?;
        let zy = self.transform(y)?;
        let (nx, ny, d) = (zx.nrows(), zy.nrows(), zx.ncols());
        let mut k = Array2::<f64>::zeros((nx, ny));
        for i in 0..nx {
            for j in 0..ny {
                k[[i, j]] = (0..d).map(|l| zx[[i, l]] * zy[[j, l]]).sum();
            }
        }
        Ok(k)
    }

    /// Return whether the transformer has been fitted.
    pub fn is_fitted(&self) -> bool {
        self.weights.is_some()
    }
}

// ============================================================================
// Landmark Selection for Nyström
// ============================================================================

/// Strategy for selecting landmark points in the Nyström approximation.
#[derive(Debug, Clone, PartialEq)]
pub enum LandmarkSelection {
    /// Uniform random subset of training points
    Random,
    /// K-means++ centroids
    KMeans {
        /// Number of k-means iterations for centroid refinement
        n_iter: usize,
    },
    /// Uniformly spaced (by index) from sorted training data
    Uniform,
}

// ============================================================================
// Nyström Approximation
// ============================================================================

/// Nyström approximation of the kernel matrix.
///
/// Given m ≪ n landmark points C, the approximation is:
/// K_nn ≈ K_nm K_mm^{-1} K_mn
///
/// The feature map φ(x) satisfies K_nn ≈ φ(X) φ(X)^T where
/// φ(X) = K_xm K_mm^{-1/2}
///
/// Uses eigendecomposition K_mm = V Λ V^T, so K_mm^{-1/2} = V Λ^{-1/2} V^T
/// (pseudoinverse for numerical stability).
///
/// # Example
///
/// ```rust,no_run
/// use scirs2_transform::random_features::{NystromApproximation, ShiftInvariantKernel, LandmarkSelection};
/// use scirs2_core::ndarray::Array2;
///
/// let x = Array2::<f64>::zeros((100, 5));
/// let kernel = ShiftInvariantKernel::RBF { gamma: 1.0 };
/// let mut nystrom = NystromApproximation::new(20, kernel);
/// let phi = nystrom.fit_transform(x.view(), LandmarkSelection::Random, 0).expect("should succeed");
/// assert_eq!(phi.shape(), &[100, 20]);
/// ```
#[derive(Debug, Clone)]
pub struct NystromApproximation {
    /// m: number of landmark points
    pub n_components: usize,
    /// Kernel function
    pub kernel: ShiftInvariantKernel,
    /// Selected landmark points, shape (m, d)
    landmarks: Option<Array2<f64>>,
    /// K_mm^{-1/2}, shape (m, m)
    kernel_inv_sqrt: Option<Array2<f64>>,
}

impl NystromApproximation {
    /// Create a new Nyström approximation.
    pub fn new(n_components: usize, kernel: ShiftInvariantKernel) -> Self {
        NystromApproximation {
            n_components,
            kernel,
            landmarks: None,
            kernel_inv_sqrt: None,
        }
    }

    /// Fit: select landmarks and compute K_mm^{-1/2}.
    pub fn fit(
        &mut self,
        x: ArrayView2<f64>,
        selection: LandmarkSelection,
        seed: u64,
    ) -> Result<()> {
        let (n_samples, n_features) = (x.nrows(), x.ncols());
        if n_samples == 0 || n_features == 0 {
            return Err(TransformError::InvalidInput("Empty input data".to_string()));
        }
        let m = self.n_components.min(n_samples);
        if m == 0 {
            return Err(TransformError::InvalidInput(
                "n_components must be positive".to_string(),
            ));
        }

        // Select landmark indices
        let landmark_indices = match selection {
            LandmarkSelection::Random => {
                sample_without_replacement(n_samples, m, seed)?
            }
            LandmarkSelection::Uniform => {
                // Evenly spaced indices in [0, n_samples)
                (0..m)
                    .map(|i| i * n_samples / m)
                    .collect::<Vec<usize>>()
            }
            LandmarkSelection::KMeans { n_iter } => {
                kmeans_plus_plus_indices(x, m, n_iter, seed)?
            }
        };

        // Build landmarks matrix (m, d)
        let mut landmarks = Array2::<f64>::zeros((m, n_features));
        for (row_out, &idx) in landmark_indices.iter().enumerate() {
            landmarks.row_mut(row_out).assign(&x.row(idx));
        }

        // Compute K_mm: kernel matrix among landmarks
        let k_mm = compute_kernel_matrix_between(&landmarks.view(), &landmarks.view(), &self.kernel);

        // Eigendecompose K_mm = V Λ V^T
        let (eigenvalues, eigenvectors) = eigh(&k_mm.view(), None)
            .map_err(|e| TransformError::ComputationError(e.to_string()))?;

        // Compute K_mm^{-1/2} = V Λ^{-1/2} V^T (using pseudoinverse threshold)
        let threshold = 1e-10 * eigenvalues
            .iter()
            .cloned()
            .fold(f64::NEG_INFINITY, f64::max)
            .max(1e-12);

        let n_eig = eigenvalues.len();
        let mut inv_sqrt_diag = Vec::with_capacity(n_eig);
        for &ev in eigenvalues.iter() {
            if ev > threshold {
                inv_sqrt_diag.push(1.0 / ev.sqrt());
            } else {
                inv_sqrt_diag.push(0.0);
            }
        }

        // K_mm^{-1/2} = V * diag(inv_sqrt_diag) * V^T
        let mut kernel_inv_sqrt = Array2::<f64>::zeros((m, m));
        for i in 0..m {
            for j in 0..m {
                let val: f64 = (0..n_eig)
                    .map(|k| eigenvectors[[i, k]] * inv_sqrt_diag[k] * eigenvectors[[j, k]])
                    .sum();
                kernel_inv_sqrt[[i, j]] = val;
            }
        }

        self.landmarks = Some(landmarks);
        self.kernel_inv_sqrt = Some(kernel_inv_sqrt);
        Ok(())
    }

    /// Transform: compute Nyström features φ(X) = K_xm K_mm^{-1/2}.
    ///
    /// Output shape: (n_samples, n_components)
    pub fn transform(&self, x: ArrayView2<f64>) -> Result<Array2<f64>> {
        let landmarks = self.landmarks.as_ref().ok_or_else(|| {
            TransformError::NotFitted("NystromApproximation not fitted".to_string())
        })?;
        let kernel_inv_sqrt = self.kernel_inv_sqrt.as_ref().ok_or_else(|| {
            TransformError::NotFitted("NystromApproximation not fitted".to_string())
        })?;

        let (n_samples, n_features) = (x.nrows(), x.ncols());
        let expected_dim = landmarks.ncols();
        if n_features != expected_dim {
            return Err(TransformError::InvalidInput(format!(
                "Expected {} features, got {}",
                expected_dim, n_features
            )));
        }

        let m = landmarks.nrows();

        // Compute K_xm: shape (n_samples, m)
        let k_xm = compute_kernel_matrix_between(&x, &landmarks.view(), &self.kernel);

        // φ(X) = K_xm @ K_mm^{-1/2}: shape (n_samples, m)
        let mut phi = Array2::<f64>::zeros((n_samples, m));
        for i in 0..n_samples {
            for j in 0..m {
                phi[[i, j]] = (0..m)
                    .map(|k| k_xm[[i, k]] * kernel_inv_sqrt[[k, j]])
                    .sum();
            }
        }

        Ok(phi)
    }

    /// Fit and transform in one step.
    pub fn fit_transform(
        &mut self,
        x: ArrayView2<f64>,
        selection: LandmarkSelection,
        seed: u64,
    ) -> Result<Array2<f64>> {
        self.fit(x, selection, seed)?;
        self.transform(x)
    }

    /// Approximate kernel matrix K ≈ φ(X) φ(X)^T.
    pub fn approximate_kernel(&self, x: ArrayView2<f64>) -> Result<Array2<f64>> {
        let phi = self.transform(x)?;
        let n = phi.nrows();
        let m = phi.ncols();
        let mut k = Array2::<f64>::zeros((n, n));
        for i in 0..n {
            for j in i..n {
                let dot: f64 = (0..m).map(|l| phi[[i, l]] * phi[[j, l]]).sum();
                k[[i, j]] = dot;
                k[[j, i]] = dot;
            }
        }
        Ok(k)
    }
}

// ============================================================================
// Tensor Sketch (Count Sketch for Polynomial Kernels)
// ============================================================================

/// Tensor Sketch approximation for polynomial kernels.
///
/// Approximates k(x,y) = (x^T y + c)^p via count sketch with FFT convolution.
/// The sketch of x ⊗ x ⊗ ... ⊗ x (p times) is computed using p count sketch
/// hash functions combined through convolution in the frequency domain.
///
/// # Reference
/// Pham & Pagh (2013): Fast and Scalable Polynomial Kernels via Explicit Feature Maps
///
/// # Example
///
/// ```rust,no_run
/// use scirs2_transform::random_features::TensorSketch;
/// use scirs2_core::ndarray::Array2;
///
/// let x = Array2::<f64>::zeros((50, 8));
/// let mut ts = TensorSketch::new(64, 3);
/// let z = ts.fit_transform(x.view(), 0).expect("should succeed");
/// assert_eq!(z.shape(), &[50, 64]);
/// ```
#[derive(Debug, Clone)]
pub struct TensorSketch {
    /// Output dimensionality D
    pub n_components: usize,
    /// Polynomial degree p
    pub degree: usize,
    /// p hash functions, each mapping {0,...,d-1} → {0,...,D-1}
    hash_functions: Option<Vec<Array1<usize>>>,
    /// p sign functions, each mapping {0,...,d-1} → {-1, +1}
    signs: Option<Vec<Array1<f64>>>,
}

impl TensorSketch {
    /// Create a new Tensor Sketch.
    pub fn new(n_components: usize, degree: usize) -> Self {
        TensorSketch {
            n_components,
            degree,
            hash_functions: None,
            signs: None,
        }
    }

    /// Fit: initialize p count sketch hash/sign functions.
    pub fn fit(&mut self, input_dim: usize, seed: u64) -> Result<()> {
        if input_dim == 0 {
            return Err(TransformError::InvalidInput("input_dim must be > 0".to_string()));
        }
        if self.degree == 0 {
            return Err(TransformError::InvalidInput("degree must be > 0".to_string()));
        }
        if self.n_components == 0 {
            return Err(TransformError::InvalidInput(
                "n_components must be > 0".to_string(),
            ));
        }

        let d = self.degree;
        let mut hash_functions = Vec::with_capacity(d);
        let mut signs = Vec::with_capacity(d);

        for k in 0..d {
            let mut rng = seeded_rng(seed.wrapping_add(k as u64));
            let h_dist = Uniform::new(0_usize, self.n_components)
                .map_err(|e| TransformError::ComputationError(e.to_string()))?;
            let s_dist = Uniform::new(0_usize, 2_usize)
                .map_err(|e| TransformError::ComputationError(e.to_string()))?;

            let h: Vec<usize> = (0..input_dim)
                .map(|_| h_dist.sample(&mut rng))
                .collect();
            let s: Vec<f64> = (0..input_dim)
                .map(|_| if s_dist.sample(&mut rng) == 0 { -1.0 } else { 1.0 })
                .collect();

            hash_functions.push(Array1::from_vec(h));
            signs.push(Array1::from_vec(s));
        }

        self.hash_functions = Some(hash_functions);
        self.signs = Some(signs);
        Ok(())
    }

    /// Transform: compute Tensor Sketch of X.
    ///
    /// Output shape: (n_samples, n_components)
    pub fn transform(&self, x: ArrayView2<f64>) -> Result<Array2<f64>> {
        let hash_functions = self.hash_functions.as_ref().ok_or_else(|| {
            TransformError::NotFitted("TensorSketch not fitted".to_string())
        })?;
        let signs = self.signs.as_ref().ok_or_else(|| {
            TransformError::NotFitted("TensorSketch not fitted".to_string())
        })?;

        let (n_samples, n_features) = (x.nrows(), x.ncols());
        let expected_dim = hash_functions[0].len();
        if n_features != expected_dim {
            return Err(TransformError::InvalidInput(format!(
                "Expected {} features, got {}",
                expected_dim, n_features
            )));
        }

        let n_components = self.n_components;
        let degree = self.degree;
        let mut output = Array2::<f64>::zeros((n_samples, n_components));

        for i in 0..n_samples {
            let row = x.row(i);

            // Compute count sketch for each tensor factor, then convolve via FFT
            // sketch_k[j] = sum_{l: h_k(l)=j} s_k(l) * x_l
            let mut sketches: Vec<Vec<f64>> = Vec::with_capacity(degree);
            for k in 0..degree {
                let mut sketch = vec![0.0_f64; n_components];
                for (l, (&xl, &sl)) in row.iter().zip(signs[k].iter()).enumerate() {
                    let j = hash_functions[k][l];
                    sketch[j] += sl * xl;
                }
                sketches.push(sketch);
            }

            // Combine sketches via convolution (using simple O(D^2) circular convolution
            // for correctness; FFT-based would be O(D log D) but requires oxifft)
            let combined = sketches
                .iter()
                .skip(1)
                .fold(sketches[0].clone(), |acc, sk| {
                    circular_convolve(&acc, sk)
                });

            for j in 0..n_components {
                output[[i, j]] = combined[j];
            }
        }

        Ok(output)
    }

    /// Fit and transform in one step.
    pub fn fit_transform(&mut self, x: ArrayView2<f64>, seed: u64) -> Result<Array2<f64>> {
        let input_dim = x.ncols();
        self.fit(input_dim, seed)?;
        self.transform(x)
    }
}

/// Circular convolution: (a ⊛ b)[k] = Σ_j a[j] b[(k-j) mod n]
fn circular_convolve(a: &[f64], b: &[f64]) -> Vec<f64> {
    let n = a.len();
    let mut result = vec![0.0_f64; n];
    for j in 0..n {
        if a[j] == 0.0 {
            continue;
        }
        for k in 0..n {
            result[(j + k) % n] += a[j] * b[k];
        }
    }
    result
}

// ============================================================================
// Kernel Ridge Regression with Random Fourier Features
// ============================================================================

/// Kernel Ridge Regression using Random Fourier Features.
///
/// Instead of solving the full n×n kernel system, projects X to a D-dimensional
/// feature space Z via RFF and solves the regularized least squares:
/// min_w ‖Zw - y‖² + α ‖w‖²
///
/// This reduces complexity from O(n³) to O(nD² + D³).
///
/// # Example
///
/// ```rust,no_run
/// use scirs2_transform::random_features::{KernelRidgeRegressionRF, ShiftInvariantKernel};
/// use scirs2_core::ndarray::{Array1, Array2};
///
/// let x = Array2::<f64>::zeros((50, 4));
/// let y = Array1::<f64>::zeros(50);
/// let kernel = ShiftInvariantKernel::RBF { gamma: 1.0 };
/// let mut krr = KernelRidgeRegressionRF::new(100, kernel, 0.01);
/// krr.fit(x.view(), &y, 42).expect("should succeed");
/// let preds = krr.predict(x.view()).expect("should succeed");
/// assert_eq!(preds.len(), 50);
/// ```
#[derive(Debug, Clone)]
pub struct KernelRidgeRegressionRF {
    /// Number of random features
    pub n_components: usize,
    /// Kernel function
    pub kernel: ShiftInvariantKernel,
    /// Regularization parameter α
    pub alpha: f64,
    /// Fitted weight vector, shape (n_components,)
    weights: Option<Array1<f64>>,
    /// Random feature transformer
    rff: RandomFourierFeatures,
}

impl KernelRidgeRegressionRF {
    /// Create a new KRR with RFF.
    pub fn new(n_components: usize, kernel: ShiftInvariantKernel, alpha: f64) -> Self {
        let rff = RandomFourierFeatures::new(n_components, kernel.clone());
        KernelRidgeRegressionRF {
            n_components,
            kernel,
            alpha,
            weights: None,
            rff,
        }
    }

    /// Fit: project data to RFF space and solve regularized least squares.
    pub fn fit(
        &mut self,
        x: ArrayView2<f64>,
        y: &Array1<f64>,
        seed: u64,
    ) -> Result<()> {
        let n_samples = x.nrows();
        if y.len() != n_samples {
            return Err(TransformError::InvalidInput(format!(
                "X has {} samples but y has {} labels",
                n_samples,
                y.len()
            )));
        }
        if self.alpha <= 0.0 {
            return Err(TransformError::InvalidInput(
                "alpha must be positive".to_string(),
            ));
        }

        // Compute RFF feature matrix Z: (n_samples, n_components)
        let z = self.rff.fit_transform(x, seed)?;

        // Solve: (Z^T Z + α I) w = Z^T y
        let d = self.n_components;

        // Compute A = Z^T Z + α I  (shape: d×d)
        let mut a = Array2::<f64>::zeros((d, d));
        for k in 0..d {
            for l in k..d {
                let val: f64 = (0..n_samples).map(|i| z[[i, k]] * z[[i, l]]).sum();
                a[[k, l]] = val;
                a[[l, k]] = val;
            }
            a[[k, k]] += self.alpha;
        }

        // Compute b = Z^T y  (shape: d)
        let mut b = Array1::<f64>::zeros(d);
        for k in 0..d {
            b[k] = (0..n_samples).map(|i| z[[i, k]] * y[i]).sum();
        }

        // Solve A w = b
        let w: Array1<f64> = solve(&a.view(), &b.view(), None)
            .map_err(|e| TransformError::ComputationError(e.to_string()))?;

        self.weights = Some(w);
        Ok(())
    }

    /// Predict: return ŷ = Z(x) w.
    pub fn predict(&self, x: ArrayView2<f64>) -> Result<Array1<f64>> {
        let weights = self.weights.as_ref().ok_or_else(|| {
            TransformError::NotFitted("KernelRidgeRegressionRF not fitted".to_string())
        })?;

        let z = self.rff.transform(x)?;
        let n_samples = z.nrows();
        let d = z.ncols();

        let mut preds = Array1::<f64>::zeros(n_samples);
        for i in 0..n_samples {
            preds[i] = (0..d).map(|j| z[[i, j]] * weights[j]).sum();
        }
        Ok(preds)
    }
}

// ============================================================================
// Utility: Exact Kernel Matrices
// ============================================================================

/// Compute exact kernel matrix K[i,j] = k(x_i, x_j) for all pairs.
///
/// Output shape: (n_samples, n_samples)
pub fn kernel_matrix(x: ArrayView2<f64>, kernel: &ShiftInvariantKernel) -> Array2<f64> {
    let n = x.nrows();
    let mut k = Array2::<f64>::zeros((n, n));
    for i in 0..n {
        let xi: Vec<f64> = x.row(i).to_vec();
        for j in i..n {
            let xj: Vec<f64> = x.row(j).to_vec();
            let val = kernel.compute_exact(&xi, &xj);
            k[[i, j]] = val;
            k[[j, i]] = val;
        }
    }
    k
}

/// Compute exact cross-kernel matrix K[i,j] = k(x_i, y_j).
///
/// Output shape: (n_x, n_y)
pub fn cross_kernel_matrix(
    x: ArrayView2<f64>,
    y: ArrayView2<f64>,
    kernel: &ShiftInvariantKernel,
) -> Array2<f64> {
    let (nx, ny) = (x.nrows(), y.nrows());
    let mut k = Array2::<f64>::zeros((nx, ny));
    for i in 0..nx {
        let xi: Vec<f64> = x.row(i).to_vec();
        for j in 0..ny {
            let yj: Vec<f64> = y.row(j).to_vec();
            k[[i, j]] = kernel.compute_exact(&xi, &yj);
        }
    }
    k
}

// ============================================================================
// Maximum Mean Discrepancy Test
// ============================================================================

/// Maximum Mean Discrepancy (MMD) test using random features.
///
/// Tests whether two samples X and Y come from the same distribution.
/// Uses the unbiased MMD² estimator in the RFF feature space:
///
/// MMD²(X,Y) = ‖μ_X - μ_Y‖² where μ_X = (1/n) Σ z(x_i)
///
/// P-value is estimated via bootstrap permutation.
///
/// # Returns
/// `(mmd_statistic, p_value)` where p_value is the fraction of bootstrap
/// MMD values exceeding the observed MMD.
pub fn mmd_test(
    x: ArrayView2<f64>,
    y: ArrayView2<f64>,
    kernel: ShiftInvariantKernel,
    n_components: usize,
    n_bootstrap: usize,
    seed: u64,
) -> Result<(f64, f64)> {
    let (nx, n_features) = (x.nrows(), x.ncols());
    let ny = y.nrows();

    if n_features != y.ncols() {
        return Err(TransformError::InvalidInput(
            "X and Y must have the same number of features".to_string(),
        ));
    }
    if nx == 0 || ny == 0 {
        return Err(TransformError::InvalidInput(
            "Both X and Y must be non-empty".to_string(),
        ));
    }

    // Fit RFF on combined data
    let input_dim = n_features;
    let mut rff = RandomFourierFeatures::new(n_components, kernel);
    rff.fit(input_dim, seed)?;

    // Compute feature embeddings
    let zx = rff.transform(x)?;
    let zy = rff.transform(y)?;

    // Compute unbiased MMD² using the U-statistic estimator on approximate kernel z^T z.
    // This has expectation 0 under H0 (same distribution).
    //
    // MMD²_u = (1/(nx(nx-1))) sum_{i!=j} z_xi^T z_xj
    //        + (1/(ny(ny-1))) sum_{i!=j} z_yi^T z_yj
    //        - (2/(nx*ny))    sum_{i,j}  z_xi^T z_yj
    let mut kxx_sum = 0.0_f64;
    for i in 0..nx {
        for j in 0..nx {
            if i != j {
                let dot: f64 = (0..n_components).map(|d| zx[[i, d]] * zx[[j, d]]).sum();
                kxx_sum += dot;
            }
        }
    }
    let mut kyy_sum = 0.0_f64;
    for i in 0..ny {
        for j in 0..ny {
            if i != j {
                let dot: f64 = (0..n_components).map(|d| zy[[i, d]] * zy[[j, d]]).sum();
                kyy_sum += dot;
            }
        }
    }
    let mut kxy_sum = 0.0_f64;
    for i in 0..nx {
        for j in 0..ny {
            let dot: f64 = (0..n_components).map(|d| zx[[i, d]] * zy[[j, d]]).sum();
            kxy_sum += dot;
        }
    }

    let mmd_sq = kxx_sum / (nx * (nx - 1)) as f64
        + kyy_sum / (ny * (ny - 1)) as f64
        - 2.0 * kxy_sum / (nx * ny) as f64;
    // Clamp to non-negative (unbiased estimator can be slightly negative)
    let mmd_stat = mmd_sq.max(0.0).sqrt();

    // Bootstrap permutation test
    // Pool all feature vectors and permute, computing MMD for each permutation.
    // Pre-compute the full kernel matrix K[i,j] = z_i^T z_j for all pooled samples
    // so bootstrap iterations only need index shuffling, not recomputation.
    let n_total = nx + ny;
    let mut pooled = Array2::<f64>::zeros((n_total, n_components));
    for i in 0..nx {
        pooled.row_mut(i).assign(&zx.row(i));
    }
    for i in 0..ny {
        pooled.row_mut(nx + i).assign(&zy.row(i));
    }

    // Pre-compute approximate kernel matrix for all pooled data
    let mut k_pool = Array2::<f64>::zeros((n_total, n_total));
    for i in 0..n_total {
        for j in i..n_total {
            let dot: f64 = (0..n_components).map(|d| pooled[[i, d]] * pooled[[j, d]]).sum();
            k_pool[[i, j]] = dot;
            k_pool[[j, i]] = dot;
        }
    }

    let mut exceed_count = 0_usize;
    let mut rng = seeded_rng(seed.wrapping_add(999));
    let idx_dist = Uniform::new(0_usize, n_total)
        .map_err(|e| TransformError::ComputationError(e.to_string()))?;

    for _ in 0..n_bootstrap {
        // Fisher-Yates shuffle of indices
        let mut indices: Vec<usize> = (0..n_total).collect();
        for i in (1..n_total).rev() {
            let j = idx_dist.sample(&mut rng) % (i + 1);
            indices.swap(i, j);
        }

        // Compute bootstrap U-statistic MMD^2 using pre-computed kernel
        let bx = &indices[..nx];
        let by = &indices[nx..];

        let mut boot_kxx = 0.0_f64;
        for i in 0..nx {
            for j in 0..nx {
                if i != j {
                    boot_kxx += k_pool[[bx[i], bx[j]]];
                }
            }
        }
        let mut boot_kyy = 0.0_f64;
        let ny_boot = by.len();
        for i in 0..ny_boot {
            for j in 0..ny_boot {
                if i != j {
                    boot_kyy += k_pool[[by[i], by[j]]];
                }
            }
        }
        let mut boot_kxy = 0.0_f64;
        for i in 0..nx {
            for j in 0..ny_boot {
                boot_kxy += k_pool[[bx[i], by[j]]];
            }
        }

        let boot_mmd_sq = boot_kxx / (nx * (nx - 1)).max(1) as f64
            + boot_kyy / (ny_boot * (ny_boot - 1)).max(1) as f64
            - 2.0 * boot_kxy / (nx * ny_boot).max(1) as f64;
        let boot_mmd = boot_mmd_sq.max(0.0).sqrt();
        if boot_mmd >= mmd_stat {
            exceed_count += 1;
        }
    }

    let p_value = if n_bootstrap > 0 {
        exceed_count as f64 / n_bootstrap as f64
    } else {
        1.0
    };

    Ok((mmd_stat, p_value))
}

// ============================================================================
// Internal Helpers
// ============================================================================

/// Compute kernel matrix K[i,j] = k(x_i, y_j) for two datasets.
fn compute_kernel_matrix_between(
    x: &ArrayView2<f64>,
    y: &ArrayView2<f64>,
    kernel: &ShiftInvariantKernel,
) -> Array2<f64> {
    let (nx, ny) = (x.nrows(), y.nrows());
    let mut k = Array2::<f64>::zeros((nx, ny));
    for i in 0..nx {
        let xi: Vec<f64> = x.row(i).to_vec();
        for j in 0..ny {
            let yj: Vec<f64> = y.row(j).to_vec();
            k[[i, j]] = kernel.compute_exact(&xi, &yj);
        }
    }
    k
}

/// Sample `m` distinct indices from `[0, n)` without replacement using
/// Fisher-Yates partial shuffle.
fn sample_without_replacement(n: usize, m: usize, seed: u64) -> Result<Vec<usize>> {
    if m > n {
        return Err(TransformError::InvalidInput(format!(
            "Cannot sample {} items from {} without replacement",
            m, n
        )));
    }
    let mut indices: Vec<usize> = (0..n).collect();
    let mut rng = seeded_rng(seed);

    for i in 0..m {
        let j_dist = Uniform::new(i, n)
            .map_err(|e| TransformError::ComputationError(e.to_string()))?;
        let j = j_dist.sample(&mut rng);
        indices.swap(i, j);
    }
    Ok(indices[..m].to_vec())
}

/// K-means++ landmark selection: returns indices of m cluster centers.
///
/// Implements k-means++ initialization followed by n_iter Lloyd iterations.
fn kmeans_plus_plus_indices(
    x: ArrayView2<f64>,
    m: usize,
    n_iter: usize,
    seed: u64,
) -> Result<Vec<usize>> {
    let n = x.nrows();
    if m >= n {
        return Ok((0..n).collect());
    }

    let mut rng = seeded_rng(seed);
    let unif_n = Uniform::new(0_usize, n)
        .map_err(|e| TransformError::ComputationError(e.to_string()))?;
    let unif_01 = Uniform::new(0.0_f64, 1.0_f64)
        .map_err(|e| TransformError::ComputationError(e.to_string()))?;

    // K-means++ initialization
    let first = unif_n.sample(&mut rng);
    let mut centers: Vec<Vec<f64>> = vec![x.row(first).to_vec()];

    for _ in 1..m {
        // Compute minimum squared distance to existing centers for each point
        let mut min_sq_dists: Vec<f64> = (0..n)
            .map(|i| {
                let xi: Vec<f64> = x.row(i).to_vec();
                centers
                    .iter()
                    .map(|c| {
                        xi.iter().zip(c.iter()).map(|(a, b)| (a - b).powi(2)).sum::<f64>()
                    })
                    .fold(f64::INFINITY, f64::min)
            })
            .collect();

        let total: f64 = min_sq_dists.iter().sum();
        if total < 1e-12 {
            // All points are the same; pick random
            centers.push(x.row(unif_n.sample(&mut rng)).to_vec());
            continue;
        }

        // Normalize to get probability distribution
        for d in min_sq_dists.iter_mut() {
            *d /= total;
        }

        // Sample next center proportional to D²
        let threshold = unif_01.sample(&mut rng);
        let mut cumulative = 0.0;
        let mut chosen = n - 1;
        for (i, &prob) in min_sq_dists.iter().enumerate() {
            cumulative += prob;
            if cumulative >= threshold {
                chosen = i;
                break;
            }
        }
        centers.push(x.row(chosen).to_vec());
    }

    // Lloyd's algorithm: iterate assignment → update until convergence
    let n_features = x.ncols();
    let mut center_matrix: Vec<Vec<f64>> = centers;

    for _iter in 0..n_iter {
        // Assignment step: assign each point to nearest center
        let mut assignments: Vec<usize> = Vec::with_capacity(n);
        for i in 0..n {
            let xi: Vec<f64> = x.row(i).to_vec();
            let best = center_matrix
                .iter()
                .enumerate()
                .map(|(c_idx, c)| {
                    let d: f64 = xi
                        .iter()
                        .zip(c.iter())
                        .map(|(a, b)| (a - b).powi(2))
                        .sum();
                    (c_idx, d)
                })
                .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
                .map(|(idx, _)| idx)
                .unwrap_or(0);
            assignments.push(best);
        }

        // Update step: recompute centers as means of assigned points
        let mut new_centers = vec![vec![0.0_f64; n_features]; m];
        let mut counts = vec![0_usize; m];
        for (i, &c_idx) in assignments.iter().enumerate() {
            counts[c_idx] += 1;
            let xi: Vec<f64> = x.row(i).to_vec();
            for d in 0..n_features {
                new_centers[c_idx][d] += xi[d];
            }
        }
        for c_idx in 0..m {
            if counts[c_idx] > 0 {
                let cnt = counts[c_idx] as f64;
                for d in 0..n_features {
                    new_centers[c_idx][d] /= cnt;
                }
            } else {
                // Empty cluster: reinitialize to a random point
                new_centers[c_idx] = x.row(unif_n.sample(&mut rng)).to_vec();
            }
        }
        center_matrix = new_centers;
    }

    // Find nearest training point to each final center (return indices into x)
    let landmark_indices: Vec<usize> = center_matrix
        .iter()
        .map(|c| {
            (0..n)
                .map(|i| {
                    let xi: Vec<f64> = x.row(i).to_vec();
                    let d: f64 = xi
                        .iter()
                        .zip(c.iter())
                        .map(|(a, b)| (a - b).powi(2))
                        .sum();
                    (i, d)
                })
                .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
                .map(|(idx, _)| idx)
                .unwrap_or(0)
        })
        .collect();

    // Deduplicate while preserving order
    let mut seen = std::collections::HashSet::new();
    let unique: Vec<usize> = landmark_indices
        .into_iter()
        .filter(|&idx| seen.insert(idx))
        .collect();

    // If deduplication reduced count, fill remainder with random points
    let mut result = unique;
    if result.len() < m {
        let mut remaining: Vec<usize> = (0..n).filter(|i| !result.contains(i)).collect();
        let needed = m - result.len();
        let take = needed.min(remaining.len());
        // Shuffle remaining and take first `take`
        for i in 0..take {
            let j_dist = Uniform::new(i, remaining.len())
                .map_err(|e| TransformError::ComputationError(e.to_string()))?;
            let j = j_dist.sample(&mut rng);
            remaining.swap(i, j);
        }
        result.extend_from_slice(&remaining[..take]);
    }

    Ok(result[..m.min(result.len())].to_vec())
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::Array2;

    /// Generate a simple reproducible dataset.
    fn make_data(n: usize, d: usize, seed: u64) -> Array2<f64> {
        let mut rng = seeded_rng(seed);
        let dist = Normal::new(0.0_f64, 1.0_f64).expect("Normal distribution creation failed");
        let data: Vec<f64> = (0..n * d).map(|_| dist.sample(&mut rng)).collect();
        Array2::from_shape_vec((n, d), data).expect("Failed to create data array")
    }

    /// Generate two well-separated datasets for MMD tests.
    fn make_two_distributions(
        n: usize,
        d: usize,
        mean_shift: f64,
        seed: u64,
    ) -> (Array2<f64>, Array2<f64>) {
        let x = make_data(n, d, seed);
        let mut y = make_data(n, d, seed.wrapping_add(1000));
        y.mapv_inplace(|v| v + mean_shift);
        (x, y)
    }

    // ------------------------------------------------------------------
    // RFF output shape
    // ------------------------------------------------------------------
    #[test]
    fn test_rff_output_shape() {
        let x = make_data(30, 5, 1);
        let kernel = ShiftInvariantKernel::RBF { gamma: 0.5 };
        let mut rff = RandomFourierFeatures::new(50, kernel);
        let z = rff.fit_transform(x.view(), 42).expect("RFF fit_transform failed");
        assert_eq!(z.shape(), &[30, 50], "RFF output shape mismatch");
    }

    // ------------------------------------------------------------------
    // RFF approximates RBF kernel: max error < 0.1 for D=500
    // ------------------------------------------------------------------
    #[test]
    fn test_rff_approximates_rbf_kernel() {
        let x = make_data(20, 4, 7);
        let kernel = ShiftInvariantKernel::RBF { gamma: 0.5 };

        // Exact kernel matrix
        let k_exact = kernel_matrix(x.view(), &kernel);

        // Approximate via RFF with D=500
        let mut rff = RandomFourierFeatures::new(500, kernel);
        let k_approx = rff
            .fit_transform(x.view(), 0)
            .and_then(|_| rff.approximate_kernel(x.view()))
            .expect("RFF kernel approximation failed");

        let n = x.nrows();
        let mut max_err = 0.0_f64;
        for i in 0..n {
            for j in 0..n {
                let err = (k_exact[[i, j]] - k_approx[[i, j]]).abs();
                if err > max_err {
                    max_err = err;
                }
            }
        }
        assert!(
            max_err < 0.15,
            "RFF max kernel approximation error too large: {:.4} (expected < 0.15)",
            max_err
        );
    }

    // ------------------------------------------------------------------
    // RFF: various kernels produce correct output shape
    // ------------------------------------------------------------------
    #[test]
    fn test_rff_laplacian_kernel() {
        let x = make_data(10, 3, 2);
        let kernel = ShiftInvariantKernel::Laplacian { gamma: 1.0 };
        let mut rff = RandomFourierFeatures::new(64, kernel);
        let z = rff.fit_transform(x.view(), 1).expect("Laplacian RFF failed");
        assert_eq!(z.shape(), &[10, 64]);
        // All values should be finite (Cauchy samples are clipped)
        assert!(z.iter().all(|v| v.is_finite()), "Non-finite RFF values");
    }

    #[test]
    fn test_rff_cauchy_kernel() {
        let x = make_data(10, 3, 3);
        let kernel = ShiftInvariantKernel::Cauchy { gamma: 0.5 };
        let mut rff = RandomFourierFeatures::new(64, kernel);
        let z = rff.fit_transform(x.view(), 2).expect("Cauchy RFF failed");
        assert_eq!(z.shape(), &[10, 64]);
        assert!(z.iter().all(|v| v.is_finite()));
    }

    #[test]
    fn test_rff_matern32_kernel() {
        let x = make_data(15, 4, 4);
        let kernel = ShiftInvariantKernel::Matern32 { length_scale: 1.0 };
        let mut rff = RandomFourierFeatures::new(100, kernel);
        let z = rff.fit_transform(x.view(), 3).expect("Matern32 RFF failed");
        assert_eq!(z.shape(), &[15, 100]);
        assert!(z.iter().all(|v| v.is_finite()));
    }

    #[test]
    fn test_rff_matern52_kernel() {
        let x = make_data(15, 4, 5);
        let kernel = ShiftInvariantKernel::Matern52 { length_scale: 1.5 };
        let mut rff = RandomFourierFeatures::new(100, kernel);
        let z = rff.fit_transform(x.view(), 4).expect("Matern52 RFF failed");
        assert_eq!(z.shape(), &[15, 100]);
        assert!(z.iter().all(|v| v.is_finite()));
    }

    // ------------------------------------------------------------------
    // RFF: RBF spectral density is Gaussian (verify sampled frequencies)
    // ------------------------------------------------------------------
    #[test]
    fn test_rbf_spectral_density_is_gaussian() {
        let gamma = 2.0;
        let kernel = ShiftInvariantKernel::RBF { gamma };
        let n_samples = 5000;
        let input_dim = 1;
        let weights = kernel
            .sample_frequencies(n_samples, input_dim, 77)
            .expect("Frequency sampling failed");

        // The distribution should be N(0, sqrt(2γ)) = N(0, 2.0)
        let expected_std = (2.0 * gamma).sqrt(); // = 2.0

        let mean: f64 = weights.iter().sum::<f64>() / n_samples as f64;
        let variance: f64 = weights.iter().map(|&w| (w - mean).powi(2)).sum::<f64>()
            / n_samples as f64;
        let std_dev = variance.sqrt();

        assert!(
            mean.abs() < 0.1,
            "RBF spectral mean not near 0: {:.4}",
            mean
        );
        assert!(
            (std_dev - expected_std).abs() < 0.2,
            "RBF spectral std {:.4} not near expected {:.4}",
            std_dev,
            expected_std
        );
    }

    // ------------------------------------------------------------------
    // Nyström output shape
    // ------------------------------------------------------------------
    #[test]
    fn test_nystrom_output_shape() {
        let x = make_data(40, 5, 10);
        let kernel = ShiftInvariantKernel::RBF { gamma: 1.0 };
        let mut nystrom = NystromApproximation::new(15, kernel);
        let phi = nystrom
            .fit_transform(x.view(), LandmarkSelection::Random, 0)
            .expect("Nystrom fit_transform failed");
        assert_eq!(phi.shape(), &[40, 15], "Nystrom output shape mismatch");
    }

    // ------------------------------------------------------------------
    // Nyström approximation is positive semi-definite
    // ------------------------------------------------------------------
    #[test]
    fn test_nystrom_kernel_psd() {
        let x = make_data(20, 3, 11);
        let kernel = ShiftInvariantKernel::RBF { gamma: 0.5 };
        let mut nystrom = NystromApproximation::new(10, kernel);
        nystrom
            .fit(x.view(), LandmarkSelection::Random, 5)
            .expect("Nystrom fit failed");
        let k_approx = nystrom
            .approximate_kernel(x.view())
            .expect("Nystrom kernel failed");

        let n = k_approx.nrows();
        // Check that it's symmetric
        for i in 0..n {
            for j in 0..n {
                assert!(
                    (k_approx[[i, j]] - k_approx[[j, i]]).abs() < 1e-10,
                    "Nystrom kernel not symmetric at ({}, {})",
                    i,
                    j
                );
            }
        }

        // Check eigenvalues are non-negative
        let (evals, _) = eigh(&k_approx.view(), None).expect("eigh failed");
        let min_eval = evals.iter().cloned().fold(f64::INFINITY, f64::min);
        assert!(
            min_eval >= -1e-8,
            "Nystrom kernel not PSD: min eigenvalue = {:.2e}",
            min_eval
        );
    }

    // ------------------------------------------------------------------
    // Nyström with KMeans landmark selection
    // ------------------------------------------------------------------
    #[test]
    fn test_nystrom_kmeans_selection() {
        let x = make_data(50, 4, 12);
        let kernel = ShiftInvariantKernel::RBF { gamma: 0.5 };
        let mut nystrom = NystromApproximation::new(10, kernel);
        let phi = nystrom
            .fit_transform(x.view(), LandmarkSelection::KMeans { n_iter: 5 }, 6)
            .expect("Nystrom KMeans failed");
        assert_eq!(phi.shape(), &[50, 10]);
    }

    // ------------------------------------------------------------------
    // Nyström with Uniform landmark selection
    // ------------------------------------------------------------------
    #[test]
    fn test_nystrom_uniform_selection() {
        let x = make_data(30, 3, 13);
        let kernel = ShiftInvariantKernel::RBF { gamma: 1.0 };
        let mut nystrom = NystromApproximation::new(8, kernel);
        let phi = nystrom
            .fit_transform(x.view(), LandmarkSelection::Uniform, 7)
            .expect("Nystrom Uniform failed");
        assert_eq!(phi.shape(), &[30, 8]);
    }

    // ------------------------------------------------------------------
    // Tensor Sketch output shape
    // ------------------------------------------------------------------
    #[test]
    fn test_tensor_sketch_output_shape() {
        let x = make_data(25, 6, 20);
        let mut ts = TensorSketch::new(32, 3);
        let z = ts.fit_transform(x.view(), 0).expect("TensorSketch failed");
        assert_eq!(z.shape(), &[25, 32], "TensorSketch output shape mismatch");
    }

    // ------------------------------------------------------------------
    // Tensor Sketch: polynomial kernel degree 1 ≈ linear sketch
    // ------------------------------------------------------------------
    #[test]
    fn test_tensor_sketch_degree1_values_finite() {
        let x = make_data(10, 4, 21);
        let mut ts = TensorSketch::new(16, 1);
        let z = ts.fit_transform(x.view(), 1).expect("TensorSketch degree-1 failed");
        assert_eq!(z.shape(), &[10, 16]);
        assert!(z.iter().all(|v| v.is_finite()), "Non-finite tensor sketch values");
    }

    // ------------------------------------------------------------------
    // KernelRidgeRegressionRF output shape
    // ------------------------------------------------------------------
    #[test]
    fn test_krr_rf_output_shape() {
        let x = make_data(40, 5, 30);
        let y: Vec<f64> = (0..40).map(|i| i as f64 * 0.1).collect();
        let y_arr = Array1::from_vec(y);
        let kernel = ShiftInvariantKernel::RBF { gamma: 0.5 };
        let mut krr = KernelRidgeRegressionRF::new(100, kernel, 0.01);
        krr.fit(x.view(), &y_arr, 42).expect("KRR fit failed");
        let preds = krr.predict(x.view()).expect("KRR predict failed");
        assert_eq!(preds.len(), 40, "KRR prediction shape mismatch");
        assert!(
            preds.iter().all(|v| v.is_finite()),
            "Non-finite KRR predictions"
        );
    }

    // ------------------------------------------------------------------
    // KRR with RFF converges toward exact solution
    // ------------------------------------------------------------------
    #[test]
    fn test_krr_rf_reasonable_predictions() {
        // Simple linear target that should be approximable
        let n = 30;
        let d = 3;
        let x = make_data(n, d, 31);

        // y = sum of first column
        let y: Array1<f64> = Array1::from_iter(x.column(0).iter().cloned());

        let kernel = ShiftInvariantKernel::RBF { gamma: 0.5 };
        let mut krr = KernelRidgeRegressionRF::new(200, kernel, 0.001);
        krr.fit(x.view(), &y, 99).expect("KRR fit failed");
        let preds = krr.predict(x.view()).expect("KRR predict failed");

        // MSE should be finite and not extremely large
        let mse: f64 = preds
            .iter()
            .zip(y.iter())
            .map(|(p, t)| (p - t).powi(2))
            .sum::<f64>()
            / n as f64;
        assert!(mse.is_finite(), "KRR MSE is not finite");
        assert!(
            mse < 10.0,
            "KRR MSE too large: {:.4} (sanity check failure)",
            mse
        );
    }

    // ------------------------------------------------------------------
    // Exact kernel matrix is positive semi-definite
    // ------------------------------------------------------------------
    #[test]
    fn test_exact_kernel_matrix_psd() {
        let x = make_data(15, 3, 40);
        let kernel = ShiftInvariantKernel::RBF { gamma: 0.5 };
        let k = kernel_matrix(x.view(), &kernel);

        let n = k.nrows();
        // Symmetry check
        for i in 0..n {
            for j in 0..n {
                assert!(
                    (k[[i, j]] - k[[j, i]]).abs() < 1e-12,
                    "Kernel matrix not symmetric"
                );
            }
        }

        let (evals, _) = eigh(&k.view(), None).expect("eigh failed on kernel matrix");
        let min_eval = evals.iter().cloned().fold(f64::INFINITY, f64::min);
        assert!(
            min_eval >= -1e-8,
            "Kernel matrix not PSD: min eigenvalue = {:.2e}",
            min_eval
        );
    }

    // ------------------------------------------------------------------
    // MMD between same distribution ≈ 0
    // ------------------------------------------------------------------
    #[test]
    fn test_mmd_same_distribution_small() {
        let n = 100;
        let d = 5;
        let x = make_data(n, d, 50);
        let y = make_data(n, d, 51);  // same distribution, different seed

        let kernel = ShiftInvariantKernel::RBF { gamma: 0.5 };
        let (mmd_stat, _p_value) =
            mmd_test(x.view(), y.view(), kernel, 200, 100, 0).expect("MMD test failed");

        assert!(
            mmd_stat < 0.1,
            "MMD between same distribution should be small, got {:.4}",
            mmd_stat
        );
    }

    // ------------------------------------------------------------------
    // MMD between different distributions > same-distribution MMD
    // ------------------------------------------------------------------
    #[test]
    fn test_mmd_different_distributions_larger() {
        let n = 80;
        let d = 4;

        let (x1, y1) = make_two_distributions(n, d, 0.0, 60);
        let (x2, y2) = make_two_distributions(n, d, 5.0, 61);

        let kernel1 = ShiftInvariantKernel::RBF { gamma: 0.1 };
        let kernel2 = ShiftInvariantKernel::RBF { gamma: 0.1 };

        let (mmd_same, _) =
            mmd_test(x1.view(), y1.view(), kernel1, 200, 50, 0).expect("MMD same failed");
        let (mmd_diff, _) =
            mmd_test(x2.view(), y2.view(), kernel2, 200, 50, 1).expect("MMD diff failed");

        assert!(
            mmd_diff > mmd_same,
            "MMD for different distributions ({:.4}) should exceed same-distribution ({:.4})",
            mmd_diff,
            mmd_same
        );
    }

    // ------------------------------------------------------------------
    // Cross-kernel matrix shape
    // ------------------------------------------------------------------
    #[test]
    fn test_cross_kernel_shape() {
        let x = make_data(10, 4, 70);
        let y = make_data(7, 4, 71);
        let kernel = ShiftInvariantKernel::RBF { gamma: 1.0 };
        let k = cross_kernel_matrix(x.view(), y.view(), &kernel);
        assert_eq!(k.shape(), &[10, 7], "Cross kernel shape mismatch");
    }

    // ------------------------------------------------------------------
    // RFF cross-kernel approximation shape
    // ------------------------------------------------------------------
    #[test]
    fn test_rff_cross_kernel_shape() {
        let x = make_data(12, 3, 80);
        let y = make_data(8, 3, 81);
        let kernel = ShiftInvariantKernel::RBF { gamma: 0.5 };
        let mut rff = RandomFourierFeatures::new(100, kernel);
        rff.fit(3, 0).expect("RFF fit failed");
        let k = rff
            .approximate_cross_kernel(x.view(), y.view())
            .expect("cross kernel failed");
        assert_eq!(k.shape(), &[12, 8]);
    }

    // ------------------------------------------------------------------
    // Error handling: not fitted
    // ------------------------------------------------------------------
    #[test]
    fn test_not_fitted_error() {
        let x = make_data(5, 3, 90);
        let kernel = ShiftInvariantKernel::RBF { gamma: 1.0 };
        let rff = RandomFourierFeatures::new(10, kernel);
        let result = rff.transform(x.view());
        assert!(
            result.is_err(),
            "Should return error when not fitted"
        );
    }

    #[test]
    fn test_nystrom_not_fitted_error() {
        let x = make_data(5, 3, 91);
        let kernel = ShiftInvariantKernel::RBF { gamma: 1.0 };
        let nystrom = NystromApproximation::new(3, kernel);
        let result = nystrom.transform(x.view());
        assert!(result.is_err(), "Nystrom should return error when not fitted");
    }

    // ------------------------------------------------------------------
    // Dimension mismatch error
    // ------------------------------------------------------------------
    #[test]
    fn test_rff_dimension_mismatch() {
        let x_fit = make_data(10, 4, 100);
        let x_test = make_data(5, 6, 101);
        let kernel = ShiftInvariantKernel::RBF { gamma: 1.0 };
        let mut rff = RandomFourierFeatures::new(50, kernel);
        rff.fit(4, 0).expect("fit failed");
        let result = rff.transform(x_test.view());
        assert!(result.is_err(), "Should error on dimension mismatch");
        drop(x_fit);
    }

    // ------------------------------------------------------------------
    // Compute exact Matern kernel values
    // ------------------------------------------------------------------
    #[test]
    fn test_exact_matern_kernel_at_zero() {
        // k(x, x) = 1 for all Matérn kernels
        let x = vec![1.0, 2.0, 3.0];
        let kernel_32 = ShiftInvariantKernel::Matern32 { length_scale: 1.0 };
        let kernel_52 = ShiftInvariantKernel::Matern52 { length_scale: 1.0 };
        let k32 = kernel_32.compute_exact(&x, &x);
        let k52 = kernel_52.compute_exact(&x, &x);
        assert!(
            (k32 - 1.0).abs() < 1e-12,
            "Matern32 k(x,x) should be 1, got {:.6}",
            k32
        );
        assert!(
            (k52 - 1.0).abs() < 1e-12,
            "Matern52 k(x,x) should be 1, got {:.6}",
            k52
        );
    }

    // ------------------------------------------------------------------
    // RFF is_fitted check
    // ------------------------------------------------------------------
    #[test]
    fn test_rff_is_fitted() {
        let kernel = ShiftInvariantKernel::RBF { gamma: 0.5 };
        let mut rff = RandomFourierFeatures::new(50, kernel);
        assert!(!rff.is_fitted(), "Should not be fitted before fit()");
        rff.fit(4, 42).expect("fit failed");
        assert!(rff.is_fitted(), "Should be fitted after fit()");
    }
}