Exercise: Measure bias and variance at a fixed input#
Investigate how flexibility changes prediction across repeated training samples. A single train/test curve cannot reveal the sampling variance of a fitted procedure.
See the shared submission guidance and rubric. Run from book/exercises; the setup below supplies data, not completed answers.
# Shared simulator and small utility functions; no live service is called.
source("../R/course.R")
truth <- function(x) sin(pi*x)
set.seed(109)
x_grid <- seq(-1, 1, length.out=81)
sigma <- 0.35
degrees <- c(1, 3, 9)
1. Compare three learners on one sample#
Generate 40 training x values uniformly on [-1,1] and y=truth(x)+Normal(0,sigma). Fit polynomial regressions of degrees 1, 3, and 9. Plot predictions over x_grid with the true curve; report training MSE. Use poly through a formula so prediction uses the fitted basis.
# Generate a training table and fit the three degrees.
Your explanation and evidence:
2. Separate bias from variance#
Repeat training 300 times. Save each model’s predictions on x_grid, not just its training loss. At x=0.5 calculate squared bias and empirical prediction variance. Compare their sum plus sigma^2 with MSE against freshly generated noisy outcomes at that same x. Explain finite-Monte-Carlo disagreement.
# Store predictions by repetition and degree; summarize the decomposition at x=0.5.
Your explanation and evidence:
3. Check where the conclusion holds#
Plot mean prediction and a central 90% range across fits over the grid. Compare interior and boundary variability. Explain why the across-fit range is neither a confidence interval from one fit nor a prediction interval for a new response. Decide whether your results support a universal U-shaped test-error claim.
# Plot across-fit summaries and compare boundary and interior locations.
Your explanation and evidence:
What to submit#
A one-sample fit plot, a numerical bias–variance decomposition, and an across-sample variability plot. Report simulation settings and retain unstable fits as findings to investigate.