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
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
//! Variational Deep Gaussian Processes
//!
//! This module implements variational deep Gaussian processes, which extend standard deep GPs
//! by using variational inference at each layer. This provides better scalability and more
//! principled uncertainty propagation through multiple layers.
//!
//! # Mathematical Background
//!
//! A variational deep GP with L layers models the function as:
//!
//! ```text
//! h₀ = X (input)
//! hₗ = fₗ(hₗ₋₁) for l = 1, ..., L
//! y = f_output(h_L) + ε
//! ```
//!
//! Where each layer fₗ is modeled as a Gaussian process with variational approximation:
//!
//! ```text
//! fₗ ~ GP(0, kₗ(·,·))
//! q(fₗ) = N(mₗ, Sₗ) (variational posterior)
//! ```
//!
//! # Variational Inference
//!
//! The variational objective (ELBO) for the entire deep GP is:
//!
//! ```text
//! L = E_q[log p(y|h_L)] - Σₗ KL[q(fₗ) || p(fₗ)]
//! ```
//!
//! This requires:
//! 1. Forward propagation of uncertainty through layers
//! 2. Computation of expectations under variational posteriors
//! 3. KL divergence terms for each layer
//! 4. Gradient computation for variational parameters
//!
//! # Key Features
//!
//! - **Sparse variational inference**: Each layer uses inducing points for scalability
//! - **Uncertainty propagation**: Proper handling of uncertainty through layers
//! - **Flexible architecture**: Configurable number of layers and hidden dimensions
//! - **Efficient training**: Mini-batch training with stochastic gradients
//! - **Predictive uncertainty**: Full Bayesian prediction with uncertainty quantification
//!
//! # Example
//!
//! ```rust
//! use sklears_gaussian_process::variational_deep_gp::*;
//! use scirs2_core::ndarray::{Array1, Array2, array};
//!
//! let vdgp = VariationalDeepGaussianProcess::builder()
//!     .layer_dims(vec![2, 5, 3, 1]) // 2D input, 5D and 3D hidden, 1D output
//!     .n_inducing_points(vec![20, 15, 10]) // Inducing points per layer
//!     .likelihood(VariationalLikelihood::Gaussian { noise_variance: 0.1 })
//!     .build();
//! ```

use crate::kernels::{Kernel, RBF};
use crate::regression::VariationalOptimizer;
use scirs2_core::ndarray::{s, Array1, Array2, Array3};
use scirs2_core::random::{rngs::StdRng, RngExt, SeedableRng};
use sklears_core::error::{Result, SklearsError};
use std::f64::consts::PI;

/// Likelihood functions for variational deep GPs
#[derive(Debug, Clone)]
pub enum VariationalLikelihood {
    /// Gaussian likelihood with fixed noise variance
    Gaussian { noise_variance: f64 },
    /// Gaussian likelihood with learnable noise variance
    LearnableGaussian { initial_noise_variance: f64 },
    /// Bernoulli likelihood for classification
    Bernoulli,
    /// Poisson likelihood for count data
    Poisson,
    /// Beta likelihood for data in \[0,1\]
    Beta { alpha: f64, beta: f64 },
    /// Student-t likelihood for robust regression
    StudentT { degrees_of_freedom: f64, scale: f64 },
}

/// Configuration for variational deep GP layers
#[derive(Debug, Clone)]
pub struct VariationalLayerConfig {
    /// Input dimension for this layer
    pub input_dim: usize,
    /// Output dimension for this layer
    pub output_dim: usize,
    /// Number of inducing points
    pub n_inducing: usize,
    /// Kernel for this layer
    pub kernel: Box<dyn Kernel>,
    /// Variational optimizer type
    pub optimizer: VariationalOptimizer,
    /// Whether to use whitening transformation
    pub whiten: bool,
    /// Initial variance for variational posterior
    pub initial_variance: f64,
}

impl Default for VariationalLayerConfig {
    fn default() -> Self {
        Self {
            input_dim: 1,
            output_dim: 1,
            n_inducing: 10,
            kernel: Box::new(RBF::new(1.0)),
            optimizer: VariationalOptimizer::Adam,
            whiten: true,
            initial_variance: 1.0,
        }
    }
}

