use thiserror::Error;
use crate::error::GprError;
use crate::kernel::{KernelSpec, Supply};
use crate::likelihood::GaussianLikelihood;
#[derive(Clone, Copy, Debug, Error, PartialEq)]
#[non_exhaustive]
pub enum IntervalError {
#[error("interval bounds must be finite and satisfy lo < hi (got lo={lo}, hi={hi})")]
InvalidBounds {
lo: f64,
hi: f64,
},
#[error("value {value} is not strictly inside ({lo}, {hi})")]
OutOfRange {
value: f64,
lo: f64,
hi: f64,
},
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Interval {
lo: f64,
hi: f64,
}
impl Interval {
pub const DEFAULT_POSITIVE: Self = Self { lo: 1e-5, hi: 1e5 };
pub fn new(lo: f64, hi: f64) -> Result<Self, IntervalError> {
if lo.is_finite() && hi.is_finite() && lo < hi {
Ok(Self { lo, hi })
} else {
Err(IntervalError::InvalidBounds { lo, hi })
}
}
pub fn lo(self) -> f64 {
self.lo
}
pub fn hi(self) -> f64 {
self.hi
}
pub fn contains(self, value: f64) -> bool {
value.is_finite() && value > self.lo && value < self.hi
}
pub(crate) fn width(self) -> f64 {
self.hi - self.lo
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BoundedParam {
value: f64,
interval: Interval,
}
impl BoundedParam {
pub fn new(value: f64, interval: Interval) -> Result<Self, IntervalError> {
if interval.contains(value) {
Ok(Self { value, interval })
} else {
Err(IntervalError::OutOfRange {
value,
lo: interval.lo(),
hi: interval.hi(),
})
}
}
pub fn default_positive(value: f64) -> Result<Self, IntervalError> {
Self::new(value, Interval::DEFAULT_POSITIVE)
}
pub fn value(self) -> f64 {
self.value
}
pub fn interval(self) -> Interval {
self.interval
}
pub fn ln(self) -> f64 {
self.value.ln()
}
pub fn with_value(self, value: f64) -> Result<Self, IntervalError> {
Self::new(value, self.interval)
}
pub fn with_interval(self, interval: Interval) -> Result<Self, IntervalError> {
Self::new(self.value, interval)
}
}
pub(crate) fn write_params<S: Supply>(
kernel: &KernelSpec<S>,
likelihood: &GaussianLikelihood,
out: &mut [f64],
) -> Result<(), GprError> {
let n_kernel = kernel.num_params();
crate::data::require_count(out.len(), n_kernel + likelihood.num_params(), "parameters")?;
kernel.get_params(&mut out[..n_kernel])?;
likelihood.get_params(&mut out[n_kernel..])
}
#[cfg(test)]
mod tests {
use super::{BoundedParam, Interval, IntervalError};
use crate::test_check::assert_send_sync;
#[test]
fn is_send_sync() {
assert_send_sync::<Interval>();
assert_send_sync::<BoundedParam>();
assert_send_sync::<IntervalError>();
}
#[test]
fn new_rejects_non_finite_and_unordered() {
assert!(matches!(
Interval::new(f64::NAN, 1.0),
Err(IntervalError::InvalidBounds { .. })
));
assert!(matches!(
Interval::new(0.0, f64::INFINITY),
Err(IntervalError::InvalidBounds { .. })
));
assert!(matches!(
Interval::new(1.0, 1.0),
Err(IntervalError::InvalidBounds { .. })
));
assert!(matches!(
Interval::new(2.0, 1.0),
Err(IntervalError::InvalidBounds { .. })
));
}
#[test]
fn bounded_param_rejects_endpoint_and_outside() {
let interval = Interval::DEFAULT_POSITIVE;
assert!(matches!(
BoundedParam::new(interval.lo(), interval),
Err(IntervalError::OutOfRange { .. })
));
assert!(matches!(
BoundedParam::new(interval.hi(), interval),
Err(IntervalError::OutOfRange { .. })
));
assert!(matches!(
BoundedParam::new(1e6, interval),
Err(IntervalError::OutOfRange { .. })
));
let param = BoundedParam::default_positive(1.0).expect("inside");
assert!((param.value() - 1.0).abs() < 1e-15);
assert!((param.ln() - 1.0_f64.ln()).abs() < 1e-15);
}
}