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 to the short formula. In the last step the culprit appeared: I was dividing by and the sample variance divides by . 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.
Table of Contents
Before you start: the four summation rules
Almost every proof in this topic is about handling sums. These four rules justify every step:
| Rule | In symbols | In words |
|---|---|---|
| (S1) Sum of sums | A sum can be split | |
| (S2) Constant factor | What does not depend on comes out | |
| (S3) Sum of a constant | Adding the same thing times | |
| (M) Definition of mean | Solve 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 is a constant inside the sum: it does not depend on .
When you do not know how to carry on, look for a and replace it with . 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 :
What we start from. The definition of the mean, , that is, rule (M).
Step by step.
- Split the sum in two (S1): .
- The first sum is by (M).
- The second adds the constant times, so it is by (S3).
- We are left with .
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.
Where people get stuck. In step 3: writing (forgetting it is added times). If you get , check that step.
Result for your cheat sheet. . 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 : 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 , and their deviations from 9.3 add up to : exactly what the 31.5 contributed, which is no longer there to balance them. They add up to 0 about their own mean, , 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 and . 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 only comes in at the end, when dividing.
What we want to prove.
and, dividing by , the shortcut formula for the sample variance:
What we start from. The definition and rule (M).
Step by step.
- Expand the square of each term (square of a difference): .
- Split the sum in three (S1):
- In the second one, is constant and comes out (S2): , using (M).
- The third adds the constant times (S3): .
- Put them together: .
- For the second form, substitute : .
The same chain, with the reason for each step (numbered as in the list):
| Step | What is done | Why |
|---|---|---|
| 1 | with , . | |
| 2 | Split into three sums | (S1). |
| 3 | is the same number in all terms and comes out (S2); then (M). | |
| 4 | (S3): does not depend on . | |
| 5 | Same term: . | |
| 6 | When you square , the is squared too: . |
Where people get stuck. In step 5, adding instead of subtracting: , not . And in step 6, forgetting that the outside cancels one of the two in the denominator.
Why it is useful. By hand you only need two sums, and , instead of subtracting the mean from every value (dragging decimals along). With the first week: and , so
Result for your cheat sheet. .
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 , the variance dividing by , which is . The difference is small with 40 values and large with 5. In the exam, the sample variance is the one with .
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: (the whole sum is squared, before dividing). The subtraction, , is without subtracting the mean even once. Divided by : , 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 transformation.
What we want to prove. If the same linear transformation (with constants and ) is applied to all the data, then
With the bus: if roadworks make every wait 2 minutes longer (), the mean goes up by 2 and the standard deviation does not change. If we convert them to seconds (), 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 .
Step by step (mean).
- by definition.
- Split (S1) and take the constants out (S2, S3): .
- Use (M): .
Step by step (variance).
- The deviation of each : . The cancels out: shifting all the data does not change how far they are from their mean.
- Squared: .
- Add up and take out (S2): .
- The square root: .
The three steps that go fast:
- The cancels: , and is taken out as a common factor from .
- When squaring, the is squared too: the square of a product is the product of the squares.
- appears without computing it: after taking out, what is left is the definition of , letter by letter.
Where people get stuck. In the last step: , not . If is negative (for example, counting “minutes left” instead of “minutes gone by”), the standard deviation is still positive.
Result for your cheat sheet. If : , , . 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 () and the standard deviation 356.99 (); the ratio of variances is exactly 3600, which is .
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 with and . Mean: . Standard deviation: . The 31.5 becomes : 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 acts as a value and counts as many times as its frequency, so every sum carries a weight and the weights add up to . This is where the formula for the grouped variance comes from.
What we want to prove. With a table of classes with marks and frequencies , and :
What we start from. The grouped mean, which gives the grouped version of (M): . And .
Step by step (first equality).
- Split (S1): .
- The first is (grouped mean). In the second, comes out (S2), leaving .
- .
Step by step (second equality). The same as proof 2, with a weight in every term:
The chain above, piece by piece:
| Piece | Why |
|---|---|
| Square of a difference, multiplied by . | |
| comes out (S2) and . | |
| It is not added times: the weights are added, and . |
Where people get stuck. Using (the number of classes) where (the number of values) should go: without weights would be , but here each class weighs and the sum of the weights is .
Result for your cheat sheet. , with .
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 , 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. . , so . . Minus leaves 1615, and divided by 39, . 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 :
and, since the last term is never negative, is smallest when .
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 ().
Step by step.
- The trick: add and subtract inside the bracket, which changes nothing: .
- Square it (square of a sum): .
- Sum over and split (S1). In the cross term, does not depend on and comes out (S2):
- The middle sum is 0 by proof 1. The last one adds a constant times (S3): .
- We are left with , and since , the smallest it can be is with .
Why this proves that the minimum is at the mean. The first term, , has no in it: with the 40 waits it is 1380.6 whatever you choose. Everything that changes when you move is in , which is never negative and is only 0 when . 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 unless they have seen it before. Write it down as a trick: whenever a proof has and you want to reach , add and subtract .
Result for your cheat sheet. ; the minimum is at .
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 . 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 : . The bracket is and, squared, : the sign does not matter, so overshooting and undershooting are penalised the same. , and : 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 groups of sizes , means and variances , and :
What we start from. Rule (M) inside each group (, where is value of group ) and proof 5.
Step by step (mean).
- The sum of all the data is the sum of the sums of each group: .
- By (M) in each group, the sum of group is .
- So : the overall mean is the mean of the means weighted by the size of each group.
Step by step (variance).
- Split the sum of squares of all the data by groups: .
- Inside group , apply proof 5 with (the overall mean) and the group mean : .
- The first term is by the definition of the group variance.
- Sum over and divide by .
The hard step is the one using proof 5. There we had some data, their mean and any number ; in group , each role is played by something else:
| In proof 5 | In group j | Why it fits |
|---|---|---|
| The data and their mean, | The group’s data and its mean, | Proof 5 holds for any data. |
| Any number | The overall mean, | For the group it is a fixed number, which need not be its own mean. |
| The definition of , multiplied by . | ||
| Now values are added. |
What it means. The total spread has two parts: the one within each group () 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 (), 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. and .
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: (11.375 is the unrounded mean of the third group). The total is , and : 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 of waits and I note one more, . Prove that the new mean is
See the solution to 1
By (M), the sum of the first is . The sum of the is , so . Write and you get . It reads like this: the mean moves towards the new value by a fraction of how far it is. With 40 waits with mean 9.1, a 50-minute wait would raise the mean by minute.
2. Dividing by n. Calling , prove that and that .
See the solution to 2
By proof 2, . Dividing by : , “the mean of the squares minus the square of the mean”. And since by the definition of , . It is the calculation from the story at the top: .
3. The mean deviation under a change of scale. If , prove that , with .
See the solution to 3
By proof 3, , so . The absolute value of a product is the product of the absolute values: . Add up, take out (S2) and divide by : . 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 for | Use |
|---|---|---|
| The data, by hand | The variance | The shortcut formula (proof 2) |
| The data in minutes | Mean and deviation in seconds, or with a fixed delay | Change of origin and scale (3), without recomputing |
| A frequency table | Mean and variance | The grouped versions (4) |
| Means and variances of several groups | The mean and variance of all of them | Weighted mean and variance within + between (6) |
| “Prove that…” | Any of them | Rules S1, S2, S3 and M, step by step |
Common mistakes when proving
Treating as if it depended on . is a fixed number: it comes out of sums (S2) and, added times, gives (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:
- Run the script several times (Ctrl+Shift+Enter): the data change, the equalities do not.
sum(x - mean(x))always comes out around1e-14or1e-15: it is zero.- In the Environment tab, look at
xandy:yhas the values flipped round ( is negative) and still its standard deviation is positive. - Change
njtoc(5, 5)andgtolist(x[1:5], x[6:10]): with equal groups, the simple mean of the means would match.
Summary
| Result | Formula |
|---|---|
| Deviations from the mean | |
| Variance, shortcut form | |
| Change of origin and scale | , |
| Grouped data | , |
| Minimum of the sum of squares | |
| Several groups | , |
Three ideas to take away:
- Four rules (split, take constants out, add constants and ) are enough for every proof in the topic.
- The trick of adding and subtracting turns any sum of squares into “within + between”: it is the key to the variance of several groups.
- R does not prove, but it checks: if your formula and
var()do not match, there is a mistake (often an where an 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 →