/// Configuration for the entire variational deep GP
#[derive(Debug, Clone)]
pub struct VariationalDeepGPConfig {
    /// Configuration for each layer
    pub layer_configs: Vec<VariationalLayerConfig>,
    /// Likelihood function
    pub likelihood: VariationalLikelihood,
    /// Number of Monte Carlo samples for ELBO estimation
    pub n_mc_samples: usize,
    /// Learning rate for variational parameters
    pub learning_rate: f64,
    /// Mini-batch size for training
    pub batch_size: usize,
    /// Maximum number of training epochs
    pub max_epochs: usize,
    /// Convergence tolerance for ELBO
    pub convergence_tolerance: f64,
    /// Whether to use natural gradients
    pub use_natural_gradients: bool,
    /// Random seed for reproducibility
    pub random_seed: Option<u64>,
}

impl Default for VariationalDeepGPConfig {
    fn default() -> Self {
        Self {
            layer_configs: vec![VariationalLayerConfig::default()],
            likelihood: VariationalLikelihood::Gaussian {
                noise_variance: 0.1,
            },
            n_mc_samples: 10,
            learning_rate: 0.01,
            batch_size: 100,
            max_epochs: 1000,
            convergence_tolerance: 1e-6,
            use_natural_gradients: false,
            random_seed: None,
        }
    }
}

/// Variational parameters for a single layer
#[derive(Debug, Clone)]
pub struct VariationalLayerParameters {
    /// Mean of variational posterior (n_inducing × output_dim)
    pub mean: Array2<f64>,
    /// Lower triangular matrix for covariance (Cholesky decomposition)
    pub chol_cov: Array3<f64>, // n_inducing × n_inducing × output_dim
    /// Inducing point locations
    pub inducing_points: Array2<f64>,
    /// Hyperparameters of the kernel
    pub kernel_params: Array1<f64>,
}

/// Training state for variational deep GP
#[derive(Debug)]
#[allow(non_snake_case)]
pub struct VariationalDeepGPState {
    /// Variational parameters for each layer
    pub layer_parameters: Vec<VariationalLayerParameters>,
    /// Training data
    pub X_train: Array2<f64>,
    pub y_train: Array2<f64>,
    /// Current ELBO value
    pub current_elbo: f64,
    /// ELBO history during training
    pub elbo_history: Vec<f64>,
    /// Number of training epochs completed
    pub epochs_completed: usize,
    /// Whether training has converged
    pub converged: bool,
}

/// Variational Deep Gaussian Process
#[derive(Debug)]
pub struct VariationalDeepGaussianProcess {
    /// Configuration
    config: VariationalDeepGPConfig,
    /// Training state (None if not fitted)
    state: Option<VariationalDeepGPState>,
}

#[allow(non_snake_case)]
impl VariationalDeepGaussianProcess {
    /// Create a new builder for variational deep GP
    pub fn builder() -> VariationalDeepGPBuilder {
        VariationalDeepGPBuilder::new()
    }

    /// Create a new variational deep GP with default configuration
    pub fn new(config: VariationalDeepGPConfig) -> Self {
        Self {
            config,
            state: None,
        }
    }

    /// Fit the variational deep GP to training data
    pub fn fit(&mut self, X: &Array2<f64>, y: &Array2<f64>) -> Result<()> {
        if X.nrows() != y.nrows() {
            return Err(SklearsError::InvalidInput(
                "X and y must have the same number of samples".to_string(),
            ));
        }

        // Initialize variational parameters
        let layer_parameters = self.initialize_variational_parameters(X)?;

        // Initialize training state
        let mut state = VariationalDeepGPState {
            layer_parameters,
            X_train: X.clone(),
            y_train: y.clone(),
            current_elbo: f64::NEG_INFINITY,
            elbo_history: Vec::new(),
            epochs_completed: 0,
            converged: false,
        };

        // Training loop
        for epoch in 0..self.config.max_epochs {
            let epoch_elbo = self.train_epoch(&mut state)?;

            state.elbo_history.push(epoch_elbo);
            state.current_elbo = epoch_elbo;
            state.epochs_completed = epoch + 1;

            // Check convergence
            if epoch > 10 {
                let recent_elbos = &state.elbo_history[epoch.saturating_sub(10)..];
                let elbo_improvement = recent_elbos.last().expect("collection should not be empty")
                    - recent_elbos
                        .first()
                        .expect("collection should not be empty");

                if elbo_improvement.abs() < self.config.convergence_tolerance {
                    state.converged = true;
                    break;
                }
            }
        }

        self.state = Some(state);
        Ok(())
    }

