Menu
CoddyTech

Bigger Samples, Less Noise

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 22 of 64.

Bigger samples give better estimates, but how much better? Here are 1000 means of 25 shopping baskets, and 1000 of 100 baskets:

Two bells of sample means of items per basket, both centered near 30. The means of 25 baskets in gray spread from about 20 to 40; the means of 100 baskets in blue form a bell half as wide

sample_means measures each spread. The standard error, std / np.sqrt(n), predicts it with no simulation:

for n in [25, 100]:
    spread = sample_means(baskets, n, 1000).std()
    print(n, np.round(spread, 2), np.round(baskets.std() / np.sqrt(n), 2))

This prints:

25 3.43 3.45
100 1.72 1.72

Four times the data, half the noise: that is the square root at work. A model tested on 400 examples has a score twice as precise as on 100.

challenge icon

Challenge

Medium

bags holds the weights of a mill's 5000 flour bags, in grams. Quality control weighs a sample hourly; the program reads the standard error it accepts.

  1. Find the smallest n whose standard error, bags.std() / np.sqrt(n), is at most that: start at 1, add 1 while too large.
  2. Check it: the std of 300 sample means from sample_means.
  3. Print as below, to 2 decimals.

For another mill and 2.5:

Bags to weigh: 24
Standard error: 2.46 g
Simulated spread: 2.37 g

Try it yourself

import numpy as np

target = float(input())

# Don't change the lines below: the weight in grams of every bag of flour
rng = np.random.default_rng(22)
bags = np.round(rng.normal(500, 8, size=5000), 1)


# Your function from Sample Means Vary: the means of count samples
def sample_means(data, size, count):
    means = []
    for i in range(count):
        sample = rng.choice(data, size=size, replace=False)
        means.append(sample.mean())
    return np.array(means)


# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler