Statistics and Probability Primer
A collection of measurements does not explain itself. We need ways to describe what is typical, how much the values differ, and how much confidence we should place in the patterns we observe.
33 sections · 25 questions · 4 coding challenges
The arrow keys move between sections too.
What Could a City Learn from Its Records?
BOTIE
In 1662, John Graunt published an analysis of London's Bills of Mortality. He compared recorded deaths across causes and time, and used the records to reason about the wider population. Statistics has many origins, but this is a useful early example of the problem it addresses: how do we turn individual observations into something we can say about a group?
We start by counting and organising the observations. We can then describe a typical value, how much values differ, and which quantities change together. The records still have limitations. A pattern in the people we observed may not describe everyone, and a change may come from the way the records were collected. That is why this primer moves from describing a sample to asking how much we can infer from it.
Sources for this history
What Can a Handful of Measurements Tell Us?
Five people's measurements fit in a small table. Thousands do not. We need to describe where values gather, how much they differ, and what an observed group can tell us about a larger one.
Start with the observations. A summary is useful only when we know what information it keeps.
A row is one person; a column is one quantity
Reading across describes one person. Reading down compares the same quantity across people. We will summarize one column before asking how the columns vary together.
Heights, centimetres
- 160
- 165
- 170
- 175
- 180
Weights, kilograms
- 58
- 66
- 68
- 74
- 74
Describe the sample, then consider uncertainty
First we will calculate centres, spreads, and paired relationships for the measured group. Then probability and repeated sampling will help us ask what changes when another group is observed.
Keeping those questions separate prevents a sample average from quietly becoming a claim about everyone.
1. Observations and Variables
Five people, each measured twice. That is the whole dataset, and it carries every idea on this page.
height (cm): 160 165 170 175 180
weight (kg): 58 66 68 74 74Two words are worth fixing before anything is computed. An observation is one of the five people, one row, everything measured about a single case. A variable is one of the two measurements taken across all of them, one column, the same quantity recorded for everybody.
Almost everything in this primer is a statement about a column, or about how two columns relate. The rows are what we have; the columns are what we describe.
The distinction earns its keep because the two directions answer different questions. Reading across a row describes one person and nobody else, which is what a model does each time it makes a prediction. Reading down a column compares the same quantity across everybody, and every summary on this page is computed that way, down a column. The units belong to the column too, centimetres for one and kilograms for the other, which is a small point now and returns in section 10.
2. Why We Summarize Data
Five heights can simply be read. Five thousand cannot, and neither can the columns a model is usually handed.
A summary trades detail for something a person can hold in mind. Two numbers about a column, one saying roughly where its values sit and one saying how spread out they are, answer most of the questions anyone asks of it, and they can be compared across columns and across datasets in a way that a list of raw values cannot.
The cost is real and worth stating up front. Every summary discards something, and the rest of this primer is partly about noticing what each one throws away.
3. The Mean
The mean is the most familiar summary. Add the values and divide by how many there are.
Worked example
mean height = (160 + 165 + 170 + 175 + 180) / 5 = 850 / 5 = 170
mean weight = (58 + 66 + 68 + 74 + 74) / 5 = 340 / 5 = 68There is a picture that explains why the mean is the number it is. Put the values on a line and imagine a plank with a weight at each value. The mean is the point where the plank balances, because the pull of the values above it exactly cancels the pull of the values below.
Drag a person and watch the mean chase them while the deviations go on cancelling to zero.
Keep in mind
The mean is a measure of centre, and that is not the same as a typical observation. It need not be a value anyone actually has, and with a lopsided column it can sit somewhere no one is.
Four people earning 25,000 and one earning 500,000 have a mean income of 120,000, which describes none of the five. The mean is still the balance point; it just stops being a good answer to “what does someone here look like”.
4. Deviations from the Mean
Once there is a centre, every value can be described by its distance from it. Subtract the mean from each value, and what is left is called the deviation.
Worked example
height: 160 165 170 175 180
height − mean: −10 −5 0 5 10 sum = 0Negative below the mean, positive above it, zero for the person sitting exactly on it. The same subtraction for weight, around its mean of 68, cancels in the same way.
weight: 58 66 68 74 74
weight − mean: −10 −2 0 6 6 sum = 0A deviation carries two pieces of information. Its sign says which side of the centre a person is on, and its size says how far out they sit. The shortest person is 10 below the mean height and the tallest is 10 above it, the same distance in opposite directions, which is what lets the two cancel. Every summary that follows, the variance, the covariance and the correlation, is built from these signed distances.
That the deviations sum to zero is not a coincidence of these numbers. It is the balance point from the last section, stated arithmetically, and it holds for every column there has ever been. It is also about to be inconvenient.
5. Center Is Not Spread
The mean alone leaves out something important. Consider two groups of five.
| group | values | mean |
|---|---|---|
| tightly packed | 169, 170, 170, 170, 171 | 170 |
| widely spread | 140, 155, 170, 185, 200 | 170 |
Identical means, and nobody would describe them as the same. One group is nearly uniform and the other ranges over sixty centimetres. Whatever separates them is not centre, and the mean cannot see it at all.
The deviations already see what the mean cannot. Subtract 170 from each value and the two groups stop looking alike.
tightly packed: −1 0 0 0 1 sum = 0
widely spread: −30 −15 0 15 30 sum = 0Both lists sum to zero, as every list of deviations does, so adding them up cannot tell the groups apart either. The sizes can. One group never strays more than a centimetre from its centre and the other strays thirty.
So a second summary is needed, one that answers how far from the centre the values tend to sit. The deviations from section 4 are exactly the raw material for it, and the whole difficulty, which section 6 opens with, is using their sizes without letting their signs cancel.
Check yourself
Questions on Sections 1 to 5
Question 1 of 5
The five height deviations sum to zero, and so do the deviations of every column there has ever been.
6. Variance
The obvious first attempt is to average the deviations, and it fails immediately, for the reason section 4 gave.
(−10 + −5 + 0 + 5 + 10) / 5 = 0It gives zero for every column that has ever existed, because the positive and negative deviations cancel by construction. The cancellation is the problem, so the fix is to stop the signs from cancelling. Two ways suggest themselves, taking the absolute value of each deviation or squaring it, and both are used in practice.
Squaring is the one that leads here. Squares are easier to work with algebraically, which matters once calculus starts differentiating them, and they punish a large deviation far more than several small ones, which is often the behaviour wanted. Averaging the absolute values instead gives the mean absolute deviation, a perfectly respectable statistic that this primer simply does not follow.
Worked example
| height | deviation | squared deviation |
|---|---|---|
| 160 | −10 | 100 |
| 165 | −5 | 25 |
| 170 | 0 | 0 |
| 175 | 5 | 25 |
| 180 | 10 | 100 |
sum of squared deviations = 250
variance = 250 / 5 = 50That is the variance, the average squared deviation from the mean, and dividing by 5 says something specific about what we are doing. We are treating these five people as the complete group we want to describe, not as a sample of anybody else.
Keep in mind
The denominator depends on that choice, and it is worth meeting now rather than being surprised by it later.
population: σ² = Σ(xᵢ − μ)² / N divide by 5, giving 50
sample: s² = Σ(xᵢ − x̄)² / (n − 1) divide by 4, giving 62.5When the five are the whole group, divide by how many there are. When the five are a sample being used to estimate the spread of something larger, divide by one fewer, because the mean was itself estimated from the same five and that makes the deviations slightly too small.
This primer divides by 5 throughout, since the five people are the group being described. Section 16 is where they become a sample.
7. Standard Deviation
Variance has one awkward feature. Squaring the deviations squared the units too, so a variance of 50 is 50 square centimetres, which is not a quantity anyone has intuition about.
Take the square root and the units come back.
standard deviation of height = √50 ≈ 7.07 cmTaking the square root restores the original unit, so a standard deviation can be compared with the measurements themselves. Repeat the same calculation for weight.
weight variance = 176 / 5 = 35.2 kg²
weight standard deviation = √35.2 ≈ 5.93 kgKeep in mind
It is tempting to call this the typical distance from the mean, and it is close enough to be useful intuition, though it is not literally that. It is the root-mean-square distance, which squares first, averages, then takes the root, and that is a different number from the average distance.
For our five heights the average absolute distance is 6, while the standard deviation is 7.07. Squaring gives the far-out values more say, so the standard deviation sits above the plain average whenever the deviations are uneven.
Three experiments in the box below separate centre from spread. Move one point a little and both shift slightly. Move one point a long way and the spread jumps while the centre barely stirs. Then drag each of the five points up by the same amount, one after another. The centre travels with them and the spread ends up where it started, give or take the precision of a drag, because a shift shared by everybody changes nobody’s distance from the centre.
…
8. Relationships Between Two Variables
Everything so far describes one column at a time. The interesting question sits between the columns.
When a person is taller than average, are they also heavier than average? If they tend to be, then the two quantities carry information about each other, and knowing one tells you something about the other. That is exactly what a prediction is, so a summary of how two columns move together is the last descriptive idea this primer needs.
Look down the two columns together and the pattern is already visible. The shortest person, at 160 cm, is also the lightest at 58 kg. The tallest, at 180 cm, shares the heaviest weight of 74 kg. Each step up in height comes with a step up in weight, apart from the last, where 175 and 180 weigh the same. Nobody is tall and light, and nobody is short and heavy.
What is missing is a number for “tend to”. Reading five rows is easy and reading five thousand is not, which is the problem section 2 raised for one column, now asked of two at once. The summary wanted has to say how strongly the two columns move together, and the next two sections build it out of the deviations each column already has.
9. Paired Deviations
The deviations already hold the answer, once they are paired up person by person rather than looked at one column at a time.
Multiply each person’s height deviation by their own weight deviation. Somebody below average in both gives a positive product, since a negative times a negative is positive. Somebody above average in both also gives a positive product. The product only comes out negative when the two disagree, tall but light, or short but heavy.
Worked example
| height − mean | weight − mean | product |
|---|---|---|
| −10 | −10 | 100 |
| −5 | −2 | 10 |
| 0 | 0 | 0 |
| 5 | 6 | 30 |
| 10 | 6 | 60 |
sum of products = 200Every product is positive or zero here, so these five people agree with each other throughout. Nobody is tall and light.
Reading down the table, the 160 cm person is ten below the mean in height and ten below it in weight, and that agreement in both directions gives the largest product, 100. The 170 cm person sits exactly on both means and contributes nothing, since a deviation of zero times anything is zero. A short, heavy person would have contributed a negative product, a positive weight deviation times a negative height one, and would have pulled the sum down.
The products have units of their own, centimetres times kilograms, which is about to matter.
Check yourself
Questions on Sections 6 to 9
Question 1 of 5
Why does this primer square the deviations rather than take their absolute values?
10. Covariance
Average those products and the result is the covariance.
covariance = 200 / 5 = 40Its sign is the part that reads directly. Positive means the two columns tend to move together, negative means one rises as the other falls, and near zero means the agreements and disagreements roughly cancel.
Keep in mind
The size, on the other hand, cannot be read at all yet. It is tempting to call 40 large, and there is nothing here to say whether it is.
Measure the same five heights in metres instead of centimetres and every height deviation shrinks by a hundred, so the covariance falls from 40 to 0.4 without one thing about these people changing. A number that moves with the choice of ruler cannot be compared against anything, including itself.
The sum of 200 is also worth remembering. The regression page built that exact number on its way to a slope without giving it a name, and section 14 collects the debt.
11. Removing Scale
The repair follows from what went wrong. Covariance carries the scale of height and the scale of weight, both at once, so dividing by a measure of each column’s own spread should leave the relationship and take the units away.
The standard deviations are exactly those measures, so divide by both.
r = covariance / (SD of height × SD of weight)Worked example
r = 40 / (7.07 × 5.93) ≈ 0.953In metres the covariance becomes 0.4 and the height standard deviation becomes 0.0707, and the two hundredfolds cancel. r is 0.953 either way.
›Why this works
There is a second way to write the same quantity that makes what happened clearer. Divide each deviation by its own column’s standard deviation first, turning it into a unit-free number saying how many standard deviations from the mean that person is. Then average the products of those.
r = average of ((xᵢ − x̄)/σₓ) × ((yᵢ − ȳ)/σᵧ)Which is to say correlation is covariance computed after both columns have had their units removed, rather than a different idea.
12. Correlation
That pure number is the correlation, written r, and it always lands between −1 and 1. Sign and strength read separately.
| r | what it says |
|---|---|
| near +1 | a rising straight-line relationship, points close to the line |
| near −1 | a falling straight-line relationship, points close to the line |
| near 0 | no straight-line relationship worth speaking of |
The five people, in standard deviations
Section 11 said r is an average of products once each column is measured in its own standard deviations. Here is that calculation, with every height deviation divided by 7.07 and every weight deviation by 5.93.
| height − mean | ÷ 7.07 | weight − mean | ÷ 5.93 | product |
|---|---|---|---|---|
| −10 | −1.414 | −10 | −1.685 | 2.384 |
| −5 | −0.707 | −2 | −0.337 | 0.238 |
| 0 | 0 | 0 | 0 | 0 |
| 5 | 0.707 | 6 | 1.011 | 0.715 |
| 10 | 1.414 | 6 | 1.011 | 1.430 |
r = (2.384 + 0.238 + 0 + 0.715 + 1.430) / 5 = 4.767 / 5 ≈ 0.953The same 0.953 as the covariance over the two spreads, reached by a route with no units anywhere in it. Pressing the button for the measured five in the box above shows the same figure in its correlation readout.
The bounds come from that same table. Each column now has a spread of exactly one, so the products can only average 1 when every person sits in the same place in both columns, as many standard deviations up or down in weight as in height, which is a perfect rising line. Every departure from it, somebody further out in one column than the other, lowers the average, and a perfect falling line pins it at −1.
Our 0.953 says these five people sit very close to a rising line, which the plot confirms at a glance. Drag one person well off the trend in the box above and watch r fall away from 1, then press the button for no pattern and watch it drop near 0.
13. What Correlation Does Not Say
Two qualifications, and both matter more than the formula.
Zero correlation is not no relationship
Correlation measures linear co-movement, and only that. A strong, obvious, perfectly deterministic relationship can have a correlation of zero if it is curved.
Points along a symmetric U shape are a clean example. The left arm falls and the right arm rises, the two contributions cancel exactly, and r comes out near zero while every point sits on an exact curve. Reporting “no correlation” there would be arithmetically true and completely misleading.
Correlation is also easily moved by one unusual observation, since a single far-out point contributes a large product to the average.
Correlation is not causation
Correlation says two columns vary together. It says nothing about why, and the possibilities it cannot distinguish include the first influencing the second, the second influencing the first, some third thing driving both, an artefact of how the data was selected or measured, and plain coincidence in a small sample.
None of that makes correlation less useful. It makes it a description rather than an explanation.
Check yourself
Questions on Sections 10 to 13
Question 1 of 5
Measuring the same five heights in metres rather than centimetres changes the covariance without changing anything about the people.
14. Covariance, Variance, and Regression Slope
Now the payoff. The regression page handed you a formula for the slope, derived from calculus, and it looked like a ratio of two sums.
slope = Σ(xᵢ − x̄)(yᵢ − ȳ) / Σ(xᵢ − x̄)²Divide the top and the bottom by the same count of five, which changes nothing, and both halves turn into things this primer has named.
slope = covariance(x, y) / variance(x)
slope = 40 / 50 = 0.8Read in two steps, that says everything. The covariance measures how height and weight move together. The variance measures how much height moves on its own. Their ratio is how much of the joint movement should be credited to each centimetre of height.
›Why the units come out right
The units survive the division and land exactly where they should.
covariance is in cm · kg
variance is in cm²
slope is in cm · kg / cm² = kg / cmKilograms per centimetre, which is what a slope on this plot has to mean. The regression was computing a covariance over a variance the whole time without either being named.
15. Correlation and R²
One more connection, and this one needs its conditions stated before the identity rather than after it. All of the following have to hold.
| condition |
|---|
| one predictor |
| one response |
| the model fits an intercept |
| both numbers computed on the same observations |
Under exactly those conditions,
R² = r²
0.909 = 0.953²The squaring is where the difference between them sits. Correlation keeps its sign, so it can tell a rising relationship from a falling one. R² measures the share of variation the fit accounts for, which cannot sensibly be negative here, and squaring is what removes the direction.
›Why the two numbers coincide
R² is the share of the variation in weight that the fitted line accounts for. The line’s predictions move only with height, so the variation they carry is the slope squared times the variation in height, and section 14 showed the slope is the covariance over the variance of height. Put those together and both quantities are the squared covariance over the product of the two variances, once written with sums and once with averages.
variation the line accounts for = 0.8² × 250 = 160
total variation in weight = 176
R² = 160 / 176 ≈ 0.909
left over = 176 − 160 = 16
r² = 40² / (50 × 35.2) = 1600 / 1760 ≈ 0.909What the line leaves unaccounted for is the 16 the regression page scored the line at. Splitting the total into an accounted-for part and a left-over part is what needs the intercept, and the conditions in the table above are the conditions for that split to hold.
Keep in mind
This identity does not carry over unchanged. With more than one predictor, without an intercept, or when R² is computed on data the model did not fit, the two quantities come apart and the equality stops holding.
It is a fact about simple linear regression rather than a general law relating the two.
16. Samples and Populations
Everything up to here described five people. The second half of this primer asks a different question, which is what those five say about anybody else.
Three words keep it straight. The population is the whole group we care about, or more generally the process that produces observations. The sample is what we actually got, our five people. A statistic is a number computed from the sample, and a parameter is the corresponding number for the population, which is usually unknown and is what the statistic is trying to estimate.
| in the sample | in the population |
|---|---|
| sample mean x̄ | population mean μ |
| sample variance s² | population variance σ² |
| computed from data you have | usually unknown |
| changes if you collect again | fixed for a defined population |
Our mean height of 170 was a statistic all along. Whether it is close to the population mean is a question the five people cannot answer by themselves.
One debt from section 6 comes due here. While the five were the whole group, their spread divided by five. The moment they stand for a larger population, the spread being estimated is the population’s, and the sample mean sits a little closer to these five values than the population mean would, so the deviations measured from it run slightly small. Dividing by one fewer corrects for that.
as the whole group: variance = 250 / 5 = 50 SD ≈ 7.07 cm
as a sample: variance = 250 / 4 = 62.5 SD ≈ 7.91 cmThe mean needs no such correction, and 170 is the estimate of the population mean either way. Only the spread is adjusted, and only because the centre it is measured from was itself estimated from the same five people.
17. What Probability Describes
Statistics looks at observations and reasons back to the process. Probability goes the other way, starting from a process and describing what it might produce, and the second half of this primer needs a little of it.
It is needed for one reason. To say what five people tell us about anybody else, there has to be a way of describing the process that produced the five, and that is what probability provides. The box in section 19 is built on exactly this. A population with a fixed mean height sits underneath it, and every press draws people from that process rather than from a list somebody wrote down.
Four words carry the whole of what is needed here. An outcome is something that could happen. A probability is a number saying how likely an outcome is. A random variable attaches a number to an uncertain outcome. And a distribution says which values that number can take and how often each occurs.
Worked example
Pick a person at random from the population and measure their height.
| the idea | here |
|---|---|
| outcome | which person gets picked |
| random variable | their height, unknown until measured |
| distribution | which heights are common in the population |
18. Distributions
A distribution has more to it than a centre and a spread, and it is worth saying so before any particular shape arrives.
Two columns can have identical means and identical standard deviations and still look nothing alike. One might be symmetric and another lean heavily to one side. One might have a single peak and another two, which usually means two different groups have been mixed together. One might have long tails that occasionally produce extreme values where another never does.
Section 3 already met the lopsided case. Four incomes of 25,000 and one of 500,000 lean entirely to one side, and the mean of 120,000 lands where nobody is. That is skew, and neither a centre nor a spread can report it, since a column shaped symmetrically about 120,000 could be built to share both numbers while looking nothing like those five incomes.
| feature of a distribution | what it describes |
|---|---|
| centre | where the values sit |
| spread | how far they range |
| skew | whether one side stretches further than the other |
| peaks | one group, or several mixed together |
| tails | how often extreme values appear |
The mean and the standard deviation summarise a distribution. They do not describe it. That is why the shape of a column is worth looking at before either number is trusted, and why section 21 introduces one particular shape as one distribution among many rather than as what data looks like.
Check yourself
Questions on Sections 14 to 18
Question 1 of 5
The regression slope for these five people is 0.8. What is that number, in this primer’s vocabulary?
19. Sampling Variability
Here is the fact that makes the second half of this primer necessary. Collect five people, compute the mean, and it will not be the population mean. Collect five different people and it will be different again.
The box below draws people from a population whose true mean height is 170 cm. Press for more and watch the solid line, the mean of everyone drawn so far, move around the dashed line, which marks the truth.
The dashed line is the true mean and the solid line is the mean of the people drawn so far. The curve is the bell itself, sized to the current head count, waiting for the bars to fill it.
Measuring the first people now.
With a handful of people the solid line wanders noticeably. With a few hundred it settles close to the dashed line and stops making large excursions.
Keep in mind
More data does not pull the individual observations closer together. The people drawn are as varied at four hundred as they were at forty; it is the mean that steadies, not the population.
And a larger sample cannot fix a sample collected badly. If the drawing process favours some people over others, more of it produces a more confident wrong answer rather than a right one.
20. Why Sample Means Stabilize
That settling has a name. The law of large numbers says that as the number of independent, representative observations grows, the sample mean tends toward the population mean.
The mechanism is cancellation. Each person drawn is as far from the true mean as people ever are, since the population’s own spread does not shrink. But the people above the truth and the people below it pull the average in opposite directions, and the more of them there are, the more completely those pulls cancel. The typical gap between a sample mean and the population mean shrinks with the square root of the count, which is slower than it sounds.
typical gap ≈ population SD / √count
5 people: 7 / √5 ≈ 3.1 cm
40 people: 7 / √40 ≈ 1.1 cm
400 people: 7 / √400 ≈ 0.35 cmThe 7 is the spread of the population behind the box in section 19, which draws heights with a true mean of 170 cm and a standard deviation of 7 cm. Four times the people halves the typical gap, and a hundred times the people cuts it to a tenth. The box opens on forty people already drawn, and their mean reads 167.24 cm, nearly three centimetres under the truth, a larger gap than is usual for forty and exactly the kind of excursion a few more presses settle.
The law is the reason averages are worth computing at all, and the reason more data usually helps. It is also narrower than it first sounds, and the two qualifications are the ones from the last section. The observations have to represent the population you mean to describe, and collecting more of a biased sample does not undo the bias.
Keep in mind
The rule describes the typical gap across the many samples that could have been drawn, not the gap of the sample in front of you. A particular forty can land further off than a particular five. And it says nothing about the next person drawn, who is as likely to be far from the mean as anyone before them. The settling is in the average and nowhere else.
21. The Normal Distribution
The curve in the box above is the normal distribution, the bell shape, and it is worth introducing as one distribution among many rather than as what data looks like.
It is symmetric about its mean, most of its probability sits near the centre, and progressively less lies further into the tails. Two numbers fix it completely. The mean slides the bell left or right, and the standard deviation makes it narrow and tall or wide and flat.
What the two numbers fix
Fixing the mean and the standard deviation fixes how much of the population sits within any distance of the centre, and the shares are the same for every bell. For the population behind the box, mean 170 cm and standard deviation 7 cm, they come out as follows.
| within | band of heights | share of the population |
|---|---|---|
| one standard deviation | 163 to 177 cm | about 68% |
| two standard deviations | 156 to 184 cm | about 95% |
| three standard deviations | 149 to 191 cm | about 99.7% |
So a person more than two standard deviations from the centre is about one in twenty, and the bars in the box thin out at the same rate on both sides. The shares belong to the bell and to nothing else, which is the point of the warning below.
Keep in mind
Many measurements are approximately normal, and many important ones are not. Incomes, waiting times, city populations and word frequencies are all strongly skewed, and treating any of them as normal produces confident nonsense about their tails.
Being numerical is not a reason to assume the bell. It is worth looking at the shape of a column before assuming anything about it.
22. Individual Values Versus Sample Means
Two different convergence ideas are easy to run together, and keeping them apart is worth a section, because the box above demonstrates one of them and not the other.
| idea | what it concerns |
|---|---|
| law of large numbers | one sample mean settling toward the population mean as the sample grows |
| central limit theorem | the distribution of many sample means, across repeated samples, becoming approximately normal |
The box above draws individual people and piles them into bins, so what fills in as you press is the shape of the population itself, and what settles is the one sample mean. That is the empirical distribution converging, and the law of large numbers alongside it.
It is not the central limit theorem, which is a claim about a different quantity entirely. Demonstrating that one would mean drawing many separate samples, computing the mean of each, and plotting the distribution of those means rather than of the people. The striking part of the result is that this distribution of means comes out approximately normal even when the population it drew from is not, and the population here already is normal, so the box could not show it even in principle.
23. What a Model Learns from a Sample
Everything in the last few sections applies directly to a fitted model, because a fitted coefficient is a statistic like any other.
The training data is the sample. The slope of 0.8 from section 14 was computed from five people, and five different people would have given a different number. More relevant data makes such estimates steadier, for exactly the reason sample means steady. And more data from the wrong source makes the model more confident about the wrong thing.
Keep in mind
It would be tidy to say every model assumes its data was drawn at random from one fixed population, and it would not be true of much real work.
Datasets are often assembled deliberately rather than sampled. Populations change over time, so a model fitted last year meets a different world this year. Data collected through a particular channel over-represents whoever uses that channel. These are not exotic edge cases, and none of them is repaired by collecting more.
24. How Machine Learning Uses These Ideas
Every idea in this primer reappears, usually without announcement, in the pages that follow.
| idea | question it answers | where it turns up |
|---|---|---|
| mean | where is this column centred | centring, filling gaps |
| deviation | how far is this value from centre | residuals, features |
| variance | how widely does it spread | scaling, PCA |
| standard deviation | spread in the original units | standardisation |
| covariance | do two columns move together | regression, PCA |
| correlation | how strong is that, without units | exploration, diagnostics |
| distribution | which values occur, how often | modelling noise |
| sampling variability | how might this estimate change | generalisation |
Two rows of the table are this primer’s own numbers wearing other names. Standardising a column subtracts its mean and divides by its standard deviation, which for the five heights means 170 and 7.07, and the result is exactly the unit-free column section 12 multiplied by its partner. The slope of 0.8 is the covariance of 40 over the variance of 50 from section 14, so a regression coefficient is a statement about how two columns move together, scaled by how much one of them moves alone.
The last row is the second half of the primer in one line. A score measured on the rows a model was fitted to says how well it summarised its sample, and the question that matters is how it would have come out on different rows, which is why data is held back before fitting.
In a model
The through line is that a model is a summary too. It compresses many observations into a few numbers, it is computed from a sample, and it discards whatever its form cannot represent.
Which means the questions worth asking about a mean are the questions worth asking about a model. What did it leave out, and would it have come out differently with different data.
Check yourself
Questions on Sections 19 to 24
Question 1 of 5
Drawing more people from the population makes the individual heights cluster more tightly around the true mean.
Try it with Botie
Build with Botie
Write and run Python