Implement more and more logic.

This commit is contained in:
2020-02-16 12:32:14 +01:00
parent dd5667d82c
commit fd249da405
5 changed files with 287 additions and 16 deletions

View File

@@ -1,3 +1,5 @@
use ndarray::prelude::*;
mod constant;
mod exponential;
mod matern52;
@@ -6,6 +8,73 @@ pub use constant::Constant;
pub use exponential::Exponential;
pub use matern52::Matern52;
pub trait Kernel {}
pub trait Kernel {
fn k_diag(&self, ts: &[f64]) -> Array1<f64>;
fn order(&self) -> usize;
fn state_mean(&self, t: f64) -> Array1<f64>;
fn state_cov(&self, t: f64) -> Array2<f64>;
fn measurement_vector(&self) -> Array1<f64>;
fn transition(&self, t0: f64, t1: f64) -> Array2<f64>;
}
impl Kernel for Vec<Box<dyn Kernel>> {}
impl Kernel for Vec<Box<dyn Kernel>> {
fn k_diag(&self, ts: &[f64]) -> Array1<f64> {
self.iter()
.fold(Array1::zeros(ts.len()), |k_diag: Array1<f64>, kernel| {
k_diag + kernel.k_diag(ts)
})
}
fn order(&self) -> usize {
self.iter().map(|kernel| kernel.order()).sum()
}
fn state_mean(&self, t: f64) -> Array1<f64> {
let data = self
.iter()
.flat_map(|kernel| kernel.state_mean(t).to_vec().into_iter())
.collect::<Vec<f64>>();
Array1::from(data)
}
fn state_cov(&self, t: f64) -> Array2<f64> {
let data = self
.iter()
.map(|kernel| kernel.state_cov(t))
.collect::<Vec<_>>();
let dim = data
.iter()
.fold((0, 0), |(w, h), m| (w + m.ncols(), h + m.nrows()));
let mut cov = Array2::zeros(dim);
let mut r_d = 0;
let mut c_d = 0;
for m in data {
for ((r, c), v) in m.indexed_iter() {
cov[(r + r_d, c + c_d)] = *v;
}
r_d += m.nrows();
c_d += m.ncols();
}
cov
}
fn measurement_vector(&self) -> Array1<f64> {
let data = self
.iter()
.flat_map(|kernel| kernel.measurement_vector().to_vec().into_iter())
.collect::<Vec<f64>>();
Array1::from(data)
}
fn transition(&self, t0: f64, t1: f64) -> Array2<f64> {
todo!();
}
}