Struct egobox_gp::SparseGaussianProcess

source ·
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 [ParamEstimation]). 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, 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: &Array2<f64>) -> Array2<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>, Array2<f64>) {
    let normal = Normal::new(0., eta2.sqrt()).unwrap();
    let mut rng = rand::thread_rng();
    let gaussian_noise = Array::<f64, _>::random_using((nt, 1), normal, &mut rng);
    let xt = 2. * Array::<f64, _>::random_using((nt, 1), Uniform::new(0., 1.), &mut rng) - 1.;
    let yt = f_obj(&xt) + gaussian_noise;
    (xt, 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

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>

source

pub fn params<NewCorr: CorrelationModel<F>>( corr: NewCorr, inducings: Inducings<F> ) -> SgpParams<F, NewCorr>

Gp parameters contructor

source

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.

source

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.

source

pub fn kpls_dim(&self) -> Option<usize>

Retrieve number of PLS components 1 <= n <= x dimension

source

pub fn input_dim(&self) -> usize

Retrieve input dimension before kpls dimension reduction if any

source

pub fn output_dim(&self) -> usize

Retrieve output dimension

source

pub fn theta(&self) -> &Array1<F>

Optimal theta

source

pub fn variance(&self) -> F

Estimated variance

source

pub fn noise_variance(&self) -> F

Estimated noise variance

source

pub fn likelihood(&self) -> F

Retrieve reduced likelihood value

source

pub fn inducings(&self) -> &Array2<F>

Inducing points

source

pub fn predict_gradients( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2> ) -> Array2<F>

source

pub fn predict_var_gradients( &self, x: &ArrayBase<impl Data<Elem = F>, Ix2> ) -> Array2<F>

source

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

source

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

source

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)

Trait Implementations§

source§

impl<F: Float, Corr: CorrelationModel<F>> Clone for SparseGaussianProcess<F, Corr>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<F: Debug + Float, Corr: Debug + CorrelationModel<F>> Debug for SparseGaussianProcess<F, Corr>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<F: Float, Corr: CorrelationModel<F>> Display for SparseGaussianProcess<F, Corr>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<F, D, Corr> PredictInplace<ArrayBase<D, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>> for SparseGaussianProcess<F, Corr>
where F: Float, D: Data<Elem = F>, Corr: CorrelationModel<F>,

source§

fn predict_inplace(&self, x: &ArrayBase<D, Ix2>, y: &mut Array2<F>)

Predict something in place
source§

fn default_target(&self, x: &ArrayBase<D, Ix2>) -> Array2<F>

Create targets that predict_inplace works with.

Auto Trait Implementations§

§

impl<F, Corr> Freeze for SparseGaussianProcess<F, Corr>
where Corr: Freeze, F: Freeze,

§

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> UnwindSafe for SparseGaussianProcess<F, Corr>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<'a, F, D, DM, T, O> Predict<&'a ArrayBase<D, DM>, T> for O
where D: Data<Elem = F>, DM: Dimension, O: PredictInplace<ArrayBase<D, DM>, T>,

source§

fn predict(&self, records: &'a ArrayBase<D, DM>) -> T

source§

impl<'a, F, R, T, S, O> Predict<&'a DatasetBase<R, T>, S> for O
where R: Records<Elem = F>, O: PredictInplace<R, S>,

source§

fn predict(&self, ds: &'a DatasetBase<R, T>) -> S

source§

impl<F, D, E, T, O> Predict<ArrayBase<D, Dim<[usize; 2]>>, DatasetBase<ArrayBase<D, Dim<[usize; 2]>>, T>> for O
where D: Data<Elem = F>, T: AsTargets<Elem = E>, O: PredictInplace<ArrayBase<D, Dim<[usize; 2]>>, T>,

source§

fn predict( &self, records: ArrayBase<D, Dim<[usize; 2]>> ) -> DatasetBase<ArrayBase<D, Dim<[usize; 2]>>, T>

source§

impl<F, R, T, E, S, O> Predict<DatasetBase<R, T>, DatasetBase<R, S>> for O
where R: Records<Elem = F>, S: AsTargets<Elem = E>, O: PredictInplace<R, S>,

source§

fn predict(&self, ds: DatasetBase<R, T>) -> DatasetBase<R, S>

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> SendAlias for T

source§

impl<T> SyncAlias for T