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
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
//! Linear Model of Coregionalization (LMC) for multi-output Gaussian Process regression
//!
//! This module implements the Linear Model of Coregionalization, which learns multiple
//! correlated outputs simultaneously by using linear combinations of latent functions.
//!
//! # Mathematical Background
//!
//! The LMC assumes that each output is a linear combination of Q latent functions:
//! f_i(x) = Σ_q A_iq g_q(x), where g_q ~ GP(0, k_q)
//!
//! # Examples
//!
//! ```
//! use sklears_gaussian_process::{LinearModelCoregionalization, kernels::RBF, Kernel};
//! use sklears_core::traits::{Fit, Predict};
//! use scirs2_core::ndarray::array;
//!
//! let X = array![[1.0], [2.0], [3.0], [4.0]];
//! let Y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
//!
//! let kernels: Vec<Box<dyn Kernel>> = vec![Box::new(RBF::new(1.0)), Box::new(RBF::new(2.0))];
//! let lmc = LinearModelCoregionalization::new()
//!     .kernels(kernels)
//!     .n_outputs(2)
//!     .alpha(1e-6);
//! let fitted = lmc.fit(&X.view(), &Y.view()).expect("fit should succeed with valid training data");
//! let predictions = fitted.predict(&X.view()).expect("predict should succeed on trained model");
//! ```

use crate::classification::GpcConfig;
use crate::kernels::Kernel;
use crate::utils;
use scirs2_core::ndarray::{Array1, Array2, ArrayView2};
use scirs2_core::random::rngs::StdRng;
use scirs2_core::random::{RngExt, SeedableRng};
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    traits::{Estimator, Fit, Predict, Untrained},
};
use std::f64::consts::PI;

/// Linear Model of Coregionalization for multi-output Gaussian Process regression
///
/// The LMC models multiple outputs using linear combinations of independent latent functions,
/// each with its own kernel. This allows the model to capture correlations between outputs
/// while maintaining computational efficiency.
#[derive(Debug, Clone)]
pub struct LinearModelCoregionalization<S = Untrained> {
    kernels: Vec<Box<dyn Kernel>>,
    mixing_matrices: Vec<Array2<f64>>, // One per kernel
    alpha: f64,
    n_outputs: usize,
    n_latent: usize,
    optimize_mixing: bool,
    _state: S,
}

/// Trained state for Linear Model of Coregionalization
#[derive(Debug, Clone)]
#[allow(non_snake_case)]
pub struct LmcTrained {
    pub(crate) X_train: Array2<f64>,
    #[allow(dead_code)]
    pub(crate) Y_train: Array2<f64>,
    pub(crate) kernels: Vec<Box<dyn Kernel>>,
    pub(crate) mixing_matrices: Vec<Array2<f64>>,
    #[allow(dead_code)]
    pub(crate) alpha: f64,
    pub(crate) n_outputs: usize,
    pub(crate) n_latent: usize,
    #[allow(dead_code)]
    pub(crate) gram_matrices: Vec<Array2<f64>>, // Gram matrices for each kernel
    pub(crate) alpha_vectors: Vec<Array1<f64>>, // Alpha vectors for each latent function
    pub(crate) log_marginal_likelihood_value: f64,
}

impl Default for LinearModelCoregionalization<Untrained> {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(non_snake_case)]
impl LinearModelCoregionalization<Untrained> {
    /// Create a new Linear Model of Coregionalization instance
    pub fn new() -> Self {
        Self {
            kernels: Vec::new(),
            mixing_matrices: Vec::new(),
            alpha: 1e-10,
            n_outputs: 1,
            n_latent: 1,
            optimize_mixing: true,
            _state: Untrained,
        }
    }

    /// Set the kernel functions for each latent process
    pub fn kernels(mut self, kernels: Vec<Box<dyn Kernel>>) -> Self {
        self.n_latent = kernels.len();
        self.kernels = kernels;
        self
    }

    /// Set the mixing matrices for each kernel
    pub fn mixing_matrices(mut self, mixing_matrices: Vec<Array2<f64>>) -> Self {
        self.mixing_matrices = mixing_matrices;
        self
    }

    /// Set the regularization parameter
    pub fn alpha(mut self, alpha: f64) -> Self {
        self.alpha = alpha;
        self
    }

    /// Set the number of outputs
    pub fn n_outputs(mut self, n_outputs: usize) -> Self {
        self.n_outputs = n_outputs;
        self
    }

