Stable Online Variance and Welford's Algorithm
Install and import#
npm install fintech-algorithmsimport { stableOnlineVarianceAndWelfordsAlgorithm } from "fintech-algorithms/foundations/statistical-computing-and-reproducibility/stable-online-variance-and-welford-s-algorithm";Signature#
stableOnlineVarianceAndWelfordsAlgorithm(input)Runs Welford's single-pass update to produce the mean and sample variance without ever accumulating a sum of raw squares.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | D00Input | Reads values, a list of at least two finite numbers. |
Returns#
D00Output
count is the number of observations consumed, mean the running mean after the last of them, m2 the accumulated sum of squared deviations, and sampleVariance that sum divided by count minus one.
Errors#
- When
valuesis absent, empty, or holds a non-finite number — throws RangeError - When
valuesholds fewer than two observations — throws RangeError
Complexity: time O(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, 3, 4, 5],
"floatingValue": 0.1,
"maxSafeMagnitude": 1.7976931348623157e+308,
"window": 3,
"rawValues": [1, null, 2, "bad", 3],
"invalidPolicy": "drop-and-report",
"seed": 42,
"sampleCount": 5,
"leftVector": [
{
"index": "A",
"value": 1
},
{
"index": "B",
"value": 2
}
],
"rightVector": [
{
"index": "B",
"value": 20
},
{
"index": "C",
"value": 30
}
],
"train": [10, 12, 14, 16],
"test": [18, 20],
"actual": [1, 2.0000001, 3],
"expected": [1, 2, 3]
}Showing 14 of 18 fields.
Call#
stableOnlineVarianceAndWelfordsAlgorithm(input)Returns#
object with 2 fields: count, mean
{
"count": 5,
"mean": 3
}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#
- Floating-Point Arithmetic — IEEE 754-2019
- math.fsum — Python Documentation
- Random Sampling — NumPy Documentation
- Common Pitfalls and Recommended Practices — scikit-learn
- Historical-example decision