From d0ede130579a000d3ec6188c8855eddf7bef4ed4 Mon Sep 17 00:00:00 2001 From: zietzm Date: Tue, 23 Jul 2024 19:19:03 -0500 Subject: [PATCH] fix: allow bad degrees-of-freedom --- src/stats/sumstats.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/stats/sumstats.rs b/src/stats/sumstats.rs index d376d94..0208e50 100644 --- a/src/stats/sumstats.rs +++ b/src/stats/sumstats.rs @@ -1,3 +1,4 @@ +use anyhow::Context; use statrs::distribution::ContinuousCDF; use statrs::distribution::StudentsT; @@ -9,7 +10,12 @@ pub fn compute_neg_log_pvalue(t_statistic: f32, degrees_of_freedom: i32) -> f32 f if f.is_nan() => f32::NAN, f if f.is_infinite() => f32::INFINITY, _ => { - let t_dist = StudentsT::new(0.0, 1.0, dof).unwrap(); + if dof <= 1.0 { + return f32::NAN; + } + let t_dist = StudentsT::new(0.0, 1.0, dof) + .with_context(|| format!("Failed to compute t-statistic for dof {}", dof)) + .unwrap(); let p = 2.0 * t_dist.cdf(-t.abs()); -p.log10() as f32 }