    /// Set whether to optimize mixing matrices during training
    pub fn optimize_mixing(mut self, optimize_mixing: bool) -> Self {
        self.optimize_mixing = optimize_mixing;
        self
    }

    /// Initialize mixing matrices randomly if not provided
    fn initialize_mixing_matrices(&self) -> Vec<Array2<f64>> {
        if !self.mixing_matrices.is_empty() {
            return self.mixing_matrices.clone();
        }

        let mut matrices = Vec::new();
        // SciRS2 Policy - Use scirs2-core for random number generation
        let mut rng = StdRng::seed_from_u64(42);

        for _ in 0..self.n_latent {
            let mut matrix = Array2::<f64>::zeros((self.n_outputs, 1));

            // Generate random values in range [-1, 1]
            for i in 0..self.n_outputs {
                let random_val = rng.random_range(-1.0..1.0);
                matrix[[i, 0]] = random_val;
            }

            matrices.push(matrix);
        }

        matrices
    }

    /// Compute the coregionalization kernel matrix
    #[allow(non_snake_case)]
    fn compute_coregionalization_kernel(
        &self,
        X1: &Array2<f64>,
        X2: Option<&Array2<f64>>,
        mixing_matrices: &[Array2<f64>],
    ) -> SklResult<Array2<f64>> {
        let n1 = X1.nrows();
        let n2 = X2.map_or(n1, |x| x.nrows());

        // Initialize the full coregionalization kernel matrix
        let mut K_full = Array2::<f64>::zeros((n1 * self.n_outputs, n2 * self.n_outputs));

        // For each latent function r
        for (r, kernel) in self.kernels.iter().enumerate() {
            // Compute the kernel matrix for this latent function
            let K_r = kernel.compute_kernel_matrix(X1, X2)?;

            // Get the mixing matrix for this kernel
            let A_r = &mixing_matrices[r];

            // For each pair of outputs (i, j)
            for i in 0..self.n_outputs {
                for j in 0..self.n_outputs {
                    // The coregionalization coefficient
                    let coeff = A_r[[i, 0]] * A_r[[j, 0]];

                    // Add the contribution to the full kernel matrix
                    for n in 0..n1 {
                        for m in 0..n2 {
                            K_full[[n * self.n_outputs + i, m * self.n_outputs + j]] +=
                                coeff * K_r[[n, m]];
                        }
                    }
                }
            }
        }

        Ok(K_full)
    }

    /// Vectorize the multi-output targets
    fn vectorize_targets(&self, Y: &Array2<f64>) -> Array1<f64> {
        let (n_samples, n_outputs) = Y.dim();
        let mut y_vec = Array1::<f64>::zeros(n_samples * n_outputs);

        for i in 0..n_samples {
            for j in 0..n_outputs {
                y_vec[i * n_outputs + j] = Y[[i, j]];
            }
        }

        y_vec
    }

    /// Devectorize predictions back to matrix form
    #[allow(dead_code)]
    fn devectorize_predictions(&self, y_vec: &Array1<f64>, n_samples: usize) -> Array2<f64> {
        let mut Y = Array2::<f64>::zeros((n_samples, self.n_outputs));

        for i in 0..n_samples {
            for j in 0..self.n_outputs {
                Y[[i, j]] = y_vec[i * self.n_outputs + j];
            }
        }

        Y
    }
}

impl Estimator for LinearModelCoregionalization<Untrained> {
    type Config = GpcConfig; // Reuse existing config for now
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        // This is a temporary implementation - in a real scenario, we'd create a specific LMC config
        // For now, we'll use a default config to avoid compilation errors
        static DEFAULT_CONFIG: GpcConfig = GpcConfig {
            kernel_name: String::new(),
            optimizer: None,
            n_restarts_optimizer: 0,
            max_iter_predict: 100,
            warm_start: false,
            copy_x_train: true,
            random_state: None,
        };
        &DEFAULT_CONFIG
    }
}

impl Estimator for LinearModelCoregionalization<LmcTrained> {
    type Config = GpcConfig;
    type Error = SklearsError;
    type Float = f64;

    fn config(&self) -> &Self::Config {
        // This is a temporary implementation - in a real scenario, we'd create a specific LMC config
        static DEFAULT_CONFIG: GpcConfig = GpcConfig {
            kernel_name: String::new(),
            optimizer: None,
            n_restarts_optimizer: 0,
            max_iter_predict: 100,
            warm_start: false,
            copy_x_train: true,
            random_state: None,
        };
        &DEFAULT_CONFIG
    }
}

