The arithmetic underneath

The formula sets the power, the sum sets the constant

The one-pass variance was found losing three to sixty-nine times more than the digits-lost rule allows, and the explanation offered — operands built from a thousand roundings — was never priced. Measured against the exact variance of the stored data, the rule's ratio is the variance's own condition number squared, and the formula decides what power of it the error grows with: two for every one-pass variant, one for Welford's update, none for two passes or a shift. The summation decides only the constant in front — about 0.4√n for running sums, one for pairwise or compensated ones — and the sixty-nine was neither: it belonged to the random numbers.

Worth reading first: Cancellation takes the answer, not a digit · Orthogonal is a number · The order they are added in.

Cancellation takes the answer, not a digit put a rule beside the subtraction it was about. If a computation forms a small quantity ss as the difference of operands of size mm, the relative error in ss is about u m/∣s∣u\,m/|s|. For a single subtraction of correctly rounded operands the rule held, and was conservative by between two and a half and twenty-eight times. For the one-pass variance — the mean of the squares minus the square of the mean — it failed in the dangerous direction, under-predicting by 11, 11, 69 and 46 times on four spreads of a thousand values near 10610^6. The explanation given was that the operands of that subtraction were not stored numbers but the output of a thousand-term accumulation, so u mu\,m was the wrong estimate of the error they carried.

That is true, and it leaves the factor unpriced. Eleven and sixty-nine are not the same number, the essay did not say where between them the next dataset would land, and nothing said which part of the failure belongs to the formula and which to the sums inside it.

The rule’s ratio is a condition number

For the one-pass variance the operands are 1n∑xi2\frac1n\sum x_i^2 and xˉ 2\bar x^{\,2}, and the difference is the variance vv. So the rule’s m/∣s∣m/|s| is the mean square over the variance:

m∣s∣  =  xˉ 2+vv  =  1+xˉ 2v  =  κ2.\frac{m}{|s|} \;=\; \frac{\bar x^{\,2} + v}{v} \;=\; 1 + \frac{\bar x^{\,2}}{v} \;=\; \kappa^2 .

κ\kappa is the condition number of the variance with respect to the data: how far a relative change in the values can move it. A sample whose mean is a thousand standard deviations from zero has κ=1000\kappa = 1000, and its variance is determined only to about κ u\kappa\,u by the data whatever is done with it — but every formula below is applied to data stored exactly, so the question is purely what the arithmetic adds. The rule says the one-pass formula adds κ2u\kappa^2 u.

To measure that without a reference that carries its own error, every value is taken as the exact rational it is — a double is a 53-bit integer times a power of two — and ∑xi\sum x_i and ∑xi2\sum x_i^2 are formed exactly in integer arithmetic before one division to sixty bits — the move an answer that is known makes for a linear system, applied to two sums. The earlier essay compared against the two-pass formula, which is accurate to a few units of roundoff and, measured here, never more than 43 off on its data; that was good enough for its table, and it is not good enough to separate a constant of one from a constant of three.

Six formulas against κ

Six ways of computing the same number, each in the working precision at every operation: the one-pass formula with running sums, as written in every textbook and many instruction sets; the same formula with both sums formed pairwise; the same with both sums compensated; the one-pass formula applied to every value minus the first; Welford’s running-mean update; and two passes, the mean first and then the squares of the deviations. Each is applied to a thousand Gaussian values of standard deviation one and mean κ2−1\sqrt{\kappa^2-1}, eleven seeds, the median error drawn.

