Train Delays
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 61 of 64.
Two commuter lines, Maple and Birch, share a station. For 24 days it logged how late each line's rush-hour train was, in trains.csv:
Three signal failures held trains an hour or more: real, but big enough to swamp any mean.
The manager asks: is Maple really later, or could the gap be chance? Tame the extremes, then let shuffling decide.
Challenge
HardThe program reads the fence factor k; 1.5 is the box plot's rule.
- Fence all delays at
Q1 - k * IQRandQ3 + k * IQR, count the outliers, then cap every delay withnp.clip. - Print each line's mean capped delay and the difference, Maple minus Birch.
- Shuffle 2000 times: pool Maple's capped delays then Birch's, one
rng.permutationper round, the first 24 as fake Maple. - Print the p-value, sign ignored, and the verdict:
unlikely to be chancebelow 0.05, elsecould be chance.
Round to 2 decimals, p to 3. For k = 2:
Fences: 0.25 to 26.5 (3 outliers)
Maple mean: 15.27
Birch mean: 12.21
Difference: 3.06
p-value: 0.03
Verdict: unlikely to be chanceDoes the verdict hold for a stricter or a looser k?
Try it yourself
import numpy as np
import pandas as pd
k = float(input())
df = pd.read_csv("trains.csv")
delays = df["delay_min"]
# Don't change the line below
rng = np.random.default_rng(5)
# 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