Bias–Variance Decomposition by Simulation

medium
Unit 12 · Ensembles & Model Selection0/3 to milestone

The expected test error of a model splits into bias² (how far the average prediction is from the truth), variance (how much predictions move between training sets) and irreducible noise. Measure it by simulating many training sets.

Implement bias_variance(f, noise_sd, degree, n_train, n_sims, x_test, rng):

  • For each simulation, draw x = rng.uniform(0, 1, n_train), then y = f(x) + rng.normal(0, noise_sd, n_train) (in that order), fit np.polyfit(x, y, degree) and predict at x_test.
  • Return a dict with "bias2" (the mean over test points of (mean_prediction - f(x_test))²), "variance" (the mean over test points of the variance of predictions across simulations) and "noise" (noise_sd²).

Also implement expected_mse(result): bias2 + variance + noise.

#model-selection#bias-variance#simulation2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Bias–Variance Decomposition by Simulation · TechGradify