The relative error of six variance formulas, in units of the unit roundoff, against the variance's condition number squared, on 1,000 Gaussian values at 53 bitsMedian over eleven seeds; κ squared is one plus the mean squared over the variance. At κ squared = 3.2·10¹⁴: one-pass 2.33·10¹⁵u, one-pass, pairwise sums 4.42·10¹⁴u, one-pass, compensated sums 3.22·10¹⁴u, shifted one-pass 11.7u, Welford 2.67·10⁶u, two-pass 4.01u. The one-pass formula's error is about 7κ²u, the pairwise and compensated sums about κ²u, Welford's update about 0.15κu, and the shifted and two-pass formulas do not grow with κ. The ceiling is a relative error of one, every digit gone.53 bits, κ² 3.2·10¹⁴one-pass ÷ κ²u7.4compensated ÷ κ²u1Welford ÷ κu0.15two-pass, in u4110²10⁴10⁶10⁸10¹⁰10¹²10¹⁴110²10⁴10⁶10⁸10¹⁰10¹²10¹⁴10¹⁶κ² = 1 + mean² ÷ variancerelative error ÷ uevery digit goneκ²uone-passcompensated or pairwiseWelfordshifted by x₁two-passdashed purple: κ²u, the digits-lost rulethe formula sets the power of κ
Fig. 1 Six formulas for the variance of a thousand values, in double precision, against κ2\kappa^2. The one-pass formula runs parallel to the dashed κ²u line and about seven times above it; with pairwise or compensated sums it sits on the line. Welford’s update climbs at half the slope. Two passes and the shifted formula stay at a few units of roundoff across fourteen decades. The dial changes the precision and moves nothing but the ceiling.

The picture is four slopes, not six curves. Everything built on the one-pass formula climbs with slope one in κ2\kappa^2 — the square of the condition number — whatever is done to the sums inside it. At κ2=3.2⋅1014\kappa^2 = 3.2\cdot 10^{14} the running-sum version is 7.4 κ2u7.4\,\kappa^2 u from the answer and the compensated one exactly 1.0 κ2u1.0\,\kappa^2 u. Welford’s update climbs with slope one half: its error divided by κu\kappa u sits between 0.06 and 1.2 across the range. And the two formulas that subtract the mean before squaring do not climb at all. Two passes end at 4 u4\,u at the top of the range, where the one-pass formula is wrong by a quarter of the answer.

So the rule was right about the exponent. What it called “the operands’ size over the result’s” is a property of the formula: any formula that squares values first and subtracts the squares afterwards forms a difference whose operands are κ2\kappa^2 times the result, and spends κ2\kappa^2. Any formula that subtracts first squares numbers the size of the answer and spends nothing.

The dial does not change that. Above κ2=10\kappa^2 = 10, at 16, 24 and 32 bits, the one-pass constant is 3 to 26 times κ2u\kappa^2 u, the compensated one 0.3 to 1.3, Welford’s 0.07 to 1.7 times κu\kappa u, and two passes 3 to 8 uu — the same bands double precision gives. What the precision moves is where the one-pass formula runs out — at roughly κ2=1/(10u)\kappa^2 = 1/(10u), which is 1.7⋅1061.7\cdot 10^6 in single precision — and where the stored data stops having a spread worth measuring at all.

The constant in front belongs to the sum

That leaves the constant: seven at the top of one sweep, eleven and sixty-nine in the earlier essay’s table. Hold κ2\kappa^2 at 10810^8 and change the number of values.

Each variance formula's error divided by the power of κ it spends, against the number of values, at κ squared = 10⁸ in double precisionn = 100: one-pass 2.1κ²u, pairwise 1.29κ²u, compensated 0.76κ²u, Welford 0.38κu, two-pass 1.6u; n = 300: one-pass 6.7κ²u, pairwise 0.47κ²u, compensated 0.53κ²u, Welford 0.24κu, two-pass 3.5u; n = 1000: one-pass 14.5κ²u, pairwise 1.02κ²u, compensated 0.72κ²u, Welford 0.19κu, two-pass 4.2u; n = 3000: one-pass 24.7κ²u, pairwise 1.64κ²u, compensated 0.89κ²u, Welford 0.24κu, two-pass 10.3u; n = 10000: one-pass 38.4κ²u, pairwise 0.92κ²u, compensated 0.73κ²u, Welford 0.15κu, two-pass 15.9u; n = 30000: one-pass 53.6κ²u, pairwise 1.04κ²u, compensated 1.18κ²u, Welford 0.17κu, two-pass 13.9u; n = 100000: one-pass 111.1κ²u, pairwise 0.99κ²u, compensated 0.81κ²u, Welford 0.43κu, two-pass 73.1u. Medians over fifteen seeds, seven at the two largest lengths. The dashed line is 0.4 times the square root of n.10²10³10⁴10⁵10⁻¹110¹10²number of values, nerror ÷ the power of κ it spends0.4√none-pass ÷ κ²utwo-pass, in ucompensated ÷ κ²uWelford ÷ κurunning sums carry √n; pairwise and compensated do notthe sum sets the constant
Fig. 2 Each formula’s error divided by the power of κ it spends, from a hundred to a hundred thousand values. The one-pass constant follows 0.4n0.4\sqrt{n} from 2.1 to 111. With pairwise or compensated sums it stays between 0.5 and 1.7 at every length. Welford’s constant stays between 0.15 and 0.43. Two passes grow like n\sqrt{n} too — their second sum is a running sum.

