Online Variance and Parallel Moment Merging

Maintain centered squared deviations as observations arrive and merge summaries with different means without subtracting two large squares.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Recenter when the sample mean changes
  2. A merge corrects for the difference between group means

Recenter when the sample mean changes

Keep count \(n\), mean \(\mu\), and \(M_2=\sum_i(x_i-\mu)^2\). For new value \(x\), compute \(\delta=x-\mu\), update the mean by \(\delta/(n+1)\), and add \(\delta(x-\mu_{\mathrm{new}})\) to \(M_2\). The formula follows by expanding the change of center and using the zero sum of old centered deviations. Return population variance \(M_2/n\) or sample variance \(M_2/(n-1)\) under the desired definition.

A merge corrects for the difference between group means

With two nonempty groups, add their individual \(M_2\) values and a correction \((\mu_a-\mu_b)^2 n_an_b/(n_a+n_b)\). For groups \((1,1)\) and \((3,3)\), their separate deviations vanish while the correction gives four. Mergeable summaries explain why counts and means must travel together. Stable arithmetic motivates avoiding a subtraction of nearly equal squared quantities.[1][2]

References

  1. Tyler Akidau et al., The Dataflow Model ↩
  2. Apache Beam, Programming Guide ↩