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
131
132
133
134
135
136
137
138
mod extendable_matrix;
pub use extendable_matrix::{EMatrix, EVector};
use crate::parameters::kernel::Kernel;
use nalgebra::{storage::Storage, Cholesky, DMatrix, DVector, Dynamic, Matrix, SliceStorage, U1};
pub type SMatrix<S> = Matrix<f64, Dynamic, Dynamic, S>;
pub type SRowVector<S> = Matrix<f64, U1, Dynamic, S>;
pub type SVector<S> = Matrix<f64, Dynamic, U1, S>;
pub type MatrixSlice<'a> =
Matrix<f64, Dynamic, Dynamic, SliceStorage<'a, f64, Dynamic, Dynamic, U1, Dynamic>>;
pub type VectorSlice<'a> =
Matrix<f64, Dynamic, U1, SliceStorage<'a, f64, Dynamic, U1, U1, Dynamic>>;
pub fn make_covariance_matrix<
S1: Storage<f64, Dynamic, Dynamic>,
S2: Storage<f64, Dynamic, Dynamic>,
K: Kernel,
>(
m1: &SMatrix<S1>,
m2: &SMatrix<S2>,
kernel: &K,
) -> DMatrix<f64> {
DMatrix::<f64>::from_fn(m1.nrows(), m2.nrows(), |r, c| {
let x = m1.row(r);
let y = m2.row(c);
kernel.kernel(&x, &y)
})
}
pub fn make_cholesky_cov_matrix<S: Storage<f64, Dynamic, Dynamic>, K: Kernel>(
inputs: &SMatrix<S>,
kernel: &K,
diagonal_noise: f64,
) -> Cholesky<f64, Dynamic> {
let mut covmatix = DMatrix::<f64>::from_element(inputs.nrows(), inputs.nrows(), std::f64::NAN);
for (col_index, x) in inputs.row_iter().enumerate() {
for (row_index, y) in inputs.row_iter().enumerate().skip(col_index) {
covmatix[(row_index, col_index)] = kernel.kernel(&x, &y);
}
covmatix[(col_index, col_index)] += diagonal_noise * diagonal_noise;
}
covmatix.cholesky().expect("Cholesky decomposition failed!")
}
pub fn add_rows_cholesky_cov_matrix<S: Storage<f64, Dynamic, Dynamic>, K: Kernel>(
covmat_cholesky: &mut Cholesky<f64, Dynamic>,
all_inputs: &SMatrix<S>,
nb_new_inputs: usize,
kernel: &K,
diagonal_noise: f64,
) {
let nb_old_inputs = all_inputs.nrows() - nb_new_inputs;
let new_inputs = all_inputs.rows(nb_old_inputs, nb_new_inputs);
for (row_index, row) in new_inputs.row_iter().enumerate() {
let col_index = nb_old_inputs + row_index;
let column_size = col_index + 1;
let mut new_column = DVector::<f64>::from_fn(column_size, |training_row_index, _| {
let training_row = all_inputs.row(training_row_index);
kernel.kernel(&training_row, &row)
});
new_column[col_index] += diagonal_noise * diagonal_noise;
*covmat_cholesky = covmat_cholesky.insert_column(col_index, new_column);
}
}
pub fn make_gradient_covariance_matrices<S: Storage<f64, Dynamic, Dynamic>, K: Kernel>(
inputs: &SMatrix<S>,
kernel: &K,
) -> Vec<DMatrix<f64>> {
let mut covmatrices: Vec<_> = (0..kernel.nb_parameters())
.map(|_| DMatrix::<f64>::from_element(inputs.nrows(), inputs.nrows(), std::f64::NAN))
.collect();
for (col_index, x) in inputs.row_iter().enumerate() {
for (row_index, y) in inputs.row_iter().enumerate().skip(col_index) {
for (&grad, mat) in kernel.gradient(&x, &y).iter().zip(covmatrices.iter_mut()) {
mat[(row_index, col_index)] = grad;
mat[(col_index, row_index)] = grad;
}
}
}
covmatrices
}