Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Welford's method

A way to keep a running mean and variance, one value at a time, without big sums that lose precision.

Welford's method updates n, the mean μ and M2 for each value x by δ = x - μ, μ += δ/n, M2 += δ(x - μ_new); the sample variance is M2/(n - 1). It needs three numbers of state and avoids catastrophic cancellation.

Examples

  • Adding 30, 32 and 31 one at a time gives μ = 31 and M2 = 2, so σ² = 2/(3 − 1) = 1.

  • The fan's detector keeps only n, μ and M2 per feature, updating once a window without storing past readings.

Read the full section →