Skip to content

Estimate uncertainty

  • Lesson 14
  • 45 min
  • Module 5: Sampling and statistical evidence
Lesson 14 of 18 · Essential Mathematics and Statistics for AIEstimate uncertainty

What you will be able to do

Report a result together with how much it can change by chance, so that nobody reads “92%” as an exact number.

NotebookOptional: check the numbers in a notebookColab · Kaggle · GitHub

The notebook is optional: this lesson works with a calculator. It has the 6,000 reply times of this page’s explorer, so you can count how many 95% intervals contain the true mean. Then you redo the worked examples, the bootstrap and the guided practice in Python, with checks. It uses only Python’s standard library, so it needs no install.

Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.

Before you startWhat to know and to have readyAbout 45 minutes

What you need to know

What you need to have ready (the notebook needs none of it)

  • A calculator with a square root key. A phone calculator is enough.
  • Your c03-notes.md file, with the heading ## Sampling and statistical evidence from the “Before you start” box of Samples and populations. If you cannot find the file, make a new empty file with that name: this lesson does not use earlier answers.

The notebook was tested in Jupyter with Python 3.14.6 (standard library only).

  • A leaderboard. Model P scores 81.2% on a benchmark and model Q scores 80.4%. The table ranks P first. With 500 questions, the two results can easily change places on another set of questions.
  • A model report. “Accuracy 92% ± 3%” or “92% (95% CI 89–95)”. The second part tells you how far the true value can be from 92%.
  • Error bars in a chart. A short line through each point shows the range that the data supports. Two bars that overlap a lot are a warning against a strong claim.
  • A weekly dashboard. The accuracy of the ticket classifier was 91.4% last week and 92.6% this week. Did the model improve, or did chance change the tickets?

Model B is correct on 276 of Larkfield’s 300 test tickets: 92.0%. In the last lesson, Mei confirmed that the 300 tickets are a fair random sample. But they are still one sample. Another random 300 tickets from the same 12 months would give a slightly different accuracy: perhaps 90.7%, perhaps 93.3%.

Nobody can test on every future ticket. So the question is: how far from the true accuracy can a sample of 300 tickets be? You can see the answer when you know the population, as in the explorer below.

The population is the same 6,000 made-up tickets as in Lesson 1, with the minutes to the first reply. The true mean is 26.8 minutes. Each sample now also gets a 95% interval: a range around the sample mean, calculated only from the sample. The next sections explain how.

Explore

How often does a 95% interval contain the true mean?

Samples drawn
20
Intervals that contain the true mean
19 of 20 (95%)
Latest sample mean
22.2 min
Latest standard error
2.6 min
Latest 95% interval
16.8 to 27.5 min

19 of 20 intervals (95%) contain the true mean. With more samples, the share settles at about 93 to 95%: the long tail keeps it a little below 95% unless the samples are large.

  • All 6,000 tickets
  • Latest sample (one mark per ticket)
05001,0001,5002,000Tickets020406080100120140Minutes to first reply0 to under 10 minutes: 488 tickets10 to under 20 minutes: 1998 tickets20 to under 30 minutes: 1600 tickets30 to under 40 minutes: 819 tickets40 to under 50 minutes: 508 tickets50 to under 60 minutes: 270 tickets60 to under 70 minutes: 135 tickets70 to under 80 minutes: 79 tickets80 to under 90 minutes: 32 tickets90 to under 100 minutes: 29 tickets100 to under 110 minutes: 21 tickets110 to under 120 minutes: 8 ticketsTrue mean 26.8
05001,0001,5002,000Tickets050100150+Minutes to first reply0 to under 10 minutes: 488 tickets10 to under 20 minutes: 1998 tickets20 to under 30 minutes: 1600 tickets30 to under 40 minutes: 819 tickets40 to under 50 minutes: 508 tickets50 to under 60 minutes: 270 tickets60 to under 70 minutes: 135 tickets70 to under 80 minutes: 79 tickets80 to under 90 minutes: 32 tickets90 to under 100 minutes: 29 tickets100 to under 110 minutes: 21 tickets110 to under 120 minutes: 8 ticketsTrue mean 26.8
010203040506070Sample mean and 95% interval (minutes)True mean 26.8Sample 20: mean 22.2, interval 16.8 to 27.5Sample 19: mean 24.7, interval 20.1 to 29.4Sample 18: mean 22.9, interval 19.2 to 26.6, misses the true meanSample 17: mean 26.6, interval 20.5 to 32.7Sample 16: mean 28.6, interval 19.3 to 37.9Sample 15: mean 22.9, interval 18.4 to 27.3Sample 14: mean 24.2, interval 18.4 to 30.1Sample 13: mean 32.3, interval 23.5 to 41.1Sample 12: mean 33.2, interval 23.3 to 43.1Sample 11: mean 26.8, interval 20.6 to 33Sample 10: mean 29.6, interval 23.2 to 36.1Sample 9: mean 26.2, interval 19.3 to 33.1Sample 8: mean 28.2, interval 21.6 to 34.7Sample 7: mean 35.7, interval 25.9 to 45.5Sample 6: mean 30.1, interval 19.2 to 41.1Sample 5: mean 23.8, interval 18.4 to 29.1Sample 4: mean 28.4, interval 23.2 to 33.5Sample 3: mean 23, interval 17.4 to 28.6Sample 2: mean 28.5, interval 19.6 to 37.3Sample 1: mean 29.9, interval 24.2 to 35.6Newest at the top
010203040506070Sample mean and 95% interval (minutes)True mean 26.8Sample 20: mean 22.2, interval 16.8 to 27.5Sample 19: mean 24.7, interval 20.1 to 29.4Sample 18: mean 22.9, interval 19.2 to 26.6, misses the true meanSample 17: mean 26.6, interval 20.5 to 32.7Sample 16: mean 28.6, interval 19.3 to 37.9Sample 15: mean 22.9, interval 18.4 to 27.3Sample 14: mean 24.2, interval 18.4 to 30.1Sample 13: mean 32.3, interval 23.5 to 41.1Sample 12: mean 33.2, interval 23.3 to 43.1Sample 11: mean 26.8, interval 20.6 to 33Sample 10: mean 29.6, interval 23.2 to 36.1Sample 9: mean 26.2, interval 19.3 to 33.1Sample 8: mean 28.2, interval 21.6 to 34.7Sample 7: mean 35.7, interval 25.9 to 45.5Sample 6: mean 30.1, interval 19.2 to 41.1Sample 5: mean 23.8, interval 18.4 to 29.1Sample 4: mean 28.4, interval 23.2 to 33.5Sample 3: mean 23, interval 17.4 to 28.6Sample 2: mean 28.5, interval 19.6 to 37.3Sample 1: mean 29.9, interval 24.2 to 35.6
Each row is one sample: the dot is the sample mean, the line is its 95% interval. Dashed orange intervals miss the true mean. Made-up data, the same on every visit.
Text description of the explorer

