assert_residuals_normal

Macro assert_residuals_normal 

Source
macro_rules! assert_residuals_normal {
    ($fit:expr $(, strict = $strict:expr)?) => { ... };
    ($fit:expr, $tolerance:expr $(, strict = $strict:expr)? $(, $msg:literal $(, $($args:tt),*)?)?) => { ... };
}
Expand description

Asserts that the residuals (the differences between the observed and predicted values) of a fit are normally distributed.

This means the errors are likely random, not based on some undiscovered pattern.

See crate::statistics::residual_normality for more details.

  • Results will be between 0.0 and 1.0, with values closer to 1.0 indicating a better fit.

§Parameters

  • $fit: A reference to the CurveFit object whose residuals will be tested.
  • $tolerance: Minimum p-value for normality. Defaults to 0.1 if omitted.
  • strict: (optional) If true, uses unfiltered residuals. Defaults to false. Use strict mode if you want to include all residuals, even those close to zero. Normally, small residuals are due to floating-point noise and are filtered out.

§Behavior

  • Computes mean, standard deviation, skewness, and excess kurtosis of residuals.
  • If p-value < $tolerance, the macro will:
    1. Optionally generate a failure plot (if the plotting feature is enabled).
    2. Panic with a clear error message indicating skew/kurtosis values.

§Example

function!(test(x) = 20.0 + 3.0 x^1 + 2.0 x^2 + 4.0 x^3 );
let data = test.solve_range(0.0..=1000.0, 1.0);

let fit = ChebyshevFit::new_auto(&data, DegreeBound::Relaxed, &Aic).expect("Failed to create model");

// Uses default tolerance 0.1
assert_residuals_normal!(fit);

// Strict mode uses unfiltered residuals - even floating point noise can cause failure
assert_residuals_normal!(fit, 0.00, strict = true);