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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
pub mod regressor;
pub use rayon::prelude::*;
pub use regressor::*;
use super::{BaseEllipticalProcessParams, EllipticalProcessParams};
use crate::nonparametric::{ey, kernel_matrix, EllipticalProcessError};
use crate::{opensrdk_linear_algebra::*, RandomVariable};
use crate::{DistributionError, EllipticalParams};
use ey::y_ey;
use opensrdk_kernel_method::*;
use opensrdk_linear_algebra::matrix::ge::sy_he::po::trf::POTRF;
#[derive(Clone, Debug)]
pub struct ExactEllipticalProcessParams<K, T>
where
K: PositiveDefiniteKernel<T>,
T: RandomVariable,
{
base: BaseEllipticalProcessParams<K, T>,
mu: Vec<f64>,
lsigma: POTRF,
sigma_inv_y: Matrix,
mahalanobis_squared: f64,
}
impl<K, T> ExactEllipticalProcessParams<K, T>
where
K: PositiveDefiniteKernel<T>,
T: RandomVariable,
{
fn new(base: BaseEllipticalProcessParams<K, T>, y: &[f64]) -> Result<Self, DistributionError> {
let n = y.len();
if n == 0 {
return Err(DistributionError::InvalidParameters(
EllipticalProcessError::Empty.into(),
));
}
if n != base.x.len() {
return Err(DistributionError::InvalidParameters(
EllipticalProcessError::DimensionMismatch.into(),
));
}
let ey = ey(y);
let mu = vec![ey; base.x.len()];
let kxx = kernel_matrix(&base.kernel, &base.theta, &base.x, &base.x)?;
let sigma = kxx + vec![base.sigma.powi(2); n].diag();
let lsigma = sigma.potrf()?;
let y_ey = y_ey(y, ey).col_mat();
let y_ey_t = y_ey.t();
let sigma_inv_y = lsigma.potrs(y_ey)?;
let mahalanobis_squared = (y_ey_t * &sigma_inv_y)[(0, 0)];
Ok(Self {
base,
mu,
lsigma,
sigma_inv_y,
mahalanobis_squared,
})
}
}
impl<K, T> BaseEllipticalProcessParams<K, T>
where
K: PositiveDefiniteKernel<T>,
T: RandomVariable,
{
pub fn exact(self, y: &[f64]) -> Result<ExactEllipticalProcessParams<K, T>, DistributionError> {
ExactEllipticalProcessParams::new(self, y)
}
}
impl<K, T> RandomVariable for ExactEllipticalProcessParams<K, T>
where
K: PositiveDefiniteKernel<T>,
T: RandomVariable,
{
type RestoreInfo = ();
fn transform_vec(&self) -> (Vec<f64>, Self::RestoreInfo) {
todo!()
}
fn len(&self) -> usize {
todo!()
}
fn restore(v: &[f64], info: &Self::RestoreInfo) -> Result<Self, DistributionError> {
todo!()
}
}
impl<K, T> EllipticalParams for ExactEllipticalProcessParams<K, T>
where
K: PositiveDefiniteKernel<T>,
T: RandomVariable,
{
fn mu(&self) -> &Vec<f64> {
&self.mu
}
fn sigma_inv_mul(&self, v: Matrix) -> Result<Matrix, DistributionError> {
Ok(self.lsigma.potrs(v)?)
}
fn lsigma_cols(&self) -> usize {
self.lsigma.0.cols()
}
fn sample(&self, z: Vec<f64>) -> Result<Vec<f64>, DistributionError> {
Ok((self.mu[0] + &self.lsigma.0 * z.col_mat()).vec())
}
}
impl<K, T> EllipticalProcessParams<K, T> for ExactEllipticalProcessParams<K, T>
where
K: PositiveDefiniteKernel<T>,
T: RandomVariable,
{
fn mahalanobis_squared(&self) -> f64 {
self.mahalanobis_squared
}
}