The top chart is a histogram of the first-reply times of 6,000 made-up tickets. Most replies come after 10 to 40 minutes, and some take more than 100 minutes. The true mean is 26.8 minutes and the SD is 17.8 minutes. Orange marks along the bottom show the tickets of the latest sample.

The bottom chart has one row per sample, newest at the top. Each row shows the sample mean as a dot and the 95% interval as a line. Intervals that contain the true mean are blue; intervals that miss it are orange and dashed.

With 30 tickets per sample, about 93 to 95 of 100 intervals contain the true mean, and each interval is about 13 minutes wide. With 200 tickets, the intervals are about 5 minutes wide, and still about 95 of 100 contain the true mean. With only 5 tickets, the intervals are very wide, often wider than the chart. Only about 90 of 100 contain the true mean, because the data has a long tail.

The readout shows the number of samples and how many intervals contain the true mean. For the latest sample, it shows the mean, the standard error and the 95% interval.

What the explorer shows:

  • Each sample mean is wrong by a different amount. Most are close to 26.8 minutes; a few are far.
  • About 95 of 100 intervals contain the true mean. You can count it: the readout keeps the score. About 5 of 100 miss it, and nothing in the sample tells you which ones.
  • Larger samples give narrower intervals. But the width does not shrink as fast as the sample grows: 4 times more tickets give an interval half as wide.

Standard error: the typical size of the chance error

Section titled “Standard error: the typical size of the chance error”

The standard error (SE) is the typical distance between an estimate from one sample and the true value. In the explorer, it is the SD of the sample means: how much the dots scatter.

You can calculate it from one sample. For a mean:

SE=SDn\text{SE} = \frac{\text{SD}}{\sqrt{n}}

Read it as: “the standard error is the SD of the values, divided by the square root of the number of values”. The SD tells you how much single tickets vary. The square root of n tells you how much averaging calms that down.

With the population SD of 17.8 minutes:

Tickets in the sample (n) √n SE = 17.8 ÷ √n (minutes)
5 2.24 7.9
30 5.48 3.2
50 7.07 2.5
200 14.14 1.3

From 50 to 200 tickets, n grows 4 times, √n grows 2 times, and the SE halves: 2.5 to 1.3 minutes. This is the square-root rule. It explains why doubling a test set helps less than people expect.

Confidence interval: a range with a known success rate

Section titled “Confidence interval: a range with a known success rate”

A confidence interval is a range around an estimate, calculated with a method that contains the true value in a known share of samples. For a 95% confidence interval, that share is 95 of 100.

For a mean or an accuracy from a few hundred values or more, the rule is short:

estimate±1.96×SE\text{estimate} \pm 1.96 \times \text{SE}

Read it as: “the estimate, plus or minus about two standard errors”. The 1.96 comes from the normal distribution in Distributions in practice: 95% of a normal distribution is within 1.96 SD of its centre. For small samples, software uses a slightly larger multiplier, for example 2.20 for 12 values.

The 95% describes the method, not one interval. You saw this in the explorer: the method succeeds about 95 times in 100. Your one interval either contains the true value or not, and you cannot see which.

Worked example 1: the mean of 12 reply times

Section titled “Worked example 1: the mean of 12 reply times”

Priya starts with the 12 first-reply times from Module 1, in minutes: 12, 15, 18, 18, 20, 22, 24, 25, 27, 31, 38, 95. Grace wants to know the mean reply time on Monday mornings.

Step Value
Number of tickets, n 12
Mean 345 ÷ 12 = 28.75 minutes
SD of the sample (software divides by n − 1) 22.0 minutes
√n √12 = 3.46
SE = SD ÷ √n 22.0 ÷ 3.46 = 6.4 minutes
Multiplier for 12 values 2.20
Half-width = 2.20 × SE 2.20 × 6.4 = 14.0 minutes
95% interval 28.75 − 14.0 to 28.75 + 14.0 = 14.7 to 42.8 minutes

Why n − 1 here? In Module 1, you described these 12 tickets only, so you divided by n and got 21.1 minutes. Here, the 12 tickets are a sample, and Priya estimates the spread of all Monday tickets. The values in a sample are a little closer to their own mean than to the true mean. Dividing by n − 1 makes the SD slightly larger, to correct for this. With 12 values, the difference is 21.1 or 22.0 minutes. With hundreds of values, it almost disappears.

Priya tells Grace: “On these Monday mornings, the mean reply time is about 29 minutes. But 12 tickets cannot tell 15 minutes from 43 minutes. The one ticket from the outage, 95 minutes, makes the SD large. Collect a few weeks of Mondays before you change the staff plan.”

