Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 6

Recursive Update for Sum of Squared Differences

Recommended resources

Hello! Welcome back to our study of Welford's algorithm.

In our previous lesson, we accomplished the first part of our derivation: we found a recursive, online formula for updating the mean of a dataset as new data points arrive. We derived , an efficient rule that requires constant time and memory.

This lesson builds directly on that result to tackle the core of Welford's algorithm. Our goal is to derive the recursive update rule for the sum of squared differences from the mean. This quantity is the numerator in the variance formula, and finding a stable way to update it is the key to overcoming the catastrophic cancellation that makes the naive single-pass algorithm unreliable.

1. Defining the Sum of Squared Differences

Recall the definition of sample variance for data points:

where is the sample mean of those points.

Our objective is to compute this value in an online fashion. We already have an online formula for the mean, . Now we need one for the sum of squared differences. Let's define this aggregate value, which is often called or , for the first data points:

Our goal is to find a recursive formula that expresses using only the previous value and information from the new data point .


2. The Algebraic Derivation

The derivation for is more involved than the one for the mean, but it's a fantastic exercise in algebraic manipulation. It beautifully combines the definition of with the recursive mean formula we derived previously.

There are several ways to derive the update rule. We will follow a direct, step-by-step approach that starts from the definition of and substitutes in our formula for . Given your math background, this should be a satisfying path to the solution.

  1. Start with the definition of :

  2. Substitute the recursive formula for :
    We know that . Let's plug this in.

  3. Rearrange the terms inside the square:
    Let's group the terms to create a binomial , where and .

  4. Expand the square and distribute the summation:
    Using , we get three separate sums:

    Let's analyze each of these three terms.

    Term 1:
    We can split this sum at :

    The first part is exactly the definition of . So, Term 1 is .

    Term 2:
    The fraction is constant with respect to the summation index , so we can pull it out:

    Now, let's look at the remaining sum: .
    The first part of this, , is the sum of deviations from the mean for the first elements, which is zero.
    So, the whole sum simplifies to just .
    Plugging this back in, Term 2 becomes .

    Term 3:
    The term being summed is constant, so we just multiply it by :

    Term 3 is .

  5. Combine the simplified terms:
    Now we assemble the pieces:

    Combine the last two terms:

  6. Factor and simplify to the final form:
    Factor out the common term :


    This is a valid recursive formula. However, there is an even more elegant and common form. If we recall that , we can rewrite as:

    By substituting this relationship into our equation, we arrive at the classic update rule:

This final formula is remarkable. It tells us that the new sum of squared differences is the old one plus a correction term. This correction is the product of the new data point's deviation from the old mean and its deviation from the new mean.


3. Reviewing the Derivation in Context

Different sources present this derivation with slight variations in notation and approach. To reinforce your understanding, please review the derivations presented in the following resources. They arrive at the same fundamental update rule.

Calculating Variance: Welford's Algorithm vs NumPy

The article 'Calculating Variance: Welford's Algorithm vs NumPy' presents a detailed, step-by-step derivation very similar to the one we just performed. It uses different notation but the algebraic steps are equivalent.

Please read the long mathematical block under the heading 'The decomposition for the variance is slightly more complicated.' Follow the expansion and see how it mirrors our derivation, even though the intermediate steps might look different at first glance.

Efficient and Accurate Rolling Variance - RAW

Next, the article 'Efficient and Accurate Rolling Variance' presents the final, elegant update rule and offers a sketch of a proof. It uses M_k for the mean and S_k for the sum of squares, which is a common convention.

Focus on the section 'Welford’s Method'. Note the recursive update formula for S_k, which is equivalent to our M_k. The proof provided is brief, but it highlights the key components.

This image provides a concise summary of both the mean and variance update rules, which are the core of Welford's algorithm.

Comparison of Naive and Welford's Formulas for Standard Deviation
This image contrasts the numerically unstable 'textbook' formula with Welford's stable recurrence relations. Equation (15) is the mean update, and Equation (16) is the update rule for the sum of squared differences, which we just derived.
Test your understanding!

Let's do a small-scale calculation. Suppose after 2 data points (), we have:

Now, a third data point, , arrives. Calculate the new sum of squared differences, , using the recursive formula.

Hint: You will first need to calculate the new mean, .

Show answer
  1. Calculate the new mean, :
    Using :

  2. Calculate the new sum of squared differences, :
    Using :

Verification: Let's calculate from scratch to be sure. The data points are (2, 4, 9) and the mean is 5.

The results match. Our recursive formula works!


Conclusion

In this lesson, we derived the heart of Welford's algorithm: the recursive update rule for the sum of squared differences from the mean. This derivation is a crucial piece of the puzzle, building upon our previous work on the recursive mean.

Key Takeaways:

  • The sum of squared differences from the mean, , can be updated recursively.
  • The standard update rule is .
  • This formula allows us to maintain a running sum of squared differences in O(1) time and space for each new data point, just like the mean.

Preview of the Next Lesson:

We have now derived both update rules needed for Welford's algorithm. But why is this method numerically stable? In the next lesson, we will analyze the structure of these update formulas and explain why Welford's algorithm avoids the catastrophic cancellation that makes the naive single-pass formula fail. This will connect the mathematical theory back to the practical problem of floating-point precision that we explored in the first module.