nuts_rs/
model.rs

1//! Core abstractions for MCMC models.
2//!
3//! Provides the `Model` trait which defines the interface for MCMC models,
4//! including the math backend and initialization methods needed for sampling.
5
6use anyhow::Result;
7use rand::Rng;
8
9use crate::math_base::Math;
10
11/// Trait for MCMC models with associated math backend and initialization.
12///
13/// Defines the interface for models that can be used with MCMC sampling algorithms.
14/// Provides access to mathematical operations needed for sampling and methods for
15/// initializing the sampling position.
16///
17/// The trait is thread-safe to enable parallel sampling scenarios.
18pub trait Model: Send + Sync + 'static {
19    /// The math backend used by this MCMC model.
20    ///
21    /// Specifies which math implementation will be used for computing log probability
22    /// densities, gradients, and other operations required during sampling.
23    ///
24    /// The lifetime parameter allows the math backend to borrow from the model instance.
25    type Math<'model>: Math
26    where
27        Self: 'model;
28
29    /// Returns the math backend for this model.
30    fn math<R: Rng + ?Sized>(&self, rng: &mut R) -> Result<Self::Math<'_>>;
31
32    /// Initializes the starting position for MCMC sampling.
33    ///
34    /// Sets initial values for the parameter vector. The starting position should
35    /// be in a reasonable region where the log probability density is finite.
36    fn init_position<R: Rng + ?Sized>(&self, rng: &mut R, position: &mut [f64]) -> Result<()>;
37}