Week 05 · Bias, variance, and model flexibility
See why adding flexibility can raise prediction error.
← Week 4 · Subject contents · Week 6 →
See why adding flexibility can raise prediction error.
Work through the problem
At a fixed input x, expected squared prediction error against a new noisy response decomposes into σ² + Var(f̂(x)) + [E(f̂(x))−f(x)]². This follows by adding and subtracting E(f̂) and noting that the centered cross terms average to zero.
Repeatedly sample noisy observations of sin(πx), then predict at x = 0.72. A line cannot represent the full bend. A degree-seven curve can vary considerably as noise changes. A middle degree can lower bias without incurring as much variance. The comparison is specific to this input and data-generating process.
Flexibility changes two components of error.
Run the Python lab
import numpy as np
rng = np.random.default_rng(22)
x = np.linspace(-1,1,12)
point, noise = .72, .25
truth = np.sin(np.pi*point)
models = [1,3,7]
pred = np.array([[np.polyval(np.polyfit(x,np.sin(np.pi*x)+rng.normal(0,noise,len(x)),d),point) for _ in range(500)] for d in models])
bias = (pred.mean(axis=1)-truth)**2
variance = pred.var(axis=1)
print("Degree, squared bias, variance:")
for row in zip(models,bias,variance):
print(*np.round(row,4))
assert np.allclose(((pred-truth)**2).mean(1),bias+variance)
Output from the lab
Degree, squared bias, variance:
1.0 0.0646 0.0119
3.0 0.0105 0.0142
7.0 0.0 0.0341
Practice
- Which component would remain if f were known exactly?
Check your reasoning
- Irreducible outcome variance σ².