An accuracy is the mean of many 1s (correct) and 0s (wrong). For a share like this, the SE has its own short formula. It needs only the accuracy p and the number of tickets n:

SE=p (1−p)n\text{SE} = \sqrt{\frac{p\,(1-p)}{n}}

Read it as: “the square root of p times one minus p, divided by n”.

Step Value
Correct tickets ÷ all tickets 276 ÷ 300 = 0.92
p × (1 − p) 0.92 × 0.08 = 0.0736
÷ n 0.0736 ÷ 300 = 0.000245
SE = √ of that 0.0157, so 1.57 percentage points
Half-width = 1.96 × SE 1.96 × 1.57 = 3.07 points
95% interval 92.0 − 3.1 to 92.0 + 3.1 = 88.9% to 95.1%

Priya writes in the report: “Model B: accuracy 92.0% on 300 random test tickets (95% confidence interval 88.9% to 95.1%).” Short form: 92% ± 3.1 points.

Note the unit. Accuracy is a percentage, and the ± is in percentage points: 92% ± 3.1 points means 88.9% to 95.1%. It does not mean “3.1% of 92%”.

The formulas work for means and shares. Many AI metrics have no simple formula, for example the median reply time or a metric that combines precision and recall. The bootstrap works for all of them, with no formula.

The idea: you have only one test set, so use it as a model of the population. Build a new test set of 300 tickets: pick tickets from the 300 at random, with replacement. A ticket can come up twice, three times, or not at all. Calculate the accuracy. Repeat this a thousand times. The middle 95% of those accuracies is the bootstrap interval.

Explore

Bootstrap: resample model B's 300 test tickets

Resamples
200
Latest accuracy
92.0%
Middle 95% of resamples
89.0% to 95.0%
Formula interval (for comparison)
88.9% to 95.1%

The middle 95% of 200 resampled accuracies runs from 89.0% to 95.0%. This is the bootstrap interval.

  • Correct ticket
  • Wrong ticket
