Skip to main content

laddu_extensions/likelihood/
term.rs

1use std::collections::HashMap;
2
3use dyn_clone::DynClone;
4use laddu_core::{amplitude::ParameterMap, LadduResult};
5use nalgebra::DVector;
6
7use super::LikelihoodExpression;
8
9/// A trait which describes a term that can be used like a likelihood (more correctly, a negative
10/// log-likelihood) in a minimization.
11pub trait LikelihoodTerm: DynClone + Send + Sync {
12    /// Evaluate the term given some input parameters.
13    fn evaluate(&self, parameters: &[f64]) -> LadduResult<f64>;
14    /// Evaluate the gradient of the term given some input parameters.
15    fn evaluate_gradient(&self, parameters: &[f64]) -> LadduResult<DVector<f64>>;
16    /// Fix a named parameter local to this term.
17    fn fix_parameter(&self, _name: &str, _value: f64) -> LadduResult<()> {
18        Ok(())
19    }
20    /// Mark a named parameter local to this term as free.
21    fn free_parameter(&self, _name: &str) -> LadduResult<()> {
22        Ok(())
23    }
24    /// Rename a single parameter local to this term.
25    fn rename_parameter(&self, _old: &str, _new: &str) -> LadduResult<()> {
26        Ok(())
27    }
28    /// Rename multiple parameters local to this term.
29    fn rename_parameters(&self, _mapping: &HashMap<String, String>) -> LadduResult<()> {
30        Ok(())
31    }
32    /// Return the parameters owned by this term in local order.
33    fn parameter_map(&self) -> ParameterMap {
34        ParameterMap::default()
35    }
36    /// A method called every step of any minimization/MCMC algorithm.
37    fn update(&self) {}
38
39    /// Convenience helper to wrap a likelihood term into a [`LikelihoodExpression`].
40    ///
41    /// This allows term constructors to return expressions without exposing the manager
42    /// machinery that previously performed registration.
43    fn into_expression(self) -> LadduResult<LikelihoodExpression>
44    where
45        Self: Sized + 'static,
46    {
47        LikelihoodExpression::from_term(Box::new(self))
48    }
49}
50
51dyn_clone::clone_trait_object!(LikelihoodTerm);