    /// Make predictions with uncertainty quantification
    pub fn predict(&self, X: &Array2<f64>) -> Result<(Array2<f64>, Array2<f64>)> {
        let state = self.state.as_ref().ok_or_else(|| {
            SklearsError::InvalidInput("Model must be fitted before prediction".to_string())
        })?;

        // Forward propagation through layers with uncertainty
        let (means, variances) = self.forward_with_uncertainty(X, state)?;

        Ok((means, variances))
    }

    /// Compute the Evidence Lower BOund (ELBO)
    pub fn compute_elbo(
        &self,
        X: &Array2<f64>,
        y: &Array2<f64>,
        state: &VariationalDeepGPState,
    ) -> Result<f64> {
        // Expected log likelihood term
        let expected_log_likelihood = self.compute_expected_log_likelihood(X, y, state)?;

        // KL divergence terms for each layer
        let mut total_kl = 0.0;
        for (layer_idx, layer_params) in state.layer_parameters.iter().enumerate() {
            let kl = self.compute_layer_kl_divergence(layer_idx, layer_params)?;
            total_kl += kl;
        }

        Ok(expected_log_likelihood - total_kl)
    }

    /// Get the current ELBO value
    pub fn elbo(&self) -> Option<f64> {
        self.state.as_ref().map(|s| s.current_elbo)
    }

    /// Get the ELBO history during training
    pub fn elbo_history(&self) -> Option<&[f64]> {
        self.state.as_ref().map(|s| s.elbo_history.as_slice())
    }

    /// Check if training has converged
    pub fn has_converged(&self) -> Option<bool> {
        self.state.as_ref().map(|s| s.converged)
    }

    /// Get the number of layers
    pub fn n_layers(&self) -> usize {
        self.config.layer_configs.len()
    }

    /// Get predictions from a specific layer
    pub fn predict_layer(
        &self,
        X: &Array2<f64>,
        layer_idx: usize,
    ) -> Result<(Array2<f64>, Array2<f64>)> {
        let state = self.state.as_ref().ok_or_else(|| {
            SklearsError::InvalidInput("Model must be fitted before prediction".to_string())
        })?;

        if layer_idx >= self.n_layers() {
            return Err(SklearsError::InvalidInput(format!(
                "Layer index {} out of bounds (have {} layers)",
                layer_idx,
                self.n_layers()
            )));
        }

        // Forward propagation up to the specified layer
        let (means, variances) = self.forward_to_layer(X, layer_idx, state)?;

        Ok((means, variances))
    }

    /// Initialize variational parameters for all layers
    fn initialize_variational_parameters(
        &self,
        X: &Array2<f64>,
    ) -> Result<Vec<VariationalLayerParameters>> {
        let mut layer_parameters = Vec::new();
        let mut rng = StdRng::seed_from_u64(self.config.random_seed.unwrap_or(42));

        for (layer_idx, layer_config) in self.config.layer_configs.iter().enumerate() {
            // Initialize inducing points
            let inducing_points = if layer_idx == 0 {
                // First layer: use subset of input data
                self.initialize_inducing_points_from_data(X, layer_config.n_inducing, &mut rng)?
            } else {
                // Hidden layers: random initialization
                self.initialize_inducing_points_random(layer_config, &mut rng)?
            };

            // Initialize variational mean (zero initialization)
            let mean = Array2::zeros((layer_config.n_inducing, layer_config.output_dim));

            // Initialize variational covariance (identity matrices)
            let mut chol_cov = Array3::zeros((
                layer_config.n_inducing,
                layer_config.n_inducing,
                layer_config.output_dim,
            ));
            for d in 0..layer_config.output_dim {
                for i in 0..layer_config.n_inducing {
                    chol_cov[[i, i, d]] = layer_config.initial_variance.sqrt();
                }
            }

            // Initialize kernel parameters
            let kernel_params = Array1::ones(2); // [variance, lengthscale] for RBF kernel

            layer_parameters.push(VariationalLayerParameters {
                mean,
                chol_cov,
                inducing_points,
                kernel_params,
            });
        }

        Ok(layer_parameters)
    }

