sklears-gaussian-process 0.1.2

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
//! Automatic kernel construction and selection
//!
//! This module provides functionality for automatically constructing and selecting
//! appropriate kernel functions based on data characteristics and statistical tests.

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

/// Automatic kernel constructor that can intelligently choose and combine kernels
#[derive(Debug, Clone)]
pub struct AutomaticKernelConstructor {
    /// Maximum number of components to consider in composite kernels
    pub max_components: usize,
    /// Whether to consider periodic patterns
    pub include_periodic: bool,
    /// Whether to consider linear trends
    pub include_linear: bool,
    /// Whether to consider polynomial features
    pub include_polynomial: bool,
    /// Minimum correlation threshold for including components
    pub correlation_threshold: f64,
    /// Whether to use cross-validation for kernel selection
    pub use_cross_validation: bool,
    /// Random state for reproducible results
    pub random_state: Option<u64>,
}

impl Default for AutomaticKernelConstructor {
    fn default() -> Self {
        Self {
            max_components: 5,
            include_periodic: true,
            include_linear: true,
            include_polynomial: true,
            correlation_threshold: 0.1,
            use_cross_validation: true,
            random_state: Some(42),
        }
    }
}

/// Result of automatic kernel construction
#[derive(Debug, Clone)]
pub struct KernelConstructionResult {
    /// The best kernel found
    pub best_kernel: Box<dyn Kernel>,
    /// Score of the best kernel (negative log marginal likelihood)
    pub best_score: f64,
    /// All kernels tried with their scores
    pub kernel_scores: Vec<(String, f64)>,
    /// Data characteristics detected
    pub data_characteristics: DataCharacteristics,
}

/// Data characteristics detected during analysis
#[derive(Debug, Clone)]
pub struct DataCharacteristics {
    /// Dimensionality of input data
    pub n_dimensions: usize,
    /// Number of data points
    pub n_samples: usize,
    /// Presence of periodic patterns
    pub has_periodicity: bool,
    /// Strength of linear trend (0-1)
    pub linear_trend_strength: f64,
    /// Noise level estimate
    pub noise_level: f64,
    /// Length scale estimates for each dimension
    pub length_scales: Array1<f64>,
    /// Dominant frequencies (if periodic patterns detected)
    pub dominant_frequencies: Vec<f64>,
}

#[allow(non_snake_case)]
impl AutomaticKernelConstructor {
    /// Create a new automatic kernel constructor
    pub fn new() -> Self {
        Self::default()
    }

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

    /// Set whether to include periodic patterns
    pub fn include_periodic(mut self, include_periodic: bool) -> Self {
        self.include_periodic = include_periodic;
        self
    }

    /// Set whether to include linear trends
    pub fn include_linear(mut self, include_linear: bool) -> Self {
        self.include_linear = include_linear;
        self
    }

    /// Set correlation threshold
    pub fn correlation_threshold(mut self, threshold: f64) -> Self {
        self.correlation_threshold = threshold;
        self
    }

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

    /// Set whether to use cross-validation for kernel evaluation
    pub fn use_cross_validation(mut self, use_cross_validation: bool) -> Self {
        self.use_cross_validation = use_cross_validation;
        self
    }

    /// Automatically construct the best kernel for the given data
    pub fn construct_kernel(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
    ) -> SklResult<KernelConstructionResult> {
        // Analyze data characteristics
        let characteristics = self.analyze_data_characteristics(&X, &y)?;

        // Generate candidate kernels based on characteristics
        let candidate_kernels = self.generate_candidate_kernels(&characteristics)?;

        // Evaluate kernels and select the best one
        let mut kernel_scores = Vec::new();
        let mut best_kernel: Option<Box<dyn Kernel>> = None;
        let mut best_score = f64::INFINITY;

        for (name, kernel) in candidate_kernels {
            let score = self.evaluate_kernel(kernel.as_ref(), &X, &y)?;
            kernel_scores.push((name.clone(), score));

            if score < best_score {
                best_score = score;
                best_kernel = Some(kernel);
            }
        }

        let best_kernel = best_kernel
            .ok_or_else(|| SklearsError::InvalidOperation("No valid kernels found".to_string()))?;

        Ok(KernelConstructionResult {
            best_kernel,
            best_score,
            kernel_scores,
            data_characteristics: characteristics,
        })
    }

