1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
pub mod exact;
pub mod ey;
pub mod kernel_matrix;
pub mod kiss_love;
pub mod regressor;
pub mod sparse;

pub use exact::*;
pub use ey::*;
pub use kernel_matrix::*;
pub use kiss_love::*;
pub use regressor::*;
pub use sparse::*;

use crate::{DistributionError, EllipticalParams, RandomVariable};
use opensrdk_kernel_method::*;

#[derive(thiserror::Error, Debug)]
pub enum EllipticalProcessError {
    #[error("Data is empty.")]
    Empty,
    #[error("Dimension mismatch.")]
    DimensionMismatch,
    #[error("NaN contaminated.")]
    NaNContamination,
}

/// You can use these methods:
/// - `exact`
/// - `sparse`
/// - `kiss_love`
#[derive(Clone, Debug)]
pub struct BaseEllipticalProcessParams<K, T>
where
    K: PositiveDefiniteKernel<T>,
    T: RandomVariable,
{
    kernel: K,
    x: Vec<T>,
    theta: Vec<f64>,
    sigma: f64,
}

impl<K, T> BaseEllipticalProcessParams<K, T>
where
    K: PositiveDefiniteKernel<T>,
    T: RandomVariable,
{
    /// - `kernel`: Kernel function
    /// - `x`: Input value
    /// - `theta`: Params of kernel function
    /// - `sigma`: White noise variance for a regularization likes Ridge regression
    pub fn new(
        kernel: K,
        x: Vec<T>,
        theta: Vec<f64>,
        sigma: f64,
    ) -> Result<Self, DistributionError> {
        if kernel.params_len() != theta.len() {
            return Err(DistributionError::InvalidParameters(
                EllipticalProcessError::DimensionMismatch.into(),
            ));
        }

        Ok(Self {
            kernel,
            x,
            theta,
            sigma,
        })
    }
}

pub trait EllipticalProcessParams<K, T>: EllipticalParams
where
    K: PositiveDefiniteKernel<T>,
    T: RandomVariable,
{
    fn mahalanobis_squared(&self) -> f64;
}