Descriptive statistics — step-by-step proofs with the bus waits: deviations that add up to zero, the short variance formula and the mean of several groups (and the variance I divided by n)

My variance of the 40 waits came out at 34.515 and R’s at 35.4, and it took me an afternoon to understand that the difference was in a single letter. The night before the first ME1 lab session I wanted to check by hand what var() was giving me. I did the calculation that felt most natural, “the mean of the squares minus the square of the mean”, and it did not match. I repeated it three times, changed calculators, suspected R. In the end I took out the notes, wrote down the definition and went step by step from ∑(xi−xˉ)2 \sum (x_i – \bar{x})^2 to the short formula. In the last step the culprit appeared: I was dividing by n n and the sample variance divides by n−1 n – 1 . Since then, whenever a formula does not add up for me, I prove it.

This article has, step by step, the proofs from the descriptive statistics theory: the ones the proposed exercises ask for and the ones that come up in ME1 exams as part of an exercise. Each one has the same structure: what we want to prove, what we start from, each step with the rule that justifies it, where people usually get stuck, the result ready for your cheat sheet (you can take the result to the exam; the proof, no) and how to check it with R using the bus waits.

Before you start: the four summation rules

Almost every proof in this topic is about handling sums. These four rules justify every step:

RuleIn symbolsIn words
(S1) Sum of sums∑i=1n(ai+bi)=∑i=1nai+∑i=1nbi \sum_{i=1}^{n}(a_i + b_i) = \sum_{i=1}^{n} a_i + \sum_{i=1}^{n} b_i A sum can be split
(S2) Constant factor∑i=1nc ai=c∑i=1nai \sum_{i=1}^{n} c\,a_i = c\sum_{i=1}^{n} a_i What does not depend on i i comes out
(S3) Sum of a constant∑i=1nc=n c \sum_{i=1}^{n} c = n\,c Adding the same thing n n times
(M) Definition of mean∑i=1nxi=n xˉ \sum_{i=1}^{n} x_i = n\,\bar{x} Solve xˉ=1n∑xi \bar{x} = \frac{1}{n}\sum x_i for the sum

Rule (M) is the most important one: it appears in all of them. And the key to using (S2) and (S3) is realising that xˉ \bar{x} is a constant inside the sum: it does not depend on i i .

The four summation rulesFour cards: S1, the sum of a sum splits; S2, a constant factor comes out; S3, adding a constant n times gives n times the constant; M, the sum of the data is n times the mean, highlighted.S1Σ(aᵢ + bᵢ) =Σaᵢ + Σbᵢsplit itS2Σ c·aᵢ = c·Σaᵢconstants come outS3Σ c = n·cn times the sameMΣ xᵢ = n·x̄definition of meanx̄ does not depend on i: inside the sum it is a constant (S2, S3)
These four rules justify every step of the proofs in this topic.

When you do not know how to carry on, look for a ∑xi \sum x_i and replace it with nxˉ n\bar{x} . It is the step that unblocks almost all of them.

Proof 1: deviations from the mean add up to zero

Where we are going. To prove that if you subtract the mean from every value and add everything up, you get 0, whatever the data. It matters for two reasons: it explains why spread is not measured by adding deviations (it would always come out as 0), and it is the piece that makes a term vanish in proofs 4, 5 and 6.

What we want to prove. For any data x1,…,xn x_1, \dots, x_n :

∑i=1n(xi−xˉ)=0 \sum_{i=1}^{n}(x_i – \bar{x}) = 0

What we start from. The definition of the mean, xˉ=1n∑i=1nxi \bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i , that is, rule (M).

Step by step.

  1. Split the sum in two (S1): ∑(xi−xˉ)=∑xi−∑xˉ \sum (x_i – \bar{x}) = \sum x_i – \sum \bar{x} .
  2. The first sum is nxˉ n\bar{x} by (M).
  3. The second adds the constant xˉ \bar{x} n n times, so it is nxˉ n\bar{x} by (S3).
  4. We are left with nxˉ−nxˉ=0 n\bar{x} – n\bar{x} = 0 .
∑i=1n(xi−xˉ)=∑i=1nxi−∑i=1nxˉ=nxˉ−nxˉ=0 \sum_{i=1}^{n}(x_i – \bar{x}) = \sum_{i=1}^{n} x_i – \sum_{i=1}^{n}\bar{x} = n\bar{x} – n\bar{x} = 0

What it means. The mean is the balance point of the data: what some values exceed it by, others fall short by. In the first week of waits, the mean is 9.3, the positive deviations add up to 24.1 and the negative ones to −24.1. The 31.5 of the rainy Monday contributes +22.2 on its own.

The mean as a balance pointThe 10 waits of the first week on an axis from 0 to 35, with the mean 9.3 as a pivot triangle. Coral arrows from the mean towards the three larger values (they add up to +24.1) and violet arrows towards the seven smaller ones (they add up to −24.1).05101520253035minutesmean 9.3+24.1−24.1
What the upward deviations add up to is cancelled by the downward ones: the total is 0.

Where people get stuck. In step 3: writing ∑xˉ=xˉ \sum \bar{x} = \bar{x} (forgetting it is added n n times). If you get nxˉ−xˉ n\bar{x} – \bar{x} , check that step.

Result for your cheat sheet. ∑(xi−xˉ)=0 \sum (x_i – \bar{x}) = 0 . That is why, to measure spread, deviations are squared (or their absolute value is taken).

Check it with R:

wait <- c(6.5, 4.0, 9.5, 31.5, 7.0, 5.5, 11.0, 8.0, 3.5, 6.5,
          2.5, 4.5, 3.0, 4.5, 1.5, 4.0, 2.0, 5.5, 7.5, 8.5,
          6.0, 9.0, 7.5, 6.5, 8.0, 9.5, 7.0, 8.5, 6.0, 12.0,
          10.5, 13.5, 12.0, 14.5, 12.5, 10.5, 16.5, 18.0, 15.5, 24.0)
