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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//! Gaussian observation noise, stored as `θ = log(σn²)`.
use crate::error::GprError;
use crate::param::{BoundedParam, Interval};
/// Represents the Gaussian likelihood with observation noise variance `σn²`.
///
/// The optimizer parameter is `θ = log(σn²)`, so positivity is unconstrained.
/// Differentiating the kernel matrix with respect to that parameter gives
/// `∂K/∂θ = σn² I`, not `2 σn I`.
///
/// Observation noise lives here. Do not also add a large white kernel term;
/// that double-counts noise. Cholesky jitter ([`crate::JitterPolicy`]) is a
/// separate numerical stabilizer and is not this parameter.
///
/// # Examples
///
/// ```rust
/// use gprx::GaussianLikelihood;
///
/// # fn main() -> Result<(), gprx::GprError> {
/// let lik = GaussianLikelihood::new(0.25)?;
/// let mut diag = [1.0, 1.0];
/// lik.add_noise_diag(&mut diag);
/// # let _ = diag;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GaussianLikelihood {
noise_variance: BoundedParam,
}
impl GaussianLikelihood {
/// Builds a likelihood from a positive finite noise variance `σn²`.
///
/// # Errors
///
/// Returns [`GprError::InvalidNoiseVariance`] if `noise_variance` is not
/// finite or not strictly positive.
///
/// See the example on [`GaussianLikelihood`].
pub fn new(noise_variance: f64) -> Result<Self, GprError> {
if !noise_variance.is_finite() {
return Err(invalid_noise("noise variance must be finite"));
}
if noise_variance <= 0.0 {
return Err(invalid_noise("noise variance must be positive"));
}
Ok(Self {
noise_variance: BoundedParam::default_positive(noise_variance)?,
})
}
/// Builds a likelihood from `θ = log(σn²)`.
///
/// # Errors
///
/// Returns [`GprError::InvalidNoiseVariance`] if `θ` is not finite, if
/// `exp(θ)` overflows to a non-finite variance, or if `exp(θ)` underflows
/// to zero.
///
/// See the example on [`GaussianLikelihood`].
pub fn from_log_noise_variance(log_noise_variance: f64) -> Result<Self, GprError> {
let log_noise_variance = validate_log_noise_variance(log_noise_variance)?;
Ok(Self {
noise_variance: BoundedParam::default_positive(log_noise_variance.exp())?,
})
}
/// Returns `σn² = exp(θ)`.
///
/// See the example on [`GaussianLikelihood`].
pub fn noise_variance(&self) -> f64 {
self.noise_variance.value()
}
/// Returns `θ = log(σn²)`.
///
/// See the example on [`GaussianLikelihood`].
pub fn log_noise_variance(&self) -> f64 {
self.noise_variance.ln()
}
/// Returns the open interval on `σn²`.
///
/// See the example on [`GaussianLikelihood`].
pub fn bounds(&self) -> Interval {
self.noise_variance.interval()
}
/// Rebuilds this likelihood with a new interval on `σn²`.
///
/// # Errors
///
/// Returns [`crate::IntervalError`] if the current `σn²` is not strictly
/// inside `interval`.
///
/// See the example on [`GaussianLikelihood`].
pub fn with_bounds(self, interval: Interval) -> Result<Self, crate::IntervalError> {
Ok(Self {
noise_variance: self.noise_variance.with_interval(interval)?,
})
}
/// Returns the number of optimizer parameters (always 1).
///
/// See the example on [`GaussianLikelihood`].
pub fn num_params(&self) -> usize {
1
}
/// Writes `θ` into a length-1 slice.
///
/// # Errors
///
/// Returns [`GprError::LengthMismatch`] if `out` is not length 1.
///
/// See the example on [`GaussianLikelihood`].
pub fn get_params(&self, out: &mut [f64]) -> Result<(), GprError> {
crate::data::require_count(out.len(), 1, "likelihood parameter")?;
out[0] = self.noise_variance.ln();
Ok(())
}
/// Replaces `θ` from a length-1 slice.
///
/// # Errors
///
/// Returns [`GprError::LengthMismatch`] if `params` is not length 1,
/// or [`GprError::InvalidNoiseVariance`] if the new `θ` is invalid.
///
/// See the example on [`GaussianLikelihood`].
pub fn set_params(&mut self, params: &[f64]) -> Result<(), GprError> {
crate::data::require_count(params.len(), 1, "likelihood parameter")?;
let log_noise_variance = validate_log_noise_variance(params[0])?;
self.noise_variance =
BoundedParam::new(log_noise_variance.exp(), self.noise_variance.interval())?;
Ok(())
}
/// Adds `σn²` to each entry of a kernel diagonal (implements `K += σn² I`).
///
/// See the example on [`GaussianLikelihood`].
pub fn add_noise_diag(&self, k_diag: &mut [f64]) {
let noise = self.noise_variance();
for entry in k_diag {
*entry += noise;
}
}
/// Writes the diagonal of `∂K/∂θ_i`.
///
/// For `i = 0` this is `σn²` in every entry.
///
/// # Errors
///
/// Returns [`GprError::IndexOutOfRange`] if `param_idx` is not 0.
///
/// See the example on [`GaussianLikelihood`].
pub fn noise_grad_diag(&self, dk_diag: &mut [f64], param_idx: usize) -> Result<(), GprError> {
if param_idx != 0 {
return Err(GprError::IndexOutOfRange {
reason: "likelihood has a single parameter at index 0".to_owned(),
});
}
let dkd_theta = self.noise_variance();
for entry in dk_diag {
*entry = dkd_theta;
}
Ok(())
}
}
fn invalid_noise(reason: &'static str) -> GprError {
GprError::InvalidNoiseVariance {
reason: reason.to_owned(),
}
}
fn validate_log_noise_variance(theta: f64) -> Result<f64, GprError> {
if !theta.is_finite() {
return Err(invalid_noise("log noise variance must be finite"));
}
let variance = theta.exp();
if !variance.is_finite() {
return Err(invalid_noise(
"noise variance overflowed to a non-finite value",
));
}
if variance <= 0.0 {
return Err(invalid_noise("noise variance underflowed to zero"));
}
Ok(theta)
}
#[cfg(test)]
mod tests {
use super::GaussianLikelihood;
use crate::error::GprError;
const TOL: f64 = 1e-10;
use crate::test_check::{assert_close, assert_send_sync};
#[test]
fn is_send_sync() {
assert_send_sync::<GaussianLikelihood>();
}
#[test]
fn add_noise_diag_adds_variance() {
let lik = GaussianLikelihood::new(0.25).expect("valid");
let mut diag = [1.0, 2.0, 3.0];
lik.add_noise_diag(&mut diag);
assert_close(diag[0], 1.25, TOL);
assert_close(diag[1], 2.25, TOL);
assert_close(diag[2], 3.25, TOL);
}
#[test]
fn noise_grad_diag_is_variance_not_twice_sigma() {
let variance = 0.25;
let lik = GaussianLikelihood::new(variance).expect("valid");
let mut diag = [0.0, 0.0];
lik.noise_grad_diag(&mut diag, 0).expect("index 0");
assert_close(diag[0], variance, TOL);
assert_close(diag[1], variance, TOL);
let twice_sigma = 2.0 * variance.sqrt();
assert!(
(diag[0] - twice_sigma).abs() > 0.1,
"must not use ∂K/∂σn = 2σn I"
);
}
#[test]
fn noise_grad_matches_finite_difference_of_variance() {
let lik = GaussianLikelihood::from_log_noise_variance(-1.5).expect("valid");
let theta = lik.log_noise_variance();
let h = 1e-6;
let plus = GaussianLikelihood::from_log_noise_variance(theta + h).expect("valid");
let minus = GaussianLikelihood::from_log_noise_variance(theta - h).expect("valid");
let fd = (plus.noise_variance() - minus.noise_variance()) / (2.0 * h);
let mut diag = [0.0];
lik.noise_grad_diag(&mut diag, 0).expect("index 0");
assert_close(diag[0], fd, TOL);
assert_close(diag[0], lik.noise_variance(), TOL);
}
#[test]
fn get_set_params_roundtrip() {
let mut lik = GaussianLikelihood::new(0.5).expect("valid");
let mut params = [0.0];
lik.get_params(&mut params).expect("len 1");
assert_close(params[0], 0.5_f64.ln(), TOL);
params[0] = 0.25_f64.ln();
lik.set_params(¶ms).expect("len 1");
assert_close(lik.noise_variance(), 0.25, TOL);
}
#[test]
fn rejects_non_positive_and_non_finite_variance() {
assert!(matches!(
GaussianLikelihood::new(0.0),
Err(GprError::InvalidNoiseVariance { .. })
));
assert!(matches!(
GaussianLikelihood::new(-1.0),
Err(GprError::InvalidNoiseVariance { .. })
));
assert!(matches!(
GaussianLikelihood::new(f64::NAN),
Err(GprError::InvalidNoiseVariance { .. })
));
assert!(matches!(
GaussianLikelihood::from_log_noise_variance(f64::INFINITY),
Err(GprError::InvalidNoiseVariance { .. })
));
assert!(matches!(
GaussianLikelihood::from_log_noise_variance(-800.0),
Err(GprError::InvalidNoiseVariance { .. })
));
let mut lik = GaussianLikelihood::new(1.0).expect("valid");
assert!(matches!(
lik.set_params(&[-800.0]),
Err(GprError::InvalidNoiseVariance { .. })
));
}
#[test]
fn noise_grad_rejects_other_param_index() {
let lik = GaussianLikelihood::new(1.0).expect("valid");
let mut diag = [0.0];
assert!(matches!(
lik.noise_grad_diag(&mut diag, 1),
Err(GprError::IndexOutOfRange { .. })
));
}
/// Subnormal noise is a positive finite value and is taken as is, inside
/// bounds that allow it (docs/design.md §4.0, #51).
#[test]
fn subnormal_noise_inside_widened_bounds_is_kept() {
let interval = crate::param::Interval::new(0.0, 1.0).expect("valid");
let mut likelihood = GaussianLikelihood::new(0.5)
.expect("valid")
.with_bounds(interval)
.expect("inside");
likelihood.set_params(&[-740.0]).expect("subnormal");
let noise = likelihood.noise_variance();
assert!(noise > 0.0 && !noise.is_normal(), "{noise:e}");
assert!(
GaussianLikelihood::new(noise).is_err(),
"outside the default interval"
);
}
}