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::{Fixed, Lbfgs, Optimizer};
use crate::precision::{DoublePrecision, GpScalar};
use crate::sgpr::SgprObjective;
use super::factor::assemble_fitted;
use super::fitted::FittedSgpr;
use super::{FixedInducing, FreeInducing, InducingLayout};
use crate::kernel::NoSupply;
#[derive(Clone, Debug)]
pub struct Sgpr<O = Lbfgs, I = FixedInducing, P = DoublePrecision, K: ModelKernel = KernelSpec> {
pub(super) spec: SparseSpec<K::Supply>,
pub(super) optimizer: O,
pub(super) inducing: PhantomData<I>,
pub(super) _precision: PhantomData<P>,
pub(super) _kernel: PhantomData<K>,
}
impl<K: ModelKernel> Sgpr<Lbfgs, FixedInducing, DoublePrecision, K> {
pub fn new(kernel: K, likelihood: GaussianLikelihood) -> Self {
Self {
spec: SparseSpec::new(<K as ModelKernelParts>::into_spec(kernel), likelihood),
optimizer: Lbfgs::new(),
inducing: PhantomData,
_precision: PhantomData,
_kernel: PhantomData,
}
}
}
impl<O, I, P, K: ModelKernel> Sgpr<O, I, P, K> {
fn retype<O2, I2, P2>(self, map: impl FnOnce(O) -> O2) -> Sgpr<O2, I2, P2, K> {
Sgpr {
spec: self.spec,
optimizer: map(self.optimizer),
inducing: PhantomData,
_precision: PhantomData,
_kernel: PhantomData,
}
}
pub fn with_optimizer<O2>(self, optimizer: O2) -> Sgpr<O2, I, P, K> {
self.retype(|_| optimizer)
}
pub fn with_precision<P2: GpScalar>(self) -> Sgpr<O, I, 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, I, P, K: PointKernel> Sgpr<O, I, P, K> {
pub fn with_input_transform(mut self, transform: impl UnfittedTransform + 'static) -> Self {
self.spec.x_transform = Box::new(transform);
self
}
}
impl<O, I, P> Sgpr<O, I, P> {
pub fn with_inducing<I2>(self, _inducing: I2) -> Sgpr<O, I2, P> {
self.retype(|optimizer| optimizer)
}
pub fn kernel(&self) -> &KernelSpec {
&self.spec.kernel
}
}
impl<O, I, P, C: crate::kernel::PointUse> Sgpr<O, I, 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<O, P> Sgpr<O, FixedInducing, P>
where
P: GpScalar,
O: Clone + for<'a> Optimizer<SgprObjective<'a, O, FixedInducing, P>>,
{
#[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<FittedSgpr<O, FixedInducing, 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,
self.optimizer.clone(),
)) {
Ok(mut fitted) => match fitted.optimize_hyperparameters() {
Ok(()) => Ok(fitted),
Err(err) => Err((fitted.into_trainer(), err)),
},
Err(err) => Err((self, err)),
}
}
}
impl<O, P> Sgpr<O, FreeInducing, P>
where
P: GpScalar,
O: Clone + for<'a> Optimizer<SgprObjective<'a, O, FreeInducing, P>>,
{
#[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<FittedSgpr<O, FreeInducing, 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,
self.optimizer.clone(),
)) {
Ok(mut fitted) => match fitted.optimize_hyperparameters() {
Ok(()) => Ok(fitted),
Err(err) => Err((fitted.into_trainer(), err)),
},
Err(err) => Err((self, err)),
}
}
}
impl<I: InducingLayout<NoSupply>, P> Sgpr<Fixed, I, 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<FittedSgpr<Fixed, I, 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,
Fixed,
)) {
Ok(fitted) => Ok(fitted),
Err(err) => Err((self, err)),
}
}
}