Welford online variance tracks a stream’s count, running mean and centered sum of squares. With those three values, the next observation can update both the mean and the scatter without rereading the earlier values. The centered sum has a name here, M2. It permits two different summaries at the end of the same calculation, depending on the meaning of the denominator.
After n observations, the mean is their arithmetic average. M2 is the sum of each observation’s squared difference from that mean. With no observations, store a count of zero and treat the mean as unreported. The first finite observation sets the mean and leaves M2 at zero. Every subsequent observation changes the mean first, and the change in M2 uses the old and new mean.
Let x be the incoming value and delta be x minus the old mean. Increase the count to n. The new mean equals the old mean plus delta divided by n. Add delta times the difference between x and that new mean to M2. The two differences serve distinct roles. When the mean moves, all earlier observations have their deviations recentered, so taking only the square of the new observation’s deviation would miss part of the change.
Use the invented readings 4, 7 and 13. After reading 4, the state has count 1, mean 4 and M2 equal to 0. On adding 7, delta is 3, the new mean is 5.5, and the increment to M2 is 3 times 1.5, or 4.5. On adding 13, delta is 7.5, the new mean is 8, and the increment is 7.5 times 5, or 37.5. The final M2 is 42.

Recomputing from the batch verifies it. Subtract the final mean of 8 from each reading to get −4, −1 and 5. The squares are 16, 1 and 25. They add to the same 42. The same check works for each prefix. For 4 and 7 the deviations from 5.5 are −1.5 and 1.5, whose squared sum is 4.5.
When describing the three values as the entire set of interest, the average squared deviation is 42 divided by 3, or 14. When reporting the ordinary sample variance with Bessel’s correction, divide by 3 minus 1 to get 21. The two reports share the same M2. Their difference is the statistical interpretation of the denominator. If the values were measured in millimetres, both variances have units of square millimetres. The corresponding standard deviations are the square roots and return to the original units.

One observation has zero population variance under this convention. An ordinary corrected sample variance requires at least two observations. For an empty stream, both results are absent. Keep that distinction in saved output so a missing statistic cannot be mistaken for a stable sensor. A filtering policy, such as whether to omit nonfinite values or invalidate a window, also affects the count and should be recorded.
In exact arithmetic, the population variance equals the average of the squares minus the square of the average. With ordinary floating-point arithmetic, a large constant baseline and small differences can leave those two large intermediate terms too close to subtract accurately. The centered update works with the differences to running means. It usually retains detail better in this situation, although values that already rounded to the same machine number contain no recoverable variation.
For example, add the same exact integer offset to 4, 7 and 13. In exact arithmetic the mean moves by that offset and all deviations stay the same. A numerical test can compare the shifted stream with a centered two-pass reference and inspect the stored input precision. A test with a modest offset does not establish how a specific device or import format stores much larger values.
This state describes every accepted observation since its initialization. For daily numbers, initialize a new accumulator at the boundary and keep the sample count in the record. For a sliding last-hour window, old observations must leave as well as new ones enter. A simple append-only accumulator cannot make that change on its own. On replay, use the same record acceptance policy and compare count, mean and M2 before checking the final variance.
For a resumable import, save the three state values together with the position of the last accepted record. If an older checkpoint is restored, resume from the record after that saved position. An observation processed twice increases the count and generally changes both the mean and the scatter. Keeping the progress marker with the numerical state also helps identify which observations belong to a given reported interval.
Source notes. Philippe Pébay’s Sandia report describes incremental recurrences for centered moments. The statnet.common reference describes a Welford accumulator with running mean, sample size and squared deviations. The observations and step-by-step calculation above are original.
Explore related measurement methods in the engineering resources and technical article directory.