Latest resample: how often each test ticket was pickedTicket 1, correct: picked 1 timeTicket 2, correct: picked 1 timeTicket 3, correct: picked 0 timesTicket 4, correct: picked 2 times2Ticket 5, correct: picked 1 timeTicket 6, correct: picked 0 timesTicket 7, correct: picked 1 timeTicket 8, correct: picked 2 times2Ticket 9, correct: picked 4 times4Ticket 10, correct: picked 1 timeTicket 11, correct: picked 0 timesTicket 12, correct: picked 1 timeTicket 13, correct: picked 0 timesTicket 14, correct: picked 1 timeTicket 15, correct: picked 0 timesTicket 16, correct: picked 1 timeTicket 17, correct: picked 0 timesTicket 18, correct: picked 0 timesTicket 19, correct: picked 1 timeTicket 20, correct: picked 1 timeTicket 21, correct: picked 1 timeTicket 22, correct: picked 3 times3Ticket 23, correct: picked 1 timeTicket 24, correct: picked 0 timesTicket 25, correct: picked 0 timesTicket 26, correct: picked 0 timesTicket 27, correct: picked 2 times2Ticket 28, correct: picked 1 timeTicket 29, correct: picked 1 timeTicket 30, correct: picked 0 timesTicket 31, correct: picked 1 timeTicket 32, correct: picked 3 times3Ticket 33, correct: picked 1 timeTicket 34, correct: picked 1 timeTicket 35, correct: picked 2 times2Ticket 36, correct: picked 0 timesTicket 37, correct: picked 2 times2Ticket 38, correct: picked 2 times2Ticket 39, correct: picked 1 timeTicket 40, correct: picked 2 times2Ticket 41, correct: picked 1 timeTicket 42, correct: picked 1 timeTicket 43, correct: picked 0 timesTicket 44, correct: picked 2 times2Ticket 45, correct: picked 1 timeTicket 46, correct: picked 2 times2Ticket 47, correct: picked 0 timesTicket 48, correct: picked 1 timeTicket 49, correct: picked 2 times2Ticket 50, correct: picked 0 timesTicket 51, correct: picked 1 timeTicket 52, correct: picked 1 timeTicket 53, correct: picked 1 timeTicket 54, correct: picked 1 timeTicket 55, correct: picked 1 timeTicket 56, correct: picked 3 times3Ticket 57, correct: picked 1 timeTicket 58, correct: picked 2 times2Ticket 59, correct: picked 0 timesTicket 60, correct: picked 0 timesTicket 61, correct: picked 1 timeTicket 62, correct: picked 0 timesTicket 63, correct: picked 2 times2Ticket 64, correct: picked 1 timeTicket 65, correct: picked 0 timesTicket 66, correct: picked 1 timeTicket 67, correct: picked 0 timesTicket 68, correct: picked 1 timeTicket 69, correct: picked 1 timeTicket 70, correct: picked 0 timesTicket 71, correct: picked 0 timesTicket 72, correct: picked 1 timeTicket 73, correct: picked 1 timeTicket 74, correct: picked 0 timesTicket 75, correct: picked 0 timesTicket 76, correct: picked 0 timesTicket 77, correct: picked 1 timeTicket 78, correct: picked 1 timeTicket 79, correct: picked 1 timeTicket 80, correct: picked 0 timesTicket 81, correct: picked 4 times4Ticket 82, correct: picked 0 timesTicket 83, correct: picked 3 times3Ticket 84, correct: picked 3 times3Ticket 85, correct: picked 1 timeTicket 86, correct: picked 2 times2Ticket 87, correct: picked 3 times3Ticket 88, correct: picked 1 timeTicket 89, correct: picked 1 timeTicket 90, correct: picked 0 timesTicket 91, correct: picked 0 timesTicket 92, correct: picked 1 timeTicket 93, correct: picked 0 timesTicket 94, correct: picked 1 timeTicket 95, correct: picked 0 timesTicket 96, correct: picked 1 timeTicket 97, correct: picked 0 timesTicket 98, correct: picked 1 timeTicket 99, correct: picked 0 timesTicket 100, correct: picked 0 timesTicket 101, correct: picked 3 times3Ticket 102, correct: picked 1 timeTicket 103, correct: picked 1 timeTicket 104, correct: picked 1 timeTicket 105, correct: picked 1 timeTicket 106, correct: picked 2 times2Ticket 107, correct: picked 1 timeTicket 108, correct: picked 0 timesTicket 109, correct: picked 1 timeTicket 110, correct: picked 2 times2Ticket 111, correct: picked 1 timeTicket 112, correct: picked 2 times2Ticket 113, correct: picked 0 timesTicket 114, correct: picked 3 times3Ticket 115, correct: picked 0 timesTicket 116, correct: picked 1 timeTicket 117, correct: picked 0 timesTicket 118, correct: picked 1 timeTicket 119, correct: picked 2 times2Ticket 120, correct: picked 1 timeTicket 121, correct: picked 0 timesTicket 122, correct: picked 1 timeTicket 123, correct: picked 0 timesTicket 124, correct: picked 0 timesTicket 125, correct: picked 1 timeTicket 126, correct: picked 0 timesTicket 127, correct: picked 0 timesTicket 128, correct: picked 0 timesTicket 129, correct: picked 1 timeTicket 130, correct: picked 0 timesTicket 131, correct: picked 0 timesTicket 132, correct: picked 0 timesTicket 133, correct: picked 1 timeTicket 134, correct: picked 1 timeTicket 135, correct: picked 0 timesTicket 136, correct: picked 3 times3Ticket 137, correct: picked 3 times3Ticket 138, correct: picked 0 timesTicket 139, correct: picked 1 timeTicket 140, correct: picked 1 timeTicket 141, correct: picked 2 times2Ticket 142, correct: picked 0 timesTicket 143, correct: picked 0 timesTicket 144, correct: picked 0 timesTicket 145, correct: picked 1 timeTicket 146, correct: picked 1 timeTicket 147, correct: picked 3 times3Ticket 148, correct: picked 1 timeTicket 149, correct: picked 1 timeTicket 150, correct: picked 2 times2Ticket 151, correct: picked 0 timesTicket 152, correct: picked 1 timeTicket 153, correct: picked 1 timeTicket 154, correct: picked 0 timesTicket 155, correct: picked 1 timeTicket 156, correct: picked 0 timesTicket 157, correct: picked 1 timeTicket 158, correct: picked 1 timeTicket 159, correct: picked 0 timesTicket 160, correct: picked 1 timeTicket 161, correct: picked 1 timeTicket 162, correct: picked 0 timesTicket 163, correct: picked 0 timesTicket 164, correct: picked 0 timesTicket 165, correct: picked 2 times2Ticket 166, correct: picked 1 timeTicket 167, correct: picked 0 timesTicket 168, correct: picked 3 times3Ticket 169, correct: picked 0 timesTicket 170, correct: picked 0 timesTicket 171, correct: picked 2 times2Ticket 172, correct: picked 1 timeTicket 173, correct: picked 2 times2Ticket 174, correct: picked 1 timeTicket 175, correct: picked 5 times5Ticket 176, correct: picked 0 timesTicket 177, correct: picked 0 timesTicket 178, correct: picked 0 timesTicket 179, correct: picked 2 times2Ticket 180, correct: picked 2 times2Ticket 181, correct: picked 2 times2Ticket 182, correct: picked 0 timesTicket 183, correct: picked 1 timeTicket 184, correct: picked 2 times2Ticket 185, correct: picked 0 timesTicket 186, correct: picked 2 times2Ticket 187, correct: picked 0 timesTicket 188, correct: picked 0 timesTicket 189, correct: picked 2 times2Ticket 190, correct: picked 2 times2Ticket 191, correct: picked 2 times2Ticket 192, correct: picked 1 timeTicket 193, correct: picked 0 timesTicket 194, correct: picked 1 timeTicket 195, correct: picked 1 timeTicket 196, correct: picked 2 times2Ticket 197, correct: picked 0 timesTicket 198, correct: picked 0 timesTicket 199, correct: picked 0 timesTicket 200, correct: picked 1 timeTicket 201, correct: picked 0 timesTicket 202, correct: picked 0 timesTicket 203, correct: picked 3 times3Ticket 204, correct: picked 1 timeTicket 205, correct: picked 2 times2Ticket 206, correct: picked 0 timesTicket 207, correct: picked 2 times2Ticket 208, correct: picked 1 timeTicket 209, correct: picked 1 timeTicket 210, correct: picked 0 timesTicket 211, correct: picked 1 timeTicket 212, correct: picked 2 times2Ticket 213, correct: picked 0 timesTicket 214, correct: picked 0 timesTicket 215, correct: picked 2 times2Ticket 216, correct: picked 0 timesTicket 217, correct: picked 1 timeTicket 218, correct: picked 1 timeTicket 219, correct: picked 0 timesTicket 220, correct: picked 2 times2Ticket 221, correct: picked 1 timeTicket 222, correct: picked 3 times3Ticket 223, correct: picked 1 timeTicket 224, correct: picked 0 timesTicket 225, correct: picked 1 timeTicket 226, correct: picked 0 timesTicket 227, correct: picked 2 times2Ticket 228, correct: picked 0 timesTicket 229, correct: picked 1 timeTicket 230, correct: picked 3 times3Ticket 231, correct: picked 0 timesTicket 232, correct: picked 1 timeTicket 233, correct: picked 1 timeTicket 234, correct: picked 0 timesTicket 235, correct: picked 0 timesTicket 236, correct: picked 1 timeTicket 237, correct: picked 1 timeTicket 238, correct: picked 1 timeTicket 239, correct: picked 1 timeTicket 240, correct: picked 2 times2Ticket 241, correct: picked 3 times3Ticket 242, correct: picked 2 times2Ticket 243, correct: picked 1 timeTicket 244, correct: picked 2 times2Ticket 245, correct: picked 0 timesTicket 246, correct: picked 2 times2Ticket 247, correct: picked 1 timeTicket 248, correct: picked 0 timesTicket 249, correct: picked 0 timesTicket 250, correct: picked 1 timeTicket 251, correct: picked 0 timesTicket 252, correct: picked 0 timesTicket 253, correct: picked 0 timesTicket 254, correct: picked 1 timeTicket 255, correct: picked 0 timesTicket 256, correct: picked 2 times2Ticket 257, correct: picked 1 timeTicket 258, correct: picked 2 times2Ticket 259, correct: picked 2 times2Ticket 260, correct: picked 1 timeTicket 261, correct: picked 0 timesTicket 262, correct: picked 1 timeTicket 263, correct: picked 3 times3Ticket 264, correct: picked 1 timeTicket 265, correct: picked 1 timeTicket 266, correct: picked 2 times2Ticket 267, correct: picked 1 timeTicket 268, correct: picked 0 timesTicket 269, correct: picked 1 timeTicket 270, correct: picked 1 timeTicket 271, correct: picked 4 times4Ticket 272, correct: picked 0 timesTicket 273, correct: picked 1 timeTicket 274, correct: picked 0 timesTicket 275, correct: picked 5 times5Ticket 276, correct: picked 3 times3Ticket 277, wrong: picked 0 timesTicket 278, wrong: picked 0 timesTicket 279, wrong: picked 3 times3Ticket 280, wrong: picked 1 timeTicket 281, wrong: picked 0 timesTicket 282, wrong: picked 1 timeTicket 283, wrong: picked 1 timeTicket 284, wrong: picked 2 times2Ticket 285, wrong: picked 1 timeTicket 286, wrong: picked 2 times2Ticket 287, wrong: picked 1 timeTicket 288, wrong: picked 1 timeTicket 289, wrong: picked 1 timeTicket 290, wrong: picked 0 timesTicket 291, wrong: picked 2 times2Ticket 292, wrong: picked 0 timesTicket 293, wrong: picked 0 timesTicket 294, wrong: picked 1 timeTicket 295, wrong: picked 3 times3Ticket 296, wrong: picked 0 timesTicket 297, wrong: picked 1 timeTicket 298, wrong: picked 3 times3Ticket 299, wrong: picked 0 timesTicket 300, wrong: picked 0 times
Latest resample: times each ticket was pickedTicket 1, correct: picked 1 timeTicket 2, correct: picked 1 timeTicket 3, correct: picked 0 timesTicket 4, correct: picked 2 times2Ticket 5, correct: picked 1 timeTicket 6, correct: picked 0 timesTicket 7, correct: picked 1 timeTicket 8, correct: picked 2 times2Ticket 9, correct: picked 4 times4Ticket 10, correct: picked 1 timeTicket 11, correct: picked 0 timesTicket 12, correct: picked 1 timeTicket 13, correct: picked 0 timesTicket 14, correct: picked 1 timeTicket 15, correct: picked 0 timesTicket 16, correct: picked 1 timeTicket 17, correct: picked 0 timesTicket 18, correct: picked 0 timesTicket 19, correct: picked 1 timeTicket 20, correct: picked 1 timeTicket 21, correct: picked 1 timeTicket 22, correct: picked 3 times3Ticket 23, correct: picked 1 timeTicket 24, correct: picked 0 timesTicket 25, correct: picked 0 timesTicket 26, correct: picked 0 timesTicket 27, correct: picked 2 times2Ticket 28, correct: picked 1 timeTicket 29, correct: picked 1 timeTicket 30, correct: picked 0 timesTicket 31, correct: picked 1 timeTicket 32, correct: picked 3 times3Ticket 33, correct: picked 1 timeTicket 34, correct: picked 1 timeTicket 35, correct: picked 2 times2Ticket 36, correct: picked 0 timesTicket 37, correct: picked 2 times2Ticket 38, correct: picked 2 times2Ticket 39, correct: picked 1 timeTicket 40, correct: picked 2 times2Ticket 41, correct: picked 1 timeTicket 42, correct: picked 1 timeTicket 43, correct: picked 0 timesTicket 44, correct: picked 2 times2Ticket 45, correct: picked 1 timeTicket 46, correct: picked 2 times2Ticket 47, correct: picked 0 timesTicket 48, correct: picked 1 timeTicket 49, correct: picked 2 times2Ticket 50, correct: picked 0 timesTicket 51, correct: picked 1 timeTicket 52, correct: picked 1 timeTicket 53, correct: picked 1 timeTicket 54, correct: picked 1 timeTicket 55, correct: picked 1 timeTicket 56, correct: picked 3 times3Ticket 57, correct: picked 1 timeTicket 58, correct: picked 2 times2Ticket 59, correct: picked 0 timesTicket 60, correct: picked 0 timesTicket 61, correct: picked 1 timeTicket 62, correct: picked 0 timesTicket 63, correct: picked 2 times2Ticket 64, correct: picked 1 timeTicket 65, correct: picked 0 timesTicket 66, correct: picked 1 timeTicket 67, correct: picked 0 timesTicket 68, correct: picked 1 timeTicket 69, correct: picked 1 timeTicket 70, correct: picked 0 timesTicket 71, correct: picked 0 timesTicket 72, correct: picked 1 timeTicket 73, correct: picked 1 timeTicket 74, correct: picked 0 timesTicket 75, correct: picked 0 timesTicket 76, correct: picked 0 timesTicket 77, correct: picked 1 timeTicket 78, correct: picked 1 timeTicket 79, correct: picked 1 timeTicket 80, correct: picked 0 timesTicket 81, correct: picked 4 times4Ticket 82, correct: picked 0 timesTicket 83, correct: picked 3 times3Ticket 84, correct: picked 3 times3Ticket 85, correct: picked 1 timeTicket 86, correct: picked 2 times2Ticket 87, correct: picked 3 times3Ticket 88, correct: picked 1 timeTicket 89, correct: picked 1 timeTicket 90, correct: picked 0 timesTicket 91, correct: picked 0 timesTicket 92, correct: picked 1 timeTicket 93, correct: picked 0 timesTicket 94, correct: picked 1 timeTicket 95, correct: picked 0 timesTicket 96, correct: picked 1 timeTicket 97, correct: picked 0 timesTicket 98, correct: picked 1 timeTicket 99, correct: picked 0 timesTicket 100, correct: picked 0 timesTicket 101, correct: picked 3 times3Ticket 102, correct: picked 1 timeTicket 103, correct: picked 1 timeTicket 104, correct: picked 1 timeTicket 105, correct: picked 1 timeTicket 106, correct: picked 2 times2Ticket 107, correct: picked 1 timeTicket 108, correct: picked 0 timesTicket 109, correct: picked 1 timeTicket 110, correct: picked 2 times2Ticket 111, correct: picked 1 timeTicket 112, correct: picked 2 times2Ticket 113, correct: picked 0 timesTicket 114, correct: picked 3 times3Ticket 115, correct: picked 0 timesTicket 116, correct: picked 1 timeTicket 117, correct: picked 0 timesTicket 118, correct: picked 1 timeTicket 119, correct: picked 2 times2Ticket 120, correct: picked 1 timeTicket 121, correct: picked 0 timesTicket 122, correct: picked 1 timeTicket 123, correct: picked 0 timesTicket 124, correct: picked 0 timesTicket 125, correct: picked 1 timeTicket 126, correct: picked 0 timesTicket 127, correct: picked 0 timesTicket 128, correct: picked 0 timesTicket 129, correct: picked 1 timeTicket 130, correct: picked 0 timesTicket 131, correct: picked 0 timesTicket 132, correct: picked 0 timesTicket 133, correct: picked 1 timeTicket 134, correct: picked 1 timeTicket 135, correct: picked 0 timesTicket 136, correct: picked 3 times3Ticket 137, correct: picked 3 times3Ticket 138, correct: picked 0 timesTicket 139, correct: picked 1 timeTicket 140, correct: picked 1 timeTicket 141, correct: picked 2 times2Ticket 142, correct: picked 0 timesTicket 143, correct: picked 0 timesTicket 144, correct: picked 0 timesTicket 145, correct: picked 1 timeTicket 146, correct: picked 1 timeTicket 147, correct: picked 3 times3Ticket 148, correct: picked 1 timeTicket 149, correct: picked 1 timeTicket 150, correct: picked 2 times2Ticket 151, correct: picked 0 timesTicket 152, correct: picked 1 timeTicket 153, correct: picked 1 timeTicket 154, correct: picked 0 timesTicket 155, correct: picked 1 timeTicket 156, correct: picked 0 timesTicket 157, correct: picked 1 timeTicket 158, correct: picked 1 timeTicket 159, correct: picked 0 timesTicket 160, correct: picked 1 timeTicket 161, correct: picked 1 timeTicket 162, correct: picked 0 timesTicket 163, correct: picked 0 timesTicket 164, correct: picked 0 timesTicket 165, correct: picked 2 times2Ticket 166, correct: picked 1 timeTicket 167, correct: picked 0 timesTicket 168, correct: picked 3 times3Ticket 169, correct: picked 0 timesTicket 170, correct: picked 0 timesTicket 171, correct: picked 2 times2Ticket 172, correct: picked 1 timeTicket 173, correct: picked 2 times2Ticket 174, correct: picked 1 timeTicket 175, correct: picked 5 times5Ticket 176, correct: picked 0 timesTicket 177, correct: picked 0 timesTicket 178, correct: picked 0 timesTicket 179, correct: picked 2 times2Ticket 180, correct: picked 2 times2Ticket 181, correct: picked 2 times2Ticket 182, correct: picked 0 timesTicket 183, correct: picked 1 timeTicket 184, correct: picked 2 times2Ticket 185, correct: picked 0 timesTicket 186, correct: picked 2 times2Ticket 187, correct: picked 0 timesTicket 188, correct: picked 0 timesTicket 189, correct: picked 2 times2Ticket 190, correct: picked 2 times2Ticket 191, correct: picked 2 times2Ticket 192, correct: picked 1 timeTicket 193, correct: picked 0 timesTicket 194, correct: picked 1 timeTicket 195, correct: picked 1 timeTicket 196, correct: picked 2 times2Ticket 197, correct: picked 0 timesTicket 198, correct: picked 0 timesTicket 199, correct: picked 0 timesTicket 200, correct: picked 1 timeTicket 201, correct: picked 0 timesTicket 202, correct: picked 0 timesTicket 203, correct: picked 3 times3Ticket 204, correct: picked 1 timeTicket 205, correct: picked 2 times2Ticket 206, correct: picked 0 timesTicket 207, correct: picked 2 times2Ticket 208, correct: picked 1 timeTicket 209, correct: picked 1 timeTicket 210, correct: picked 0 timesTicket 211, correct: picked 1 timeTicket 212, correct: picked 2 times2Ticket 213, correct: picked 0 timesTicket 214, correct: picked 0 timesTicket 215, correct: picked 2 times2Ticket 216, correct: picked 0 timesTicket 217, correct: picked 1 timeTicket 218, correct: picked 1 timeTicket 219, correct: picked 0 timesTicket 220, correct: picked 2 times2Ticket 221, correct: picked 1 timeTicket 222, correct: picked 3 times3Ticket 223, correct: picked 1 timeTicket 224, correct: picked 0 timesTicket 225, correct: picked 1 timeTicket 226, correct: picked 0 timesTicket 227, correct: picked 2 times2Ticket 228, correct: picked 0 timesTicket 229, correct: picked 1 timeTicket 230, correct: picked 3 times3Ticket 231, correct: picked 0 timesTicket 232, correct: picked 1 timeTicket 233, correct: picked 1 timeTicket 234, correct: picked 0 timesTicket 235, correct: picked 0 timesTicket 236, correct: picked 1 timeTicket 237, correct: picked 1 timeTicket 238, correct: picked 1 timeTicket 239, correct: picked 1 timeTicket 240, correct: picked 2 times2Ticket 241, correct: picked 3 times3Ticket 242, correct: picked 2 times2Ticket 243, correct: picked 1 timeTicket 244, correct: picked 2 times2Ticket 245, correct: picked 0 timesTicket 246, correct: picked 2 times2Ticket 247, correct: picked 1 timeTicket 248, correct: picked 0 timesTicket 249, correct: picked 0 timesTicket 250, correct: picked 1 timeTicket 251, correct: picked 0 timesTicket 252, correct: picked 0 timesTicket 253, correct: picked 0 timesTicket 254, correct: picked 1 timeTicket 255, correct: picked 0 timesTicket 256, correct: picked 2 times2Ticket 257, correct: picked 1 timeTicket 258, correct: picked 2 times2Ticket 259, correct: picked 2 times2Ticket 260, correct: picked 1 timeTicket 261, correct: picked 0 timesTicket 262, correct: picked 1 timeTicket 263, correct: picked 3 times3Ticket 264, correct: picked 1 timeTicket 265, correct: picked 1 timeTicket 266, correct: picked 2 times2Ticket 267, correct: picked 1 timeTicket 268, correct: picked 0 timesTicket 269, correct: picked 1 timeTicket 270, correct: picked 1 timeTicket 271, correct: picked 4 times4Ticket 272, correct: picked 0 timesTicket 273, correct: picked 1 timeTicket 274, correct: picked 0 timesTicket 275, correct: picked 5 times5Ticket 276, correct: picked 3 times3Ticket 277, wrong: picked 0 timesTicket 278, wrong: picked 0 timesTicket 279, wrong: picked 3 times3Ticket 280, wrong: picked 1 timeTicket 281, wrong: picked 0 timesTicket 282, wrong: picked 1 timeTicket 283, wrong: picked 1 timeTicket 284, wrong: picked 2 times2Ticket 285, wrong: picked 1 timeTicket 286, wrong: picked 2 times2Ticket 287, wrong: picked 1 timeTicket 288, wrong: picked 1 timeTicket 289, wrong: picked 1 timeTicket 290, wrong: picked 0 timesTicket 291, wrong: picked 2 times2Ticket 292, wrong: picked 0 timesTicket 293, wrong: picked 0 timesTicket 294, wrong: picked 1 timeTicket 295, wrong: picked 3 times3Ticket 296, wrong: picked 0 timesTicket 297, wrong: picked 1 timeTicket 298, wrong: picked 3 times3Ticket 299, wrong: picked 0 timesTicket 300, wrong: picked 0 times
05101520Resamples8486889092949698100Accuracy of the resampled test set (%)93.3%: 1889.3%: 290.7%: 792.3%: 2092%: 2089.7%: 895%: 591%: 1791.7%: 1494.3%: 593%: 1291.3%: 1292.7%: 1690%: 890.3%: 993.7%: 1294%: 395.7%: 287.3%: 288.7%: 189%: 388%: 195.3%: 294.7%: 1Test set 92.0%middle 95%
05101520Resamples84889296100Accuracy of the resampled test set (%)93.3%: 1889.3%: 290.7%: 792.3%: 2092%: 2089.7%: 895%: 591%: 1791.7%: 1494.3%: 593%: 1291.3%: 1292.7%: 1690%: 890.3%: 993.7%: 1294%: 395.7%: 287.3%: 288.7%: 189%: 388%: 195.3%: 294.7%: 1Test set 92.0%middle 95%
Each resample picks 300 tickets from the test set, with replacement, and records the accuracy. The histogram collects the accuracies; the bracket marks the middle 95%.
Text description of the explorer