week <- wait[1:10]                  # the first week
d <- week - mean(week)              # deviations from the mean
d
sum(d[d > 0]); sum(d[d < 0])        # what is above and what is below
sum(d)                              # it must be 0

Output:

 [1] -2.8 -5.3  0.2 22.2 -2.3 -3.8  1.7 -1.3 -5.8 -2.8
[1] 24.1
[1] -24.1
[1] -7.105427e-15

How to read it: the positive deviations add up to 24.1 and the negative ones to −24.1. sum(d) gives -7.105427e-15, which is −7.1⋅10−15 -7.1 \cdot 10^{-15} : zero, apart from the computer’s rounding error.

Check you’ve got it: if you drop the 31.5 of the rainy Monday from the first week, do the deviations from 9.3 still add up to 0?

No. There are 9 waits left, adding up to 93−31.5=61.5 93 – 31.5 = 61.5 , and their deviations from 9.3 add up to 61.5−9⋅9.3=−22.2 61.5 – 9 \cdot 9.3 = -22.2 : exactly what the 31.5 contributed, which is no longer there to balance them. They add up to 0 about their own mean, 61.5/9=6.83 61.5 / 9 = 6.83 , because (M) is used with the mean of the same data being added.

Proof 2: the shortcut formula for the variance

Where we are going. From the definition, which makes you subtract the mean from every value, to a formula that only needs ∑xi \sum x_i and ∑xi2 \sum x_i^2 . It matters because by hand it is the one you will always use, because it comes back with weights in proof 4, and because it shows where the mistake in the anecdote comes from: the n−1 n – 1 only comes in at the end, when dividing.

What we want to prove.

∑i=1n(xi−xˉ)2=∑i=1nxi2−nxˉ2=∑i=1nxi2−1n(∑i=1nxi)2 \sum_{i=1}^{n}(x_i – \bar{x})^2 = \sum_{i=1}^{n} x_i^2 – n\bar{x}^2 = \sum_{i=1}^{n} x_i^2 – \frac{1}{n}\left(\sum_{i=1}^{n} x_i\right)^2

and, dividing by n−1 n – 1 , the shortcut formula for the sample variance:

s2=1n−1[∑i=1nxi2−1n(∑i=1nxi)2] s^2 = \frac{1}{n-1}\left[\sum_{i=1}^{n} x_i^2 – \frac{1}{n}\left(\sum_{i=1}^{n} x_i\right)^2\right]

What we start from. The definition s2=1n−1∑(xi−xˉ)2 s^2 = \frac{1}{n-1}\sum (x_i – \bar{x})^2 and rule (M).

Step by step.

  1. Expand the square of each term (square of a difference): (xi−xˉ)2=xi2−2xixˉ+xˉ2 (x_i – \bar{x})^2 = x_i^2 – 2x_i\bar{x} + \bar{x}^2 .
  2. Split the sum in three (S1):
∑(xi−xˉ)2=∑xi2−∑2xˉ xi+∑xˉ2 \sum (x_i – \bar{x})^2 = \sum x_i^2 – \sum 2\bar{x}\,x_i + \sum \bar{x}^2
  1. In the second one, 2xˉ 2\bar{x} is constant and comes out (S2): ∑2xˉ xi=2xˉ∑xi=2xˉ⋅nxˉ=2nxˉ2 \sum 2\bar{x}\,x_i = 2\bar{x}\sum x_i = 2\bar{x}\cdot n\bar{x} = 2n\bar{x}^2 , using (M).
  2. The third adds the constant xˉ2 \bar{x}^2 n n times (S3): ∑xˉ2=nxˉ2 \sum \bar{x}^2 = n\bar{x}^2 .
  3. Put them together: ∑xi2−2nxˉ2+nxˉ2=∑xi2−nxˉ2 \sum x_i^2 – 2n\bar{x}^2 + n\bar{x}^2 = \sum x_i^2 – n\bar{x}^2 .
  4. For the second form, substitute xˉ=1n∑xi \bar{x} = \frac{1}{n}\sum x_i : nxˉ2=n⋅1n2(∑xi)2=1n(∑xi)2 n\bar{x}^2 = n\cdot\frac{1}{n^2}\left(\sum x_i\right)^2 = \frac{1}{n}\left(\sum x_i\right)^2 .
∑i=1n(xi−xˉ)2=∑xi2−2nxˉ2+nxˉ2=∑xi2−nxˉ2=∑xi2−1n(∑xi)2 \sum_{i=1}^{n}(x_i – \bar{x})^2 = \sum x_i^2 – 2n\bar{x}^2 + n\bar{x}^2 = \sum x_i^2 – n\bar{x}^2 = \sum x_i^2 – \frac{1}{n}\left(\sum x_i\right)^2

The same chain, with the reason for each step (numbered as in the list):

StepWhat is doneWhy
1(xi−xˉ)2=xi2−2xixˉ+xˉ2 (x_i – \bar{x})^2 = x_i^2 – 2x_i\bar{x} + \bar{x}^2 (A−B)2=A2−2AB+B2 (A – B)^2 = A^2 – 2AB + B^2 with A=xi A = x_i , B=xˉ B = \bar{x} .
2Split into three sums(S1).
3∑2xixˉ=2xˉ∑xi=2nxˉ2 \sum 2x_i\bar{x} = 2\bar{x}\sum x_i = 2n\bar{x}^2 2xˉ 2\bar{x} is the same number in all n n terms and comes out (S2); then (M).
4∑xˉ2=nxˉ2 \sum \bar{x}^2 = n\bar{x}^2 (S3): xˉ2 \bar{x}^2 does not depend on i i .
5−2nxˉ2+nxˉ2=−nxˉ2 -2n\bar{x}^2 + n\bar{x}^2 = -n\bar{x}^2 Same term: −2+1=−1 -2 + 1 = -1 .
6nxˉ2=1n(∑xi)2 n\bar{x}^2 = \frac{1}{n}\left(\sum x_i\right)^2 When you square 1n∑xi \frac{1}{n}\sum x_i , the 1n \frac{1}{n} is squared too: n⋅1n2=1n n \cdot \frac{1}{n^2} = \frac{1}{n} .

