Skip to main content

Module normality

Module normality 

Source
Expand description

Residual-distribution diagnostics: sample moments and normality tests.

Post-fit residuals from a converged least-squares solve should look like zero-mean Gaussian noise. These primitives quantify departures from that ideal on an arbitrary residual set: sample skewness and kurtosis, the Jarque-Bera moment test, and the Shapiro-Wilk W test.

§Conventions and references

The moment definitions match scipy.stats so a caller can cross-check against the reference implementation.

  • skewness is the Fisher-Pearson coefficient g1 = m3 / m2^(3/2) (scipy.stats.skew, bias=true). The bias-corrected sample skewness G1 = sqrt(n(n-1)) / (n-2) * g1 is selected with bias = false (scipy.stats.skew, bias=false).
  • kurtosis is the Fisher (excess) kurtosis g2 = m4 / m2^2 - 3 by default (fisher = true, matching scipy.stats.kurtosis); pass fisher = false for the Pearson definition m4 / m2^2 (no -3). The bias-corrected estimator (scipy.stats.kurtosis, bias=false) is selected with bias = false.
  • The central moments are the population (biased) moments m_k = (1/n) sum_i (x_i - xbar)^k, exactly as scipy.stats._moment forms them.
  • jarque_bera is JB = n/6 * (S^2 + K^2/4) with S the biased skewness and K the biased excess kurtosis, and a chi-square(2) survival p = exp(-JB/2) (the closed form of scipy.stats.distributions.chi2.sf at two degrees of freedom), matching scipy.stats.jarque_bera.
  • shapiro_wilk is a direct double-precision port of Royston’s Remark AS R94 (1995), the same algorithm scipy.stats.shapiro uses.

§Reproducibility

The reductions here are plain left-to-right f64 folds. scipy/numpy reduce with pairwise summation, so agreement is to a tight tolerance rather than bit-for-bit (observed against scipy 1.18.0): < 1e-12 relative on the moment statistics, and ~1e-10 on the Shapiro-Wilk W with ~1e-9 on its p-value. scipy 1.18.0 adjusted its Shapiro-Wilk path, widening the gap from this AS R94 port by ~5e-11 (W) / ~5e-10 (p) relative to earlier releases; both stay well inside the test tolerances. The polynomial and rational approximations inside the Shapiro-Wilk path use the same constants as the scipy translation.

Structs§

JarqueBera
Jarque-Bera goodness-of-fit test against normality.
MomentStats
Sample mean, variance, skewness, and kurtosis of a residual set.
ShapiroWilk
Shapiro-Wilk normality test on a residual set.

Enums§

NormalityError
Why a residual-distribution diagnostic could not be computed.

Functions§

jarque_bera
Jarque-Bera normality test on a residual set.
kurtosis
Sample kurtosis of a residual set.
moments
Mean, variance, skewness, and kurtosis in one pass.
shapiro_wilk
Shapiro-Wilk W test for normality, a port of Royston’s Remark AS R94 (1995), the algorithm scipy.stats.shapiro uses.
skewness
Sample skewness of a residual set.