Bias and Variance#
Every estimator is a random variable (it depends on the random sample). How do we judge whether an estimator is "good"? Two orthogonal qualities:
- Bias: . Does the estimator systematically over- or under-shoot the truth on average?
- Variance: . How much does the estimate jump around from sample to sample?
Mean Squared Error (MSE) combines both:
Proof: Add and subtract :
The cross-term vanishes because .
This is the bias–variance decomposition: to reduce MSE, you can reduce bias or reduce variance. But estimators that reduce one often increase the other — the bias–variance tradeoff.
Estimator terminology:
- Unbiased: (bias = 0).
- Consistent: as (MSE → 0).
- Efficient: achieves the smallest possible variance among a class (e.g., among unbiased estimators).
Worked Estimators — Sample Variance#
The most famous bias–variance story. Given i.i.d. :
- MLE (denominator ): .
- Unbiased (denominator ): .
— biased downward. — unbiased.
Why ? The sample mean is the value that minimises the sum of squared deviations. This "uses up" one degree of freedom, making the sum slightly too small on average. Dividing by corrects for this.
For large , the difference is negligible. For small samples, the unbiased estimator has larger variance (its MSE can actually be higher than the biased MLE — the bias–variance tradeoff in action).
Confidence Interval (lite)#
A confidence interval gives a range of plausible values for , calibrated by the sampling distribution of the estimator.
For the mean (CLT-based, large ): By the CLT, . Standardising:
Rearranging gives the confidence interval:
For a 95% CI, . In practice, is unknown and replaced by the sample standard deviation , which adds uncertainty; for large this is fine, for small use the -distribution (not covered here).
Critical interpretation — what a CI is NOT:
A 95% CI does not mean "there is a 95% probability that lies in this interval." is a fixed constant (in the frequentist view). The correct interpretation: if we repeated the experiment many times and computed a 95% CI each time, approximately 95% of those intervals would contain the true . The procedure works 95% of the time.
This is a subtle but important distinction from the Bayesian credible interval, where you can say "the probability that is in this interval is 95%," because is treated as a random variable with a posterior distribution.
Worked example. observations, , . A 95% CI for :
Bootstrap#
The bootstrap is a computational method to estimate the sampling distribution of any statistic without assuming a parametric model. It replaces theory with computation.
Algorithm (nonparametric bootstrap):
- From the original sample , draw a bootstrap sample by sampling times with replacement from the original data.
- Compute the statistic of interest on the bootstrap sample.
- Repeat steps 1–2 times (typically or more) to get .
- The empirical distribution of the approximates the sampling distribution of .
What the bootstrap gives you:
- Standard error: .
- Confidence interval (percentile): the and quantiles of the bootstrap distribution.
- Bias estimate: (mean of bootstrap estimates minus original estimate).
The bootstrap works when: the empirical distribution of the sample is a good approximation of the true population distribution. It is especially useful when no closed-form formula for the standard error exists (e.g., median, correlation coefficient, or a complex model output).
When bootstrap fails: dependent data (need block bootstrap), heavy tails (need subsampling bootstrap), small (empirical distribution is too coarse).
What We Are Not Doing#
This course deliberately limits classical statistics to what supports ML practice:
- No Neyman–Pearson hypothesis testing (null vs alternative, rejection regions, p-values, Type I/II errors).
- No multiple testing corrections (Bonferroni, FDR).
- No ANOVA or experimental design.
- No power analysis.
These are important topics in classical applied statistics, but they are not prerequisites for understanding MLE, KL divergence, or probabilistic generative models — which is what this course targets.
As sample size n increases, which component of MSE typically shrinks?
Knowledge check#
An estimator has bias 0.2 and variance 0.09. What is its MSE?
Browser lab#
Bootstrap the sampling distribution of the sample mean and compare the bootstrap SE to the CLT formula.
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(42)
true_mu, true_sigma = 50.0, 10.0
n = 30
data = rng.normal(true_mu, true_sigma, size=n)
B = 2000
boot_means = np.empty(B)
for b in range(B):
boot_sample = rng.choice(data, size=n, replace=True)
boot_means[b] = boot_sample.mean()
se_boot = boot_means.std()
se_clt = data.std(ddof=1) / np.sqrt(n)
ci_lower = np.percentile(boot_means, 2.5)
ci_upper = np.percentile(boot_means, 97.5)
fig, axes = plt.subplots(1, 2, figsize=(12, 4.5))
axes[0].hist(boot_means, bins=40, density=True, alpha=0.7, color="tab:blue")
axes[0].axvline(data.mean(), color="tab:red", linewidth=2, label=f"Sample mean = {data.mean():.2f}")
axes[0].axvline(ci_lower, color="black", linestyle="--", linewidth=1)
axes[0].axvline(ci_upper, color="black", linestyle="--", linewidth=1, label=f"95% CI: [{ci_lower:.2f}, {ci_upper:.2f}]")
axes[0].set_title("Bootstrap distribution of the mean")
axes[0].set_xlabel("Mean"); axes[0].legend()
x_grid = np.linspace(boot_means.min(), boot_means.max(), 200)
normal_approx = (1/(np.sqrt(2*np.pi)*se_clt)) * np.exp(-0.5*((x_grid - data.mean())/se_clt)**2)
axes[1].hist(boot_means, bins=40, density=True, alpha=0.5, label="Bootstrap")
axes[1].plot(x_grid, normal_approx, "tab:red", linewidth=2, label=f"CLT approx (SE={se_clt:.2f})")
axes[1].set_title("Bootstrap vs CLT normal approximation")
axes[1].set_xlabel("Mean"); axes[1].legend()
plt.tight_layout()
plt.show()
print(f"Sample: n={n}, mean={data.mean():.3f}, std={data.std(ddof=1):.3f}")
print(f"SE (CLT): {se_clt:.4f}")
print(f"SE (boot): {se_boot:.4f}")
print(f"95% CI (boot): [{ci_lower:.3f}, {ci_upper:.3f}]")
print(f"95% CI (CLT): [{data.mean()-1.96*se_clt:.3f}, {data.mean()+1.96*se_clt:.3f}]")