Menu
CoddyTech

The Law of Large Numbers

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

Toss a coin 10 times and 8 heads is no surprise. Keep tossing, and the share of heads crawls toward 0.5:

A line chart of the share of heads over 300 tosses: it swings between 0.3 and 1 in the first tosses, then hugs a dashed red line at the probability 0.5

Each point needs the share so far. np.cumsum gives the running total; divide it by the tosses so far:

heads = np.array([True, False, True, True])
print(np.cumsum(heads))
print(np.round(np.cumsum(heads) / np.arange(1, 5), 2))

This prints:

[1 1 2 3]
[1.   0.5  0.67 0.75]

ax.axhline(y) draws the reference line straight across the chart:

ax.plot(np.arange(1, 301), share)
ax.axhline(0.5, color="red", linestyle="--")

That is the law of large numbers: more tries, closer to the probability. A model scoring well on 20 samples may be lucky; on 2000, hardly.

challenge icon

Challenge

Medium

A cereal brand hides a toy in 20% of boxes. The program reads how many get opened; the starter fills toys (True for a toy) and labels the chart.

  1. Compute the running share of toys.
  2. Print it after 10 boxes and after all, to 3 decimals.
  3. Plot it against box numbers from 1, add a black dotted line (":") at 0.2 and show it.

For 80 other boxes:

After 10 boxes: 0.1
After 80 boxes: 0.212

and draws this chart. Then Coddy describes yours.

A chart titled Toys in Cereal Boxes: the running share over 80 boxes sits at 0 for 8 boxes, jumps around, then settles near a black dotted line at 0.2

Try it yourself

import numpy as np
import matplotlib.pyplot as plt

boxes = int(input())

# Don't change the lines below
rng = np.random.default_rng(25)
toys = rng.random(boxes) < 0.2

fig, ax = plt.subplots()
ax.set_title("Toys in Cereal Boxes")
ax.set_xlabel("Boxes opened")
ax.set_ylabel("Share with a toy")

# 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