1. Courses
  2. Data
  3. Statistics for Data Work
  4. Stop 2

35 minIntermediate

Samples, uncertainty and confidence intervals

You rarely measure everything. You count stock in 40 stores, audit 400 orders or survey 1,000 customers, and then someone asks what that says about all of Mangrove Market. This lesson shows you how a fair sample speaks for the whole, how much it can wobble, and how to report a number with an honest range around it.

By the end you can

  • Tell a random sample from a convenience sample, and draw a random one in a spreadsheet, SQL or pandas
  • Explain the standard error with a simulation, and why four times the sample only halves it
  • Compute a 95% confidence interval for a mean (with t for small samples) and for a proportion
  • Say what a confidence interval does and does not mean, and check it with a bootstrap

The everyday picture

A cook making a big pot of lentil soup wants to know if it needs salt. She doesn't drink the whole pot. She stirs it well, takes one spoonful and tastes it.

Three things make that spoonful trustworthy. First, she stirred: every part of the pot had the same chance of ending up in the spoon. A spoonful skimmed from the top without stirring tells her about the top, however carefully she tastes it. Second, a bigger spoon gives a steadier answer than a tiny one. Third, the size of the pot barely matters. One well-stirred spoonful tells her as much about a 20-litre pot as about a 5-litre one.

And she knows a single spoonful can mislead a little. If the soup tastes "just about right", she might add a pinch more and taste again. Statistics gives you the same habits with numbers: stir (sample at random), use a big enough spoon (sample size), and say how much the answer could be off (a confidence interval).

Population, sample and how you pick it

The is everything you want to know about: all 320 Mangrove stores, every app order in October, every registered customer. The number you want, such as the average stock accuracy across all 320 stores, is fixed but unknown. A is the part you measure. You compute the same number on the sample and use it as your estimate.

Why sample at all? Because measuring everything costs time and money. Stock accuracy is the share of counted items where the system's stock figure matches what is on the shelf; at Mangrove it is about 82%. To check it, staff count items by hand. Counting every item in every store would stop the stores for days. Counting 200 random items in 40 random stores takes an afternoon per store.

How you pick the sample matters more than how big it is. In a , chance decides which units get in, so every store has the same chance. In a , people choose: the stores near head office, the managers who volunteer, the customers who answer a survey. Those units often differ from the rest. Managers who volunteer for a stock count tend to run tidy stores. That error is called , and a bigger sample does not fix it. Ask 400 volunteers instead of 40 and you get the same wrong answer, only more confidently.

Drawing a random sample takes a minute in any tool:

  • Spreadsheet: next to the list of 320 stores, add a column with =RAND(), then sort by it and take the first 40 rows. RAND changes every time the sheet recalculates, so copy the column and paste it as values before you sort.
  • SQL (Postgres): SELECT id FROM site WHERE kind = 'store' ORDER BY random() LIMIT 40;
  • pandas: stores.sample(n=40, random_state=2026). The random_state number makes the draw repeatable, so a colleague gets the same 40 stores.

If cities differ a lot, you can also draw at random within each city in proportion to its store count. That is a , and it makes sure no city is left out by bad luck.

Quick check

Last year the stock-accuracy audit used the 40 stores whose managers volunteered. Tomás suggests counting in 120 volunteer stores this year, "so the result is more reliable". What is wrong with the plan?

How much does a sample wobble? Simulate it

Draw a different sample and you get a slightly different answer. You can see how much by trying it many times on a population you know. In Describing data honestly you met Mangrove's app baskets: average about $18.40, median about $14, with a long right tail from big monthly shops. Tomás built a simulated population of one million baskets with that shape. Its standard deviation is about $15.70.

Then he did what you can never do with real data: he drew a random sample of 100 baskets, wrote down its mean, and repeated that 1,000 times. The 1,000 sample means look like this:

  • They pile up around the true $18.40. No single sample is far off very often.
  • They spread much less than single baskets do. Their standard deviation is $1.55, against $15.70 for single baskets.
  • The middle 95% of them fall between $15.71 and $21.55. The lowest was $13.51 and the highest $26.95.
  • Their histogram is close to a bell shape, even though single baskets are heavily skewed.

