fetish_lib/
sigma_points.rs

1extern crate ndarray;
2extern crate ndarray_linalg;
3
4use ndarray::*;
5
6use crate::schmear::*;
7use crate::feature_space_info::*;
8use crate::sqrtm::*;
9
10///Gets the canonical set of 2n + 1 sigma points (see: unscented transform)
11///for the given [`Schmear`]. These points are chosen in such a way
12///that their mean and covariance match that of the given schmear.
13pub fn get_sigma_points(in_schmear : &Schmear) -> Vec<Array1<f32>> {
14    let mean = &in_schmear.mean;
15    let n = mean.shape()[0];
16
17    let mut covariance_sqrt = sqrtm(&in_schmear.covariance);
18    let n_sqrt = (n as f32).sqrt();  
19    covariance_sqrt *= n_sqrt;
20
21    let mut result = Vec::new();
22    result.push(mean.clone());
23    for i in 0..n {
24        let covariance_col = covariance_sqrt.column(i);
25        let plus_vec = mean + &covariance_col;
26        let minus_vec = mean - &covariance_col;
27        result.push(plus_vec);
28        result.push(minus_vec);
29    }
30
31    result
32}
33
34///Given some points, yields a [`Schmear`] with their mean and (sample) covariance.
35pub fn sigma_points_to_schmear(in_points : &Vec<Array1<f32>>) -> Schmear {
36    Schmear::from_sample_vectors(in_points)
37}
38
39///Given a [`Schmear`] in a compressed space and some [`FeatureSpaceInfo`], computes
40///an estimated featurized [`Schmear`] using an unscented transform on the canonical set
41///of 2n + 1 sigma points.
42pub fn unscented_transform_schmear(in_schmear : &Schmear, feat_space_info : &FeatureSpaceInfo) -> Schmear {
43    let in_sigma_points = get_sigma_points(in_schmear);
44    let mut out_sigma_points = Vec::new();
45    for in_sigma_point in in_sigma_points {
46        let out_sigma_point = feat_space_info.get_features(in_sigma_point.view());
47        out_sigma_points.push(out_sigma_point);
48    }
49    
50    let result = sigma_points_to_schmear(&out_sigma_points);
51    result
52}