Where people get stuck. In step 5, adding instead of subtracting: −2nxˉ2+nxˉ2=−nxˉ2 -2n\bar{x}^2 + n\bar{x}^2 = -n\bar{x}^2 , not −3nxˉ2 -3n\bar{x}^2 . And in step 6, forgetting that the n n outside cancels one of the two n n in the denominator.

Why it is useful. By hand you only need two sums, ∑xi \sum x_i and ∑xi2 \sum x_i^2 , instead of subtracting the mean from every value (dragging decimals along). With the first week: ∑xi=93 \sum x_i = 93 and ∑xi2=1459.5 \sum x_i^2 = 1459.5 , so

s2=19(1459.5−93210)=1459.5−864.99=594.69=66.07 s^2 = \frac{1}{9}\left(1459.5 – \frac{93^2}{10}\right) = \frac{1459.5 – 864.9}{9} = \frac{594.6}{9} = 66.07

Result for your cheat sheet. s2=1n−1[∑xi2−1n(∑xi)2]=∑xi2−nxˉ2n−1 s^2 = \frac{1}{n-1}\left[\sum x_i^2 – \frac{1}{n}\left(\sum x_i\right)^2\right] = \frac{\sum x_i^2 – n\bar{x}^2}{n-1} .

Check it with R (and look at my mistake from that night):

n <- length(wait)
(sum(wait^2) - sum(wait)^2 / n) / (n - 1)   # shortcut formula
var(wait)                                   # the same
mean(wait^2) - mean(wait)^2                 # my mistake: divides by n
(n - 1) / n * var(wait)                     # which is why it matches this

Output:

[1] 35.4
[1] 35.4
[1] 34.515
[1] 34.515

How to read it: the shortcut formula gives 35.4, like var(). “Mean of the squares minus square of the mean” gives 34.515: it is 1n∑(xi−xˉ)2 \frac{1}{n}\sum (x_i – \bar{x})^2 , the variance dividing by n n , which is n−1ns2=3940⋅35.4 \frac{n-1}{n}s^2 = \frac{39}{40}\cdot 35.4 . The difference is small with 40 values and large with 5. In the exam, the sample variance is the one with n−1 n – 1 .

Check you’ve got it: the second week has a sum of 43.5 and a sum of squares of 236.75. What is its variance with the shortcut formula?

What gets subtracted: 43.5210=189.225 \frac{43.5^2}{10} = 189.225 (the whole sum is squared, before dividing). The subtraction, 236.75−189.225=47.525 236.75 – 189.225 = 47.525 , is ∑(xi−xˉ)2 \sum (x_i – \bar{x})^2 without subtracting the mean even once. Divided by n−1=9 n – 1 = 9 : s2=5.28 s^2 = 5.28 , the value R gives in proof 6. Dividing by 10 would give 4.75: the mistake in the anecdote.

Proof 3: change of origin and scale

Where we are going. To know what happens to the mean and the spread when you change all the data in the same way, without recalculating anything. It matters because it comes up exactly like this (“convert the waits to seconds”) and because standardising, subtracting the mean and dividing by the standard deviation, is an a+bx a + bx transformation.

What we want to prove. If the same linear transformation yi=a+b xi y_i = a + b\,x_i (with constants a a and b b ) is applied to all the data, then

yˉ=a+b xˉ,sy2=b2sx2,sy=∣b∣ sx \bar{y} = a + b\,\bar{x}, \qquad s_y^2 = b^2 s_x^2, \qquad s_y = |b|\,s_x

With the bus: if roadworks make every wait 2 minutes longer (a=2, b=1 a = 2,\ b = 1 ), the mean goes up by 2 and the standard deviation does not change. If we convert them to seconds (a=0, b=60 a = 0,\ b = 60 ), the mean and the standard deviation are multiplied by 60 and the variance by 3600.

What we start from. The definitions of mean and variance applied to the yi y_i .

Step by step (mean).

  1. yˉ=1n∑(a+b xi) \bar{y} = \frac{1}{n}\sum (a + b\,x_i) by definition.
  2. Split (S1) and take the constants out (S2, S3): 1n(n a+b∑xi) \frac{1}{n}\left(n\,a + b\sum x_i\right) .
  3. Use (M): 1n(n a+b nxˉ)=a+b xˉ \frac{1}{n}\left(n\,a + b\,n\bar{x}\right) = a + b\,\bar{x} .

Step by step (variance).

  1. The deviation of each yi y_i : yi−yˉ=(a+b xi)−(a+b xˉ)=b (xi−xˉ) y_i – \bar{y} = (a + b\,x_i) – (a + b\,\bar{x}) = b\,(x_i – \bar{x}) . The a a cancels out: shifting all the data does not change how far they are from their mean.
  2. Squared: (yi−yˉ)2=b2(xi−xˉ)2 (y_i – \bar{y})^2 = b^2 (x_i – \bar{x})^2 .
  3. Add up and take b2 b^2 out (S2): sy2=1n−1∑b2(xi−xˉ)2=b2sx2 s_y^2 = \frac{1}{n-1}\sum b^2 (x_i – \bar{x})^2 = b^2 s_x^2 .
  4. The square root: sy=b2sx2=∣b∣ sx s_y = \sqrt{b^2 s_x^2} = |b|\,s_x .

