Skip to content
Oakfield Operator Calculus Function Reference Site

Welford Online Statistics

Given samples x1,,xnx_1,\dots,x_n, maintain mean μn\mu_n and accumulated second moment M2nM2_n via

δ=xnμn1,μn=μn1+δn,M2n=M2n1+δ(xnμn)\delta = x_n - \mu_{n-1},\qquad \mu_n = \mu_{n-1} + \frac{\delta}{n},\qquad M2_n = M2_{n-1} + \delta (x_n - \mu_n)

initialized with μ0=0\mu_0 = 0, M20=0M2_0 = 0, n=0n=0. The variance is σn2=M2n/n\sigma_n^{2} = M2_n / n (population) or M2n/(n1)M2_n/(n-1) (unbiased, n>1n>1).

Applies to real or complex data streams with finite second moments. For complex data with variance defined as σ2=EXEX2\sigma^2=\mathbb{E}|X-\mathbb{E}X|^2, a common update is

M2n=M2n1+δ(xnμn)M2_n = M2_{n-1} + \delta\,\overline{(x_n-\mu_n)}

which keeps M2nM2_n real and nonnegative.


Single-pass and numerically stable; M2nM2_n accumulates squared deviations without storing history. Order-invariant: results depend only on the multiset of samples, not their order.


For constant data, variance remains zero. As nn \to \infty for stationary processes with finite variance, estimates converge almost surely to true moments.


  • Constant stream yields zero variance.
  • Suitable for long streams where two-pass variance would be numerically fragile or memory-intensive.

Exponential moving averages provide biased but rapidly adapting moments; basic variance formulas require two passes or stored history.


Oakfield uses Welford-style online accumulation in its runtime field statistics engine:

  • SimFieldStatsAccumulator updates mean and M2 in a single pass over field data (numerically stable, streaming-friendly).
  • The resulting means/variances (plus magnitude moments) feed UI diagnostics and downstream metrics without storing full histories.

Welford’s update is a classic numerically stable one-pass method for mean and variance computation and is widely used in streaming statistics.

It is a standard building block for online diagnostics and adaptive methods that must track variance without storing history.


  • Welford, “Note on a Method for Calculating Corrected Sums of Squares and Products” (1962)
  • Chan, Golub, LeVeque, “Algorithms for Computing the Sample Variance” (1983)