That pile of possible answers is the . Its standard deviation has its own name, the (SE), and you don't need 1,000 samples to know it. For a mean:

Output
standard error = s / √n        s = standard deviation of the values, n = sample size

baskets: s = $15.70, n = 100  →  SE = 15.70 / √100 = 15.70 / 10 = $1.57
the simulation measured $1.55: the formula predicts the wobble from ONE sample

The s comes from your one sample (STDEV.S in a spreadsheet, stddev_samp in Postgres, .std() in pandas, which divides by n − 1 by default). So one sample tells you both an estimate and how much to trust it.

1 million baskets mean $18.40 long right tail $0 $40 $70+ draw 100, take the mean $17.05 $15.93 $17.83 and so on, 1,000 times 1,000 means, zoomed in true $18.40 spread $1.55 95% of means: $15.71–$21.55 $14 $19 $24
Top: one million simulated baskets, heavily skewed (bars are $10 wide). Middle: draw 100 baskets at random and take their mean, 1,000 times; the first three samples gave $17.05, $15.93 and $17.83. Bottom: the 1,000 means on a stretched $14 to $24 axis. They form a narrow, nearly symmetric bell around the true $18.40 with a standard deviation of $1.55. On the top axis the whole bell would be only about as wide as one bar.

Why √n? Errors in single values partly cancel when you average them: a big basket here, a small one there. They cancel more slowly than the sample grows. Tomás's simulation, repeated for three sample sizes, shows the pattern:

Output
sample size   SE by formula ($15.70 / √n)   SD of 1,000 simulated means
     25               $3.14                          $3.12
    100               $1.57                          $1.55
    400               $0.79                          $0.80

Four times the sample halves the error, because √4 = 2. To cut the error to a tenth you need 100 times the data. This is why precision gets expensive fast, and why a sample of 1,000 is common in surveys: going to 4,000 costs four times as much and only halves the error.

The bell shape at the bottom of the diagram is the at work. In plain words: averages of enough random values pile up in a bell shape, even when the values themselves are lopsided. How many is "enough" depends on the skew. For mildly skewed data 30 often does; for baskets with a long tail, means of 25 are still a little lopsided and 100 or more look like a bell. The theorem is about averages; it never makes the baskets themselves normal.

Quick check

A sample of 100 baskets gives a mean with a standard error of about $1.57. Imran wants the standard error under $0.80. About how many baskets does he need?

From standard error to a confidence interval

Because sample means form a bell, about 95% of them land within 1.96 standard errors of the true value. Turn that around and you get a recipe for a range around your one estimate, the (CI):

Output
95% CI ≈ estimate ± 1.96 × SE          the part after ± is the margin of error

With small samples you also estimate the spread s from few values, which adds uncertainty. So instead of 1.96 you use a slightly bigger multiplier from the with n − 1 degrees of freedom: 2.262 for 10 values, 2.201 for 12, 2.023 for 40, and 1.984 for 100. Above a few hundred values it is practically 1.96. A spreadsheet gives it with =T.INV.2T(0.05, n-1), Python with scipy.stats.t.ppf(0.975, n - 1).

For a proportion (a share of yes or no answers), the standard error has its own formula, because the spread of yes/no data depends only on the share itself:

Output
SE = √( p × (1 − p) / n )

Store 17 counts 200 random items; 158 match the system:  p = 158 / 200 = 0.79
SE = √(0.79 × 0.21 / 200) = 0.0288
95% CI = 0.79 ± 1.96 × 0.0288 = 0.79 ± 0.056  →  73.4% to 84.6%

This shortcut works when there are at least about 10 yes and 10 no answers. With fewer, or with a share near 0% or 100%, it can even give a range below zero. Then use the , which statsmodels offers as proportion_confint(count, n, method="wilson"). For store 17 it gives 72.8% to 84.1%, close to the shortcut. For 1 cut item in 20 orders, the shortcut says −4.6% to 14.6% and Wilson says 0.9% to 23.6%.

