Start learning free

LESSON 123 OF 165 · MACHINE LEARNING: STATISTICS AND A/B TESTS

How Sure? The Bootstrap

Practice

Your 300 customers spent 189.93 on average. Other customers would give another average, so how sure can you be of it? A confidence interval answers with a range, and the bootstrap finds one using only the data you already have.

The trick is to make up new samples by drawing from your data with replacement. rng.choice(values, size=len(values), replace=True) gives back as many values as there are, picked at random: a value can come up twice, or not at all. So each made-up sample, a resample, has its own mean.

ages = np.array([18, 25, 31, 40, 52])
resample = rng.choice(ages, size=len(ages), replace=True)
# for example [25 25 40 52 18]: 25 twice, 31 left out

Make 2,000 resamples and keep each one's mean in a list. np.quantile(values, [0.025, 0.975]) gives back two numbers: 2.5% of the values are below the first and 2.5% above the second. The middle 95% of the resample means, between those two numbers, is a 95% confidence interval for the average spend.

When you are done, your script prints:

Average spend: 189.93
First three: [190.87 187.73 191.48]
95% confidence interval: 173.12 to 207.21

Your task: fill a list means with the means of 2,000 resamples of spent, print the first three rounded to 2 decimals, then print the 2.5% and 97.5% quantiles of means as the 95% confidence interval.


Assignment
Follow the Coding Tutorial to bootstrap a 95% confidence interval for the average spend.


Hint
The resample and the append go inside the loop, indented; the prints and np.quantile come after it. Use the rng from the starting code: the expected numbers depend on it. np.round(a_list, 2) rounds every number of a list at once.


STEP 1 OF 7
    Ctrl + Enter to check