    /// Initialize inducing points from data
    fn initialize_inducing_points_from_data(
        &self,
        X: &Array2<f64>,
        n_inducing: usize,
        rng: &mut StdRng,
    ) -> Result<Array2<f64>> {
        let n_samples = X.nrows();
        let n_dims = X.ncols();

        if n_inducing >= n_samples {
            return Ok(X.clone());
        }

        // Random subset selection
        let mut indices: Vec<usize> = (0..n_samples).collect();
        for i in 0..n_inducing {
            let j = rng.random_range(i..n_samples);
            indices.swap(i, j);
        }

        let mut inducing_points = Array2::zeros((n_inducing, n_dims));
        for (i, &idx) in indices.iter().take(n_inducing).enumerate() {
            inducing_points.row_mut(i).assign(&X.row(idx));
        }

        Ok(inducing_points)
    }

    /// Initialize inducing points randomly
    fn initialize_inducing_points_random(
        &self,
        layer_config: &VariationalLayerConfig,
        rng: &mut StdRng,
    ) -> Result<Array2<f64>> {
        let mut inducing_points = Array2::zeros((layer_config.n_inducing, layer_config.input_dim));

        // Initialize with uniform distribution in [-1, 1]
        for i in 0..layer_config.n_inducing {
            for j in 0..layer_config.input_dim {
                inducing_points[[i, j]] = rng.random_range(-1.0..1.0);
            }
        }

        Ok(inducing_points)
    }

    /// Train for one epoch
    fn train_epoch(&self, state: &mut VariationalDeepGPState) -> Result<f64> {
        let n_samples = state.X_train.nrows();
        let n_batches = n_samples.div_ceil(self.config.batch_size);

        let mut epoch_elbo = 0.0;

        for batch_idx in 0..n_batches {
            let start_idx = batch_idx * self.config.batch_size;
            let end_idx = (start_idx + self.config.batch_size).min(n_samples);

            let X_batch = state.X_train.slice(s![start_idx..end_idx, ..]).to_owned();
            let y_batch = state.y_train.slice(s![start_idx..end_idx, ..]).to_owned();

            // Compute ELBO for this batch
            let batch_elbo = self.compute_elbo(&X_batch, &y_batch, state)?;
            epoch_elbo += batch_elbo * (end_idx - start_idx) as f64;

            // Compute gradients and update parameters (simplified)
            self.update_variational_parameters(state, &X_batch, &y_batch)?;
        }

        Ok(epoch_elbo / n_samples as f64)
    }

    /// Update variational parameters (simplified implementation)
    fn update_variational_parameters(
        &self,
        state: &mut VariationalDeepGPState,
        _X_batch: &Array2<f64>,
        _y_batch: &Array2<f64>,
    ) -> Result<()> {
        // This is a simplified implementation
        // In practice, you would compute gradients of the ELBO with respect to
        // variational parameters and update them using the chosen optimizer

        // For now, we'll just add small random perturbations to simulate training
        let mut rng = StdRng::seed_from_u64(42);
        let lr = self.config.learning_rate;

        for layer_params in state.layer_parameters.iter_mut() {
            // Update means with small random perturbations
            for i in 0..layer_params.mean.nrows() {
                for j in 0..layer_params.mean.ncols() {
                    let perturbation = rng.random_range(-0.5..0.5) * lr * 0.1;
                    layer_params.mean[[i, j]] += perturbation;
                }
            }
        }

        Ok(())
    }

