Skip to main content

gam_problem/
finite_validation.rs

1//! Shared finite-value validation helpers for estimation/result contracts.
2
3use crate::EstimationError;
4use ndarray::Array1;
5
6pub fn ensure_finite_scalar_estimation(
7    name: &str,
8    value: f64,
9) -> Result<(), EstimationError> {
10    if value.is_finite() {
11        Ok(())
12    } else {
13        Err(EstimationError::InvalidInput(format!(
14            "{name} must be finite, got {value}"
15        )))
16    }
17}
18
19pub fn validate_all_finite_estimation<I>(
20    label: &str,
21    values: I,
22) -> Result<(), EstimationError>
23where
24    I: IntoIterator<Item = f64>,
25{
26    for (idx, value) in values.into_iter().enumerate() {
27        if !value.is_finite() {
28            return Err(EstimationError::InvalidInput(format!(
29                "{label}[{idx}] must be finite, got {value}"
30            )));
31        }
32    }
33    Ok(())
34}
35
36#[inline]
37pub fn bail_if_cached_beta_non_finite(beta: &Array1<f64>) -> Result<(), EstimationError> {
38    if beta.iter().any(|v| !v.is_finite()) {
39        return Err(EstimationError::InvalidInput(
40            "cached inner beta contains non-finite entries".to_string(),
41        ));
42    }
43    Ok(())
44}
45
46/// Public wrapper returning `String` errors for use outside the estimation module.
47pub fn ensure_finite_scalar(name: &str, value: f64) -> Result<(), String> {
48    ensure_finite_scalar_estimation(name, value).map_err(|err| err.to_string())
49}
50
51/// Public wrapper returning `String` errors for use outside the estimation module.
52pub fn validate_all_finite<I: IntoIterator<Item = f64>>(
53    label: &str,
54    values: I,
55) -> Result<(), String> {
56    validate_all_finite_estimation(label, values).map_err(|err| err.to_string())
57}