impl Fit<ArrayView2<'_, f64>, ArrayView2<'_, f64>> for LinearModelCoregionalization<Untrained> {
    type Fitted = LinearModelCoregionalization<LmcTrained>;

    #[allow(non_snake_case)]
    fn fit(self, X: &ArrayView2<f64>, Y: &ArrayView2<f64>) -> SklResult<Self::Fitted> {
        if X.nrows() != Y.nrows() {
            return Err(SklearsError::InvalidInput(
                "X and Y must have the same number of samples".to_string(),
            ));
        }

        if self.kernels.is_empty() {
            return Err(SklearsError::InvalidInput(
                "At least one kernel must be specified".to_string(),
            ));
        }

        // Ensure n_outputs matches Y dimensions
        let n_outputs = Y.ncols();
        if self.n_outputs != n_outputs {
            return Err(SklearsError::InvalidInput(format!(
                "n_outputs ({}) must match Y dimensions ({})",
                self.n_outputs, n_outputs
            )));
        }

        let X_owned = X.to_owned();
        let Y_owned = Y.to_owned();

        // Initialize mixing matrices
        let mixing_matrices = self.initialize_mixing_matrices();

        // Compute the coregionalization kernel matrix
        let K_full = self.compute_coregionalization_kernel(&X_owned, None, &mixing_matrices)?;

        // Add regularization to the diagonal
        let mut K_reg = K_full.clone();
        for i in 0..K_reg.nrows() {
            K_reg[[i, i]] += self.alpha;
        }

        // Vectorize targets
        let y_vec = self.vectorize_targets(&Y_owned);

        // Solve the linear system K * alpha = y
        let chol_decomp = utils::robust_cholesky(&K_reg)?;
        let alpha_vec = utils::triangular_solve(&chol_decomp, &y_vec)?;

        // Compute log marginal likelihood
        let log_det = chol_decomp.diag().iter().map(|x| x.ln()).sum::<f64>() * 2.0;
        let data_fit = y_vec.dot(&alpha_vec);
        let n_total = y_vec.len();
        let log_marginal_likelihood =
            -0.5 * (data_fit + log_det + n_total as f64 * (2.0 * PI).ln());

        // Store individual Gram matrices for each kernel
        let mut gram_matrices = Vec::new();
        for kernel in &self.kernels {
            let K_r = kernel.compute_kernel_matrix(&X_owned, None)?;
            gram_matrices.push(K_r);
        }

        // Clone kernels for the trained state
        let kernels_clone: Vec<Box<dyn Kernel>> = self.kernels.to_vec();

        Ok(LinearModelCoregionalization {
            kernels: self.kernels,
            mixing_matrices: mixing_matrices.clone(),
            alpha: self.alpha,
            n_outputs: self.n_outputs,
            n_latent: self.n_latent,
            optimize_mixing: self.optimize_mixing,
            _state: LmcTrained {
                X_train: X_owned,
                Y_train: Y_owned,
                kernels: kernels_clone,
                mixing_matrices,
                alpha: self.alpha,
                n_outputs: self.n_outputs,
                n_latent: self.n_latent,
                gram_matrices,
                alpha_vectors: vec![alpha_vec],
                log_marginal_likelihood_value: log_marginal_likelihood,
            },
        })
    }
}

#[allow(non_snake_case)]
impl LinearModelCoregionalization<LmcTrained> {
    /// Access the trained state
    pub fn trained_state(&self) -> &LmcTrained {
        &self._state
    }

    /// Get the learned mixing matrices
    pub fn mixing_matrices(&self) -> &[Array2<f64>] {
        &self._state.mixing_matrices
    }

    /// Get the log marginal likelihood for model selection
    pub fn log_marginal_likelihood(&self) -> f64 {
        self._state.log_marginal_likelihood_value
    }

    /// Compute the coregionalization kernel matrix for trained model
    #[allow(non_snake_case)]
    fn compute_coregionalization_kernel(
        &self,
        X1: &Array2<f64>,
        X2: Option<&Array2<f64>>,
        mixing_matrices: &[Array2<f64>],
    ) -> SklResult<Array2<f64>> {
        let n1 = X1.nrows();
        let n2 = X2.map_or(n1, |x| x.nrows());

        // Initialize the full coregionalization kernel matrix
        let mut K_full =
            Array2::<f64>::zeros((n1 * self._state.n_outputs, n2 * self._state.n_outputs));

        // For each latent function r
        for (r, kernel) in self._state.kernels.iter().enumerate() {
            // Compute the kernel matrix for this latent function
            let K_r = kernel.compute_kernel_matrix(X1, X2)?;

            // Get the mixing matrix for this kernel
            let A_r = &mixing_matrices[r];

            // For each pair of outputs (i, j)
            for i in 0..self._state.n_outputs {
                for j in 0..self._state.n_outputs {
                    // The coregionalization coefficient
                    let coeff = A_r[[i, 0]] * A_r[[j, 0]];

                    // Add the contribution to the full kernel matrix
                    for n in 0..n1 {
                        for m in 0..n2 {
                            K_full
                                [[n * self._state.n_outputs + i, m * self._state.n_outputs + j]] +=
                                coeff * K_r[[n, m]];
                        }
                    }
                }
            }
        }

        Ok(K_full)
    }