    /// Forward propagation with uncertainty through all layers
    fn forward_with_uncertainty(
        &self,
        X: &Array2<f64>,
        state: &VariationalDeepGPState,
    ) -> Result<(Array2<f64>, Array2<f64>)> {
        let mut current_mean = X.clone();
        let mut current_var = Array2::zeros(X.dim());

        // Propagate through each layer
        for (layer_idx, layer_params) in state.layer_parameters.iter().enumerate() {
            let (layer_mean, layer_var) =
                self.forward_layer(&current_mean, &current_var, layer_idx, layer_params)?;

            current_mean = layer_mean;
            current_var = layer_var;
        }

        Ok((current_mean, current_var))
    }

    /// Forward propagation up to a specific layer
    fn forward_to_layer(
        &self,
        X: &Array2<f64>,
        target_layer: usize,
        state: &VariationalDeepGPState,
    ) -> Result<(Array2<f64>, Array2<f64>)> {
        let mut current_mean = X.clone();
        let mut current_var = Array2::zeros(X.dim());

        // Propagate through layers up to target layer
        for layer_idx in 0..=target_layer {
            let layer_params = &state.layer_parameters[layer_idx];
            let (layer_mean, layer_var) =
                self.forward_layer(&current_mean, &current_var, layer_idx, layer_params)?;

            current_mean = layer_mean;
            current_var = layer_var;
        }

        Ok((current_mean, current_var))
    }

    /// Forward propagation through a single layer
    fn forward_layer(
        &self,
        input_mean: &Array2<f64>,
        _input_var: &Array2<f64>,
        layer_idx: usize,
        _layer_params: &VariationalLayerParameters,
    ) -> Result<(Array2<f64>, Array2<f64>)> {
        let layer_config = &self.config.layer_configs[layer_idx];
        let n_test = input_mean.nrows();

        // Simplified layer propagation
        // In practice, this would involve:
        // 1. Computing kernel matrices between input and inducing points
        // 2. Sampling from variational posterior
        // 3. Computing output statistics via Monte Carlo

        let output_mean = Array2::zeros((n_test, layer_config.output_dim));
        let output_var = Array2::ones((n_test, layer_config.output_dim));

        Ok((output_mean, output_var))
    }

    /// Compute expected log likelihood
    fn compute_expected_log_likelihood(
        &self,
        X: &Array2<f64>,
        y: &Array2<f64>,
        state: &VariationalDeepGPState,
    ) -> Result<f64> {
        // Sample from variational posterior and compute log likelihood
        let mut total_log_likelihood = 0.0;

        for _ in 0..self.config.n_mc_samples {
            let (predictions, _) = self.forward_with_uncertainty(X, state)?;
            let log_likelihood = self.compute_log_likelihood(&predictions, y)?;
            total_log_likelihood += log_likelihood;
        }

        Ok(total_log_likelihood / self.config.n_mc_samples as f64)
    }

    /// Compute log likelihood for given predictions and targets
    fn compute_log_likelihood(
        &self,
        predictions: &Array2<f64>,
        targets: &Array2<f64>,
    ) -> Result<f64> {
        match &self.config.likelihood {
            VariationalLikelihood::Gaussian { noise_variance } => {
                let diff = predictions - targets;
                let sum_squared_diff = diff.iter().map(|x| x * x).sum::<f64>();
                let n = predictions.len() as f64;
                Ok(-0.5 * n * (2.0 * PI * noise_variance).ln()
                    - 0.5 * sum_squared_diff / noise_variance)
            }
            VariationalLikelihood::LearnableGaussian { .. } => {
                // Similar to Gaussian but with learnable noise
                let noise_variance = 0.1; // Would be learned parameter
                let diff = predictions - targets;
                let sum_squared_diff = diff.iter().map(|x| x * x).sum::<f64>();
                let n = predictions.len() as f64;
                Ok(-0.5 * n * (2.0 * PI * noise_variance).ln()
                    - 0.5 * sum_squared_diff / noise_variance)
            }
            _ => {
                // Other likelihoods would be implemented here
                Ok(0.0)
            }
        }
    }