The top picture shows the 300 test tickets as squares: 276 correct (blue) and 24 wrong (orange). After a resample, an empty square shows a ticket that the resample did not pick. A number in a square shows how many times the resample picked that ticket.

The histogram below shows the accuracy of each resampled test set, from about 87% to 97%. The results gather around 92.0%, the real test result. After a few hundred resamples, the middle 95% runs from about 89% to 95%. The formula interval from worked example 2, 88.9% to 95.1%, is almost the same.

The readout shows the number of resamples, the latest accuracy, the middle 95% of all resamples so far, and the formula interval for comparison.

The bootstrap and the formula agree: about 89% to 95%. That agreement is the point. The bootstrap gets there by simulation, so it also works when you have no formula. In Python, data scientists often use scipy.stats.bootstrap or a short loop with numpy.

Now read the leaderboard from the start of the lesson. It shows four made-up models on a public benchmark of 500 questions. Each row has the accuracy and its 95% interval.

A made-up leaderboard with error bars: the accuracy of four models on 500 questions, with 95% confidence intervals.
Text description of the chart

Four rows, each with a dot and an interval bar. Model P: 81.2% (77.8% to 84.6%). Model Q: 80.4% (76.9% to 83.9%). Model R: 78.6% (75.0% to 82.2%). Model S: 74.0% (70.2% to 77.8%). The intervals of P, Q and R overlap almost completely. The interval of S overlaps the others only a little.