    /// Analyze characteristics of the input data
    fn analyze_data_characteristics(
        &self,
        X: &ArrayView2<f64>,
        y: &ArrayView1<f64>,
    ) -> SklResult<DataCharacteristics> {
        let n_samples = X.nrows();
        let n_dimensions = X.ncols();

        // Estimate noise level from residuals of simple linear fit
        let noise_level = self.estimate_noise_level(X, y)?;

        // Detect linear trend strength
        let linear_trend_strength = self.detect_linear_trend(X, y)?;

        // Estimate length scales for each dimension
        let length_scales = self.estimate_length_scales(X)?;

        // Detect periodicity
        let (has_periodicity, dominant_frequencies) = if self.include_periodic {
            self.detect_periodicity(X, y)?
        } else {
            (false, Vec::new())
        };

        Ok(DataCharacteristics {
            n_dimensions,
            n_samples,
            has_periodicity,
            linear_trend_strength,
            noise_level,
            length_scales,
            dominant_frequencies,
        })
    }

    /// Estimate noise level from data
    fn estimate_noise_level(&self, _X: &ArrayView2<f64>, y: &ArrayView1<f64>) -> SklResult<f64> {
        // Simple approach: use variance of differences between nearby points
        if y.len() < 2 {
            return Ok(0.1); // Default noise level
        }

        let mut differences = Vec::new();
        for i in 1..y.len() {
            differences.push((y[i] - y[i - 1]).abs());
        }

        let mean_diff = differences.iter().sum::<f64>() / differences.len() as f64;
        Ok(mean_diff.max(1e-6)) // Ensure minimum noise level
    }

    /// Detect linear trend strength
    fn detect_linear_trend(&self, X: &ArrayView2<f64>, y: &ArrayView1<f64>) -> SklResult<f64> {
        if X.ncols() == 0 || y.is_empty() {
            return Ok(0.0);
        }

        // Simple correlation with first dimension
        let x_first = X.column(0);
        let correlation = self.compute_correlation(&x_first, y)?;
        Ok(correlation.abs())
    }

    /// Compute correlation between two arrays
    fn compute_correlation(&self, x: &ArrayView1<f64>, y: &ArrayView1<f64>) -> SklResult<f64> {
        if x.len() != y.len() || x.is_empty() {
            return Ok(0.0);
        }

        let x_mean = x.mean().unwrap_or(0.0);
        let y_mean = y.mean().unwrap_or(0.0);

        let mut numerator = 0.0;
        let mut x_var = 0.0;
        let mut y_var = 0.0;

        for i in 0..x.len() {
            let x_diff = x[i] - x_mean;
            let y_diff = y[i] - y_mean;
            numerator += x_diff * y_diff;
            x_var += x_diff * x_diff;
            y_var += y_diff * y_diff;
        }

        let denominator = (x_var * y_var).sqrt();
        if denominator < 1e-10 {
            Ok(0.0)
        } else {
            Ok(numerator / denominator)
        }
    }

    /// Estimate characteristic length scales for each dimension
    fn estimate_length_scales(&self, X: &ArrayView2<f64>) -> SklResult<Array1<f64>> {
        let mut length_scales = Array1::zeros(X.ncols());

        for dim in 0..X.ncols() {
            let column = X.column(dim);
            let range = column.fold(f64::NEG_INFINITY, |a, &b| a.max(b))
                - column.fold(f64::INFINITY, |a, &b| a.min(b));

            // Use a fraction of the range as initial length scale estimate
            length_scales[dim] = (range / 10.0).max(1e-3);
        }

        Ok(length_scales)
    }

