Skip to main content

laddu_likelihood/
error.rs

1use laddu_data::LadduDataError;
2use laddu_expr::parameters::ParamError;
3use laddu_runtime::RuntimeError;
4use thiserror::Error;
5
6/// Result type returned by likelihood operations.
7pub type LikelihoodResult<T> = Result<T, LikelihoodError>;
8
9/// Error produced while constructing or evaluating a likelihood.
10#[derive(Clone, Debug, Error)]
11pub enum LikelihoodError {
12    /// Dataset access failed.
13    #[error(transparent)]
14    Data(#[from] LadduDataError),
15    /// Model preparation or execution failed.
16    #[error(transparent)]
17    Runtime(#[from] RuntimeError),
18    /// Parameter registration, validation, or lookup failed.
19    #[error(transparent)]
20    Params(#[from] ParamError),
21    /// A likelihood intensity was non-positive.
22    #[error("{dataset} intensity must be positive for a likelihood term, got {value}")]
23    NonPositiveIntensity {
24        /// Dataset on which the invalid intensity occurred.
25        dataset: &'static str,
26        /// Invalid intensity value.
27        value: f64,
28    },
29    /// The accepted Monte Carlo integral was non-positive.
30    #[error("accepted MC integral must be positive for acceptance correction, got {0}")]
31    NonPositiveAcceptedIntegral(f64),
32    /// A cross-section calculation received non-positive luminosity.
33    #[error("luminosity must be positive for a cross section, got {0}")]
34    NonPositiveLuminosity(f64),
35    /// Two likelihood terms used the same name.
36    #[error("duplicate likelihood term name: {0}")]
37    DuplicateTermName(String),
38    /// No likelihood term had the requested name.
39    #[error("unknown likelihood term: {0}")]
40    MissingTerm(String),
41    /// The requested term was not an intensity term.
42    #[error("likelihood term is not an intensity term: {0}")]
43    NotIntensityTerm(String),
44    /// The requested term does not determine an absolute intensity scale.
45    #[error("likelihood term does not determine an absolute rate: {0}")]
46    AbsoluteRateUnavailable(String),
47    /// A term referenced a parameter absent from the global layout.
48    #[error("likelihood term {term} references unknown parameter {parameter}")]
49    MissingParameter {
50        /// Likelihood term name.
51        term: String,
52        /// Missing parameter name.
53        parameter: String,
54    },
55    /// A regularization term received an invalid weight.
56    #[error("likelihood term {term} has invalid penalty weight {lambda}")]
57    InvalidPenaltyWeight {
58        /// Penalty term name.
59        term: String,
60        /// Invalid regularization weight.
61        lambda: f64,
62    },
63    /// Parameter values belong to a different layout.
64    #[error("parameter values were built for a different likelihood parameter layout")]
65    ParameterLayoutMismatch,
66    /// A destination gradient had the wrong length.
67    #[error("gradient has length {actual}, expected {expected}")]
68    GradientLengthMismatch {
69        /// Expected gradient length.
70        expected: usize,
71        /// Actual gradient length.
72        actual: usize,
73    },
74    /// A likelihood term was used before resolution.
75    #[error("likelihood term has not been resolved: {0}")]
76    UnresolvedTerm(String),
77    /// A stochastic batch fraction was outside `(0, 1]`.
78    #[error("stochastic batch fraction must be in (0, 1], got {0}")]
79    InvalidBatchFraction(f64),
80    /// A cross-section analysis input or operation was invalid.
81    #[error("invalid cross-section analysis: {0}")]
82    InvalidCrossSection(String),
83}