The one-pass constant is 0.4n0.4\sqrt n to within the scatter of fifteen seeds: 2.1 at a hundred values, 14.5 at a thousand, 38 at ten thousand, 111 at a hundred thousand. That is the running sum’s own random walk. Each addition in ∑xi2\sum x_i^2 rounds a partial sum near k xˉ 2k\,\bar x^{\,2}, the errors take both signs, and their total grows like the square root of the count — the same half-power three walks and one bound found under a left-to-right sum, a chain of rotations and a residual recurrence. The one-pass formula then multiplies that walk by κ2\kappa^2, because the subtraction exposes it.

Sum pairwise and the walk is gone: a tree of depth log⁡2n\log_2 n has nothing long enough to wander, and the constant is 0.5 to 1.7 at every length from a hundred to a hundred thousand. Compensated summation does the same thing by carrying each rounding forward. Neither does anything to κ2\kappa^2.

Two passes grow like n\sqrt n as well — 1.6 uu at a hundred values, 73 at a hundred thousand — and that is not a contradiction. Their second pass sums the squared deviations with a running sum, and that walk is there to be seen. It is simply not multiplied by anything, because the quantities being summed are already the size of the answer.

So the factor the earlier essay left unexplained has two parts, and they come from different lines of the program. The κ2\kappa^2 is the formula’s: it squares before it subtracts. The 0.4n0.4\sqrt n is the summation’s: it walks. A thousand values give 0.41000=12.60.4\sqrt{1000} = 12.6, against the 11.1 and 11.4 the earlier table found at its two narrowest spreads.

A better sum restores the rule and no more

There is a piece of advice attached to the one-pass variance almost as often as the warning against it: if the formula must be used, sum it accurately. The measurement says exactly what that buys.

It buys the n\sqrt n. With compensated sums the error is 0.50.5 to 1.2 κ2u1.2\,\kappa^2 u at every length and 0.30.3 to 1.3 κ2u1.3\,\kappa^2u at every precision — the digits-lost rule, met to its constant. It does not buy anything below the rule, and there is a reason that has nothing to do with how well the sums are formed. Even an exactly computed ∑xi2\sum x_i^2 has to be rounded once to be stored, and so does (∑xi)2/n(\sum x_i)^2/n; each of those roundings is uu relative to a quantity κ2\kappa^2 times the variance. Two numbers each wrong by uκ2vu\kappa^2 v cannot be subtracted to anything better. At κ2=1012\kappa^2 = 10^{12} the compensated formula is still 4.7⋅1011 u4.7\cdot 10^{11}\,u from the exact variance, where two passes over the same data are six.

A summation that is perfect inside a formula that squares first is the rule, exactly. That is an improvement of 0.4n0.4\sqrt n, which for a hundred thousand values is a factor of 130, and it is worth having when a single pass is forced. It is not a repair.

The sixty-nine was the data

Thirty-six times κ2u\kappa^2 u is the largest constant forty-one seeds of a well-mixed random generator give at a thousand values near 10610^6 with a spread of one. The earlier essay’s 69 at a spread of one, and 46 at a spread of ten, sit outside that.

Rerunning its exact recipe — its 31-bit linear congruential generator, values 106+(r−12)10^6 + (r - \tfrac12), a thousand of them — against the exact variance reproduces its numbers to the digit: 11.1, 11.4, 68.9 and 45.5. Then running the same recipe from forty-one seeds, and the same mean and spread from the well-mixed generator:

