Struct egobox_gp::GaussianProcess
source · pub struct GaussianProcess<F: Float, Mean: RegressionModel<F>, Corr: CorrelationModel<F>> { /* private fields */ }
Expand description
A GP regression is an interpolation method where the interpolated values are modeled by a Gaussian process with a mean and governed by a prior covariance kernel, which depends on some parameters to be determined.
The interpolated output is modeled as stochastic process as follows:
Y(x) = mu(x) + Z(x)
where:
mu(x)
is the trend i.e. the mean of the gaussian processZ(x)
the realization of stochastic gaussian process ~Normal(0, sigma^2)
which in turn is written as:
Y(x) = betas.regr(x) + sigma^2*corr(x, x')
where:
betas
is a vector of linear regression parameters to be determinedregr(x)
a vector of polynomial basis functionssigma^2
is the process variancecorr(x, x')
is a correlation function which depends ondistance(x, x')
and a set of unknown parametersthetas
to be determined.
§Implementation
- Based on ndarray and linfa and strive to follow linfa guidelines
- GP mean model can be constant, linear or quadratic
- GP correlation model can be build the following kernels: squared exponential, absolute exponential, matern 3/2, matern 5/2
cf. SMT Kriging - For high dimensional problems, the classic GP algorithm does not perform well as it depends on the inversion of a correlation (n, n) matrix which is an O(n3) operation. To work around this problem the library implements dimension reduction using Partial Least Squares method upon Kriging method also known as KPLS algorithm (see Reference)
- GP models can be saved and loaded using serde.
See
serializable
feature section below.
§Features
§serializable
The serializable
feature enables the serialization of GP models using the serde crate
.
§blas
The blas
feature enables the use of BLAS/LAPACK linear algebra backend available with ndarray-linalg
.
§Example
use egobox_gp::{correlation_models::*, mean_models::*, GaussianProcess};
use linfa::prelude::*;
use ndarray::{arr2, concatenate, Array, Array2, Axis};
// one-dimensional test function to approximate
fn xsinx(x: &Array2<f64>) -> Array2<f64> {
(x - 3.5) * ((x - 3.5) / std::f64::consts::PI).mapv(|v| v.sin())
}
// training data
let xt = arr2(&[[0.0], [5.0], [10.0], [15.0], [18.0], [20.0], [25.0]]);
let yt = xsinx(&xt);
// GP with constant mean model and squared exponential correlation model
// i.e. Oridinary Kriging model
let kriging = GaussianProcess::<f64, ConstantMean, SquaredExponentialCorr>::params(
ConstantMean::default(),
SquaredExponentialCorr::default())
.fit(&Dataset::new(xt, yt))
.expect("Kriging trained");
// Use trained model for making predictions
let xtest = Array::linspace(0., 25., 26).insert_axis(Axis(1));
let ytest = xsinx(&xtest);
let ypred = kriging.predict(&xtest).expect("Kriging prediction");
let yvariances = kriging.predict_var(&xtest).expect("Kriging prediction");
§Reference:
Bouhlel, Mohamed Amine, et al. Improving kriging surrogates of high-dimensional design models by Partial Least Squares dimension reduction Structural and Multidisciplinary Optimization 53.5 (2016): 935-952.
Implementations§
source§impl<F: Float, Mean: RegressionModel<F>, Corr: CorrelationModel<F>> GaussianProcess<F, Mean, Corr>
impl<F: Float, Mean: RegressionModel<F>, Corr: CorrelationModel<F>> GaussianProcess<F, Mean, Corr>
sourcepub fn params<NewMean: RegressionModel<F>, NewCorr: CorrelationModel<F>>(
mean: NewMean,
corr: NewCorr
) -> GpParams<F, NewMean, NewCorr>
pub fn params<NewMean: RegressionModel<F>, NewCorr: CorrelationModel<F>>( mean: NewMean, corr: NewCorr ) -> GpParams<F, NewMean, NewCorr>
Gp parameters contructor
sourcepub fn predict(
&self,
x: &ArrayBase<impl Data<Elem = F>, Ix2>
) -> Result<Array2<F>>
pub fn predict( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2> ) -> Result<Array2<F>>
Predict output values at n given x
points of nx components specified as a (n, nx) matrix.
Returns n scalar output values as (n, 1) column vector.
sourcepub fn predict_var(
&self,
x: &ArrayBase<impl Data<Elem = F>, Ix2>
) -> Result<Array2<F>>
pub fn predict_var( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2> ) -> Result<Array2<F>>
Predict variance values at n given x
points of nx components specified as a (n, nx) matrix.
Returns n variance values as (n, 1) column vector.
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
sourcepub fn sample_eig(
&self,
x: &ArrayBase<impl Data<Elem = F>, Ix2>,
n_traj: usize
) -> Array2<F>
pub fn sample_eig( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2>, n_traj: usize ) -> Array2<F>
Sample the gaussian process for n_traj
trajectories using eigenvalues decomposition
sourcepub fn sample(
&self,
x: &ArrayBase<impl Data<Elem = F>, Ix2>,
n_traj: usize
) -> Array2<F>
pub fn sample( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2>, n_traj: usize ) -> Array2<F>
Sample the gaussian process for n_traj
trajectories using eigenvalues decomposition (alias of sample_eig
)
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 input_dim(&self) -> usize
pub fn input_dim(&self) -> usize
Retrieve input dimension before kpls dimension reduction if any
sourcepub fn output_dim(&self) -> usize
pub fn output_dim(&self) -> usize
Retrieve output dimension
sourcepub fn predict_kth_derivatives(
&self,
x: &ArrayBase<impl Data<Elem = F>, Ix2>,
kx: usize
) -> Array1<F>
pub fn predict_kth_derivatives( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2>, kx: usize ) -> Array1<F>
Predict derivatives of the output prediction
wrt the kxth component at a set of n points x
specified as a (n, nx) matrix where x has nx components.
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 derivatives at a set of point x
specified as a (n, nx) matrix where x has nx components.
Returns a (n, nx) matrix containing output derivatives at x wrt each nx components
sourcepub fn predict_jacobian(
&self,
x: &ArrayBase<impl Data<Elem = F>, Ix1>
) -> Array2<F>
pub fn predict_jacobian( &self, x: &ArrayBase<impl Data<Elem = F>, Ix1> ) -> Array2<F>
Predict gradient at a given x point Note: output is one dimensional, named jacobian as result is given as a one-column matrix
Trait Implementations§
source§impl<F: Float, Mean: RegressionModel<F>, Corr: CorrelationModel<F>> Clone for GaussianProcess<F, Mean, Corr>
impl<F: Float, Mean: RegressionModel<F>, Corr: CorrelationModel<F>> Clone for GaussianProcess<F, Mean, Corr>
source§impl<F: Debug + Float, Mean: Debug + RegressionModel<F>, Corr: Debug + CorrelationModel<F>> Debug for GaussianProcess<F, Mean, Corr>
impl<F: Debug + Float, Mean: Debug + RegressionModel<F>, Corr: Debug + CorrelationModel<F>> Debug for GaussianProcess<F, Mean, Corr>
source§impl<F: Float, Mean: RegressionModel<F>, Corr: CorrelationModel<F>> Display for GaussianProcess<F, Mean, Corr>
impl<F: Float, Mean: RegressionModel<F>, Corr: CorrelationModel<F>> Display for GaussianProcess<F, Mean, Corr>
Auto Trait Implementations§
impl<F, Mean, Corr> Freeze for GaussianProcess<F, Mean, Corr>
impl<F, Mean, Corr> RefUnwindSafe for GaussianProcess<F, Mean, Corr>
impl<F, Mean, Corr> Send for GaussianProcess<F, Mean, Corr>
impl<F, Mean, Corr> Sync for GaussianProcess<F, Mean, Corr>
impl<F, Mean, Corr> Unpin for GaussianProcess<F, Mean, Corr>
impl<F, Mean, Corr> UnwindSafe for GaussianProcess<F, Mean, 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> 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