Skip to main content

numra_stats/
error.rs

1//! Error types for statistical computations.
2//!
3//! Author: Moussa Leblouba
4//! Date: 9 February 2026
5//! Modified: 2 May 2026
6
7use core::fmt;
8
9/// Errors that can occur during statistical computations.
10#[derive(Clone, Debug, PartialEq)]
11pub enum StatsError {
12    /// Empty data set.
13    EmptyData,
14    /// Data sets have mismatched lengths.
15    LengthMismatch { expected: usize, got: usize },
16    /// Invalid parameter value.
17    InvalidParameter(String),
18    /// Dimension mismatch.
19    DimensionMismatch(String),
20    /// Singular matrix encountered.
21    SingularMatrix,
22    /// Computation did not converge.
23    ConvergenceFailure(String),
24}
25
26impl fmt::Display for StatsError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            StatsError::EmptyData => write!(f, "empty data set"),
30            StatsError::LengthMismatch { expected, got } => {
31                write!(f, "length mismatch: expected {}, got {}", expected, got)
32            }
33            StatsError::InvalidParameter(msg) => write!(f, "invalid parameter: {}", msg),
34            StatsError::DimensionMismatch(msg) => write!(f, "dimension mismatch: {}", msg),
35            StatsError::SingularMatrix => write!(f, "singular matrix"),
36            StatsError::ConvergenceFailure(msg) => write!(f, "convergence failure: {}", msg),
37        }
38    }
39}
40
41impl From<StatsError> for numra_core::NumraError {
42    fn from(e: StatsError) -> Self {
43        numra_core::NumraError::Stats(e.to_string())
44    }
45}