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:
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
MediumA 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.
- Compute the running share of toys.
- Print it after 10 boxes and after all, to 3 decimals.
- 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.212and draws this chart. Then Coddy describes yours.
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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler