fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Z-Score, Robust Z-Score, and Standardization

Install and import#

bash
npm install fintech-algorithms
ts
import { 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#

NameTypeNotes
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 input is null, an array, or not an object — throws TypeError
  • When values is missing, is not an array, or is empty — throws RangeError
  • When any entry of values does not coerce to a finite number — throws RangeError
  • When values holds fewer than two observations, so the sample divisor n - 1 would 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#

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#

Z-Score, Robust Z-Score, and Standardization — article hero
Z-Score, Robust Z-Score, and Standardization — calculation ledger
Z-Score, Robust Z-Score, and Standardization — concept anatomy
Z-Score, Robust Z-Score, and Standardization — failure boundary
Z-Score, Robust Z-Score, and Standardization — method map
Z-Score, Robust Z-Score, and Standardization — scenario contrast

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.

Read the article →

References#

The rest of the Dispersion, Shape, and Robust Statistics family#