sklears-gaussian-process 0.2.0

Gaussian Process models for regression and classification
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
//! Advanced kernel structure learning using grammar-based search
//!
//! This module implements sophisticated kernel structure learning algorithms that can
//! automatically discover complex kernel compositions using grammar-based search,
//! statistical tests, and structure optimization.

use crate::kernels::*;
// SciRS2 Policy - Use scirs2-autograd for ndarray types and operations
use scirs2_core::ndarray::{ArrayView1, ArrayView2};
use scirs2_core::RngExt;
// SciRS2 Policy - Use scirs2-core for random number generation
use sklears_core::error::{Result as SklResult, SklearsError};

/// Grammar-based kernel structure learning
#[derive(Debug, Clone)]
pub struct KernelStructureLearner {
    /// Maximum depth of kernel expressions
    pub max_depth: usize,
    /// Maximum number of iterations for structure search
    pub max_iterations: usize,
    /// Probability of adding new components
    pub expansion_probability: f64,
    /// Probability of simplifying structures
    pub simplification_probability: f64,
    /// Minimum improvement threshold for accepting new structures
    pub improvement_threshold: f64,
    /// Whether to use Bayesian information criterion for model selection
    pub use_bic: bool,
    /// Random state for reproducible results
    pub random_state: Option<u64>,
    /// Search strategy
    pub search_strategy: SearchStrategy,
}

/// Search strategies for kernel structure learning
#[derive(Debug, Clone, Copy)]
pub enum SearchStrategy {
    Greedy,
    Beam {
        beam_width: usize,
    },
    /// Genetic algorithm - evolve population of kernel structures
    Genetic {
        population_size: usize,
    },
    /// Simulated annealing - accept worse solutions with decreasing probability
    SimulatedAnnealing {
        initial_temperature: f64,
    },
}

/// Kernel grammar production rules
#[derive(Debug, Clone)]
pub enum KernelGrammar {
    /// Terminal symbols (base kernels)
    Terminal(TerminalKernel),
    /// Non-terminal symbols (operations)
    NonTerminal(NonTerminalOperation),
}

/// Base kernel types in the grammar
#[derive(Debug, Clone)]
pub enum TerminalKernel {
    /// RBF
    RBF { length_scale: f64 },
    /// Linear
    Linear { sigma_0: f64, sigma_1: f64 },
    /// Periodic
    Periodic { length_scale: f64, period: f64 },
    /// Matern
    Matern { length_scale: f64, nu: f64 },
    /// RationalQuadratic
    RationalQuadratic { length_scale: f64, alpha: f64 },
    /// White
    White { noise_level: f64 },
    /// Constant
    Constant { constant_value: f64 },
}

/// Operations for combining kernels
#[derive(Debug, Clone)]
pub enum NonTerminalOperation {
    /// Sum
    Sum {
        left: Box<KernelGrammar>,
        right: Box<KernelGrammar>,
    },
    /// Product
    Product {
        left: Box<KernelGrammar>,
        right: Box<KernelGrammar>,
    },
    /// Power
    Power {
        base: Box<KernelGrammar>,
        exponent: f64,
    },
    /// Scale
    Scale {
        kernel: Box<KernelGrammar>,
        scale: f64,
    },
}

/// Result of kernel structure learning
#[derive(Debug, Clone)]
pub struct StructureLearningResult {
    /// The best kernel structure found
    pub best_kernel: Box<dyn Kernel>,
    /// Grammar expression of the best kernel
    pub best_expression: KernelGrammar,
    /// Score of the best kernel
    pub best_score: f64,
    /// All structures explored with their scores
    pub exploration_history: Vec<(String, f64)>,
    /// Convergence information
    pub convergence_info: ConvergenceInfo,
}