A useful sense of scale: a poll of 1,000 people on a question near 50/50 has a margin of error of about 3 points, because 1.96 × √(0.25 / 1,000) = 0.031. You will hear "plus or minus 3 points" in news polls for that reason.

20 samples of 100 true mean $18.40 misses $12 $15 $21 $24
Twenty of Tomás's simulated samples, each turned into a 95% confidence interval (mean ± t × SE). Each bar is one sample's interval, and they all differ. Nineteen cross the true $18.40; one sample happened to catch mostly small baskets and its interval misses. Over all 1,000 samples, 943 intervals contained the true mean.

What a confidence interval does and does not mean

The "95%" describes the method. If you repeat the whole process (draw a fresh random sample, compute the interval) many times, about 95% of the intervals will contain the true value. The second diagram shows it: 19 of these 20 intervals catch $18.40. Your one interval either contains the true value or it doesn't, and you can't tell which. That is why careful people say "we are 95% confident" and describe the method behind it.

Four readings to avoid:

  • "95% of stores lie in the interval." The interval is about the average. Single stores spread far wider: in the audit below, the stores ranged from 63.5% to 97%, while the interval for the average is about 5 points wide.
  • "The interval covers every kind of error." It covers only the luck of the draw. A convenience sample gives an interval just as narrow around the wrong value. Miscounted shelves and a wrong system figure are not in it either.
  • "Values near the edge are as likely as the middle." Values near the centre fit the data best; the edges are the least plausible values still in range.
  • "The method is exactly 95% every time." It is close to 95% when its assumptions hold. With skewed data and small samples it misses more often. In Tomás's simulation the intervals caught the true basket mean about 91% of the time with samples of 25, about 93% with 100 and about 95% with 400.

The width is the useful part. A result of "81.9%, between 79.4% and 84.4%" tells a manager whether the answer is precise enough to act on. A bare "81.9%" hides that.

Quick check

From a random sample of 40 stores, the 95% CI for average stock accuracy is 79.4% to 84.4%. Which sentence is right?

The bootstrap: a confidence interval by resampling

The formulas above work for means and proportions. For a median, a 90th percentile or a ratio, the formula is hard or missing. The , introduced by Bradley Efron in 1979, gets an interval by simulation from the one sample you have:

  1. Treat your sample as a small model of the population.
  2. Draw a new sample of the same size from it with replacement (a store can be picked twice, another not at all).
  3. Compute your statistic (mean, median, anything) on it.
  4. Repeat 10,000 times. The middle 95% of the 10,000 results is a 95% interval (the percentile method).

It is the simulation from earlier, run on your sample instead of on a population you can't see. It needs no bell-shape formula, so it works for medians and ratios. It still needs a random sample, and with very small samples (say under 20) it tends to be too narrow, because a small sample shows too little of the population's spread.

In Python, a few lines do it by hand, and SciPy (version 1.7 or later) has it built in:

Python
import numpy as np
from scipy import stats

values = np.array(accuracy_pct)              # the 40 measured stores
rng = np.random.default_rng(7)               # a seed makes the result repeatable
boot = [rng.choice(values, size=len(values), replace=True).mean()
        for _ in range(10_000)]              # 10,000 resampled means
print(np.percentile(boot, [2.5, 97.5]))      # middle 95% = the interval

# built in; its default method is "BCa", a corrected version of the percentile method
res = stats.bootstrap((values,), np.mean, confidence_level=0.95, n_resamples=10_000)
print(res.confidence_interval)

Spreadsheets have no bootstrap function. You can build one with RANDBETWEEN and INDEX across many columns, but it is slow and fragile; this is a good place to move to Python (the same analysis in Python).

Do it

