Hello! Welcome to the final lesson in our first module.
In our previous lesson, we took a deep dive into catastrophic cancellation, understanding it as the severe loss of precision that happens when subtracting two nearly equal numbers. We saw that this isn't an arbitrary hardware flaw but a vulnerability in how certain mathematical formulas are computed.
Today, we will connect that knowledge directly back to our original problem: the failure of the single-pass variance formula. Our goal is to analyze why the single-pass variance formula is numerically unstable, particularly for datasets with a large mean and small variance. We'll see that this formula is a perfect, real-world example of a catastrophic cancellation time bomb.
1. The Variance Formula: A Hidden Subtraction
Let's recall the "textbook" or "shortcut" single-pass formula for variance we derived in our first lesson:
Based on our last lesson, your alarm bells should be ringing. This formula contains a subtraction. For this to be a site of catastrophic cancellation, the two terms being subtracted, and , must be nearly equal.
But under what conditions does this happen? The following reading explains it perfectly.
One-Pass Naive Algorithm for Computing Variance
The article 'One-Pass Naive Algorithm for Computing Variance' provides a clear and direct explanation for why this formula is prone to instability.
Please read the section titled 'Numerical Instability'. Focus on the central argument: which specific characteristics of a dataset cause the two terms in the variance formula to become 'mathematically close large values'.
The article states that the problem occurs when the variance is very small and the mean is very large. Let's prove this mathematically. It's a simple but crucial derivation.
We know the definitional formula for variance is , where . Let's expand this:
- Using the linearity of expectation:
- Since is a constant:
- Substituting :
- This simplifies to: , which is our single-pass formula.
Now, let's rearrange this slightly:
This tells us exactly what the two terms in the variance formula are. The single-pass formula is effectively computing:
Now you can see the problem clearly. If the variance is very small compared to the mean-squared , then is a number that is very close to . We are subtracting two nearly equal, large numbers—the exact recipe for catastrophic cancellation.
2. A Practical Demonstration of Failure
Let's see this in action. The following resource provides a simple, devastating example.
Stat 3701 Lecture Notes: Computer Arithmetic
These lecture notes, 'Stat 3701 Lecture Notes: Computer Arithmetic', contain a section that demonstrates the failure of the shortcut formula with a concrete example in R.
Please read section 7.3, 'The Short-Cut Formula for Variance Fails'. Observe how the formula works for a simple dataset but fails completely when a large constant is added to shift the mean.
Let's break down the example from the notes.
-
Dataset 1:
x = {1, 2, ..., 10}.- Mean , Variance (for population variance).
- Here, is not drastically larger than . The subtraction is fine.
mean(x^2) - mean(x)^2works and gives 8.25.
-
Dataset 2:
x = {10⁹ + 1, 10⁹ + 2, ..., 10⁹ + 10}.- The variance is unchanged: .
- The mean is huge: .
- Now, .
- The computer needs to calculate .
A standard double precision floating-point number has about 15-16 significant decimal digits. The term is a number around . To represent , you'd need to store a number like 1,000,000,000,000,000,008.25.... This requires more precision than is available.
The 8.25 part (which represents all the information about the variance!) is completely lost when it's added to the enormous term. This is an example of the "swamping" we discussed in the last lesson. The computer ends up storing a value for that is indistinguishable from its stored value for . The subsequent subtraction mean(x^2) - mean(x)^2 yields 0 or some other small, meaningless number that is pure computational noise.
This instability is not just a theoretical curiosity; it's a very practical problem.

Test your understanding!
You are given three datasets and asked to calculate their variance using the single-pass formula on a standard computer. For which dataset would you be most concerned about numerical instability?
A = {1, -2, 0, 3, -1, 5}B = {100.001, 100.002, 100.001, 100.003}C = {100000, 200000, 50000, 150000}
Explain your reasoning based on the concepts we've discussed.
Show answer
You should be most concerned about Dataset B.
Here's the reasoning:
- Dataset B has a large mean (around 100) but a very small variance (the data points are all very close to each other). This creates the exact condition for catastrophic cancellation: . The term will be very close to .
- Dataset A has a mean close to zero. When the mean is near zero, the formula is stable because is also near zero, so you are not subtracting two large, nearly equal numbers.
- Dataset C has a large mean, but it also appears to have a very large variance. The data points are spread far apart. Because is also large, the two terms in the subtraction will not be nearly equal, and the calculation will likely be stable.
3. A Glimpse of a Solution: The Data Shift Trick
Since the problem is caused by a large mean, what if we could somehow make the mean smaller before the calculation? It turns out we can. Shifting all data points by a constant value does not change the variance.
The article "One-Pass Naive Algorithm for Computing Variance" describes this simple but effective workaround.
One-Pass Naive Algorithm for Computing Variance
The same article we read earlier also proposes a simple fix for the naive algorithm's instability.
Please read the section 'Data Shift Trick'. It includes a short proof showing that variance is invariant to data shifts and explains how to apply this trick.
The data shift trick—subtracting the first data point (or any other constant) from all values—effectively computes the variance on a new dataset whose mean is close to zero. This neatly sidesteps the catastrophic cancellation problem. While clever, this still requires you to know a value from the dataset beforehand, which isn't always ideal for true streaming applications. This observation beautifully sets the stage for Welford's algorithm, which we'll see accomplishes a similar stabilization but in a running, online fashion.
Conclusion
This lesson closes our investigation into why common statistical algorithms can fail.
Key Takeaways:
- The single-pass variance formula, , is vulnerable to catastrophic cancellation.
- The instability occurs when the variance () is small relative to the square of the mean ().
- In this scenario, and become two large, nearly-equal numbers. The information about the variance is contained in the tiny difference between them, which is wiped out by floating-point inaccuracies during the subtraction.
- This problem is not theoretical; it happens in practice with common datasets that have a large offset from the origin.
Preview of the Next Module:
We have now thoroughly explored the problem. We derived the naive formula, benchmarked it against a stable two-pass method, and diagnosed its failure as a case of catastrophic cancellation.
The next module, "Welford's Algorithm: Derivation and Properties," will finally introduce the solution. In our next lesson, we will begin by deriving the recursive update rules for the mean and the sum of squared differences that are at the heart of Welford's elegant and numerically stable one-pass algorithm.