Each interval is about ±3.5 points wide, because 500 questions is a small sample. The order of P, Q and R is not supported by the data: their intervals overlap almost completely. S is probably weaker than P, but the gap is less certain than the ranking suggests. Lesson 3 shows the right way to compare two models directly.

Model A is correct on 273 of the same 300 tickets.

  1. In c03-notes.md, add a heading: ### Interval for model A.
  2. Calculate the accuracy p = 273 ÷ 300.
  3. Calculate p × (1 − p).
  4. Divide the result by 300.
  5. Take the square root. This is the SE.
  6. Multiply the SE by 1.96. This is the half-width.
  7. Write the interval: p minus the half-width to p plus the half-width.
  8. Write one sentence for a report, as Priya did.

Expected result: SE about 1.65 points, half-width about 3.2 points, interval about 87.8% to 94.2%.

Show an example answer
Step Value
p 273 ÷ 300 = 0.91
p × (1 − p) 0.91 × 0.09 = 0.0819
÷ 300 0.000273
SE √0.000273 = 0.0165 = 1.65 points
Half-width 1.96 × 1.65 = 3.24 points
Interval 91.0 − 3.2 to 91.0 + 3.2 = 87.8% to 94.2%

Report sentence: “Model A: accuracy 91.0% on 300 random test tickets (95% confidence interval 87.8% to 94.2%).”

