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: PriorTypeValue to which the process will regress in the absence of information.
kernel: KernelTypeKernel used to fit the process on the data.
noise: f64Amplitude 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>
impl GaussianProcess<Gaussian, ConstantPrior>
Sourcepub fn default<T: Input>(
training_inputs: T,
training_outputs: T::InVector,
) -> Self
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);Sourcepub fn builder<T: Input>(
training_inputs: T,
training_outputs: T::InVector,
) -> GaussianProcessBuilder<Gaussian, ConstantPrior>
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>
impl<KernelType: Kernel, PriorType: Prior> GaussianProcess<KernelType, PriorType>
Sourcepub fn new<T: Input>(
prior: PriorType,
kernel: KernelType,
noise: f64,
cholesky_epsilon: Option<f64>,
training_inputs: T,
training_outputs: T::InVector,
) -> Self
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.
Sourcepub fn add_samples<T: Input>(&mut self, inputs: &T, outputs: &T::InVector)
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.
Sourcepub fn likelihood(&self) -> f64
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.
Sourcepub fn predict<T: Input>(&self, inputs: &T) -> T::OutVector
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.
Sourcepub fn predict_variance<T: Input>(&self, inputs: &T) -> T::OutVector
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.
Sourcepub fn predict_mean_variance<T: Input>(
&self,
inputs: &T,
) -> (T::OutVector, T::OutVector)
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());Sourcepub fn predict_covariance<T: Input>(&self, inputs: &T) -> DMatrix<f64>
pub fn predict_covariance<T: Input>(&self, inputs: &T) -> DMatrix<f64>
Returns the covariance matrix for the rows of the input.
Sourcepub fn sample_at<T: Input>(&self, inputs: &T) -> MultivariateNormal<T>
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));Sourcepub fn fit_parameters(
&mut self,
fit_prior: bool,
fit_kernel: bool,
max_iter: usize,
convergence_fraction: f64,
max_time: Duration,
)
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>
impl<'de, KernelType, PriorType> Deserialize<'de> for GaussianProcess<KernelType, PriorType>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<KernelType, PriorType> Serialize for GaussianProcess<KernelType, PriorType>
impl<KernelType, PriorType> Serialize for GaussianProcess<KernelType, PriorType>
Auto Trait Implementations§
impl<KernelType, PriorType> Freeze for GaussianProcess<KernelType, PriorType>
impl<KernelType, PriorType> RefUnwindSafe for GaussianProcess<KernelType, PriorType>where
PriorType: RefUnwindSafe,
KernelType: RefUnwindSafe,
impl<KernelType, PriorType> Send for GaussianProcess<KernelType, PriorType>
impl<KernelType, PriorType> Sync for GaussianProcess<KernelType, PriorType>
impl<KernelType, PriorType> Unpin for GaussianProcess<KernelType, PriorType>
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> 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<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.