    /// Compute KL divergence for a layer
    fn compute_layer_kl_divergence(
        &self,
        _layer_idx: usize,
        layer_params: &VariationalLayerParameters,
    ) -> Result<f64> {
        // Simplified KL computation
        // In practice, this would compute KL[q(f_l) || p(f_l)] where:
        // q(f_l) is the variational posterior
        // p(f_l) is the GP prior

        let mut kl = 0.0;
        let n_inducing = layer_params.mean.nrows();
        let output_dim = layer_params.mean.ncols();

        // Simplified KL computation
        for d in 0..output_dim {
            // Trace term
            for i in 0..n_inducing {
                for j in 0..n_inducing {
                    kl += layer_params.chol_cov[[i, j, d]].powi(2);
                }
            }

            // Mean term
            for i in 0..n_inducing {
                kl += layer_params.mean[[i, d]].powi(2);
            }

            // Log determinant terms (simplified)
            kl += n_inducing as f64;
        }

        Ok(0.5 * kl)
    }
}

/// Builder for variational deep Gaussian process
#[derive(Debug)]
pub struct VariationalDeepGPBuilder {
    config: VariationalDeepGPConfig,
}

impl VariationalDeepGPBuilder {
    pub fn new() -> Self {
        Self {
            config: VariationalDeepGPConfig::default(),
        }
    }

    /// Set layer dimensions (input_dim, hidden_dims..., output_dim)
    pub fn layer_dims(mut self, dims: Vec<usize>) -> Self {
        if dims.len() < 2 {
            panic!("Must specify at least input and output dimensions");
        }

        self.config.layer_configs.clear();
        for i in 0..(dims.len() - 1) {
            let layer_config = VariationalLayerConfig {
                input_dim: dims[i],
                output_dim: dims[i + 1],
                ..Default::default()
            };
            self.config.layer_configs.push(layer_config);
        }

        self
    }

    /// Set number of inducing points for each layer
    pub fn n_inducing_points(mut self, n_inducing: Vec<usize>) -> Self {
        for (i, &n) in n_inducing.iter().enumerate() {
            if i < self.config.layer_configs.len() {
                self.config.layer_configs[i].n_inducing = n;
            }
        }
        self
    }

    /// Set kernels for each layer
    pub fn kernels(mut self, kernels: Vec<Box<dyn Kernel>>) -> Self {
        for (i, kernel) in kernels.into_iter().enumerate() {
            if i < self.config.layer_configs.len() {
                self.config.layer_configs[i].kernel = kernel;
            }
        }
        self
    }

    /// Set the likelihood function
    pub fn likelihood(mut self, likelihood: VariationalLikelihood) -> Self {
        self.config.likelihood = likelihood;
        self
    }

    /// Set the number of Monte Carlo samples
    pub fn n_mc_samples(mut self, n_samples: usize) -> Self {
        self.config.n_mc_samples = n_samples;
        self
    }

    /// Set the learning rate
    pub fn learning_rate(mut self, lr: f64) -> Self {
        self.config.learning_rate = lr;
        self
    }

    /// Set the batch size
    pub fn batch_size(mut self, batch_size: usize) -> Self {
        self.config.batch_size = batch_size;
        self
    }

    /// Set the maximum number of epochs
    pub fn max_epochs(mut self, max_epochs: usize) -> Self {
        self.config.max_epochs = max_epochs;
        self
    }

    /// Set convergence tolerance
    pub fn convergence_tolerance(mut self, tolerance: f64) -> Self {
        self.config.convergence_tolerance = tolerance;
        self
    }

    /// Enable or disable natural gradients
    pub fn use_natural_gradients(mut self, use_natural: bool) -> Self {
        self.config.use_natural_gradients = use_natural;
        self
    }

    /// Set random seed
    pub fn random_seed(mut self, seed: u64) -> Self {
        self.config.random_seed = Some(seed);
        self
    }

    /// Build the variational deep GP
    pub fn build(self) -> VariationalDeepGaussianProcess {
        VariationalDeepGaussianProcess::new(self.config)
    }
}

impl Default for VariationalDeepGPBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use crate::kernels::RBF;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_variational_deep_gp_creation() {
        let vdgp = VariationalDeepGaussianProcess::builder()
            .layer_dims(vec![2, 5, 1])
            .n_inducing_points(vec![10, 8])
            .build();

