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.
skewnessis the Fisher-Pearson coefficientg1 = m3 / m2^(3/2)(scipy.stats.skew,bias=true). The bias-corrected sample skewnessG1 = sqrt(n(n-1)) / (n-2) * g1is selected withbias = false(scipy.stats.skew,bias=false).kurtosisis the Fisher (excess) kurtosisg2 = m4 / m2^2 - 3by default (fisher = true, matchingscipy.stats.kurtosis); passfisher = falsefor the Pearson definitionm4 / m2^2(no-3). The bias-corrected estimator (scipy.stats.kurtosis,bias=false) is selected withbias = false.- The central moments are the population (biased) moments
m_k = (1/n) sum_i (x_i - xbar)^k, exactly asscipy.stats._momentforms them. jarque_beraisJB = n/6 * (S^2 + K^2/4)withSthe biased skewness andKthe biased excess kurtosis, and a chi-square(2) survivalp = exp(-JB/2)(the closed form ofscipy.stats.distributions.chi2.sfat two degrees of freedom), matchingscipy.stats.jarque_bera.shapiro_wilkis a direct double-precision port of Royston’s Remark AS R94 (1995), the same algorithmscipy.stats.shapirouses.
§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§
- Jarque
Bera - Jarque-Bera goodness-of-fit test against normality.
- Moment
Stats - Sample mean, variance, skewness, and kurtosis of a residual set.
- Shapiro
Wilk - Shapiro-Wilk normality test on a residual set.
Enums§
- Normality
Error - 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.shapirouses. - skewness
- Sample skewness of a residual set.