Estimate one number for the whole chain from a random sample, and report it with a 95% confidence interval.

  1. Name the population, the unit and the number

    "All 320 stores; the unit is a store; the number is average stock accuracy." If you will measure items inside stores, the unit is still the store.

  2. Draw the sample at random, and save it

    Fix the seed so the draw can be repeated and checked. In Postgres, setseed takes a value between −1 and 1 and fixes the following random() calls in the same session.

    SQL
    SELECT setseed(0.2026);                -- makes the draw repeatable
    CREATE TABLE audit_sample AS
    SELECT id AS site_id, city
    FROM site
    WHERE kind = 'store'                   -- stores only, no distribution centres
    ORDER BY random()                      -- shuffle all 320
    LIMIT 40;                              -- keep the first 40
  3. Measure every sampled unit, including the awkward ones

    A store that is "too busy this week" stays in the sample; replacing it with a willing store brings the bias back.

  4. Compute mean, standard error and the interval

    One value per store in column B (rows 2 to 41):

    Output
    cell  label    formula                 result
    E1    n        =COUNT(B2:B41)          40
    E2    mean     =AVERAGE(B2:B41)        81.9
    E3    s        =STDEV.S(B2:B41)        7.8
    E4    SE       =E3/SQRT(E1)            1.23
    E5    t        =T.INV.2T(0.05, E1-1)   2.023
    E6    margin   =E5*E4                  2.5
    E7    low      =E2-E6                  79.4
    E8    high     =E2+E6                  84.4
    
    One-step check: =CONFIDENCE.T(0.05, E3, E1) returns the same margin.
  5. Check it with a bootstrap

    Run the resampling code from the bootstrap section. If the two intervals differ a lot, your data is skewed or small; report the bootstrap and say why.

  6. Write the sentence

    Markdown
    **Stock accuracy averages about 82% (95% CI 79% to 84%).**
    Method: 40 of 320 stores drawn at random (seed saved), 200 random items counted per store,
    store-level average with a t interval; bootstrap agrees (79.5% to 84.3%).
    Not covered by the interval: counting mistakes, and stores that changed after the count.
    Decision: the 85% target is not met; next, the 10 lowest stores.

Bring it to your team

Ask these whenever a number comes from part of the data.

  • How was the sample chosen: by chance, or by whoever was easy to reach?
  • What is the unit we sampled, and is the n in the formula that unit?
  • What is the confidence interval, and is it narrow enough for the decision we have to make?
  • Did any sampled unit drop out, and could the dropouts differ from the rest?
  • If we doubled the sample, what would it cost, and would the narrower interval change the decision?

Red flags

  • A single number with no n and no range.
  • "We asked the stores that replied" presented as a chain-wide result.
  • An interval built on items or orders when stores or customers were sampled.
  • "95% of stores fall in the confidence interval."
  • A busy store swapped out of the sample for a willing one.

Practice

A confidence interval in SQL

The table stock_count(site_id, city, in_sample, lines_counted, lines_accurate) holds this month's counts. Twelve stores were drawn at random (in_sample = true); three more volunteered and counted anyway (in_sample = false). Using only the random stores, compute each store's accuracy as a percentage, then return one row with n_stores, mean_pct, se_pct (standard deviation of the store percentages ÷ √n), ci_low and ci_high (mean ∓ 2.201 × SE; 2.201 is the t value for 11 degrees of freedom). Round the last four to one decimal.

SQL exerciseRuns in a real PostgreSQL inside your browser
-- 95% confidence interval for average stock accuracy (random stores only)
WITH store AS (
  SELECT site_id,
         100.0 * lines_accurate / lines_counted AS pct   -- one value per store
  FROM stock_count
  -- add: keep only the randomly sampled stores
)
SELECT count(*) AS n_stores,
       round(avg(pct), 1) AS mean_pct
       -- add: se_pct = stddev_samp(pct) / sqrt(count(*))
       -- add: ci_low and ci_high = avg(pct) -/+ 2.201 * se
       -- tip: wrap a float in ::numeric before round(), e.g. round((x)::numeric, 1)
