Week 12 · Capstone · Compare three prediction rules
Choose a model under one evaluation protocol and quantify the cost of that choice.
Choose a model under one evaluation protocol and quantify the cost of that choice.
Work through the problem
Compare a mean baseline, ridge, and a random forest on the same nonlinear regression task. Use identical training, validation, and test splits. Decide by validation MAE, the mean of absolute residuals. Keep the candidate list fixed when you evaluate the selected model on test data.
For each held-out row compute dᵢ = |yᵢ−ŷ_baseline,i|−|yᵢ−ŷ_chosen,i|. The average d gives the improvement. Resample these paired differences to estimate uncertainty for this fixed fitted pair. Consider whether the error reduction justifies greater operating complexity.
Run the Python lab
import numpy as np
from sklearn.dummy import DummyRegressor
from sklearn.linear_model import Ridge
from sklearn.ensemble import RandomForestRegressor
rng = np.random.default_rng(81)
X = rng.uniform(-2,2,(360,2))
y = 2*X[:,0]**2 + X[:,1] + rng.normal(0,.25,len(X))
models = [DummyRegressor(), Ridge(), RandomForestRegressor(n_estimators=75,random_state=8)]
for m in models:
m.fit(X[:180],y[:180])
valid = [abs(m.predict(X[180:270])-y[180:270]).mean() for m in models]
winner = int(np.argmin(valid))
errors = [abs(models[i].predict(X[270:])-y[270:]) for i in [0,winner]]
delta = errors[0]-errors[1]
draws = rng.choice(delta,(3000,len(delta))).mean(1)
print("Validation MAE:", np.round(valid,2))
print("Chosen:", type(models[winner]).__name__)
print("Test improvement and interval:", round(delta.mean(),2),np.quantile(draws,[.025,.975]).round(2))
assert winner==2 and delta.mean()>0
Output from the lab
Validation MAE: [2.22 2.11 0.34]
Chosen: RandomForestRegressor
Test improvement and interval: 1.64 [1.34 1.95]
Practice
- Why bootstrap paired errors instead of resampling each model’s test errors separately?
Check your reasoning
- Both models predict the same rows. Pairing preserves that shared source of variability.