    /// Devectorize predictions back to matrix form for trained model
    fn devectorize_predictions(&self, y_vec: &Array1<f64>, n_samples: usize) -> Array2<f64> {
        let mut Y = Array2::<f64>::zeros((n_samples, self._state.n_outputs));

        for i in 0..n_samples {
            for j in 0..self._state.n_outputs {
                Y[[i, j]] = y_vec[i * self._state.n_outputs + j];
            }
        }

        Y
    }

    /// Get the contribution of each latent function to the predictions
    #[allow(non_snake_case)]
    pub fn latent_contributions(&self, X: &ArrayView2<f64>) -> SklResult<Vec<Array2<f64>>> {
        let mut contributions = Vec::new();

        for r in 0..self._state.n_latent {
            // Compute cross-covariance for this latent function
            let _K_star = self._state.kernels[r]
                .compute_kernel_matrix(&self._state.X_train, Some(&X.to_owned()))?;

            // Get the mixing matrix for this latent function
            let A_r = &self._state.mixing_matrices[r];

            // Simple prediction for this latent function (this is a simplified version)
            let n_test = X.nrows();
            let mut contribution = Array2::<f64>::zeros((n_test, self._state.n_outputs));

            // This is a simplified implementation - in practice, we'd need to properly
            // decompose the alpha vector and compute individual latent predictions
            for i in 0..n_test {
                for j in 0..self._state.n_outputs {
                    contribution[[i, j]] = A_r[[j, 0]]; // Simplified - just showing the mixing weight
                }
            }

            contributions.push(contribution);
        }

        Ok(contributions)
    }
}

