Skip to main content

Module anomaly

Module anomaly 

Source
Expand description

Anomaly / outlier detection — folded into the statistics foundation (it is just statistics with a decision rule). Univariate detectors (z-score, robust modified z-score, Tukey fences, Grubbs’ test) plus a multivariate Mahalanobis gate. Each reuses the existing descriptive / robust / distribution primitives and follows the module’s Option idiom (None on degenerate input — empty, zero spread).

Mission note: outliers are flagged as candidates for human attention, never auto-acted-on — a deviation is a signal, not a verdict.

Structs§

GrubbsResult
The result of Grubbs’ test for a single outlier.

Functions§

grubbs_test
Grubbs’ test for the single most extreme value (assumes approximate normality). alpha is the significance level (e.g. 0.05). Reuses the Student-t quantile for the critical value. None if fewer than 3 points or zero spread.
iqr_outliers
Indices outside the Tukey fences.
is_multivariate_outlier
Multivariate outlier gate: a point is flagged when its squared Mahalanobis distance exceeds the χ²(d) upper-alpha quantile (the standard d-dimensional rule). None on a dimension mismatch.
mahalanobis_sq
Squared Mahalanobis distance (x − μ)ᵀ Σ⁻¹ (x − μ). The caller supplies the inverse covariance inv_cov (row-major d×d, obtained from the linear-algebra substrate) — this keeps the metric self-contained. None on a dimension mismatch.
modified_z_score_outliers
Robust outliers via the modified z-score (Iglewicz–Hoaglin): 0.6745 · (x − median) / MAD. Resistant to the very outliers it detects — the mean and SD are not. Flags indices exceeding threshold (3.5 is the standard choice).
tukey_fences
Tukey fences [Q1 − k·IQR, Q3 + k·IQR] (k = 1.5 mild, 3.0 extreme). None if fewer than 4 points.
z_score_outliers
Indices whose standard score |x − μ| / σ exceeds threshold (e.g. 3.0). None if there are fewer than 2 points or the spread is zero.