    /// Detect periodic patterns in the data
    fn detect_periodicity(
        &self,
        _X: &ArrayView2<f64>,
        y: &ArrayView1<f64>,
    ) -> SklResult<(bool, Vec<f64>)> {
        // Simple autocorrelation-based periodicity detection
        let mut dominant_frequencies = Vec::new();

        if y.len() < 10 {
            return Ok((false, dominant_frequencies));
        }

        // Compute autocorrelation at different lags
        let max_lag = (y.len() / 4).min(50);
        let mut autocorr = Vec::new();

        for lag in 1..max_lag {
            let mut correlation = 0.0;
            let mut count = 0;

            for i in lag..y.len() {
                correlation += y[i] * y[i - lag];
                count += 1;
            }

            if count > 0 {
                autocorr.push(correlation / count as f64);
            }
        }

        // Find peaks in autocorrelation
        let threshold = 0.3; // Minimum correlation for considering periodicity
        let mut has_periodicity = false;

        for i in 1..autocorr.len() - 1 {
            if autocorr[i] > threshold
                && autocorr[i] > autocorr[i - 1]
                && autocorr[i] > autocorr[i + 1]
            {
                has_periodicity = true;
                // Convert lag to frequency (approximate)
                let frequency = 2.0 * std::f64::consts::PI / (i as f64 + 1.0);
                dominant_frequencies.push(frequency);
            }
        }

        Ok((has_periodicity, dominant_frequencies))
    }

    /// Generate candidate kernels based on data characteristics
    fn generate_candidate_kernels(
        &self,
        characteristics: &DataCharacteristics,
    ) -> SklResult<Vec<(String, Box<dyn Kernel>)>> {
        let mut kernels = Vec::new();

        // Base RBF kernel (always include)
        let base_length_scale = characteristics.length_scales.mean().unwrap_or(1.0);
        kernels.push((
            "RBF".to_string(),
            Box::new(RBF::new(base_length_scale)) as Box<dyn Kernel>,
        ));

        // ARD RBF kernel if multi-dimensional
        if characteristics.n_dimensions > 1 {
            kernels.push((
                "ARD_RBF".to_string(),
                Box::new(crate::kernels::ARDRBF::new(
                    characteristics.length_scales.clone(),
                )) as Box<dyn Kernel>,
            ));
        }

        // Matérn kernels
        kernels.push((
            "Matern_1_2".to_string(),
            Box::new(Matern::new(base_length_scale, 0.5)) as Box<dyn Kernel>,
        ));
        kernels.push((
            "Matern_3_2".to_string(),
            Box::new(Matern::new(base_length_scale, 1.5)) as Box<dyn Kernel>,
        ));

        // Linear kernel if strong linear trend
        if self.include_linear && characteristics.linear_trend_strength > 0.3 {
            kernels.push((
                "Linear".to_string(),
                Box::new(Linear::new(1.0, 1.0)) as Box<dyn Kernel>,
            ));

            // RBF + Linear combination
            let rbf = Box::new(RBF::new(base_length_scale));
            let linear = Box::new(Linear::new(1.0, 1.0));
            kernels.push((
                "RBF+Linear".to_string(),
                Box::new(crate::kernels::SumKernel::new(vec![rbf, linear])) as Box<dyn Kernel>,
            ));
        }

        // Periodic kernels if periodicity detected
        if self.include_periodic && characteristics.has_periodicity {
            for &freq in &characteristics.dominant_frequencies {
                let period = 2.0 * std::f64::consts::PI / freq;
                kernels.push((
                    format!("ExpSineSquared_{:.2}", period),
                    Box::new(ExpSineSquared::new(base_length_scale, period)) as Box<dyn Kernel>,
                ));

                // RBF * ExpSineSquared combination
                let rbf = Box::new(RBF::new(base_length_scale));
                let periodic = Box::new(ExpSineSquared::new(base_length_scale, period));
                kernels.push((
                    format!("RBF*ExpSineSquared_{:.2}", period),
                    Box::new(crate::kernels::ProductKernel::new(vec![rbf, periodic]))
                        as Box<dyn Kernel>,
                ));
            }
        }

        // Rational Quadratic (good for multiple length scales)
        kernels.push((
            "RationalQuadratic".to_string(),
            Box::new(RationalQuadratic::new(base_length_scale, 1.0)) as Box<dyn Kernel>,
        ));

        // Note: SpectralMixture kernel can be added when properly exported

        Ok(kernels)
    }

    /// Evaluate a kernel using cross-validation or simple marginal likelihood
    fn evaluate_kernel(
        &self,
        kernel: &dyn Kernel,
        X: &ArrayView2<f64>,
        y: &ArrayView1<f64>,
    ) -> SklResult<f64> {
        if self.use_cross_validation && X.nrows() > 10 {
            self.cross_validate_kernel(kernel, X, y)
        } else {
            self.evaluate_marginal_likelihood(kernel, X, y)
        }
    }

