Expanding \((x_i-\mu_n)^2\) and summing over \(i=1\ldots n-1\), the cross term in \((x_i-\mu_{n-1})\) vanishes because \(\sum_{i=1}^{n-1}(x_i-\mu_{n-1})=0\). After algebraic simplification and using \(\mu_n=\mu_{n-1}+\delta/n\) one obtains
\[
M2_n = M2_{n-1} + \delta\,(x_n-\mu_n).
\]
Initialization (streaming)
n = 0
mean = 0
M2 = 0
-- on first element x1:
n = 1
mean = x1
M2 = 0
From then on apply the incremental updates above for every new observation.
Numerical note (concrete example)
Suppose data are around \(10^8\) with very small variance (e.g. variance ≈ \(10^{-2}\)). The naive formula
\(\frac{1}{n-1}\big(\sum x_i^2 - n\bar x^2\big)\) subtracts two large numbers (order \(10^{16}\)) that differ by a small quantity , this causes catastrophic cancellation and large relative errors. Welford's updates avoid that subtraction and keep rounding error small relative to the data scale.
How to use (modes)
Step-by-step : enter values one at a time with Add value to see incremental updates.
Vector mode: paste a comma-separated list; the page processes values element-by-element (keeps streaming semantics but stores values for plotting).
Benchmark: compare Welford (stable) vs sum/sumsq (fast but numerically fragile).
Simulator — Input & Controls (online)
Current data: []
Results:
No calculation performed yet.
Implementation notes
This page implements Welford's algorithm precisely as derived above (delta-based updates) and compares it with the classical sum/sumsq formula. Inputs are filtered to finite numbers only.
Optional Homework — comparing online methods
Goal: prove the recurrences, test online algorithms, and show the numerical advantages of Welford over simple incremental sum & sumsq.
N = number of elements, Trials = how many runs to average. The benchmark reports ms per run and the absolute difference in variance: |Var_sum − Var_welford|.
Benchmark results (online methods)
Press Run benchmark to execute. Charts show timings and numeric differences against Welford.
Numerical discussion & summary
Main advantages of Welford's online algorithm:
Numerical stability: avoids cancellation and overflow/underflow issues typical of the naive sums formula.
Low memory: O(1) extra space — ideal for streams.
Mergeability: partial summaries (n, mean, M2) can be combined for distributed computation.