Compare with model B, 88.9% to 95.1%. The two intervals overlap almost completely. That is a first warning against “B is better”. Lesson 3 does the full comparison.

Change one condition: the number of tickets. Mei can build a random test set of 1,200 tickets (one week’s worth, but sampled from 12 months). Suppose model B is again 92.0% correct on it.

In c03-notes.md:

  1. Calculate the SE and the 95% interval.
  2. Compare the width with the interval from 300 tickets. Use the square-root rule to explain the difference.
  3. Omar wants ±0.75 points. Estimate how many tickets that needs.
Show an example answer
  1. p × (1 − p) = 0.0736. ÷ 1,200 = 0.0000613. SE = √0.0000613 = 0.0078 = 0.78 points. Half-width = 1.96 × 0.78 = 1.53 points. Interval: 90.5% to 93.5%.
  2. 1,200 is 4 times 300. √4 = 2, so the SE and the interval are half as wide: ±1.5 points instead of ±3.1 points.
  3. ±0.75 points is half of ±1.5 points again, so 4 times more tickets: about 4,800 tickets. Each halving of the interval costs four times the data. Omar must decide if that labelling work is worth it.

You do not need code for this course. If you want, this box repeats worked example 2 and a bootstrap with the Python standard library.

Run Python in your browser

Try it: interval and bootstrap for model B