The three steps that go fast:

  • The a a cancels: a−a=0 a – a = 0 , and b b is taken out as a common factor from b xi−b xˉ b\,x_i – b\,\bar{x} .
  • When squaring, the b b is squared too: the square of a product is the product of the squares.
  • sx2 s_x^2 appears without computing it: after taking b2 b^2 out, what is left is the definition of sx2 s_x^2 , letter by letter.
Change of origin and scaleThree rows with the 10 waits of the first week. In minutes, the mean is 9.3 and the standard deviation 8.13. Adding 2 minutes, the dots shift and the mean becomes 11.3 with the same deviation. In seconds, the axis goes from 0 to 2100: the cloud has the same shape, with mean 558 and deviation 487.69.0102030minutesx̄ = 9.3 · s = 8.130102030+2 (roadworks)ȳ = 11.3 · s = 8.13060012001800×60 (seconds)ȳ = 558 · s = 487.69
Adding moves the cloud without stretching it; multiplying stretches it (the mean and s are multiplied by the same factor).

Where people get stuck. In the last step: b2=∣b∣ \sqrt{b^2} = |b| , not b b . If b b is negative (for example, counting “minutes left” instead of “minutes gone by”), the standard deviation is still positive.

Result for your cheat sheet. If y=a+bx y = a + bx : yˉ=a+bxˉ \bar{y} = a + b\bar{x} , sy2=b2sx2 s_y^2 = b^2 s_x^2 , sy=∣b∣ sx s_y = |b|\,s_x . Adding a constant moves the mean and not the spread; multiplying scales both.

Check it with R:

roadworks <- wait + 2               # all of them 2 minutes longer
c(mean(roadworks), sd(roadworks))   # the mean goes up 2; the sd, the same
seconds <- 60 * wait                # in seconds
c(mean(seconds), sd(seconds), var(seconds) / var(wait))

Output:

[1] 11.10000  5.94979
[1]  546.0000  356.9874 3600.0000

How to read it: with the roadworks, the mean goes from 9.1 to 11.1 and the standard deviation stays at 5.95. In seconds, the mean is 546 (60⋅9.1 60 \cdot 9.1 ) and the standard deviation 356.99 (60⋅5.95 60 \cdot 5.95 ); the ratio of variances is exactly 3600, which is 602 60^2 .

Check you’ve got it: if you subtract the mean (9.1) from every wait and divide by the standard deviation (5.95), what mean and standard deviation do you get? What value does the 31.5 get?

It is y=a+bx y = a + bx with b=15.95 b = \frac{1}{5.95} and a=−9.15.95 a = -\frac{9.1}{5.95} . Mean: a+bxˉ=−9.15.95+9.15.95=0 a + b\bar{x} = -\frac{9.1}{5.95} + \frac{9.1}{5.95} = 0 . Standard deviation: ∣b∣ sx=5.955.95=1 |b|\,s_x = \frac{5.95}{5.95} = 1 . The 31.5 becomes 22.4/5.95=3.76 22.4/5.95 = 3.76 : almost four standard deviations above the mean. The rainy Monday was unusual.

Proof 4: grouped data

Where we are going. To the equalities of proofs 1 and 2 when all you have is the table. The idea: each midpoint mi m_i acts as a value and counts as many times as its frequency, so every sum carries a weight fi f_i and the weights add up to n n . This is where the formula for the grouped variance comes from.

What we want to prove. With a table of K K classes with marks mi m_i and frequencies fi f_i , n=∑fi n = \sum f_i and xˉ=1n∑fimi \bar{x} = \frac{1}{n}\sum f_i m_i :

∑i=1Kfi (mi−xˉ)=0and∑i=1Kfi (mi−xˉ)2=∑i=1Kfi mi2−nxˉ2 \sum_{i=1}^{K} f_i\,(m_i – \bar{x}) = 0 \qquad \text{and} \qquad \sum_{i=1}^{K} f_i\,(m_i – \bar{x})^2 = \sum_{i=1}^{K} f_i\,m_i^2 – n\bar{x}^2

What we start from. The grouped mean, which gives the grouped version of (M): ∑fimi=nxˉ \sum f_i m_i = n\bar{x} . And ∑fi=n \sum f_i = n .

Step by step (first equality).

  1. Split (S1): ∑fi(mi−xˉ)=∑fimi−∑fixˉ \sum f_i (m_i – \bar{x}) = \sum f_i m_i – \sum f_i \bar{x} .
  2. The first is nxˉ n\bar{x} (grouped mean). In the second, xˉ \bar{x} comes out (S2), leaving xˉ∑fi=xˉ n \bar{x}\sum f_i = \bar{x}\,n .
  3. nxˉ−nxˉ=0 n\bar{x} – n\bar{x} = 0 .

Step by step (second equality). The same as proof 2, with a weight fi f_i in every term:

∑fi(mi−xˉ)2=∑fimi2−2xˉ∑fimi+xˉ2∑fi=∑fimi2−2nxˉ2+nxˉ2=∑fimi2−nxˉ2 \sum f_i (m_i – \bar{x})^2 = \sum f_i m_i^2 – 2\bar{x}\sum f_i m_i + \bar{x}^2\sum f_i = \sum f_i m_i^2 – 2n\bar{x}^2 + n\bar{x}^2 = \sum f_i m_i^2 – n\bar{x}^2

The chain above, piece by piece:

