ANOVA (Analysis of Variance) › The Mathematics of One-Way ANOVA

Michael BrenndoerferJanuary 5, 20263 min read
Machine Learning from ScratchAll chapters
Part 2 of 9Hypothesis Testing
Chapter 17ANOVA (Analysis of Variance) › The Mathematics of One-Way ANOVA
Reading section 4 of 13 · The Mathematics of One-Way ANOVA · 3 minSection 4 of 13 · 3 min

Book contents

Section 4 of 13 · 3 min read

ANOVA (Analysis of Variance) › The Mathematics of One-Way ANOVAOne section · Sections in this chapter4 / 13 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's formalize these ideas mathematically. One-way ANOVA compares means across kk groups, where "one-way" means there's a single grouping factor.

Notation

  • kk = number of groups
  • njn_j = sample size of group jj (for j=1,2,…,kj = 1, 2, \ldots, k)
  • N=∑j=1knjN = \sum_{j=1}^{k} n_j = total sample size
  • xijx_{ij} = observation ii in group jj
  • xˉj=1nj∑i=1njxij\bar{x}_j = \frac{1}{n_j}\sum_{i=1}^{n_j} x_{ij} = mean of group jj
  • xˉ=1N∑j=1k∑i=1njxij\bar{x} = \frac{1}{N}\sum_{j=1}^{k}\sum_{i=1}^{n_j} x_{ij} = grand mean (overall mean)

The ANOVA Decomposition

The total variation in the data can be decomposed into two components:

∑j=1k∑i=1nj(xij−xˉ)2⏟SSTotal=∑j=1knj(xˉj−xˉ)2⏟SSBetween+∑j=1k∑i=1nj(xij−xˉj)2⏟SSWithin\underbrace{\sum_{j=1}^{k}\sum_{i=1}^{n_j}(x_{ij} - \bar{x})^2}_{SS_{Total}} = \underbrace{\sum_{j=1}^{k} n_j(\bar{x}_j - \bar{x})^2}_{SS_{Between}} + \underbrace{\sum_{j=1}^{k}\sum_{i=1}^{n_j}(x_{ij} - \bar{x}_j)^2}_{SS_{Within}}

Let's understand each component:

Total Sum of Squares (SSTotalSS_{Total}): Measures total variation in the data. Each observation's squared deviation from the grand mean, summed across all observations. This is the same quantity we'd use to compute variance of the entire dataset.

Between-Group Sum of Squares (SSBetweenSS_{Between}): Measures variation of group means around the grand mean. Each group mean's squared deviation from the grand mean, weighted by sample size. Larger groups contribute more because their means are more reliable.

Within-Group Sum of Squares (SSWithinSS_{Within}): Measures variation within each group. Each observation's squared deviation from its own group mean, summed across all groups. This represents the "noise" or baseline variability.

From Sums of Squares to Mean Squares

Sums of squares grow with sample size, so we convert them to mean squares (variance estimates) by dividing by degrees of freedom:

MSBetween=SSBetweenk−1andMSWithin=SSWithinN−kMS_{Between} = \frac{SS_{Between}}{k - 1} \quad \text{and} \quad MS_{Within} = \frac{SS_{Within}}{N - k}

Why these degrees of freedom?

  • Between-groups (k−1k - 1): We have kk group means, but they must average to the grand mean (one constraint), leaving k−1k - 1 independent pieces of information.
  • Within-groups (N−kN - k): We have NN observations, but we estimate kk group means (one per group), losing kk degrees of freedom.

The F-Statistic

The F-statistic is the ratio of mean squares:

F=MSBetweenMSWithin=SSBetween/(k−1)SSWithin/(N−k)F = \frac{MS_{Between}}{MS_{Within}} = \frac{SS_{Between}/(k-1)}{SS_{Within}/(N-k)}

Under the null hypothesis that all population means are equal (μ1=μ2=⋯=μk\mu_1 = \mu_2 = \cdots = \mu_k), this F-statistic follows an F-distribution with df1=k−1df_1 = k - 1 and df2=N−kdf_2 = N - k degrees of freedom.

Intuition:

  • MSWithinMS_{Within} estimates σ2\sigma^2 (the common variance within groups) regardless of whether the null is true
  • MSBetweenMS_{Between} estimates σ2\sigma^2 only if the null is true; if means differ, it estimates something larger
  • If H0H_0 is true, F≈1F \approx 1; if H0H_0 is false, F>1F > 1

The Hypotheses

Null hypothesis: H0:μ1=μ2=⋯=μkH_0: \mu_1 = \mu_2 = \cdots = \mu_k (all population means are equal)

Alternative hypothesis: Ha:H_a: At least one population mean differs from the others

Note that rejecting H0H_0 tells us that some means differ, but not which ones. For that, we need post-hoc tests (discussed later).