Z-Score, Robust Z-Score, and Standardization
Install and import#
npm install fintech-algorithmsimport { zScoreRobustZScoreAndStandardization } from "fintech-algorithms/foundations/dispersion-shape-and-robust-statistics/z-score-robust-z-score-and-standardization";Signature#
zScoreRobustZScoreAndStandardization(input)Standardises every observation twice: once against the mean and sample standard deviation, and once against the median and median absolute deviation scaled by 0.67448975.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | { values: number[] } | The observations to standardise, under the key values. Both output arrays stay aligned to the caller's input order.values: at least two observations, and not all identical |
Returns#
{ zScores: number[]; robustZScores: number[] }
zScores holds (value - mean) / sampleStandardDeviation, and robustZScores holds 0.67448975 * (value - median) / mad, with every entry set to 0 when the median absolute deviation is zero.
Errors#
- When
inputis null, an array, or not an object — throws TypeError - When
valuesis missing, is not an array, or is empty — throws RangeError - When any entry of
valuesdoes not coerce to a finite number — throws RangeError - When
valuesholds fewer than two observations, so the sample divisorn - 1would be zero — throws RangeError - When the sample standard deviation is zero because every observation is identical — throws RangeError
Complexity: time O(n log n),
space O(n).
Worked example#
verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.
Input#
{
"values": [1, 2, 2, 4, 9]
}Call#
zScoreRobustZScoreAndStandardization(input)Returns#
object with 2 fields: zScores, robustZScores
{
"zScores": [
-0.8101300387046826,
-0.4985415622798047,
-0.4985415622798047,
0.12463539056995114,
1.6825777726943407
],
"robustZScores": [-0.67448975, 0, 0, 1.3489795, 4.72142825]
}Diagrams#
How it works#
This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.
References#
- Measures of Scale — NIST/SEMATECH e-Handbook
- Skewness and Kurtosis — NIST/SEMATECH e-Handbook
- Historical-example decision