The one-pass variance's error over κ²u on forty-one seeds of each of two random-number generators, at mean 10⁶ and spread 1 with 1,000 values, and with compensated sumsThe earlier essay's 31-bit congruential generator: constants 47.7 to 87.3, median 62.0, 41 of 41 over-estimates. A well-mixed generator, same mean and spread: 0.7 to 36.4, median 11.8, 16 of 41 over-estimates. With compensated sums the medians are 0.58 and 0.48.0.010.1110100earlier essay's generator, running sumswell-mixed generator, running sumsearlier essay's generator, compensatedwell-mixed generator, compensatedrelative error ÷ κ²u, one dot a seed; bar: medianrunning sums: filled dot over-estimates, open dot underthe line at one is the digits-lost rulethe sixty-nine was the data
Fig. 3 The one-pass constant on forty-one seeds from each source, a thousand values at mean 10⁶ and spread 1. The earlier essay’s generator gives 48 to 87 and over-estimates the variance on every seed. The well-mixed one gives 0.7 to 36, median 12, over-estimating on sixteen. Compensated sums put both under one.

The earlier essay’s data does not scatter around 0.4n0.4\sqrt{n}. Its constant is 48 to 87 with a median of 62, and on all forty-one seeds the formula over-estimates. The well-mixed generator, asked for the same mean and the same spread, gives 0.7 to 36 with a median of 12, and over-estimates on sixteen seeds and under-estimates on twenty-five — the signs a random walk should have. With compensated sums the two generators are indistinguishable: both below one.

A constant five times the walk and one sign on forty-one seeds is not a walk. Splitting the error by sum says where it lives.

Where the lean is: the relative error of the running sums Σx and Σx², in units of u, on nine seeds from each source at mean 10⁶ and spread 1Σx, earlier generator: -32.2, -32.2, -31.1, -30.1, -38.7, -30.1, -31.1, -31.1, -23.6; Σx, well-mixed generator: 11.8, -3.2, 4.3, -7.5, 5.4, -10.7, -3.2, -1.1, 4.3; Σx², earlier generator: 4.5, -9.0, 0.0, -4.5, -6.8, 1.1, -6.8, 0.0, 2.3; Σx², well-mixed generator: -6.8, -14.6, -7.9, -4.5, -2.3, 3.4, 3.4, 2.3, -10.1. Each value's own square is rounded too, and those errors are under a hundredth of u of the sum.-45-35-25-15-5515relative error of the running sum ÷ uΣx, earlier generatorΣx, well-mixed generatorΣx², earlier generatorΣx², well-mixed generatorΣx enters the variance squared, so its lean counts twicethe earlier generator's Σx always falls short
Fig. 4 The running sums’ own relative errors, in units of u, nine seeds from each source. On the earlier essay’s data Σx falls short on every seed by 24 to 39 u; on the well-mixed data it lands either side of zero. Σx² behaves the same on both.

The lean is in ∑xi\sum x_i. On the congruential data the running sum of the values falls short by 24 to 39 units of roundoff on every one of nine seeds, while on the well-mixed data it scatters from −11-11 to +12+12. ∑xi2\sum x_i^2 looks alike on both. Since ∑xi\sum x_i enters the formula squared and subtracted, a sum that is always short makes a variance that is always long, by about twice the shortfall times κ2\kappa^2 — which is the 62.

That is the phenomenon the direction the error leans measured with the rounding mode as the cause. Under round-to-nearest the errors of ten thousand operations combined with an exponent of 0.47; under rounding toward infinity, 1.01, because every error had the same sign. Here the mode is round-to-nearest throughout and the errors lean anyway. The data, not the arithmetic, is supplying the bias. A coin flip that fixes the average is the same fact from the other side: errors that share a sign accumulate in proportion to their number, and rounding at random is what gave them back both signs there.

