use std::marker::PhantomData;
use crate::error::GprError;
use crate::policy::{JitterPolicy, KernelExp, with_kernel_exp};
use crate::sparse::{SparseCore, SparseSpec};
use crate::transform::{UnfittedTarget, UnfittedTransform};
use crate::kernel::{KernelSpec, ModelKernel, ModelKernelParts, PointKernel};
use crate::likelihood::GaussianLikelihood;
use crate::optimizer::{Adam, Fixed};
use crate::precision::{DoublePrecision, GpScalar};
use super::factor::{assemble_fitted, run_adam_fit};
use super::fitted::FittedSvgp;
#[derive(Clone, Debug)]
pub struct Svgp<O = Fixed, P = DoublePrecision, K: ModelKernel = KernelSpec> {
pub(super) spec: SparseSpec<K::Supply>,
pub(super) optimizer: O,
pub(super) _precision: PhantomData<P>,
pub(super) _kernel: PhantomData<K>,
}
impl<K: ModelKernel> Svgp<Fixed, DoublePrecision, K> {
pub fn new(kernel: K, likelihood: GaussianLikelihood) -> Self {
Self {
spec: SparseSpec::new(<K as ModelKernelParts>::into_spec(kernel), likelihood),
optimizer: Fixed,
_precision: PhantomData,
_kernel: PhantomData,
}
}
}
impl<O, P, K: ModelKernel> Svgp<O, P, K> {
fn retype<O2, P2>(self, map: impl FnOnce(O) -> O2) -> Svgp<O2, P2, K> {
Svgp {
spec: self.spec,
optimizer: map(self.optimizer),
_precision: PhantomData,
_kernel: PhantomData,
}
}
pub fn with_optimizer<O2>(self, optimizer: O2) -> Svgp<O2, P, K> {
self.retype(|_| optimizer)
}
pub fn with_precision<P2: GpScalar>(self) -> Svgp<O, P2, K> {
self.retype(|optimizer| optimizer)
}
pub fn with_math(mut self, math: KernelExp) -> Self {
self.spec.math = math;
self
}
pub fn math(&self) -> KernelExp {
self.spec.math
}
pub fn with_jitter_policy(mut self, policy: JitterPolicy) -> Self {
self.spec.jitter = policy;
self
}
pub fn jitter_policy(&self) -> JitterPolicy {
self.spec.jitter
}
pub fn with_target_transform(mut self, transform: impl UnfittedTarget + 'static) -> Self {
self.spec.y_transform = Box::new(transform);
self
}
pub fn likelihood(&self) -> &GaussianLikelihood {
&self.spec.likelihood
}
pub fn num_params(&self) -> usize {
self.spec.theta_len()
}
pub fn get_params(&self, out: &mut [f64]) -> Result<(), GprError> {
self.spec.read_theta(out)
}
pub fn set_params(&mut self, params: &[f64]) -> Result<(), GprError> {
self.spec.write_theta(params)
}
}
impl<O, P, K: PointKernel> Svgp<O, P, K> {
pub fn with_input_transform(mut self, transform: impl UnfittedTransform + 'static) -> Self {
self.spec.x_transform = Box::new(transform);
self
}
}
impl<O, P> Svgp<O, P> {
pub fn kernel(&self) -> &KernelSpec {
&self.spec.kernel
}
}
impl<O, P, C: crate::kernel::PointUse> Svgp<O, P, crate::kernel::DistanceKernel<C>> {
pub fn to_kernel(&self) -> crate::kernel::DistanceKernel<C> {
<crate::kernel::DistanceKernel<C> as ModelKernelParts>::from_spec(self.spec.kernel.clone())
}
}
impl<P> Svgp<Fixed, P>
where
P: GpScalar,
{
#[allow(clippy::result_large_err)] pub fn factor(
self,
x: &[f64],
n_rows: usize,
n_cols: usize,
y: &[f64],
z: &[f64],
n_inducing: usize,
) -> Result<FittedSvgp<P>, (Self, GprError)> {
let core = match SparseCore::prepare(&self.spec, x, n_rows, n_cols, y, z, n_inducing) {
Ok(core) => core,
Err(err) => return Err((self, err)),
};
match with_kernel_exp!(self.spec.math, M => assemble_fitted::<M, _, KernelSpec>(core, None))
{
Ok(fitted) => Ok(fitted),
Err(err) => Err((self, err)),
}
}
}
impl<P> Svgp<Adam, P>
where
P: GpScalar,
{
#[allow(clippy::result_large_err)]
pub fn fit(
self,
x: &[f64],
n_rows: usize,
n_cols: usize,
y: &[f64],
z: &[f64],
n_inducing: usize,
) -> Result<FittedSvgp<P>, (Self, GprError)> {
let core = match SparseCore::prepare(&self.spec, x, n_rows, n_cols, y, z, n_inducing) {
Ok(core) => core,
Err(err) => return Err((self, err)),
};
match with_kernel_exp!(self.spec.math, M => assemble_fitted::<M, _, KernelSpec>(core, None))
{
Ok(mut fitted) => match with_kernel_exp!(
self.spec.math,
M => run_adam_fit::<M, _, KernelSpec>(&mut fitted, &self.optimizer)
) {
Ok(()) => Ok(fitted),
Err(err) => Err((self, err)),
},
Err(err) => Err((self, err)),
}
}
}