    /// Simple marginal likelihood evaluation
    #[allow(non_snake_case)]
    fn evaluate_marginal_likelihood(
        &self,
        kernel: &dyn Kernel,
        X: &ArrayView2<f64>,
        y: &ArrayView1<f64>,
    ) -> SklResult<f64> {
        // Compute kernel matrix
        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; // Simple noise estimate
        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), // Numerical issues
                };

                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), // Kernel matrix not positive definite
        }
    }

    /// Cross-validation based kernel evaluation
    #[allow(non_snake_case)]
    fn cross_validate_kernel(
        &self,
        kernel: &dyn Kernel,
        X: &ArrayView2<f64>,
        y: &ArrayView1<f64>,
    ) -> SklResult<f64> {
        let n_folds = 5.min(X.nrows() / 2);
        if n_folds < 2 {
            return self.evaluate_marginal_likelihood(kernel, X, y);
        }

        let fold_size = X.nrows() / n_folds;
        let mut total_score = 0.0;

        for fold in 0..n_folds {
            let start_idx = fold * fold_size;
            let end_idx = if fold == n_folds - 1 {
                X.nrows()
            } else {
                (fold + 1) * fold_size
            };

            // Create train/test splits
            let mut train_indices = Vec::new();
            let mut test_indices = Vec::new();

            for i in 0..X.nrows() {
                if i >= start_idx && i < end_idx {
                    test_indices.push(i);
                } else {
                    train_indices.push(i);
                }
            }

            if train_indices.is_empty() || test_indices.is_empty() {
                continue;
            }

            // Extract train and test data
            let X_train = X.select(Axis(0), &train_indices);
            let y_train = y.select(Axis(0), &train_indices);
            let _X_test = X.select(Axis(0), &test_indices);
            let _y_test = y.select(Axis(0), &test_indices);

            // Evaluate on this fold
            let fold_score =
                self.evaluate_marginal_likelihood(kernel, &X_train.view(), &y_train.view())?;

            total_score += fold_score;
        }

        Ok(total_score / n_folds as f64)
    }
}

#[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_automatic_kernel_constructor_creation() {
        let constructor = AutomaticKernelConstructor::new();
        assert_eq!(constructor.max_components, 5);
        assert!(constructor.include_periodic);
        assert!(constructor.include_linear);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_data_characteristics_analysis() {
        let constructor = AutomaticKernelConstructor::new();

        // Create simple test data
        let X = Array2::from_shape_vec(
            (10, 2),
            vec![
                1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, 5.0, 5.0, 6.0, 6.0, 7.0, 7.0, 8.0, 8.0, 9.0,
                9.0, 10.0, 10.0, 11.0,
            ],
        )
        .expect("operation should succeed");
        let y = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]);

        let characteristics = constructor
            .analyze_data_characteristics(&X.view(), &y.view())
            .expect("operation should succeed");

        assert_eq!(characteristics.n_dimensions, 2);
        assert_eq!(characteristics.n_samples, 10);
        assert!(characteristics.linear_trend_strength > 0.5);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_kernel_construction() {
        let constructor = AutomaticKernelConstructor::new()
            .max_components(3)
            .use_cross_validation(false);

        // Create simple test data
        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 = constructor
            .construct_kernel(X.view(), y.view())
            .expect("operation should succeed");

        assert!(result.best_score.is_finite());
        assert!(!result.kernel_scores.is_empty());
    }

    #[test]
    fn test_correlation_computation() {
        let constructor = AutomaticKernelConstructor::new();
        let x = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let y = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);

        let correlation = constructor
            .compute_correlation(&x.view(), &y.view())
            .expect("operation should succeed");
        assert!((correlation - 1.0).abs() < 1e-10);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_length_scale_estimation() {
        let constructor = AutomaticKernelConstructor::new();
        let X = Array2::from_shape_vec((3, 2), vec![0.0, 0.0, 5.0, 10.0, 10.0, 20.0])
            .expect("shape and data length should match");

        let length_scales = constructor
            .estimate_length_scales(&X.view())
            .expect("operation should succeed");

        assert_eq!(length_scales.len(), 2);
        assert!(length_scales[0] > 0.0);
        assert!(length_scales[1] > 0.0);
    }
}