PieceWhy
fi(mi−xˉ)2=fimi2−2xˉ fimi+xˉ2fi f_i (m_i – \bar{x})^2 = f_i m_i^2 – 2\bar{x}\,f_i m_i + \bar{x}^2 f_i Square of a difference, multiplied by fi f_i .
∑2xˉ fimi=2nxˉ2 \sum 2\bar{x}\,f_i m_i = 2n\bar{x}^2 2xˉ 2\bar{x} comes out (S2) and ∑fimi=nxˉ \sum f_i m_i = n\bar{x} .
∑xˉ2fi=nxˉ2 \sum \bar{x}^2 f_i = n\bar{x}^2 It is not added K K times: the weights are added, and ∑fi=n \sum f_i = n .

Where people get stuck. Using K K (the number of classes) where n n (the number of values) should go: ∑i=1Kxˉ \sum_{i=1}^{K} \bar{x} without weights would be Kxˉ K\bar{x} , but here each class weighs fi f_i and the sum of the weights is n n .

Result for your cheat sheet. s2=1n−1[∑fimi2−1n(∑fimi)2] s^2 = \frac{1}{n-1}\left[\sum f_i m_i^2 – \frac{1}{n}\left(\sum f_i m_i\right)^2\right] , with n=∑fi n = \sum f_i .

Check it with R with the waits table (frequencies 9, 18, 8, 3 and 2):

f <- c(9, 18, 8, 3, 2)
m <- c(2.5, 7.5, 12.5, 17.5, 30)
n <- sum(f); xbar <- sum(f * m) / n
sum(f * (m - xbar))                                  # first equality: 0
c(sum(f * (m - xbar)^2), sum(f * m^2) - n * xbar^2)  # second: both equal

Output:

[1] 0
[1] 1615 1615

How to read it: the weighted sum of deviations gives 0 and both forms of the sum of squares give 1615. Divided by n−1=39 n – 1 = 39 , the grouped variance is 41.41, the one from the theory.

Check you’ve got it: with the table of waits (midpoints 2.5, 7.5, 12.5, 17.5 and 30, frequencies 9, 18, 8, 3 and 2), what grouped variance do you get by hand with the shortcut formula?

Three sums. n=40 n = 40 . ∑fimi=22.5+135+100+52.5+60=370 \sum f_i m_i = 22.5 + 135 + 100 + 52.5 + 60 = 370 , so xˉ=9.25 \bar{x} = 9.25 . ∑fimi2=56.25+1012.5+1250+918.75+1800=5037.5 \sum f_i m_i^2 = 56.25 + 1012.5 + 1250 + 918.75 + 1800 = 5037.5 . Minus 40⋅9.252=3422.5 40 \cdot 9.25^2 = 3422.5 leaves 1615, and divided by 39, s2=41.41 s^2 = 41.41 . The mean comes out as 9.25 and not 9.1 because each wait has been replaced by the midpoint of its class.

Proof 5: the mean minimises the sum of squares

Where we are going. To prove that the mean is, of all numbers, the one that makes the sum of squared distances smallest. It matters because it justifies measuring the variance about the mean (about any other number it would be larger), it is the key piece of proof 6, and the idea comes back in regression, with least squares.

What we want to prove. For any number a a :

∑i=1n(xi−a)2=∑i=1n(xi−xˉ)2+n (xˉ−a)2 \sum_{i=1}^{n}(x_i – a)^2 = \sum_{i=1}^{n}(x_i – \bar{x})^2 + n\,(\bar{x} – a)^2

and, since the last term is never negative, ∑(xi−a)2 \sum (x_i – a)^2 is smallest when a=xˉ a = \bar{x} .

At the bus stop: if I had to give one single number as “my wait” and I were penalised with the square of each error, the best possible number is the mean.

What we start from. Proof 1 (∑(xi−xˉ)=0 \sum (x_i – \bar{x}) = 0 ).

Step by step.

  1. The trick: add and subtract xˉ \bar{x} inside the bracket, which changes nothing: xi−a=(xi−xˉ)+(xˉ−a) x_i – a = (x_i – \bar{x}) + (\bar{x} – a) .
  2. Square it (square of a sum): (xi−a)2=(xi−xˉ)2+2(xi−xˉ)(xˉ−a)+(xˉ−a)2 (x_i – a)^2 = (x_i – \bar{x})^2 + 2(x_i – \bar{x})(\bar{x} – a) + (\bar{x} – a)^2 .
  3. Sum over i i and split (S1). In the cross term, 2(xˉ−a) 2(\bar{x} – a) does not depend on i i and comes out (S2):
∑(xi−a)2=∑(xi−xˉ)2+2(xˉ−a)∑(xi−xˉ)+∑(xˉ−a)2 \sum (x_i – a)^2 = \sum (x_i – \bar{x})^2 + 2(\bar{x} – a)\sum (x_i – \bar{x}) + \sum (\bar{x} – a)^2
  1. The middle sum is 0 by proof 1. The last one adds a constant n n times (S3): n(xˉ−a)2 n(\bar{x} – a)^2 .
  2. We are left with ∑(xi−xˉ)2+n(xˉ−a)2 \sum (x_i – \bar{x})^2 + n(\bar{x} – a)^2 , and since n(xˉ−a)2≥0 n(\bar{x} – a)^2 \ge 0 , the smallest it can be is with a=xˉ a = \bar{x} .
The sum of squares is smallest at the meanParabola of the sum of squares of the 40 waits about a value a between 4 and 14. The minimum, 1380.6, is at a equal to the mean, 9.1. At the median, 7.75, it is 1453.5.468101214a1400170020002300a = mean 9.1 · 1380.6a = median 7.75 · 1453.5Σ(xᵢ − a)² = 1380.6 + 40(9.1 − a)²
Any a other than the mean adds n(x̄ − a)²: at the median, 40 · 1.35² = 72.9.