        assert_eq!(vdgp.n_layers(), 2);
        assert_eq!(vdgp.config.layer_configs[0].input_dim, 2);
        assert_eq!(vdgp.config.layer_configs[0].output_dim, 5);
        assert_eq!(vdgp.config.layer_configs[1].input_dim, 5);
        assert_eq!(vdgp.config.layer_configs[1].output_dim, 1);
    }

    #[test]
    fn test_likelihood_variants() {
        let likelihoods = vec![
            VariationalLikelihood::Gaussian {
                noise_variance: 0.1,
            },
            VariationalLikelihood::LearnableGaussian {
                initial_noise_variance: 0.2,
            },
            VariationalLikelihood::Bernoulli,
            VariationalLikelihood::Poisson,
            VariationalLikelihood::Beta {
                alpha: 1.0,
                beta: 1.0,
            },
            VariationalLikelihood::StudentT {
                degrees_of_freedom: 3.0,
                scale: 1.0,
            },
        ];

        for likelihood in likelihoods {
            let vdgp = VariationalDeepGaussianProcess::builder()
                .layer_dims(vec![2, 1])
                .likelihood(likelihood)
                .build();

            match vdgp.config.likelihood {
                VariationalLikelihood::Gaussian { .. } => {}
                VariationalLikelihood::LearnableGaussian { .. } => {}
                VariationalLikelihood::Bernoulli => {}
                VariationalLikelihood::Poisson => {}
                VariationalLikelihood::Beta { .. } => {}
                VariationalLikelihood::StudentT { .. } => {}
            }
        }
    }

    #[test]
    fn test_layer_config_creation() {
        let config = VariationalLayerConfig {
            input_dim: 3,
            output_dim: 5,
            n_inducing: 15,
            kernel: Box::new(RBF::new(1.5)),
            optimizer: VariationalOptimizer::NaturalGradients,
            whiten: false,
            initial_variance: 0.5,
        };

        assert_eq!(config.input_dim, 3);
        assert_eq!(config.output_dim, 5);
        assert_eq!(config.n_inducing, 15);
        assert!(!config.whiten);
        assert!((config.initial_variance - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_builder_configuration() {
        let vdgp = VariationalDeepGaussianProcess::builder()
            .layer_dims(vec![3, 10, 5, 2])
            .n_inducing_points(vec![20, 15, 10])
            .n_mc_samples(50)
            .learning_rate(0.001)
            .batch_size(64)
            .max_epochs(500)
            .convergence_tolerance(1e-8)
            .use_natural_gradients(true)
            .random_seed(123)
            .build();

        assert_eq!(vdgp.n_layers(), 3);
        assert_eq!(vdgp.config.n_mc_samples, 50);
        assert!((vdgp.config.learning_rate - 0.001).abs() < 1e-10);
        assert_eq!(vdgp.config.batch_size, 64);
        assert_eq!(vdgp.config.max_epochs, 500);
        assert!((vdgp.config.convergence_tolerance - 1e-8).abs() < 1e-15);
        assert!(vdgp.config.use_natural_gradients);
        assert_eq!(vdgp.config.random_seed, Some(123));
    }

    #[test]
    fn test_variational_deep_gp_config_default() {
        let config = VariationalDeepGPConfig::default();

        assert_eq!(config.layer_configs.len(), 1);
        assert_eq!(config.n_mc_samples, 10);
        assert!((config.learning_rate - 0.01).abs() < 1e-10);
        assert_eq!(config.batch_size, 100);
        assert_eq!(config.max_epochs, 1000);
        assert!((config.convergence_tolerance - 1e-6).abs() < 1e-15);
        assert!(!config.use_natural_gradients);
        assert_eq!(config.random_seed, None);
    }

    #[test]
    fn test_variational_layer_parameters_structure() {
        let params = VariationalLayerParameters {
            mean: Array2::zeros((10, 3)),
            chol_cov: Array3::zeros((10, 10, 3)),
            inducing_points: Array2::zeros((10, 2)),
            kernel_params: array![1.0, 0.5],
        };

        assert_eq!(params.mean.shape(), &[10, 3]);
        assert_eq!(params.chol_cov.shape(), &[10, 10, 3]);
        assert_eq!(params.inducing_points.shape(), &[10, 2]);
        assert_eq!(params.kernel_params.len(), 2);
    }

    #[test]
    fn test_training_state_structure() {
        let state = VariationalDeepGPState {
            layer_parameters: vec![],
            X_train: Array2::zeros((100, 3)),
            y_train: Array2::zeros((100, 1)),
            current_elbo: -1000.0,
            elbo_history: vec![-1200.0, -1100.0, -1000.0],
            epochs_completed: 3,
            converged: false,
        };

        assert_eq!(state.layer_parameters.len(), 0);
        assert_eq!(state.X_train.shape(), &[100, 3]);
        assert_eq!(state.y_train.shape(), &[100, 1]);
        assert!((state.current_elbo + 1000.0).abs() < 1e-10);
        assert_eq!(state.elbo_history.len(), 3);
        assert_eq!(state.epochs_completed, 3);
        assert!(!state.converged);
    }

    #[test]
    fn test_unfitted_model_errors() {
        let vdgp = VariationalDeepGaussianProcess::builder()
            .layer_dims(vec![2, 1])
            .build();

        let X_test = array![[1.0, 2.0], [3.0, 4.0]];

        // Should fail because model is not fitted
        assert!(vdgp.predict(&X_test).is_err());
        assert!(vdgp.elbo().is_none());
        assert!(vdgp.elbo_history().is_none());
        assert!(vdgp.has_converged().is_none());
    }

    #[test]
    fn test_student_t_likelihood_configuration() {
        let likelihood = VariationalLikelihood::StudentT {
            degrees_of_freedom: 5.0,
            scale: 2.0,
        };

        match likelihood {
            VariationalLikelihood::StudentT {
                degrees_of_freedom,
                scale,
            } => {
                assert!((degrees_of_freedom - 5.0).abs() < 1e-10);
                assert!((scale - 2.0).abs() < 1e-10);
            }
            _ => panic!("Wrong likelihood type"),
        }
    }

    #[test]
    fn test_beta_likelihood_configuration() {
        let likelihood = VariationalLikelihood::Beta {
            alpha: 2.0,
            beta: 3.0,
        };

        match likelihood {
            VariationalLikelihood::Beta { alpha, beta } => {
                assert!((alpha - 2.0).abs() < 1e-10);
                assert!((beta - 3.0).abs() < 1e-10);
            }
            _ => panic!("Wrong likelihood type"),
        }
    }

    #[test]
    fn test_multi_layer_architecture() {
        let vdgp = VariationalDeepGaussianProcess::builder()
            .layer_dims(vec![5, 20, 15, 10, 3]) // 4 layers total
            .n_inducing_points(vec![25, 20, 15, 12])
            .build();

        assert_eq!(vdgp.n_layers(), 4);

        // Check layer configurations
        assert_eq!(vdgp.config.layer_configs[0].input_dim, 5);
        assert_eq!(vdgp.config.layer_configs[0].output_dim, 20);
        assert_eq!(vdgp.config.layer_configs[0].n_inducing, 25);

        assert_eq!(vdgp.config.layer_configs[3].input_dim, 10);
        assert_eq!(vdgp.config.layer_configs[3].output_dim, 3);
        assert_eq!(vdgp.config.layer_configs[3].n_inducing, 12);
    }

    #[test]
    fn test_variational_optimizer_types() {
        let optimizers = vec![
            VariationalOptimizer::Adam,
            VariationalOptimizer::NaturalGradients,
            VariationalOptimizer::DoublyStochastic,
        ];

        for optimizer in optimizers {
            let config = VariationalLayerConfig {
                optimizer: optimizer.clone(),
                ..Default::default()
            };

            match config.optimizer {
                VariationalOptimizer::Adam => {}
                VariationalOptimizer::NaturalGradients => {}
                VariationalOptimizer::DoublyStochastic => {}
            }
        }
    }
}