Skip to main content

uncertain_numerics/
bayesian_quadrature_error.rs

1use core::fmt;
2
3use crate::{ConditioningError, PosteriorError};
4
5/// Errors raised while constructing a Bayesian quadrature posterior.
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub enum BayesianQuadratureError {
8    /// No function observations were supplied.
9    EmptyObservations,
10    /// Observation nodes and function values have different lengths.
11    ObservationLengthMismatch,
12    /// At least one observation node is not finite.
13    NonFiniteObservationNode,
14    /// At least one observed function value is not finite.
15    NonFiniteObservationValue,
16    /// Gaussian conditioning failed.
17    Conditioning(ConditioningError),
18    /// The computed posterior could not be represented as a valid scalar Gaussian posterior.
19    Posterior(PosteriorError),
20    /// The posterior variance is negative by more than the documented roundoff tolerance.
21    MateriallyNegativePosteriorVariance {
22        /// Raw variance before roundoff handling.
23        value: f64,
24        /// Maximum negative magnitude treated as floating-point roundoff.
25        tolerance: f64,
26    },
27}
28
29impl fmt::Display for BayesianQuadratureError {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Self::EmptyObservations => write!(f, "at least one observation is required"),
33            Self::ObservationLengthMismatch => {
34                write!(f, "observation nodes and values must have equal lengths")
35            }
36            Self::NonFiniteObservationNode => {
37                write!(f, "observation nodes must be finite")
38            }
39            Self::NonFiniteObservationValue => {
40                write!(f, "observed function values must be finite")
41            }
42            Self::Conditioning(error) => write!(f, "Gaussian conditioning failed: {error}"),
43            Self::Posterior(error) => write!(f, "posterior construction failed: {error}"),
44            Self::MateriallyNegativePosteriorVariance { value, tolerance } => write!(
45                f,
46                "posterior variance {value:.16e} is negative beyond roundoff tolerance {tolerance:.16e}"
47            ),
48        }
49    }
50}
51
52impl std::error::Error for BayesianQuadratureError {
53    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54        match self {
55            Self::Conditioning(error) => Some(error),
56            Self::Posterior(error) => Some(error),
57            _ => None,
58        }
59    }
60}
61
62impl From<ConditioningError> for BayesianQuadratureError {
63    fn from(error: ConditioningError) -> Self {
64        Self::Conditioning(error)
65    }
66}
67
68impl From<PosteriorError> for BayesianQuadratureError {
69    fn from(error: PosteriorError) -> Self {
70        Self::Posterior(error)
71    }
72}