What about the data does it is not established here. The obvious candidate fails: the generator’s values sit on a lattice of 2−312^{-31}, two bits coarser than the double grid at 10610^6 that what a float can hold draws, but the well-mixed values quantised to the same lattice give a median of 12.9 and no lean. Shuffling the congruential values with an unrelated generator leaves the median at 56, so the lean is in the multiset of values and not in their order. The low-order bits of a power-of-two congruential generator are known to be far from random — the lowest bit alternates — and the bits a running sum near 10910^9 discards are exactly those; that is the mechanism a reader would reach for, and it has not been tested.

The useful fact does not depend on it. A one-pass variance’s constant is 0.4n0.4\sqrt n only when the rounding errors of its sums are free to cancel, and nothing guarantees that for data with structure in its low bits. The earlier essay happened to draw its examples from such a source. A compensated sum does not care: it removes the walk and the lean together.

A shift is a smaller κ

Two of the six formulas never climbed. The shifted one-pass formula is the instructive one, because it is the one-pass formula: it squares first and subtracts afterwards, exactly as the failing version does. It is applied to xi−x1x_i - x_1 instead of xix_i.

That changes κ\kappa, not the formula. The variance of xi−Kx_i - K is the variance of xix_i, and the shifted data’s mean is xˉ−K\bar x - K, so its condition number is 1+(xˉ−K)2/v1 + (\bar x - K)^2/v. Shift by the first value and xˉ−x1\bar x - x_1 is about one standard deviation, so κ2\kappa^2 is about two whatever the original was.

The one-pass variance computed after shifting every value by a number t standard deviations from the mean, drawn against one plus t squared, beside the unshifted formula against κ squared, at κ squared = 10⁸ with 1,000 valuesshift at the mean: 5.94u; shift 1 standard deviations from it: 13.6u; shift 10 standard deviations from it: 619u; shift 100 standard deviations from it: 1.78·10⁵u; shift 1000 standard deviations from it: 10⁷u; shift 10⁴ standard deviations from it: 1.26·10⁹u; shift 10⁵ standard deviations from it: 1.08·10¹¹u; shift 10⁶ standard deviations from it: 1.1·10¹³u. Past a shift of 100 the error over one plus t squared is 10.0 to 17.8, the constant the unshifted formula carries at this length.110²10⁴10⁶10⁸10¹⁰10¹²110³10⁶10⁹10¹²10¹⁵κ² unshifted, or 1 + t² for a shift t standard deviations from the meanrelative error ÷ uunshiftedshiftedlarge dots: the shift t; small: the unshifted sweepa shift is a smaller κ
Fig. 5 The one-pass formula after shifting by a number t standard deviations from the mean, drawn against 1+t21 + t^2, on top of the unshifted formula drawn against κ2\kappa^2. The two lie on one line: a shift of a hundred standard deviations behaves exactly like data whose mean is a hundred standard deviations from zero.

Shift by a number tt standard deviations from the mean, with the data’s own κ2\kappa^2 held at 10810^8, and the error lands on the unshifted curve at κ2=1+t2\kappa^2 = 1 + t^2. Past t=100t = 100 it is 10 to 18 times (1+t2) u(1+t^2)\,u, the same constant the unshifted formula carries at a thousand values, and it climbs with slope two in tt to within 0.3 at every step. A shift at the mean costs 6 uu, one standard deviation away 14. A shift a million standard deviations off is no shift at all.

So the cheapest repair of the one-pass formula is not a better sum and not a second pass. It is any value near the data — the first one, a running guess, last month’s mean — subtracted before squaring. The formula is unchanged and still spends κ2\kappa^2; the shift chooses which κ\kappa it spends.

Welford spends one power

Welford’s update keeps a running mean mkm_k and a running sum of squared deviations, and adds (xk−mk−1)(xk−mk)(x_k - m_{k-1})(x_k - m_k) at each step. It never forms ∑xi2\sum x_i^2. It still climbs, with slope one half in κ2\kappa^2 — slope one in κ\kappa — and its constant is 0.15 to 0.43 at every length from a hundred to a hundred thousand.