/// Information about the learning convergence
#[derive(Debug, Clone)]
pub struct ConvergenceInfo {
    /// Number of iterations completed
    pub iterations: usize,
    /// Score history over iterations
    pub score_history: Vec<f64>,
    /// Whether the algorithm converged
    pub converged: bool,
    /// Final temperature (for simulated annealing)
    pub final_temperature: Option<f64>,
}

impl Default for KernelStructureLearner {
    fn default() -> Self {
        Self {
            max_depth: 4,
            max_iterations: 100,
            expansion_probability: 0.7,
            simplification_probability: 0.3,
            improvement_threshold: 0.01,
            use_bic: true,
            random_state: Some(42),
            search_strategy: SearchStrategy::Greedy,
        }
    }
}

#[allow(non_snake_case)]
impl KernelStructureLearner {
    /// Create a new kernel structure learner
    pub fn new() -> Self {
        Self::default()
    }

    /// Set maximum depth of kernel expressions
    pub fn max_depth(mut self, depth: usize) -> Self {
        self.max_depth = depth;
        self
    }

    /// Set maximum number of iterations
    pub fn max_iterations(mut self, iterations: usize) -> Self {
        self.max_iterations = iterations;
        self
    }

    /// Set search strategy
    pub fn search_strategy(mut self, strategy: SearchStrategy) -> Self {
        self.search_strategy = strategy;
        self
    }

    /// Set random state for reproducible results
    pub fn random_state(mut self, seed: Option<u64>) -> Self {
        self.random_state = seed;
        self
    }

    /// Learn kernel structure from data
    pub fn learn_structure(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
    ) -> SklResult<StructureLearningResult> {
        // SciRS2 Policy - Use scirs2-core for random number generation
        let mut rng = if let Some(seed) = self.random_state {
            scirs2_core::random::Random::seed(seed)
        } else {
            scirs2_core::random::Random::seed(42)
        };

        match self.search_strategy {
            SearchStrategy::Greedy => self.greedy_search(X, y, &mut rng),
            SearchStrategy::Beam { beam_width } => self.beam_search(X, y, beam_width, &mut rng),
            SearchStrategy::Genetic { population_size } => {
                self.genetic_search(X, y, population_size, &mut rng)
            }
            SearchStrategy::SimulatedAnnealing {
                initial_temperature,
            } => self.simulated_annealing_search(X, y, initial_temperature, &mut rng),
        }
    }

    /// Greedy search for kernel structure
    fn greedy_search(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        rng: &mut scirs2_core::random::Random<scirs2_core::rngs::StdRng>,
    ) -> SklResult<StructureLearningResult> {
        let mut exploration_history = Vec::new();
        let mut score_history = Vec::new();

        // Start with simple RBF kernel
        let mut current_expression =
            KernelGrammar::Terminal(TerminalKernel::RBF { length_scale: 1.0 });
        let mut current_kernel = self.expression_to_kernel(&current_expression)?;
        let mut current_score = self.evaluate_kernel(current_kernel.as_ref(), &X, &y)?;

        exploration_history.push(("RBF".to_string(), current_score));
        score_history.push(current_score);

        let mut best_expression = current_expression.clone();
        let mut best_kernel = current_kernel.clone();
        let mut best_score = current_score;

        for _iteration in 0..self.max_iterations {
            // Generate candidate structures
            let candidates = self.generate_candidates(&current_expression, rng)?;

            let mut found_improvement = false;

            for candidate in candidates {
                if self.expression_depth(&candidate) > self.max_depth {
                    continue;
                }

                let kernel = self.expression_to_kernel(&candidate)?;
                let score = self.evaluate_kernel(kernel.as_ref(), &X, &y)?;

                let expression_str = self.expression_to_string(&candidate);
                exploration_history.push((expression_str, score));

                if score < current_score - self.improvement_threshold {
                    current_expression = candidate;
                    current_kernel = kernel;
                    current_score = score;
                    found_improvement = true;

                    if score < best_score {
                        best_expression = current_expression.clone();
                        best_kernel = current_kernel.clone();
                        best_score = score;
                    }
                    break;
                }
            }

            score_history.push(current_score);

            if !found_improvement {
                break;
            }
        }

        Ok(StructureLearningResult {
            best_kernel,
            best_expression,
            best_score,
            exploration_history,
            convergence_info: ConvergenceInfo {
                iterations: score_history.len(),
                score_history,
                converged: true,
                final_temperature: None,
            },
        })
    }