Why this proves that the minimum is at the mean. The first term, ∑(xi−xˉ)2 \sum (x_i – \bar{x})^2 , has no a a in it: with the 40 waits it is 1380.6 whatever a a you choose. Everything that changes when you move a a is in n(xˉ−a)2 n(\bar{x} – a)^2 , which is never negative and is only 0 when a=xˉ a = \bar{x} . The sum never goes below 1380.6, and it only gets there with the mean.

Where people get stuck. In step 1: nobody thinks of adding and subtracting xˉ \bar{x} unless they have seen it before. Write it down as a trick: whenever a proof has (xi−something) (x_i – \text{something}) and you want to reach (xi−xˉ) (x_i – \bar{x}) , add and subtract xˉ \bar{x} .

Result for your cheat sheet. ∑(xi−a)2=∑(xi−xˉ)2+n(xˉ−a)2 \sum (x_i – a)^2 = \sum (x_i – \bar{x})^2 + n(\bar{x} – a)^2 ; the minimum is at a=xˉ a = \bar{x} .

Check it with R:

ss <- function(a) sum((wait - a)^2)       # sum of squares about a
sapply(c(7.75, 9.1, 11), ss)              # median, mean and another value
ss(7.75) - ss(9.1)                        # the difference...
40 * (9.1 - 7.75)^2                       # ...is n (mean - a)^2
optimize(ss, c(0, 40))$minimum            # R looks for the minimum: the mean

Output:

[1] 1453.5 1380.6 1525.0
[1] 72.9
[1] 72.9
[1] 9.1

How to read it: about the mean (9.1) the sum of squares is 1380.6; about the median (7.75), 1453.5. The difference, 72.9, is exactly 40⋅(9.1−7.75)2 40 \cdot (9.1 – 7.75)^2 . optimize searches numerically for the value that makes the function smallest between 0 and 40, and finds 9.1.

Check you’ve got it: without working value by value, what is the sum of squares of the 40 waits about 11?

With a=11 a = 11 : 1380.6+40⋅(9.1−11)2 1380.6 + 40 \cdot (9.1 – 11)^2 . The bracket is −1.9 -1.9 and, squared, 3.61 3.61 : the sign does not matter, so overshooting and undershooting are penalised the same. 40⋅3.61=144.4 40 \cdot 3.61 = 144.4 , and 1380.6+144.4=1525 1380.6 + 144.4 = 1525 : the third number in R’s output.

Proof 6: mean and variance of several groups together

This is the one that comes up most: you have the means and variances of several groups (for example, the waits of each week) and you are asked for the mean and variance of all the data together, without having the data.

Where we are going. To the mean and variance of everything with only each group’s summary. The mean comes from (M); the variance, from proof 5 applied inside each group with the roles swapped. If proof 5 is clear to you, this one is short.

What we want to prove. With k k groups of sizes nj n_j , means xˉj \bar{x}_j and variances sj2 s_j^2 , and n=∑j=1knj n = \sum_{j=1}^{k} n_j :

xˉ=∑j=1knj xˉjn \bar{x} = \frac{\sum_{j=1}^{k} n_j\,\bar{x}_j}{n}
s2=1n−1[∑j=1k(nj−1) sj2+∑j=1knj (xˉj−xˉ)2] s^2 = \frac{1}{n-1}\left[\sum_{j=1}^{k}(n_j – 1)\,s_j^2 + \sum_{j=1}^{k} n_j\,(\bar{x}_j – \bar{x})^2\right]

What we start from. Rule (M) inside each group (∑ixij=njxˉj \sum_i x_{ij} = n_j\bar{x}_j , where xij x_{ij} is value i i of group j j ) and proof 5.

Step by step (mean).

  1. The sum of all the data is the sum of the sums of each group: ∑j∑ixij \sum_{j}\sum_{i} x_{ij} .
  2. By (M) in each group, the sum of group j j is njxˉj n_j\bar{x}_j .
  3. So xˉ=1n∑jnjxˉj \bar{x} = \frac{1}{n}\sum_j n_j\bar{x}_j : the overall mean is the mean of the means weighted by the size of each group.

Step by step (variance).

  1. Split the sum of squares of all the data by groups: (n−1)s2=∑j∑i(xij−xˉ)2 (n-1)s^2 = \sum_j \sum_i (x_{ij} – \bar{x})^2 .
  2. Inside group j j , apply proof 5 with a=xˉ a = \bar{x} (the overall mean) and the group mean xˉj \bar{x}_j : ∑i(xij−xˉ)2=∑i(xij−xˉj)2+nj(xˉj−xˉ)2 \sum_i (x_{ij} – \bar{x})^2 = \sum_i (x_{ij} – \bar{x}_j)^2 + n_j(\bar{x}_j – \bar{x})^2 .
  3. The first term is (nj−1)sj2 (n_j – 1)s_j^2 by the definition of the group variance.
  4. Sum over j j and divide by n−1 n – 1 .

The hard step is the one using proof 5. There we had some data, their mean and any number a a ; in group j j , each role is played by something else:

In proof 5In group jWhy it fits
The data and their mean, xˉ \bar{x} The group’s data and its mean, xˉj \bar{x}_j Proof 5 holds for any data.
Any number a a The overall mean, xˉ \bar{x} For the group it is a fixed number, which need not be its own mean.
∑(xi−xˉ)2 \sum (x_i – \bar{x})^2 (nj−1)sj2 (n_j – 1)s_j^2 The definition of sj2 s_j^2 , multiplied by nj−1 n_j – 1 .
n(xˉ−a)2 n(\bar{x} – a)^2 nj(xˉj−xˉ)2 n_j(\bar{x}_j – \bar{x})^2 Now nj n_j values are added.
Variance of several groups: within and betweenThe 40 waits split into three rows: week 1 (mean 9.3), week 2 (mean 4.35) and weeks 3 and 4 (mean 11.38), each with a tick at its mean. A dashed line marks the overall mean, 9.1, and coral arrows join each group mean to it.05101520253035minutesoverall mean 9.1week 19.3week 24.35weeks 3 and 411.38dots around their tick: within · coral arrows: between
Total spread = each group around its own mean + the group means around the overall mean.