The mechanism is one rounding in the wrong place. The running mean is a number of size xˉ\bar x, so it carries an absolute error of about u xˉu\,\bar x, which is uκu\kappa standard deviations. Every deviation xk−mkx_k - m_k is formed against that mean and inherits a relative error of uκu\kappa, and so does each product added to the sum. The errors do not compound, because each step starts from the values rather than from the previous deviation — which is why the constant does not grow with nn — but they do not shrink either.

That makes Welford the formula of choice where a single pass is forced and the data cannot be shifted: it costs a division and two subtractions per value more than the one-pass formula and spends a single power of κ\kappa where the one-pass formula spends two. At κ=104\kappa = 10^4, a mean ten thousand standard deviations from zero, the one-pass formula is 1.2⋅109 u1.2\cdot 10^9\,u from the answer and Welford’s 1.9⋅103 u1.9\cdot 10^3\,u: seven correct digits against twelve.

What each formula costs, and what it spends

formula passes power of κ constant at n values
one-pass, running sums 1 2 about 0.4n0.4\sqrt n, and more on data whose sums lean
one-pass, pairwise or compensated sums 1 2 about 1
one-pass on xi−x1x_i - x_1 1 0 0.2 to 0.4n0.4\sqrt n
Welford’s update 1 1 0.15 to 0.43
two passes 2 0 0.1 to 0.2n0.2\sqrt n

Read down the third column and the formulas sort themselves; read down the fourth and the sums do. The two columns answer different questions and the earlier essay’s rule answered only the first. The order they are added in is about the fourth column; the condition number is an amplifier is about what the third column multiplies.

The same split is the one the vector that hides it found in a parallel inner product. The summation condition number of the data decides how much of the reduction’s walk reaches the answer; the reduction decides how long the walk is. Positive values sat at the safe end there, with condition number one. The one-pass variance is the opposite: its two sums are of positive numbers and well conditioned on their own, and it is the subtraction afterwards that hands their error a factor of κ2\kappa^2.

What a thousand Gaussian values do not show

Every sweep here uses Gaussian data of unit variance, and every formula is evaluated with each operation rounded once to the working precision, the way the formats are specified. Data with heavy tails could make the running sum’s walk lopsided — a single large value dominates its partial sums — and the constant would no longer be a clean 0.4n0.4\sqrt n. Hardware that accumulates in a wider register than it stores, as some vector units do, would be expected to shrink the one-pass constant toward the compensated one without changing the power; that is not measured here. And the congruential generator’s lean is measured on one mean and one spread; how it varies with either is untested, which is the first question below.

Still open: the low bits, a pairwise Welford, and a mean that moves

What the congruential data supplies. The lean sits in ∑xi\sum x_i, survives shuffling, and is absent from the well-mixed generator on the same lattice. The prediction with a sign is that replacing only the lowest five bits of each congruential value with bits from an unrelated generator removes the lean — the median constant falls from 62 to within a factor of 1.5 of the well-mixed 12, and the over-estimates fall from forty-one seeds to between ten and thirty — and that replacing only the top bits of the fraction leaves it. If the lean survives the low-bit replacement, it is in the distribution of the values rather than their bit patterns, and the explanation the low bits invite is wrong.

Welford combined pairwise. Welford’s update merges two partial results with a formula of Chan, Golub and LeVeque, and merging them as a tree is how a parallel reduction would compute a variance. The prediction is that the tree keeps Welford’s single power of κ\kappa and its constant stays under 0.5 — that the update’s error is set by the running mean’s rounding and not by the order of combination — while a tree of one-pass partials keeps two powers.

A mean that drifts. The shift by x1x_1 works because x1x_1 is within a standard deviation or two of the mean. A stream whose mean drifts by dd standard deviations over its length makes that shift stale. The prediction is that the shifted formula’s error then follows 1+d2/31 + d^2/3 in place of κ2\kappa^2 — the mean square distance of a linear drift from its start — and that re-shifting at every block of a hundred values, merging the blocks exactly, removes the drift’s cost for under one per cent more arithmetic.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A flat tag is an object no other essay names yet.

Catastrophic cancellationCondition numberKahan summationPairwise summationRelative errorStable formulationSummation condition numberUnit roundoff