Skip to main content

GaussianProcess

Struct GaussianProcess 

Source
pub struct GaussianProcess<KernelType: Kernel, PriorType: Prior> {
    pub prior: PriorType,
    pub kernel: KernelType,
    pub noise: f64,
    pub cholesky_epsilon: Option<f64>,
    /* private fields */
}
Expand description

A Gaussian process that can be used to make predictions based on its training data

Fields§

§prior: PriorType

Value to which the process will regress in the absence of information.

§kernel: KernelType

Kernel used to fit the process on the data.

§noise: f64

Amplitude of the noise of the data as provided by the user or deduced by the optimizer.

§cholesky_epsilon: Option<f64>

During Cholesky decomposition, this epsilon is used in place of the diagonal term if and only if the decomposition would otherwise fail. This is especially useful for noiseless sampling processes where very small covariance can result in numerical errors causing Cholesky to fail. See https://github.com/nestordemeure/friedrich/issues/43 for details.

Implementations§

Source§

impl GaussianProcess<Gaussian, ConstantPrior>

Source

pub fn default<T: Input>( training_inputs: T, training_outputs: T::InVector, ) -> Self

Returns a gaussian process with a Gaussian kernel and a constant prior, both fitted to the data.

// training data
let training_inputs = vec![vec![0.8], vec![1.2], vec![3.8], vec![4.2]];
let training_outputs = vec![3.0, 4.0, -2.0, -2.0];

// defining and training a model
let gp = GaussianProcess::default(training_inputs, training_outputs);
Source

pub fn builder<T: Input>( training_inputs: T, training_outputs: T::InVector, ) -> GaussianProcessBuilder<Gaussian, ConstantPrior>

Returns a builder to define specific parameters of the gaussian process.

// model parameters
let input_dimension = 1;
let output_noise = 0.1;
let exponential_kernel = Exponential::default();
let linear_prior = LinearPrior::default(input_dimension);

// defining and training a model
let gp = GaussianProcess::builder(training_inputs, training_outputs).set_noise(output_noise)
                                                                    .set_kernel(exponential_kernel)
                                                                    .fit_kernel()
                                                                    .set_prior(linear_prior)
                                                                    .fit_prior()
                                                                    .train();
Source§

impl<KernelType: Kernel, PriorType: Prior> GaussianProcess<KernelType, PriorType>

Source

pub fn new<T: Input>( prior: PriorType, kernel: KernelType, noise: f64, cholesky_epsilon: Option<f64>, training_inputs: T, training_outputs: T::InVector, ) -> Self

Raw method to create a new gaussian process with the given parameters / data. We recommend that you use either the default parameters or the builder to simplify the definition process.

Source

pub fn add_samples<T: Input>(&mut self, inputs: &T, outputs: &T::InVector)

Adds new samples to the model.

Updates the model (which is faster than a retraining from scratch) but does not refit the parameters.

Source

pub fn likelihood(&self) -> f64

Computes the log likelihood of the current model given the training data.

This quantity can be used for model selection. Given two models, the one with the highest score would be the one with the highest probability of producing the data.

Source

pub fn predict<T: Input>(&self, inputs: &T) -> T::OutVector

Makes a prediction (the mean of the gaussian process) for each row of the input.

Source

pub fn predict_variance<T: Input>(&self, inputs: &T) -> T::OutVector

Predicts the variance of the gaussian process for each row of the input. This quantity (and its square root) can be used as a proxy for the uncertainty of the prediction.

Source

pub fn predict_mean_variance<T: Input>( &self, inputs: &T, ) -> (T::OutVector, T::OutVector)

Predicts both the mean and the variance of the gaussian process for each row of the input.

Faster than calling predict and predict_variance separately.

let gp = GaussianProcess::default(training_inputs, training_outputs);
let input = vec![1.];
let (mean, var) = gp.predict_mean_variance(&input);
println!("prediction: {} ± {}", mean, var.sqrt());
Source

pub fn predict_covariance<T: Input>(&self, inputs: &T) -> DMatrix<f64>

Returns the covariance matrix for the rows of the input.

Source

pub fn sample_at<T: Input>(&self, inputs: &T) -> MultivariateNormal<T>

Produces a multivariate gaussian that can be used to sample at the input points.

The sampling requires a random number generator compatible with the rand crate:

// computes the distribution at some new coordinates
let new_inputs = vec![vec![1.], vec![2.]];
let sampler = gp.sample_at(&new_inputs);

// samples from the distribution
let mut rng = rand::rng();
println!("samples a vector : {:?}", sampler.sample(&mut rng));
Source

pub fn fit_parameters( &mut self, fit_prior: bool, fit_kernel: bool, max_iter: usize, convergence_fraction: f64, max_time: Duration, )

Fits the requested parameters and retrains the model.

The fit of the noise and kernel parameters is done by gradient descent. It runs for a maximum of max_iter iterations and stops prematurely if all gradients are below convergence_fraction time their associated parameter or if it runs for more than max_time.

Good default values for max_iter, convergence_fraction and max_time are 100, 0.05 and chrono::Duration::seconds(3600) (one hour)

Note that, if the noise parameter ends up unnaturally large after the fit, it is a good sign that the kernel is unadapted to the data.

Trait Implementations§

Source§

impl<'de, KernelType, PriorType> Deserialize<'de> for GaussianProcess<KernelType, PriorType>
where KernelType: Deserialize<'de> + Kernel, PriorType: Deserialize<'de> + Prior,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<KernelType, PriorType> Serialize for GaussianProcess<KernelType, PriorType>
where KernelType: Serialize + Kernel, PriorType: Serialize + Prior,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<KernelType, PriorType> Freeze for GaussianProcess<KernelType, PriorType>
where PriorType: Freeze, KernelType: Freeze,

§

impl<KernelType, PriorType> RefUnwindSafe for GaussianProcess<KernelType, PriorType>
where PriorType: RefUnwindSafe, KernelType: RefUnwindSafe,

§

impl<KernelType, PriorType> Send for GaussianProcess<KernelType, PriorType>
where PriorType: Send, KernelType: Send,

§

impl<KernelType, PriorType> Sync for GaussianProcess<KernelType, PriorType>
where PriorType: Sync, KernelType: Sync,

§

impl<KernelType, PriorType> Unpin for GaussianProcess<KernelType, PriorType>
where PriorType: Unpin, KernelType: Unpin,

§

impl<KernelType, PriorType> UnsafeUnpin for GaussianProcess<KernelType, PriorType>
where PriorType: UnsafeUnpin, KernelType: UnsafeUnpin,

§

impl<KernelType, PriorType> UnwindSafe for GaussianProcess<KernelType, PriorType>
where PriorType: UnwindSafe, KernelType: UnwindSafe,

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

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

Source§

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>,

Source§

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,