What it means. The total spread has two parts: the one within each group (∑(nj−1)sj2 \sum (n_j – 1)s_j^2 ) and the one between groups (how far the group means are from the overall mean). Even if every week were very regular, if one week I wait 4 minutes on average and another 12, the data together will be very spread out. This idea comes back in ME2 with ANOVA.

Where people get stuck. Two classic mistakes: taking the simple mean of the means ((xˉ1+xˉ2+xˉ3)/3 (\bar{x}_1 + \bar{x}_2 + \bar{x}_3)/3 ), which only works if all the groups have the same size, and adding up the variances without the between-groups term.

Result for your cheat sheet. xˉ=∑njxˉjn \bar{x} = \frac{\sum n_j\bar{x}_j}{n} and s2=∑(nj−1)sj2+∑nj(xˉj−xˉ)2n−1 s^2 = \frac{\sum (n_j – 1)s_j^2 + \sum n_j(\bar{x}_j – \bar{x})^2}{n – 1} .

Check it with R, splitting my 40 waits into three groups (week 1, week 2 and weeks 3 and 4):

groups <- list(wait[1:10], wait[11:20], wait[21:40])
nj <- sapply(groups, length); mj <- sapply(groups, mean); vj <- sapply(groups, var)
round(cbind(nj, mj, vj), 2)                 # the summary of each group
n <- sum(nj)
xbar <- sum(nj * mj) / n
xbar; mean(wait)                            # overall mean = mean of everything
s2 <- (sum((nj - 1) * vj) + sum(nj * (mj - xbar)^2)) / (n - 1)
s2; var(wait)                               # pooled variance = variance of everything
mean(mj)                                    # the simple mean of the means does NOT work

Output:

     nj    mj    vj
[1,] 10  9.30 66.07
[2,] 10  4.35  5.28
[3,] 20 11.38 21.52
[1] 9.1
[1] 9.1
[1] 35.4
[1] 35.4
[1] 8.341667

How to read it: the table gives the three summaries (10, 10 and 20 waits, with means 9.3, 4.35 and 11.38). The formula gives a mean of 9.1 and a variance of 35.4, exactly those of the 40 values. The simple mean of the three means, 8.34, does not match: the third group has twice as many values and must weigh twice as much.

Check you’ve got it: with the summaries of the three weeks, what share of the total spread of the 40 waits is due to the group means being different?

Between groups: 10 (9.3−9.1)2+10 (4.35−9.1)2+20 (11.375−9.1)2=0.4+225.63+103.51=329.54 10\,(9.3 – 9.1)^2 + 10\,(4.35 – 9.1)^2 + 20\,(11.375 – 9.1)^2 = 0.4 + 225.63 + 103.51 = 329.54 (11.375 is the unrounded mean of the third group). The total is 39⋅35.4=1380.6 39 \cdot 35.4 = 1380.6 , and 329.54/1380.6=0.24 329.54 / 1380.6 = 0.24 : almost a quarter of how much my waits vary is explained by the week alone, above all by the second one, which was much shorter.

Prove it yourself

Three new proofs in the same style. Try them on paper before opening the solution.

1. Wait number 41. I have the mean xˉn \bar{x}_n of n n waits and I note one more, xn+1 x_{n+1} . Prove that the new mean is

xˉn+1=xˉn+xn+1−xˉnn+1 \bar{x}_{n+1} = \bar{x}_n + \frac{x_{n+1} – \bar{x}_n}{n + 1}
See the solution to 1

By (M), the sum of the first n n is nxˉn n\bar{x}_n . The sum of the n+1 n + 1 is nxˉn+xn+1 n\bar{x}_n + x_{n+1} , so xˉn+1=nxˉn+xn+1n+1 \bar{x}_{n+1} = \frac{n\bar{x}_n + x_{n+1}}{n+1} . Write nxˉn=(n+1)xˉn−xˉn n\bar{x}_n = (n+1)\bar{x}_n – \bar{x}_n and you get xˉn+1=(n+1)xˉn+xn+1−xˉnn+1=xˉn+xn+1−xˉnn+1 \bar{x}_{n+1} = \frac{(n+1)\bar{x}_n + x_{n+1} – \bar{x}_n}{n+1} = \bar{x}_n + \frac{x_{n+1} – \bar{x}_n}{n+1} . It reads like this: the mean moves towards the new value by a fraction 1/(n+1) 1/(n+1) of how far it is. With 40 waits with mean 9.1, a 50-minute wait would raise the mean by (50−9.1)/41≈1 (50 – 9.1)/41 \approx 1 minute.

2. Dividing by n. Calling sn2=1n∑(xi−xˉ)2 s_n^2 = \frac{1}{n}\sum (x_i – \bar{x})^2 , prove that sn2=1n∑xi2−xˉ2 s_n^2 = \frac{1}{n}\sum x_i^2 – \bar{x}^2 and that sn2=n−1n s2 s_n^2 = \frac{n-1}{n}\,s^2 .

See the solution to 2

By proof 2, ∑(xi−xˉ)2=∑xi2−nxˉ2 \sum (x_i – \bar{x})^2 = \sum x_i^2 – n\bar{x}^2 . Dividing by n n : sn2=1n∑xi2−xˉ2 s_n^2 = \frac{1}{n}\sum x_i^2 – \bar{x}^2 , “the mean of the squares minus the square of the mean”. And since ∑(xi−xˉ)2=(n−1)s2 \sum (x_i – \bar{x})^2 = (n-1)s^2 by the definition of s2 s^2 , sn2=(n−1)s2n s_n^2 = \frac{(n-1)s^2}{n} . It is the calculation from the story at the top: 3940⋅35.4=34.515 \frac{39}{40}\cdot 35.4 = 34.515 .

