Hello! Welcome to your course on Welford's algorithm.
Your goal is to understand the theory, derivation, and implementation of this powerful algorithm. Given your background in data science and machine learning, you know that calculating statistics like mean and variance is a fundamental task. However, the standard methods can sometimes fail in surprising ways on a computer.
This course is structured to build your understanding from the ground up. We'll start by exploring the challenges of calculating variance, which will reveal why a specialized algorithm like Welford's is necessary. Then, we will dive into the derivation and implementation of Welford's algorithm itself.
In this first lesson, we will focus on a foundational piece of the puzzle. Our goal is to derive the common single-pass 'textbook' formula for variance from its statistical definition. This will lay the groundwork for understanding both the potential efficiencies and the hidden dangers of variance calculation.
1. Defining Mean and Variance
Let's start with a quick refresher on the fundamental definitions of mean and variance. For a dataset with data points , the mean () is simply the average of all the data points:
The variance () measures the average squared deviation of each data point from the mean. It tells us how "spread out" the data is. Its definitional formula is:
The image below distinguishes between population variance (when you have data for an entire population) and sample variance (when you have a sample from a larger population).

The definitional formula is intuitive, but implementing it directly requires two passes over the data:
- First pass: Calculate the mean ().
- Second pass: Use the calculated mean to find the sum of squared differences, .
This can be inefficient for large datasets that don't fit into memory. This motivates us to find a formula that can be computed in a single pass.
2. Deriving the Single-Pass "Shortcut" Formula
We can algebraically rearrange the definitional formula for variance into a more computationally convenient form. This new form is often called the "shortcut" or "textbook" formula.
To see how this is done, I recommend you read through a formal derivation. The following resource from ProbabilityCourse.com provides a clear, step-by-step proof.
This reading from ProbabilityCourse.com walks through the mathematical proof for the computational formula for variance. It uses the language of expected values, which provides a solid theoretical foundation that aligns well with your background in math and machine learning.
Please read the section titled 'Computational formula for the variance'. Focus on how the linearity of expectation is used to rearrange the terms. The proof starts with Var(X) = E[(X - μ_X)^2] and ends with Var(X) = E[X^2] - [EX]^2.
Let's walk through that derivation again, using the summation notation for a finite dataset. This will make the connection to computation more direct.
-
Start with the definition of variance:
-
Expand the squared term inside the summation: .
-
Distribute the summation across the three terms:
-
Factor out constants from the summations. Remember that is a constant for the entire dataset. The term can be pulled out of the second summation, and can be pulled out of the third.
Note that summing the constant for times is simply . -
Substitute the definition of the mean. We know that . This can be rewritten as . Let's substitute this into the equation:
-
Simplify the expression:
And there we have it! This is the single-pass formula for variance:
This formula is powerful because it allows us to calculate variance by iterating through the data only once, keeping track of just three quantities:
- The number of data points, .
- The sum of the data points, .
- The sum of the squares of the data points, .
Once we have these three sums, we can compute the mean and variance without needing to store the entire dataset. This is ideal for processing data streams or very large files.
Test your understanding!
Consider the small dataset: .
- Calculate the variance using the two-pass definitional formula: .
- Calculate the variance using the single-pass formula we just derived: .
- Verify that both formulas give the same result.
Show answer
The dataset is , so .
1. Using the two-pass definitional formula:
- Pass 1: Calculate the mean ()
- Pass 2: Calculate the sum of squared differences
- Calculate variance
2. Using the single-pass formula:
- Single Pass: Calculate , , and
- Calculate variance
Both formulas correctly yield a variance of 2.5.
Conclusion
In this lesson, we have achieved our first objective. We started from the intuitive, two-pass definition of variance and mathematically derived an equivalent one-pass formula.
Key Takeaways:
- The definitional formula for variance, , requires two passes over the data.
- Through algebraic manipulation, we derived the single-pass or "shortcut" formula: .
- This formula is computationally efficient as it only requires one pass through the data to gather the necessary sums (, , and ).
This single-pass formula seems like a clear winner. However, as you'll see in upcoming lessons, its elegance in mathematics hides a significant flaw in practical computation.
Preview of the Next Lesson:
Next, we will formally examine the two-pass algorithm as a stable (though less efficient) baseline. Then, we will introduce the concept of catastrophic cancellation, a key source of error in computer arithmetic. This will set the stage for understanding exactly why the single-pass formula we just derived can fail dramatically, and why algorithms like Welford's are so important.