Skip to main content

lichtung_plot/
stats.rs

1//! Display-only statistics submodules.
2
3use statrs::distribution::{ContinuousCDF, StudentsT};
4
5/// KDE, quantiles, and other distribution primitives.
6pub mod distribution;
7/// OLS regression and related fit-line primitives.
8pub mod regression;
9
10pub(crate) fn student_t_critical_975(df: usize) -> Result<f64, &'static str> {
11    student_t_critical(df, 0.95)
12}
13
14#[expect(
15    clippy::cast_precision_loss,
16    reason = "an in-memory sample cannot approach the first usize not exactly representable as f64"
17)]
18pub(crate) fn student_t_critical(df: usize, level: f64) -> Result<f64, &'static str> {
19    if df == 0 {
20        return Err("Student-t critical value requires positive degrees of freedom");
21    }
22    if !level.is_finite() || level <= 0.0 || level >= 1.0 {
23        return Err("Student-t confidence level must be strictly between zero and one");
24    }
25    let distribution = StudentsT::new(0.0, 1.0, df as f64)
26        .map_err(|_| "could not construct Student-t distribution")?;
27    let critical = distribution.inverse_cdf(0.5 + level / 2.0);
28    if critical.is_finite() && critical > 0.0 {
29        Ok(critical)
30    } else {
31        Err("Student-t critical value is not finite and positive")
32    }
33}