Run the code. The formula interval is 88.9 to 95.1. The bootstrap gives almost the same range. Then change correct to 273 (model A) and run it again: you get the interval from the guided practice.

Common mistake

An interval read as a promise about one week

Symptom: the model report says “accuracy 92% ± 3.1 points (88.9% to 95.1%)”. Omar tells the director of customer service: “Every week, at least 89% of tickets will go to the right team.” In the first week of December, Grace’s team checks a sample and finds 87.6%. The director asks what went wrong with the model.

Cause: the interval was read as certainty about one outcome. It is not. The interval estimates one number: the accuracy on tickets like the 300 test tickets, over the long run. It says nothing certain about one week. One week of 1,200 tickets has its own chance variation. December also has a different ticket mix (45% delivery tickets), so its tickets are not like the test tickets. And a 95% method misses 1 time in 20, without a warning. It also says nothing about one ticket: each ticket is either right or wrong.

Fix: write what the interval covers. For example: “On tickets like our test set, we estimate the accuracy at 92%. Values from 89% to 95% fit the test result.” Do not turn it into a promise for a week or a ticket. For weekly results, check a random sample of tickets each week and track the accuracy. Expect it to move. Check months with a different ticket mix separately.

Check your understanding

A test set of 300 tickets gives an interval of ±3.1 points. Explain why a test set of 1,200 tickets gives about ±1.5 points, not ±0.8 points.

Show the answer

The SE is the SD divided by the square root of n. Four times more tickets make √n two times larger, so the SE and the interval become half as wide: ±1.5 points. The interval would be a quarter as wide (±0.8 points) only if it shrank in proportion to n. It does not, because chance errors partly cancel out when you average, but only in proportion to √n.

Check your understanding

In the explorer, about 5 of 100 intervals miss the true mean. In real work, you have only one test set and one interval. Can you tell if your interval is one of the misses? What does the 95% tell you, then?

Show the answer

No. A missing interval looks like any other interval; nothing in the sample marks it. The 95% describes the method: if you use it on many samples, about 95 of 100 intervals contain the true value. So you can trust the method most of the time, but you cannot be certain about any one interval. This is why one result is never final evidence, and why a second, independent test set is valuable.

Recap

  • One test set is one sample. Another sample gives a different result, by chance.
  • The standard error is the typical size of that chance error. For a mean, SE = SD ÷ √n. For an accuracy p on n tickets, SE = √(p(1 − p)/n).
  • A 95% confidence interval is about the estimate ± 1.96 SE. The method contains the true value in about 95 of 100 samples. It is not a promise about one week or one ticket.
  • Four times more data gives an interval half as wide.
  • The bootstrap resamples the test set with replacement. Its middle 95% gives an interval for any metric, without a formula.
  • Model B: 92.0% (88.9% to 95.1%). Model A: 91.0% (87.8% to 94.2%).

Next, you compare A and B directly, on the same 300 tickets, and decide what the one-point difference supports.

Next: Compare alternatives