⚙️ Setup¶
import numpy as np
from scipy import stats
rng = np.random.default_rng(381)
print("ready")
ready
print(f"P(X < 3) = 3/10 = {stats.uniform.cdf(3, 0, 10):.3f}")
print(f"mean = (a+b)/2 = {stats.uniform.mean(0, 10):.1f}")
P(X < 3) = 3/10 = 0.300 mean = (a+b)/2 = 5.0
Answer: for a uniform, probability is proportional to length, so P(X < 3) = 3/10 = 0.30, and the mean is the midpoint (0 + 10)/2 = 5 minutes.
scale = 10 # mean = 1/lambda = 10
print(f"P(wait > 15) = e^(-15/10) = {1 - stats.expon.cdf(15, scale=scale):.4f}")
print(f"P(wait < 5) = 1 - e^(-5/10) = {stats.expon.cdf(5, scale=scale):.4f}")
P(wait > 15) = e^(-15/10) = 0.2231 P(wait < 5) = 1 - e^(-5/10) = 0.3935
Answer: P(wait > 15) = e⁻¹·⁵ ≈ 0.223, and P(wait < 5) = 1 − e⁻⁰·⁵ ≈ 0.393. The exponential has a long right tail, so long waits, while less likely, are far from rare.
mu, sd = 100, 15
print(f"P(X < 115) = {stats.norm.cdf(115, mu, sd):.4f}")
p_mid = stats.norm.cdf(115, mu, sd) - stats.norm.cdf(85, mu, sd)
print(f"P(85 < X < 115) = {p_mid:.4f}")
P(X < 115) = 0.8413 P(85 < X < 115) = 0.6827
Answer: 115 is one standard deviation above the mean, so P(X < 115) = Φ(1) ≈ 0.8413. The range 85 to 115 is exactly μ ± 1σ, so P(85 < X < 115) ≈ 0.6827, the famous 68% from the empirical rule (Chapter 39).
mu, sd = 100, 15
p90 = stats.norm.ppf(0.90, mu, sd)
print(f"90th percentile = {p90:.2f}")
90th percentile = 119.22
Answer: the 90th percentile is μ + z₀.⁹₀·σ = 100 + 1.2816(15) ≈ 119.2. The percent-point function (inverse CDF) turns a probability back into a value, the reverse of asking "what fraction is below x".
sample = rng.normal(50, 12, size=300)
mu_hat, sigma_hat = sample.mean(), sample.std()
print(f"MLE mu_hat = {mu_hat:.2f}")
print(f"MLE sigma_hat = {sigma_hat:.2f}")
print("Assuming Gaussian errors, minimizing squared error == maximizing this likelihood.")
MLE mu_hat = 48.98 MLE sigma_hat = 12.52 Assuming Gaussian errors, minimizing squared error == maximizing this likelihood.
Answer: the maximum-likelihood estimates are simply the sample mean and sample standard deviation (here near 50 and 12, the values we generated from). The deeper point: assuming normally distributed errors makes least-squares fitting the maximum-likelihood choice, which is why squared-error loss is everywhere in regression and deep learning.