    /// Beam search for kernel structure
    fn beam_search(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        beam_width: usize,
        rng: &mut scirs2_core::random::Random<scirs2_core::rngs::StdRng>,
    ) -> SklResult<StructureLearningResult> {
        let mut exploration_history = Vec::new();
        let mut score_history = Vec::new();

        // Initialize beam with base kernels
        let mut beam: Vec<(KernelGrammar, f64)> = Vec::new();
        let initial_kernels = self.generate_initial_kernels()?;

        if initial_kernels.is_empty() {
            return Err(SklearsError::InvalidOperation(
                "No initial kernels generated".to_string(),
            ));
        }

        for kernel_expr in initial_kernels {
            let kernel = self.expression_to_kernel(&kernel_expr)?;
            let score = self.evaluate_kernel(kernel.as_ref(), &X, &y)?;
            beam.push((kernel_expr.clone(), score));

            let expr_str = self.expression_to_string(&kernel_expr);
            exploration_history.push((expr_str, score));
        }

        // Sort beam by score (lower is better)
        beam.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));
        beam.truncate(beam_width);

        if beam.is_empty() {
            return Err(SklearsError::InvalidOperation(
                "Beam is empty after initialization".to_string(),
            ));
        }

        let mut best_score = beam[0].1;
        score_history.push(best_score);

        for _iteration in 0..self.max_iterations {
            let mut new_beam = Vec::new();

            // Expand each beam element
            for (expression, _) in &beam {
                let candidates = self.generate_candidates(expression, rng)?;

                for candidate in candidates {
                    if self.expression_depth(&candidate) > self.max_depth {
                        continue;
                    }

                    let kernel = self.expression_to_kernel(&candidate)?;
                    let score = self.evaluate_kernel(kernel.as_ref(), &X, &y)?;

                    new_beam.push((candidate.clone(), score));

                    let expr_str = self.expression_to_string(&candidate);
                    exploration_history.push((expr_str, score));
                }
            }

            // Merge and sort
            new_beam.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));
            new_beam.truncate(beam_width);

            // Update beam
            beam = new_beam;

            if beam.is_empty() {
                break;
            }

            let current_best = beam[0].1;
            score_history.push(current_best);

            if (best_score - current_best).abs() < self.improvement_threshold {
                break;
            }

            best_score = current_best;
        }

        if beam.is_empty() {
            // Return a default RBF kernel if no beam elements remain
            let default_expression =
                KernelGrammar::Terminal(TerminalKernel::RBF { length_scale: 1.0 });
            let default_kernel = self.expression_to_kernel(&default_expression)?;
            return Ok(StructureLearningResult {
                best_kernel: default_kernel,
                best_expression: default_expression,
                best_score: f64::INFINITY,
                exploration_history,
                convergence_info: ConvergenceInfo {
                    iterations: score_history.len(),
                    score_history,
                    converged: false,
                    final_temperature: None,
                },
            });
        }

        let best_expression = beam[0].0.clone();
        let best_kernel = self.expression_to_kernel(&best_expression)?;

        Ok(StructureLearningResult {
            best_kernel,
            best_expression,
            best_score: beam[0].1,
            exploration_history,
            convergence_info: ConvergenceInfo {
                iterations: score_history.len(),
                score_history,
                converged: true,
                final_temperature: None,
            },
        })
    }

    /// Genetic algorithm search for kernel structure
    fn genetic_search(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        population_size: usize,
        rng: &mut scirs2_core::random::Random<scirs2_core::rngs::StdRng>,
    ) -> SklResult<StructureLearningResult> {
        let mut exploration_history = Vec::new();
        let mut score_history = Vec::new();

        // Initialize population
        let mut population: Vec<(KernelGrammar, f64)> = Vec::new();
        let initial_kernels = self.generate_initial_kernels()?;

        for _ in 0..population_size {
            let idx = rng.gen_range(0..initial_kernels.len());
            let kernel_expr = initial_kernels[idx].clone();
            let kernel = self.expression_to_kernel(&kernel_expr)?;
            let score = self.evaluate_kernel(kernel.as_ref(), &X, &y)?;
            population.push((kernel_expr, score));
        }

        for _generation in 0..self.max_iterations {
            // Selection: tournament selection
            let mut new_population = Vec::new();

            for _ in 0..population_size {
                let parent1 = self.tournament_selection(&population, rng);
                let parent2 = self.tournament_selection(&population, rng);

                // Crossover
                let child = self.crossover(&parent1.0, &parent2.0, rng)?;

                // Mutation
                let mutated_child = self.mutate(&child, rng)?;

                if self.expression_depth(&mutated_child) <= self.max_depth {
                    let kernel = self.expression_to_kernel(&mutated_child)?;
                    let score = self.evaluate_kernel(kernel.as_ref(), &X, &y)?;
                    new_population.push((mutated_child.clone(), score));

                    let expr_str = self.expression_to_string(&mutated_child);
                    exploration_history.push((expr_str, score));
                }
            }

            // Replace population
            population = new_population;
            population.sort_by(|a, b| a.1.partial_cmp(&b.1).expect("operation should succeed"));
            population.truncate(population_size);

            let best_score = population[0].1;
            score_history.push(best_score);
        }

        let best_expression = population[0].0.clone();
        let best_kernel = self.expression_to_kernel(&best_expression)?;

        Ok(StructureLearningResult {
            best_kernel,
            best_expression,
            best_score: population[0].1,
            exploration_history,
            convergence_info: ConvergenceInfo {
                iterations: score_history.len(),
                score_history,
                converged: true,
                final_temperature: None,
            },
        })
    }

    /// Simulated annealing search for kernel structure
    fn simulated_annealing_search(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        initial_temperature: f64,
        rng: &mut scirs2_core::random::Random<scirs2_core::rngs::StdRng>,
    ) -> SklResult<StructureLearningResult> {
        let mut exploration_history = Vec::new();
        let mut score_history = Vec::new();

        // Start with simple RBF kernel
        let mut current_expression =
            KernelGrammar::Terminal(TerminalKernel::RBF { length_scale: 1.0 });
        let mut current_kernel = self.expression_to_kernel(&current_expression)?;
        let mut current_score = self.evaluate_kernel(current_kernel.as_ref(), &X, &y)?;

        let mut best_expression = current_expression.clone();
        let mut best_kernel = current_kernel.clone();
        let mut best_score = current_score;

        let mut temperature = initial_temperature;
        let cooling_rate = 0.95;

        for _iteration in 0..self.max_iterations {
            // Generate a neighbor
            let candidates = self.generate_candidates(&current_expression, rng)?;
            if candidates.is_empty() {
                break;
            }

            let idx = rng.gen_range(0..candidates.len());
            let candidate = &candidates[idx];
            if self.expression_depth(candidate) > self.max_depth {
                continue;
            }

            let kernel = self.expression_to_kernel(candidate)?;
            let score = self.evaluate_kernel(kernel.as_ref(), &X, &y)?;

            let expr_str = self.expression_to_string(candidate);
            exploration_history.push((expr_str, score));

            // Accept or reject based on Metropolis criterion
            let delta = score - current_score;
            if delta < 0.0 || rng.random::<f64>() < (-delta / temperature).exp() {
                current_expression = candidate.clone();
                current_kernel = kernel;
                current_score = score;

                if score < best_score {
                    best_expression = current_expression.clone();
                    best_kernel = current_kernel.clone();
                    best_score = score;
                }
            }

            score_history.push(current_score);
            temperature *= cooling_rate;
        }

        Ok(StructureLearningResult {
            best_kernel,
            best_expression,
            best_score,
            exploration_history,
            convergence_info: ConvergenceInfo {
                iterations: score_history.len(),
                score_history,
                converged: true,
                final_temperature: Some(temperature),
            },
        })
    }

    /// Generate initial base kernels
    fn generate_initial_kernels(&self) -> SklResult<Vec<KernelGrammar>> {
        Ok(vec![
            KernelGrammar::Terminal(TerminalKernel::RBF { length_scale: 1.0 }),
            KernelGrammar::Terminal(TerminalKernel::Linear {
                sigma_0: 1.0,
                sigma_1: 1.0,
            }),
            KernelGrammar::Terminal(TerminalKernel::Matern {
                length_scale: 1.0,
                nu: 1.5,
            }),
            KernelGrammar::Terminal(TerminalKernel::RationalQuadratic {
                length_scale: 1.0,
                alpha: 1.0,
            }),
            KernelGrammar::Terminal(TerminalKernel::Periodic {
                length_scale: 1.0,
                period: 1.0,
            }),
            KernelGrammar::Terminal(TerminalKernel::White { noise_level: 0.1 }),
            KernelGrammar::Terminal(TerminalKernel::Constant {
                constant_value: 1.0,
            }),
        ])
    }

    /// Generate candidate structures from current expression
    fn generate_candidates(
        &self,
        expression: &KernelGrammar,
        rng: &mut scirs2_core::random::Random<scirs2_core::rngs::StdRng>,
    ) -> SklResult<Vec<KernelGrammar>> {
        let mut candidates = Vec::new();

        // Add a new component with sum
        if rng.random::<f64>() < self.expansion_probability {
            let new_kernels = self.generate_initial_kernels()?;
            for new_kernel in new_kernels {
                candidates.push(KernelGrammar::NonTerminal(NonTerminalOperation::Sum {
                    left: Box::new(expression.clone()),
                    right: Box::new(new_kernel),
                }));
            }
        }

        // Add a new component with product
        if rng.random::<f64>() < self.expansion_probability {
            let new_kernels = self.generate_initial_kernels()?;
            for new_kernel in new_kernels {
                candidates.push(KernelGrammar::NonTerminal(NonTerminalOperation::Product {
                    left: Box::new(expression.clone()),
                    right: Box::new(new_kernel),
                }));
            }
        }

        // Scale the current kernel
        if rng.random::<f64>() < 0.3 {
            let scale = rng.gen_range(0.1..10.0);
            candidates.push(KernelGrammar::NonTerminal(NonTerminalOperation::Scale {
                kernel: Box::new(expression.clone()),
                scale,
            }));
        }

        Ok(candidates)
    }

    /// Tournament selection for genetic algorithm
    fn tournament_selection<'a>(
        &self,
        population: &'a [(KernelGrammar, f64)],
        rng: &mut scirs2_core::random::Random<scirs2_core::rngs::StdRng>,
    ) -> &'a (KernelGrammar, f64) {
        let tournament_size = 3.min(population.len());
        let mut best_idx = rng.gen_range(0..population.len());
        let mut best_score = population[best_idx].1;

        for _ in 1..tournament_size {
            let idx = rng.gen_range(0..population.len());
            if population[idx].1 < best_score {
                best_idx = idx;
                best_score = population[idx].1;
            }
        }

        &population[best_idx]
    }

    /// Crossover operation for genetic algorithm
    fn crossover(
        &self,
        parent1: &KernelGrammar,
        parent2: &KernelGrammar,
        rng: &mut scirs2_core::random::Random<scirs2_core::rngs::StdRng>,
    ) -> SklResult<KernelGrammar> {
        if rng.random::<f64>() < 0.5 {
            Ok(KernelGrammar::NonTerminal(NonTerminalOperation::Sum {
                left: Box::new(parent1.clone()),
                right: Box::new(parent2.clone()),
            }))
        } else {
            Ok(KernelGrammar::NonTerminal(NonTerminalOperation::Product {
                left: Box::new(parent1.clone()),
                right: Box::new(parent2.clone()),
            }))
        }
    }

    /// Mutation operation for genetic algorithm
    fn mutate(
        &self,
        expression: &KernelGrammar,
        rng: &mut scirs2_core::random::Random<scirs2_core::rngs::StdRng>,
    ) -> SklResult<KernelGrammar> {
        if rng.random::<f64>() < 0.1 {
            // Replace with random kernel
            let new_kernels = self.generate_initial_kernels()?;
            let idx = rng.gen_range(0..new_kernels.len());
            Ok(new_kernels[idx].clone())
        } else {
            // Keep original
            Ok(expression.clone())
        }
    }

    /// Calculate depth of kernel expression
    fn expression_depth(&self, expression: &KernelGrammar) -> usize {
        match expression {
            KernelGrammar::Terminal(_) => 1,
            KernelGrammar::NonTerminal(op) => match op {
                NonTerminalOperation::Sum { left, right }
                | NonTerminalOperation::Product { left, right } => {
                    1 + self
                        .expression_depth(left)
                        .max(self.expression_depth(right))
                }
                NonTerminalOperation::Scale { kernel, .. }
                | NonTerminalOperation::Power { base: kernel, .. } => {
                    1 + self.expression_depth(kernel)
                }
            },
        }
    }

    /// Convert grammar expression to actual kernel
    fn expression_to_kernel(&self, expression: &KernelGrammar) -> SklResult<Box<dyn Kernel>> {
        match expression {
            KernelGrammar::Terminal(terminal) => match terminal {
                TerminalKernel::RBF { length_scale } => Ok(Box::new(RBF::new(*length_scale))),
                TerminalKernel::Linear { sigma_0, sigma_1 } => {
                    Ok(Box::new(Linear::new(*sigma_0, *sigma_1)))
                }
                TerminalKernel::Periodic {
                    length_scale,
                    period,
                } => Ok(Box::new(ExpSineSquared::new(*length_scale, *period))),
                TerminalKernel::Matern { length_scale, nu } => {
                    Ok(Box::new(Matern::new(*length_scale, *nu)))
                }
                TerminalKernel::RationalQuadratic {
                    length_scale,
                    alpha,
                } => Ok(Box::new(RationalQuadratic::new(*length_scale, *alpha))),
                TerminalKernel::White { noise_level } => {
                    Ok(Box::new(WhiteKernel::new(*noise_level)))
                }
                TerminalKernel::Constant { constant_value } => {
                    Ok(Box::new(ConstantKernel::new(*constant_value)))
                }
            },
            KernelGrammar::NonTerminal(op) => match op {
                NonTerminalOperation::Sum { left, right } => {
                    let left_kernel = self.expression_to_kernel(left)?;
                    let right_kernel = self.expression_to_kernel(right)?;
                    Ok(Box::new(crate::kernels::SumKernel::new(vec![
                        left_kernel,
                        right_kernel,
                    ])))
                }
                NonTerminalOperation::Product { left, right } => {
                    let left_kernel = self.expression_to_kernel(left)?;
                    let right_kernel = self.expression_to_kernel(right)?;
                    Ok(Box::new(crate::kernels::ProductKernel::new(vec![
                        left_kernel,
                        right_kernel,
                    ])))
                }
                NonTerminalOperation::Scale { kernel, scale } => {
                    let base_kernel = self.expression_to_kernel(kernel)?;
                    // Scale by creating a constant kernel and using product
                    let scale_kernel = Box::new(ConstantKernel::new(*scale));
                    Ok(Box::new(crate::kernels::ProductKernel::new(vec![
                        base_kernel,
                        scale_kernel,
                    ])))
                }
                NonTerminalOperation::Power { base, exponent: _ } => {
                    // For now, just return the base kernel
                    // Power kernels would need special implementation
                    self.expression_to_kernel(base)
                }
            },
        }
    }

    /// Convert expression to human-readable string
    fn expression_to_string(&self, expression: &KernelGrammar) -> String {
        match expression {
            KernelGrammar::Terminal(terminal) => match terminal {
                TerminalKernel::RBF { length_scale } => format!("RBF({:.3})", length_scale),
                TerminalKernel::Linear { sigma_0, sigma_1 } => {
                    format!("Linear({:.3}, {:.3})", sigma_0, sigma_1)
                }
                TerminalKernel::Periodic {
                    length_scale,
                    period,
                } => format!("Periodic({:.3}, {:.3})", length_scale, period),
                TerminalKernel::Matern { length_scale, nu } => {
                    format!("Matern({:.3}, {:.3})", length_scale, nu)
                }
                TerminalKernel::RationalQuadratic {
                    length_scale,
                    alpha,
                } => format!("RQ({:.3}, {:.3})", length_scale, alpha),
                TerminalKernel::White { noise_level } => format!("White({:.3})", noise_level),
                TerminalKernel::Constant { constant_value } => {
                    format!("Const({:.3})", constant_value)
                }
            },
            KernelGrammar::NonTerminal(op) => match op {
                NonTerminalOperation::Sum { left, right } => {
                    format!(
                        "({} + {})",
                        self.expression_to_string(left),
                        self.expression_to_string(right)
                    )
                }
                NonTerminalOperation::Product { left, right } => {
                    format!(
                        "({} * {})",
                        self.expression_to_string(left),
                        self.expression_to_string(right)
                    )
                }
                NonTerminalOperation::Scale { kernel, scale } => {
                    format!("{:.3} * {}", scale, self.expression_to_string(kernel))
                }
                NonTerminalOperation::Power { base, exponent } => {
                    format!("{}^{:.3}", self.expression_to_string(base), exponent)
                }
            },
        }
    }

    /// Evaluate kernel using BIC or cross-validation
    fn evaluate_kernel(
        &self,
        kernel: &dyn Kernel,
        X: &ArrayView2<f64>,
        y: &ArrayView1<f64>,
    ) -> SklResult<f64> {
        // For now, use simple marginal likelihood
        // This can be extended to use cross-validation or BIC
        self.compute_marginal_likelihood(kernel, X, y)
    }

    /// Compute marginal likelihood for kernel evaluation
    #[allow(non_snake_case)]
    fn compute_marginal_likelihood(
        &self,
        kernel: &dyn Kernel,
        X: &ArrayView2<f64>,
        y: &ArrayView1<f64>,
    ) -> SklResult<f64> {
        let X_owned = X.to_owned();
        let K = kernel.compute_kernel_matrix(&X_owned, Some(&X_owned))?;

        // Add noise to diagonal
        let mut K_noisy = K;
        let noise_var = 0.1;
        for i in 0..K_noisy.nrows() {
            K_noisy[[i, i]] += noise_var;
        }

        // Compute Cholesky decomposition
        match crate::utils::cholesky_decomposition(&K_noisy) {
            Ok(L) => {
                // Compute log marginal likelihood
                let mut log_det = 0.0;
                for i in 0..L.nrows() {
                    log_det += L[[i, i]].ln();
                }
                log_det *= 2.0;

                // Solve for alpha = K^(-1) * y
                let y_owned = y.to_owned();
                let alpha = match crate::utils::triangular_solve(&L, &y_owned) {
                    Ok(temp) => {
                        let L_T = L.t();
                        crate::utils::triangular_solve(&L_T.view().to_owned(), &temp)?
                    }
                    Err(_) => return Ok(f64::INFINITY),
                };

                let data_fit = -0.5 * y.dot(&alpha);
                let complexity_penalty = -0.5 * log_det;
                let normalization = -0.5 * y.len() as f64 * (2.0 * std::f64::consts::PI).ln();

                Ok(-(data_fit + complexity_penalty + normalization))
            }
            Err(_) => Ok(f64::INFINITY),
        }
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    // SciRS2 Policy - Use scirs2-autograd for ndarray types and operations
    use scirs2_core::ndarray::{Array1, Array2};

    #[test]
    fn test_kernel_structure_learner_creation() {
        let learner = KernelStructureLearner::new();
        assert_eq!(learner.max_depth, 4);
        assert_eq!(learner.max_iterations, 100);
    }

    #[test]
    fn test_expression_depth_calculation() {
        let learner = KernelStructureLearner::new();

        // Terminal kernel has depth 1
        let terminal = KernelGrammar::Terminal(TerminalKernel::RBF { length_scale: 1.0 });
        assert_eq!(learner.expression_depth(&terminal), 1);

        // Sum of two terminals has depth 2
        let sum = KernelGrammar::NonTerminal(NonTerminalOperation::Sum {
            left: Box::new(terminal.clone()),
            right: Box::new(terminal.clone()),
        });
        assert_eq!(learner.expression_depth(&sum), 2);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_expression_to_kernel_conversion() {
        let learner = KernelStructureLearner::new();

        let expression = KernelGrammar::Terminal(TerminalKernel::RBF { length_scale: 2.0 });
        let kernel = learner
            .expression_to_kernel(&expression)
            .expect("operation should succeed");

        // Test that kernel can be used
        let X = Array2::from_shape_vec((3, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .expect("shape and data length should match");
        let K = kernel
            .compute_kernel_matrix(&X, Some(&X))
            .expect("operation should succeed");
        assert_eq!(K.nrows(), 3);
        assert_eq!(K.ncols(), 3);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_greedy_search() {
        let learner = KernelStructureLearner::new().max_iterations(5).max_depth(2);

        let X = Array2::from_shape_vec((5, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0])
            .expect("shape and data length should match");
        let y = Array1::from_vec(vec![1.0, 4.0, 9.0, 16.0, 25.0]);

        let result = learner
            .learn_structure(X.view(), y.view())
            .expect("operation should succeed");

        assert!(result.best_score.is_finite());
        assert!(!result.exploration_history.is_empty());
        assert!(!result.convergence_info.score_history.is_empty());
    }

    #[test]
    fn test_expression_to_string() {
        let learner = KernelStructureLearner::new();

        let rbf = KernelGrammar::Terminal(TerminalKernel::RBF { length_scale: 1.0 });
        let linear = KernelGrammar::Terminal(TerminalKernel::Linear {
            sigma_0: 1.0,
            sigma_1: 1.0,
        });
        let sum = KernelGrammar::NonTerminal(NonTerminalOperation::Sum {
            left: Box::new(rbf),
            right: Box::new(linear),
        });

        let expr_str = learner.expression_to_string(&sum);
        assert!(expr_str.contains("RBF"));
        assert!(expr_str.contains("Linear"));
        assert!(expr_str.contains("+"));
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_beam_search() {
        let learner = KernelStructureLearner::new()
            .max_iterations(3)
            .max_depth(2)
            .search_strategy(SearchStrategy::Beam { beam_width: 2 });

        let X = Array2::from_shape_vec((4, 1), vec![1.0, 2.0, 3.0, 4.0])
            .expect("shape and data length should match");
        let y = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0]);

        let result = learner
            .learn_structure(X.view(), y.view())
            .expect("operation should succeed");

        // The search might return infinity if no valid kernels are found
        assert!(result.best_score.is_finite() || result.best_score == f64::INFINITY);
        assert!(!result.exploration_history.is_empty());
    }
}