3. The mean deviation under a change of scale. If yi=a+b xi y_i = a + b\,x_i , prove that MDy=∣b∣ MDx MD_y = |b|\,MD_x , with MD=1n∑∣xi−xˉ∣ MD = \frac{1}{n}\sum |x_i – \bar{x}| .

See the solution to 3

By proof 3, yˉ=a+bxˉ \bar{y} = a + b\bar{x} , so yi−yˉ=b(xi−xˉ) y_i – \bar{y} = b(x_i – \bar{x}) . The absolute value of a product is the product of the absolute values: ∣yi−yˉ∣=∣b∣ ∣xi−xˉ∣ |y_i – \bar{y}| = |b|\,|x_i – \bar{x}| . Add up, take ∣b∣ |b| out (S2) and divide by n n : MDy=∣b∣ MDx MD_y = |b|\,MD_x . As with the standard deviation, adding a constant does not change the mean deviation.

When to use each result

If the exam gives you……and asks forUse
The data, by handThe varianceThe shortcut formula (proof 2)
The data in minutesMean and deviation in seconds, or with a fixed delayChange of origin and scale (3), without recomputing
A frequency tableMean and varianceThe grouped versions (4)
Means and variances of several groupsThe mean and variance of all of themWeighted mean and variance within + between (6)
“Prove that…”Any of themRules S1, S2, S3 and M, step by step

Common mistakes when proving

Treating xˉ \bar{x} as if it depended on i i . xˉ \bar{x} is a fixed number: it comes out of sums (S2) and, added n n times, gives nxˉ n\bar{x} (S3).

Checking an equality with an example and calling it proved. A numerical example (like the R ones in this article) is for checking you have not made a mistake, not for proving: the proof has to work for any data.

Skipping steps with “it is easy to see that”. In the exam each step is valued with its justification; write which rule you use, even with one word: “by (M)”, “comes out because it is constant”.

If you cannot remember a formula in the exam, prove it quickly in a corner of the paper. The shortcut variance formula takes four lines.

Try it in the R playground

Proofs are easier to understand when you see that the numbers add up with any data, not just mine. Open the R playground and paste this script, which makes up new data every time:

x <- round(runif(25, 1, 30), 1)        # 25 random waits between 1 and 30 minutes
sum(x - mean(x))                        # proof 1: always ~0
c(var(x), (sum(x^2) - sum(x)^2 / 25) / 24)   # proof 2: equal
y <- 3 - 2 * x                          # a change with negative b
c(sd(y), 2 * sd(x))                     # proof 3: |b| = 2
g <- list(x[1:5], x[6:25])              # two groups of different size
nj <- c(5, 20); mj <- sapply(g, mean)
c(sum(nj * mj) / 25, mean(x))           # proof 6: equal

What to look at:

  1. Run the script several times (Ctrl+Shift+Enter): the data change, the equalities do not.
  2. sum(x - mean(x)) always comes out around 1e-14 or 1e-15: it is zero.
  3. In the Environment tab, look at x and y: y has the values flipped round (b b is negative) and still its standard deviation is positive.
  4. Change nj to c(5, 5) and g to list(x[1:5], x[6:10]): with equal groups, the simple mean of the means would match.

Summary

ResultFormula
Deviations from the mean∑(xi−xˉ)=0 \sum (x_i – \bar{x}) = 0
Variance, shortcut forms2=1n−1[∑xi2−1n(∑xi)2] s^2 = \frac{1}{n-1}\left[\sum x_i^2 – \frac{1}{n}\left(\sum x_i\right)^2\right]
Change of origin and scaleyˉ=a+bxˉ \bar{y} = a + b\bar{x} , sy=∣b∣ sx s_y = |b|\,s_x
Grouped data∑fi(mi−xˉ)=0 \sum f_i(m_i – \bar{x}) = 0 , s2=∑fimi2−nxˉ2n−1 s^2 = \frac{\sum f_i m_i^2 – n\bar{x}^2}{n-1}
Minimum of the sum of squares∑(xi−a)2=∑(xi−xˉ)2+n(xˉ−a)2 \sum (x_i – a)^2 = \sum (x_i – \bar{x})^2 + n(\bar{x} – a)^2
Several groupsxˉ=∑njxˉjn \bar{x} = \frac{\sum n_j\bar{x}_j}{n} , s2=∑(nj−1)sj2+∑nj(xˉj−xˉ)2n−1 s^2 = \frac{\sum (n_j – 1)s_j^2 + \sum n_j(\bar{x}_j – \bar{x})^2}{n-1}

Three ideas to take away:

  1. Four rules (split, take constants out, add constants and ∑xi=nxˉ \sum x_i = n\bar{x} ) are enough for every proof in the topic.
  2. The trick of adding and subtracting xˉ \bar{x} turns any sum of squares into “within + between”: it is the key to the variance of several groups.
  3. R does not prove, but it checks: if your formula and var() do not match, there is a mistake (often an n n where an n−1 n – 1 should be).

With the formulas proved, it is time to use them with real data: in the descriptive statistics practice in R you will analyse a month of waits by time slot and check the variance of several groups with 60 values. And to practice like in the EC1, the descriptive statistics exercises include proof questions with their marks.

Next step: practiceDescriptive statistics — R practice with the bus waits by time slot: summary, tables with cut and boxplots by group (and the cut() that swallowed the shortest wait)

Did this article help you? Here is how you can support SergioLearns →