pub struct SparseGaussianProcess<F: Float, Corr: CorrelationModel<F>> { /* private fields */ }Expand description
Sparse gaussian process considers a set of M inducing points either to approximate the posterior Gaussian distribution
with a low-rank representation (FITC - Fully Independent Training Conditional method), or to approximate the posterior
distribution directly (VFE - Variational Free Energy method).
These methods enable accurate modeling with large training datasets of N points while preserving
computational efficiency. With M < N, we get O(NM^2) complexity instead of O(N^3)
in time processing and O(NM) instead of O(N^2) in memory space.
See Reference section for more information.
§Implementation
SparseGaussianProcess inducing points definition can be either random or provided by the user through
the Inducings specification. The used sparse method is specified with the SparseMethod.
Noise variance can be either specified as a known constant or estimated (see ParamTuning).
Unlike GaussianProcess implementation SparseGaussianProcess
does not allow choosing a trend which is supposed to be zero.
The correlation kernel might be selected amongst available kernels.
When targetting a squared exponential kernel, one can use the SparseKriging shortcut.
§Features
§serializable
The serializable feature enables the serialization of GP models using the serde crate.
§Example
use ndarray::{Array, Array1, Array2, Axis};
use ndarray_rand::rand;
use ndarray_rand::rand::SeedableRng;
use ndarray_rand::RandomExt;
use ndarray_rand::rand_distr::{Normal, Uniform};
use linfa::prelude::{Dataset, DatasetBase, Fit, Float, PredictInplace};
use egobox_gp::{SparseKriging, Inducings};
const PI: f64 = std::f64::consts::PI;
// Let us define a hidden target function for our sparse GP example
fn f_obj(x: &Array1<f64>) -> Array1<f64> {
x.mapv(|v| (3. * PI * v).sin() + 0.3 * (9. * PI * v).cos() + 0.5 * (7. * PI * v).sin())
}
// Then we can define a utility function to generate some noisy data
// nt points with a gaussian noise with a variance eta2.
fn make_test_data(
nt: usize,
eta2: f64,
) -> (Array2<f64>, Array1<f64>) {
let normal = Normal::new(0., eta2.sqrt()).unwrap();
let mut rng = rand::thread_rng();
let gaussian_noise = Array::<f64, _>::random_using((nt, ), normal, &mut rng);
let xt = 2. * Array::<f64, _>::random_using((nt, ), Uniform::new(0., 1.), &mut rng) - 1.;
let yt = f_obj(&xt) + gaussian_noise;
(xt.insert_axis(Axis(1)), yt)
}
// Generate training data
let nt = 200;
// Variance of the gaussian noise on our training data
let eta2: f64 = 0.01;
let (xt, yt) = make_test_data(nt, eta2);
// Train our sparse gaussian process with n inducing points taken in the dataset
let n_inducings = 30;
let sgp = SparseKriging::params(Inducings::Randomized(n_inducings))
.fit(&Dataset::new(xt, yt))
.expect("SGP fitted");
println!("sgp theta={:?}", sgp.theta());
println!("sgp variance={:?}", sgp.variance());
println!("noise variance={:?}", sgp.noise_variance());
// Predict with our trained SGP
let xplot = Array::linspace(-1., 1., 100).insert_axis(Axis(1));
let sgp_vals = sgp.predict(&xplot).unwrap();
let sgp_vars = sgp.predict_var(&xplot).unwrap();§Reference
Valayer, H.; Bartoli, N.; Castaño-Aguirre, M.; Lafage, R.; Lefebvre, T.; López-Lopera, A.F.; Mouton, S. A Python Toolbox for Data-Driven Aerodynamic Modeling Using Sparse Gaussian Processes Aerospace 2024, 11, 260.
Matthias Bauer, Mark van der Wilk, and Carl Edward Rasmussen. Understanding Probabilistic Sparse Gaussian Process Approximations. In: Advances in Neural Information Processing Systems. Ed. by D. Lee et al. Vol. 29. Curran Associates, Inc., 2016
Implementations§
Source§impl<F: Float, Corr: CorrelationModel<F>> SparseGaussianProcess<F, Corr>
impl<F: Float, Corr: CorrelationModel<F>> SparseGaussianProcess<F, Corr>
Sourcepub fn params<NewCorr: CorrelationModel<F>>(
corr: NewCorr,
inducings: Inducings<F>,
) -> SgpParams<F, NewCorr>
pub fn params<NewCorr: CorrelationModel<F>>( corr: NewCorr, inducings: Inducings<F>, ) -> SgpParams<F, NewCorr>
Gp parameters contructor
Sourcepub fn predict(
&self,
x: &ArrayBase<impl Data<Elem = F>, Ix2>,
) -> Result<Array1<F>>
pub fn predict( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2>, ) -> Result<Array1<F>>
Predict output values at n given x points of nx components specified as a (n, nx) matrix.
Returns n scalar output values as as a vector (n,).
Sourcepub fn predict_var(
&self,
x: &ArrayBase<impl Data<Elem = F>, Ix2>,
) -> Result<Array1<F>>
pub fn predict_var( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2>, ) -> Result<Array1<F>>
Predict variance values at n given x points of nx components specified as a (n, nx) matrix.
Returns n variance values as (n,) column vector.
Sourcepub fn noise_variance(&self) -> F
pub fn noise_variance(&self) -> F
Estimated noise variance
Sourcepub fn likelihood(&self) -> F
pub fn likelihood(&self) -> F
Retrieve reduced likelihood value
Sourcepub fn kpls_dim(&self) -> Option<usize>
pub fn kpls_dim(&self) -> Option<usize>
Retrieve number of PLS components 1 <= n <= x dimension
Sourcepub fn predict_gradients(
&self,
x: &ArrayBase<impl Data<Elem = F>, Ix2>,
) -> Array2<F>
pub fn predict_gradients( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2>, ) -> Array2<F>
Predict gradients of the mean function at n given x points of nx components specified as a (n, nx) matrix.
Returns n gradient vectors as a (n, nx) matrix.
Sourcepub fn predict_var_gradients(
&self,
x: &ArrayBase<impl Data<Elem = F>, Ix2>,
) -> Array2<F>
pub fn predict_var_gradients( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2>, ) -> Array2<F>
Predict gradients of the variance function at n given x points of nx components specified as a (n, nx) matrix.
Returns n gradient vectors as a (n, nx) matrix.
Sourcepub fn sample_chol(
&self,
x: &ArrayBase<impl Data<Elem = F>, Ix2>,
n_traj: usize,
) -> Array2<F>
pub fn sample_chol( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2>, n_traj: usize, ) -> Array2<F>
Sample the gaussian process for n_traj trajectories using cholesky decomposition
Trait Implementations§
Source§impl<F: Float, Corr: CorrelationModel<F>> Clone for SparseGaussianProcess<F, Corr>
impl<F: Float, Corr: CorrelationModel<F>> Clone for SparseGaussianProcess<F, Corr>
Source§impl<F: Debug + Float, Corr: Debug + CorrelationModel<F>> Debug for SparseGaussianProcess<F, Corr>
impl<F: Debug + Float, Corr: Debug + CorrelationModel<F>> Debug for SparseGaussianProcess<F, Corr>
Source§impl<F: Float, Corr: CorrelationModel<F>> Display for SparseGaussianProcess<F, Corr>
impl<F: Float, Corr: CorrelationModel<F>> Display for SparseGaussianProcess<F, Corr>
Source§impl<F, D, Corr> PredictInplace<ArrayBase<D, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<F>, Dim<[usize; 1]>>> for SparseGaussianProcess<F, Corr>
impl<F, D, Corr> PredictInplace<ArrayBase<D, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<F>, Dim<[usize; 1]>>> for SparseGaussianProcess<F, Corr>
Source§impl<F, Corr> PredictScore<F, GpError, SgpParams<F, Corr>, SparseGaussianProcess<F, Corr>> for SparseGaussianProcess<F, Corr>where
F: Float,
Corr: CorrelationModel<F>,
impl<F, Corr> PredictScore<F, GpError, SgpParams<F, Corr>, SparseGaussianProcess<F, Corr>> for SparseGaussianProcess<F, Corr>where
F: Float,
Corr: CorrelationModel<F>,
Source§fn training_data(&self) -> &(Array2<F>, Array1<F>)
fn training_data(&self) -> &(Array2<F>, Array1<F>)
Source§fn looq2_score(&self) -> F
fn looq2_score(&self) -> F
Auto Trait Implementations§
impl<F, Corr> Freeze for SparseGaussianProcess<F, Corr>
impl<F, Corr> RefUnwindSafe for SparseGaussianProcess<F, Corr>where
Corr: RefUnwindSafe,
F: RefUnwindSafe,
impl<F, Corr> Send for SparseGaussianProcess<F, Corr>where
Corr: Send,
impl<F, Corr> Sync for SparseGaussianProcess<F, Corr>
impl<F, Corr> Unpin for SparseGaussianProcess<F, Corr>where
Corr: Unpin,
impl<F, Corr> UnsafeUnpin for SparseGaussianProcess<F, Corr>where
Corr: UnsafeUnpin,
F: UnsafeUnpin,
impl<F, Corr> UnwindSafe for SparseGaussianProcess<F, Corr>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more