prodef 0.1.0

A simple Rust crate for handling probability distributions.
Documentation
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]

pub mod domain;
pub mod multinormal;
pub mod multivariate;
pub mod particle;

use domain::Domain;
use nalgebra::{DefaultAllocator, Dim, OVector, Scalar, VectorView, allocator::Allocator};
use rand::Rng;

/// A trait that is shared by all probability density functions.
pub trait Density<T, D>: Clone
where
    T: PartialOrd + Scalar,
    D: Dim,
    DefaultAllocator: Allocator<D>,
{
    /// Calculates, or estimates, a density value for a sample.
    /// Returns [`None`] if the sample is outside of the function domain.
    fn density<RStride: Dim, CStride: Dim>(
        &self,
        sample: &VectorView<T, D, RStride, CStride>,
    ) -> Option<T>;

    /// Returns a copy of the underlying function [`Domain`].
    fn domain(&self) -> impl Domain<T, D> + 'static;

    /// Returns the `center` of the probability density function.
    ///
    /// Typically, this is either the mean value or the maximum.
    fn center(&self) -> OVector<T, D>;

    /// Returns a boolean vector indicating which dimensions are constant.
    fn is_constant(&self) -> OVector<bool, D>;

    /// Draw a random sample from the probability density distribution.
    ///
    /// This function is limited to a given maximum number of sampling attempts,
    /// and returns [`None`] if the threshold is exceeded.
    fn sample(&self, rng: &mut impl Rng, max_attempts: usize) -> Option<OVector<T, D>>;
}