Could It Be Chance?
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 43 of 64.
Five readers of a sans font beat five serif readers by 6.6 seconds. If the font changed nothing, any time could sit in either group. Shuffle to act that out:
rng.permutation(a) returns a shuffled copy. Repeat it 1000 times and keep every fake gap:
serif = np.array([52, 47, 58, 50, 55])
sans = np.array([45, 49, 41, 50, 44])
pooled = np.concatenate([serif, sans])
rng = np.random.default_rng(4)
diffs = []
for i in range(1000):
shuffled = rng.permutation(pooled)
diffs.append(shuffled[5:].mean() - shuffled[:5].mean())Chance gaps pile up around 0; the real one sits near the edge. This is a permutation test.
Before you trust that one model beats another, check that shuffling rarely makes a gap that big.
Challenge
MediumDogs learned to sit two ways: clicker and treats hold each dog's sessions. The starter makes rng and shuffles.
- Compute the real gap: treats mean minus clicker mean.
- Pool
clickerthentreats; shuffleshufflestimes, split atlen(clicker), keep each fake gap the same way round; no other draws. - Print the real gap, the first fake gap, the smallest and largest, to 2 decimals.
For other dogs:
Real difference: 3.0
First shuffle: -1.0
Smallest shuffled: -3.33
Largest shuffled: 3.67Try it yourself
import numpy as np
# Training sessions each dog needed to learn "sit"
clicker = np.array([9, 12, 8, 11, 10, 7, 13])
treats = np.array([14, 11, 15, 12, 16, 13])
# Don't change the lines below
rng = np.random.default_rng(12)
shuffles = 1000
# 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 Life8Comparing Groups
Box Plots by GroupDifference in MeansCould It Be Chance?P-ValuesThe t-TestRecap: A/B Test3Probability 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