FROM store;
Ctrl + Enter runs
Show a solution
SQL
WITH store AS (
  SELECT site_id,
         100.0 * lines_accurate / lines_counted AS pct
  FROM stock_count
  WHERE in_sample                                     -- volunteers would add bias
), s AS (
  SELECT count(*) AS n,
         avg(pct) AS mean,
         stddev_samp(pct) / sqrt(count(*)) AS se      -- s / √n
  FROM store
)
SELECT n AS n_stores,
       round(mean, 1) AS mean_pct,
       round(se::numeric, 1) AS se_pct,
       round((mean - 2.201 * se)::numeric, 1) AS ci_low,
       round((mean + 2.201 * se)::numeric, 1) AS ci_high
FROM s;

The random stores average 81.1%, with an interval from 77.2% to 84.9%. With the three volunteers included the average would be 83.4%: a 15-store sample that looks bigger and is less trustworthy. The SE uses the store as the unit (12 values), not the 2,400 items. sqrt(count(*)) returns a float, which is why the results are cast to numeric before rounding.

Pass the exercise to finish this task.

A confidence interval for a proportion in JavaScript

Write proportionCI(yes, n) that returns { p, se, low, high } as fractions: p = yes / n, se = √(p(1 − p) / n), and low/high = p ∓ 1.96 × se. A share can't be below 0 or above 1, so clamp low at 0 and high at 1. Example: store 17 counted 200 items and 158 matched, so the interval is about 0.734 to 0.846.

JavaScript exerciseRuns in your browser with tests
function proportionCI(yes, n) {
  const p = yes / n;
  // add: se = square root of p * (1 - p) / n   (Math.sqrt)
  // add: low = p - 1.96 * se, but never below 0   (Math.max)
  // add: high = p + 1.96 * se, but never above 1  (Math.min)
  return { p };
}
Ctrl + Enter runs
Show a solution
JavaScript
function proportionCI(yes, n) {
  const p = yes / n;
  const se = Math.sqrt(p * (1 - p) / n);       // standard error of a share
  const low = Math.max(0, p - 1.96 * se);      // a share can't go below 0
  const high = Math.min(1, p + 1.96 * se);     // or above 1
  return { p, se, low, high };
}

The clamping hides a deeper problem: with 1 yes in 20 the shortcut is unreliable, because there are fewer than 10 yes answers. The Wilson interval for 1 in 20 is about 0.009 to 0.236, much wider on the high side than the clamped 0 to 0.146.

Pass the exercise to finish this task.

Check yourself

Tomás surveys 1,000 randomly chosen app customers out of about 2 million. A colleague in a smaller company surveys 1,000 random customers out of 50,000. Both measure a share near 50%. Whose margin of error is smaller?

An audit of 400 random app orders found 36 with an item cut for being out of stock: 9%, with a margin of error of about 2.8 points. The operations lead wants a margin of about 1.4 points. Roughly how many orders must be audited?

You need a 95% interval for the median basket from a random sample of 300 baskets. What is the most sensible approach?

Flashcards

Open my review

Key takeaways

  • How you pick the sample matters first: chance must decide which units get in, or a bigger sample only repeats the bias.
  • The standard error, s / √n, says how much an estimate wobbles between samples; four times the data halves it.
  • Means of enough random values form a bell (the central limit theorem), which is why estimate ± 1.96 SE works; use t for small samples and √(p(1 − p) / n) for proportions.
  • The 95% describes the method, not single stores, and the interval covers only sampling luck, never bias or counting errors.
  • Use the unit you sampled as n, and bootstrap when there is no simple formula, such as for a median.

Sources and further reading

Episode transcriptOne spoonful, forty stores, an honest range · about 10 minutes · tap any line to play from there
  1. NadiaImran, last year's stock audit said Mangrove's stock accuracy was eighty-seven and a half percent. Do you remember how it was done?
  2. ImranForty stores counted their shelves. The managers who volunteered, I think.
  3. NadiaThat's the problem we're fixing today. How a small sample can speak for all three hundred and twenty stores, how much it can wobble, and how to report a number with an honest range around it.
  4. ImranSo, samples and confidence intervals. The part of statistics everyone nods along to.
  5. NadiaLet's make it concrete. Picture a cook with a big pot of lentil soup. She wants to know if it needs salt. She doesn't drink the whole pot.
  6. ImranShe stirs it and tastes one spoonful.
  7. NadiaRight. And three things make that spoonful trustworthy. She stirred, so every part of the pot had the same chance of reaching the spoon. A bigger spoon gives a steadier answer. And the size of the pot barely matters. One well-stirred spoonful works for a twenty-litre pot as well as a five-litre one.
  8. ImranAnd if she skims from the top without stirring, she learns about the top.
  9. NadiaHowever carefully she tastes it. That's the difference between a random sample and a convenience sample. In a random sample, chance decides who gets in. In a convenience sample, people decide: the stores near head office, the managers who volunteer.
  10. ImranVolunteers for a stock count probably run tidy stores.
  11. NadiaExactly. That's bias. And a bigger sample doesn't fix it. Ask a hundred and twenty volunteers instead of forty and you get the same wrong answer, only more confidently.
  12. ImranSo how do I draw a random forty? Without a lottery machine.
  13. NadiaIn a spreadsheet, add a column of random numbers with the RAND function, paste it as values so it stops changing, sort by it and take the top forty. In Postgres, order by random and limit forty. In pandas, the sample method with a fixed random state, so a colleague can repeat the draw.
  14. ImranFine. Now the wobble. If Tomás draws a different forty, he gets a different answer.
  15. NadiaHe does, and you can see how much with a simulation. Tomás built a pretend population of one million app baskets shaped like ours. Average eighteen dollars forty, median about fourteen, a long tail of big monthly shops. Standard deviation about fifteen dollars seventy.
  16. NadiaThen he drew a hundred baskets at random, took the mean, and did that a thousand times. If you can see your screen, the first diagram shows it. If not, picture a lopsided histogram at the top, and at the bottom a narrow bell.
  17. ImranThe bell is the thousand means.
  18. NadiaYes. They pile up around the true eighteen forty. Their spread is about a dollar fifty-five, against fifteen seventy for single baskets. And ninety-five percent of them land between about fifteen seventy and twenty-one fifty-five.
  19. ImranAnd the bell shape, even though baskets are lopsided. That's the central limit theorem?
  20. NadiaIn plain words, yes. Averages of enough random values pile up in a bell, even when the values themselves are skewed. It never makes the baskets normal. Only the averages.
  21. ImranBut in real life I only get one sample. I can't draw a thousand.
  22. NadiaYou don't need to. The spread of that pile has a name, the standard error, and there's a formula for it. The standard deviation of your one sample, divided by the square root of the sample size. Fifteen seventy divided by ten is about a dollar fifty-seven. The simulation measured a dollar fifty-five.
  23. ImranSo the square root. If I want half the error, I double the sample?
  24. NadiaThat's the common guess, and it's wrong. Doubling only divides the error by about one point four. To halve it you need four times the sample, because the square root of four is two. A hundred baskets gives about a dollar fifty-seven, four hundred gives about seventy-nine cents.
  25. ImranWhich is why precision gets expensive fast.
  26. NadiaVery. Now the confidence interval. Because the means form a bell, about ninety-five percent of them fall within one point nine six standard errors of the truth. So you take your estimate, plus or minus one point nine six standard errors.
  27. ImranAnd for small samples?
  28. NadiaYou estimate the spread from few values, which adds doubt, so you use a slightly bigger number from the t distribution. For forty stores it's about two point zero two. For twelve, two point two. Above a few hundred, it's practically one point nine six again.
  29. ImranAnd percentages? Like the share of items that match the system?
  30. NadiaThe standard error of a share is the square root of p times one minus p, over n. Store seventeen counted two hundred items and a hundred and fifty-eight matched. Seventy-nine percent, with an interval from about seventy-three to eighty-five.
  31. ImranAnd if the counts are tiny?
  32. NadiaWhen there are fewer than about ten yes or ten no answers, that shortcut breaks down. Use the Wilson interval instead.
  33. ImranThe second diagram has twenty bars and a dashed line.
  34. NadiaEach bar is the interval from one sample of a hundred baskets. The dashed line is the true eighteen forty. Nineteen bars cross it. One sample happened to catch mostly small baskets, and its bar misses. Over all thousand samples, nine hundred and forty-three intervals caught the truth.
  35. ImranSo ninety-five percent is about the method.
  36. NadiaThat's the key sentence. Repeat the whole process many times and about ninety-five percent of the intervals contain the true value. Your one interval either does or doesn't, and you can't tell which.
  37. ImranOkay, the real audit. What did Tomás find this year?
  38. NadiaForty stores drawn at random, two hundred items counted in each. Average store accuracy eighty-one point nine percent. Standard deviation about seven point eight points. Standard error about one point two three. Times two point zero two, that's plus or minus two and a half.
  39. ImranSo roughly seventy-nine point four to eighty-four point four.
  40. NadiaYes. And the head of stores wanted to know if they'd reached eighty-five percent. The whole interval is below it. So, not yet. And last year's eighty-seven and a half is outside the interval. It came from tidier, self-chosen stores, so it doesn't show a fall.
  41. ImranHere's a mistake I'd make. Forty stores times two hundred items is eight thousand items. Why not use eight thousand as the sample size? The interval would be much tighter.
  42. NadiaAbout three times tighter, and wrong. The stores were sampled. Items in the same store share the same staff, the same routine, the same system, so they rise and fall together. The sample size is forty stores. Same rule as in store pilots: count stores, not deliveries.
  43. ImranWhat does the interval not tell me? I want the list for my next review.
  44. NadiaIt's about the average, not single stores. The stores in the audit ranged from about sixty-four to ninety-seven percent. It covers only the luck of the draw, so bias and miscounted shelves aren't in it.
  45. ImranAnything else?
  46. NadiaWith skewed data and small samples it misses more than five percent of the time. With twenty-five baskets, the intervals caught the truth only about ninety-one percent of the time.
  47. ImranAnd for a median? There's no neat formula for that.
  48. NadiaThat's where the bootstrap comes in, from Bradley Efron in nineteen seventy-nine. Treat your sample as a small model of the population. Draw from it with replacement, the same size, ten thousand times, and compute your statistic each time. The middle ninety-five percent of the results is your interval.
  49. ImranAnd for the stock audit?
  50. NadiaThe bootstrap gave seventy-nine point five to eighty-four point three. Almost the same as the formula, which is reassuring.
  51. NadiaSo the Do it section, in words. Name the population, the unit and the number. Draw the sample at random with a saved seed. Measure every sampled store, even the one that says it's too busy.
  52. ImranThen the numbers.
  53. NadiaCompute the mean, the standard error and the interval, in a spreadsheet or in Python. Check it with a bootstrap. Then write one sentence with the number, the range and the method.
  54. ImranMy review questions, then. How was the sample chosen, by chance or by convenience? What unit did we sample, and is that the n in the formula? And is the interval narrow enough for the decision we have to make?
  55. NadiaAdd one. Did anyone drop out, and could the dropouts differ from the rest?
  56. NadiaA quick one to think about. Tomás surveys a thousand random customers out of two million. A smaller company surveys a thousand out of fifty thousand. Whose margin of error is smaller? Take a second.
  57. ImranI want to say the smaller company. But the soup says the pot size doesn't matter.
  58. NadiaThe soup is right. Both are about plus or minus three points. The sample size sets the margin, not the population size.
  59. ImranLet me sum up. One, how you pick the sample comes first, because size never fixes bias. Two, the standard error shrinks with the square root of n, so four times the data halves it. Three, an interval is the estimate plus or minus about two standard errors, and the ninety-five percent is about the method.
  60. NadiaGood. One nuance on the third: the interval covers only sampling luck. And make n the unit you sampled.
  61. ImranNext time?
  62. NadiaNext episode, a new checkout button looks like a winner, and we ask how surprised we should be. Hypothesis tests, without the fog.
Finished reading?Mark it complete to fill in its stop on your course line.

Use it at work

A/B test readout

Conversion per variant, the lift, a confidence interval and a plain-language verdict.

Open in the Data Lab
Next stopHypothesis tests without the fog
Was this lesson helpful?
Questions and discussion

Loading the discussion…