Skip to main content

Welcome to a Grasp lesson

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

Lesson 5

Welford's Algorithm: Mean Update Rule

Recommended resources

Hello! Welcome to the second module of our course.

In the previous module, we explored the significant challenge of numerical instability in variance calculations. We learned that the convenient single-pass formula, , can fail catastrophically when a dataset's mean is large and its variance is small. This diagnosis has set the stage for finding a solution: a one-pass algorithm that is both efficient and numerically robust.

This lesson marks our first step in constructing that solution. Our goal is to derive the recursive update rule for the mean used in Welford's algorithm. This rule is the foundational component for building the full algorithm and introduces the powerful concept of online computation.

1. The Power of Online Algorithms

So far, the algorithms we've considered (both the naive single-pass and the stable two-pass methods) are "batch" algorithms. They require the entire dataset to be available before computation can begin. However, in many real-world data science applications, this isn't feasible or desirable.

What if you're analyzing a live stream of sensor data, user activity on a website, or financial transactions? You can't wait for the stream to end to compute statistics. You need to update your metrics as each new data point arrives. This is the domain of online algorithms.

The following resource provides a great introduction to this concept and why it's so crucial for modern data processing.

Recursive Mean and Variance - Blog di rebeccaratini -

The article 'Recursive Mean and Variance' by Rebecca Ratini sets the stage by explaining the need for incremental computation in scenarios like streaming data.

Please read the short section 'Incremental Computation of Mean and Variance'. It effectively contrasts the batch approach with the online approach that we will be developing.

The key idea is to find a recursive formula: a rule that allows us to calculate the new mean using only the previous mean, the new data point, and the current count of items. This avoids storing the entire dataset and recomputing everything from scratch.


2. Deriving the Recursive Mean

Let's derive this recursive rule for the mean. The derivation is straightforward but reveals a very elegant and intuitive structure. We'll use the following notation:

  • : The -th data point.
  • : The mean of the first data points.
  • : The mean of the first data points (our target).

The derivation is presented very clearly in the following resource. I'll ask you to read it, and then we will walk through the steps together to solidify your understanding.

Welford algorithm for updating variance

The blog post 'Welford algorithm for updating variance' provides a concise, step-by-step mathematical derivation of the recursive mean formula.

Please read from the beginning down to the end of Equation (1). The post uses N and N+1 for indexing, which is equivalent to our n-1 and n. Focus on the algebraic manipulation that isolates the new mean.

Let's reconstruct that derivation step-by-step to ensure it's crystal clear.

  1. Start with the definition of the new mean, :

  2. Split the sum into two parts: the sum of the first elements and the new -th element.

  3. Recognize the definition of the old mean, . We know that . Rearranging this gives us .

  4. Substitute this back into our equation for :

This is a perfectly valid recursive formula, but we can rearrange it into a more insightful form.

  1. Rearrange the terms to isolate the old mean, :

This final equation is the recursive update rule for the mean:


3. Interpreting the Formula

This formula is more than just an algebraic trick. It provides a profound insight into how learning and updating can work.

New Mean = Old Mean + Correction Term

The term represents the "error" or "surprise" of the new data point relative to our previous running average. The formula tells us to update our old mean by adding a fraction of this error. The fraction acts like a learning rate that decreases as we gain more confidence in our estimate of the mean.

This structure is a common pattern in many machine learning and optimization algorithms, including Stochastic Gradient Descent (SGD), where you update model weights based on the error of a single data point.

The practical benefits are immense:

  • Time Complexity: Each update is an O(1) operation (a few simple arithmetic steps), regardless of how many data points you've already processed.
  • Space Complexity: You only need to store three values: the current count (), the current mean (), and (as we'll see later) one other value for the variance. This is O(1) space complexity. You can process a dataset of virtually any size without running out of memory.
Test your understanding!

You are processing a stream of numbers. After processing 4 numbers, the running mean is . The 5th number, , arrives. Use the recursive formula to calculate the new mean, .

Show answer

Using the formula :

The new mean is 11.


Conclusion

In this lesson, we took the first crucial step toward building Welford's algorithm. We moved from a "batch" mindset to an "online" one, which is essential for handling large-scale or streaming data.

Key Takeaways:

  • An online algorithm updates its results incrementally as each new data point arrives, without needing the entire dataset.
  • The recursive formula for updating the mean is .
  • This formula can be interpreted as updating the old estimate with a small correction based on the "surprise" of the new data point.
  • This update rule is highly efficient, requiring constant time and memory for each new data point.

Preview of the Next Lesson:

We have successfully derived the recursive update for the mean. However, our ultimate goal is to compute variance in a stable way. The mean update is just a stepping stone. In the next lesson, we will tackle the core of Welford's method: deriving the recursive update rule for the sum of squared differences from the mean. This is the component that ingeniously avoids the catastrophic cancellation that plagued the naive formula.