impl Predict<ArrayView2<'_, f64>, Array2<f64>> for LinearModelCoregionalization<LmcTrained> {
    #[allow(non_snake_case)]
    fn predict(&self, X: &ArrayView2<f64>) -> Result<Array2<f64>, SklearsError> {
        let X_test = X.to_owned();

        // Compute the cross-covariance matrix
        let K_star = self.compute_coregionalization_kernel(
            &self._state.X_train,
            Some(&X_test),
            &self._state.mixing_matrices,
        )?;

        // Vectorize the alpha vector from the trained state
        let alpha_vec = &self._state.alpha_vectors[0];

        // Compute predictions: K_star^T * alpha
        let n_test = X_test.nrows();
        let mut y_pred_vec = Array1::<f64>::zeros(n_test * self._state.n_outputs);

        for i in 0..n_test * self._state.n_outputs {
            for j in 0..self._state.X_train.nrows() * self._state.n_outputs {
                y_pred_vec[i] += K_star[[j, i]] * alpha_vec[j];
            }
        }

        // Devectorize predictions
        let predictions = self.devectorize_predictions(&y_pred_vec, n_test);

        Ok(predictions)
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use crate::kernels::RBF;
    use approx::assert_abs_diff_eq;
    // SciRS2 Policy - Use scirs2-core for array! macro and types
    use scirs2_core::ndarray::array;

    #[test]
    fn test_lmc_creation() {
        let kernels = vec![Box::new(RBF::new(1.0)) as Box<dyn Kernel>];
        let lmc = LinearModelCoregionalization::new()
            .kernels(kernels)
            .n_outputs(2)
            .alpha(1e-6);

        assert_eq!(lmc.n_outputs, 2);
        assert_eq!(lmc.n_latent, 1);
        assert_eq!(lmc.alpha, 1e-6);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_lmc_fit_predict() {
        let X = array![[1.0], [2.0], [3.0], [4.0]];
        let Y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];

        let kernels = vec![Box::new(RBF::new(1.0)) as Box<dyn Kernel>];
        let lmc = LinearModelCoregionalization::new()
            .kernels(kernels)
            .n_outputs(2)
            .alpha(1e-6);

        let fitted = lmc
            .fit(&X.view(), &Y.view())
            .expect("model fitting should succeed");
        let predictions = fitted
            .predict(&X.view())
            .expect("prediction should succeed");

        assert_eq!(predictions.shape(), &[4, 2]);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_lmc_multi_kernel() {
        let X = array![[1.0], [2.0], [3.0], [4.0]];
        let Y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];

        let kernels = vec![
            Box::new(RBF::new(1.0)) as Box<dyn Kernel>,
            Box::new(RBF::new(2.0)) as Box<dyn Kernel>,
        ];
        let lmc = LinearModelCoregionalization::new()
            .kernels(kernels)
            .n_outputs(2)
            .alpha(1e-6);

        let fitted = lmc
            .fit(&X.view(), &Y.view())
            .expect("model fitting should succeed");
        let predictions = fitted
            .predict(&X.view())
            .expect("prediction should succeed");

        assert_eq!(predictions.shape(), &[4, 2]);
        assert_eq!(fitted.trained_state().n_latent, 2);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_lmc_mixing_matrices() {
        let X = array![[1.0], [2.0], [3.0], [4.0]];
        let Y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];

        let kernels = vec![Box::new(RBF::new(1.0)) as Box<dyn Kernel>];
        let mixing_matrix = array![[0.5], [0.8]];
        let lmc = LinearModelCoregionalization::new()
            .kernels(kernels)
            .mixing_matrices(vec![mixing_matrix.clone()])
            .n_outputs(2)
            .alpha(1e-6);

        let fitted = lmc
            .fit(&X.view(), &Y.view())
            .expect("model fitting should succeed");
        let learned_mixing = &fitted.mixing_matrices()[0];

        assert_eq!(learned_mixing.shape(), mixing_matrix.shape());
        assert_abs_diff_eq!(learned_mixing[[0, 0]], 0.5, epsilon = 1e-10);
        assert_abs_diff_eq!(learned_mixing[[1, 0]], 0.8, epsilon = 1e-10);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_lmc_log_marginal_likelihood() {
        let X = array![[1.0], [2.0], [3.0], [4.0]];
        let Y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];

        let kernels = vec![Box::new(RBF::new(1.0)) as Box<dyn Kernel>];
        let lmc = LinearModelCoregionalization::new()
            .kernels(kernels)
            .n_outputs(2)
            .alpha(1e-6);

        let fitted = lmc
            .fit(&X.view(), &Y.view())
            .expect("model fitting should succeed");
        let log_ml = fitted.log_marginal_likelihood();

        assert!(log_ml.is_finite());
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_lmc_latent_contributions() {
        let X = array![[1.0], [2.0], [3.0], [4.0]];
        let Y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];

        let kernels = vec![
            Box::new(RBF::new(1.0)) as Box<dyn Kernel>,
            Box::new(RBF::new(2.0)) as Box<dyn Kernel>,
        ];
        let lmc = LinearModelCoregionalization::new()
            .kernels(kernels)
            .n_outputs(2)
            .alpha(1e-6);

        let fitted = lmc
            .fit(&X.view(), &Y.view())
            .expect("model fitting should succeed");
        let contributions = fitted
            .latent_contributions(&X.view())
            .expect("operation should succeed");

        assert_eq!(contributions.len(), 2); // Two latent functions
        assert_eq!(contributions[0].shape(), &[4, 2]);
        assert_eq!(contributions[1].shape(), &[4, 2]);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_lmc_errors() {
        let X = array![[1.0], [2.0], [3.0], [4.0]];
        let Y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];

        // Test with no kernels
        let lmc = LinearModelCoregionalization::new().n_outputs(2).alpha(1e-6);
        assert!(lmc.fit(&X.view(), &Y.view()).is_err());

        // Test with mismatched dimensions
        let X_wrong = array![[1.0], [2.0], [3.0]]; // Different number of samples
        let kernels = vec![Box::new(RBF::new(1.0)) as Box<dyn Kernel>];
        let lmc = LinearModelCoregionalization::new()
            .kernels(kernels)
            .n_outputs(2)
            .alpha(1e-6);
        assert!(lmc.fit(&X_wrong.view(), &Y.view()).is_err());

        // Test with wrong n_outputs
        let kernels = vec![Box::new(RBF::new(1.0)) as Box<dyn Kernel>];
        let lmc = LinearModelCoregionalization::new()
            .kernels(kernels)
            .n_outputs(3) // Y has 2 outputs, but we specify 3
            .alpha(1e-6);
        assert!(lmc.fit(&X.view(), &Y.view()).is_err());
    }
}