Bootstrap Bias-Variance of a Stacked Blend of Two Polynomial Fits

Quick Overview

Measure the squared bias and variance of a low-degree polynomial, a high-degree polynomial and their linear stack over bootstrap resamples, with blend weights fit on out-of-bag points. It tests NumPy least squares, out-of-bag bookkeeping, and why the decomposition is exact against a noiseless target.

Bootstrap Bias-Variance of a Stacked Blend of Two Polynomial Fits

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement the four starter functions below to measure the squared bias and variance of a low-degree polynomial, a high-degree polynomial and their linear stack. Run this over `n_boot` bootstrap resamples, scoring predictions on `x_test` against the noiseless truth `f_test`. ```python def fit_poly_predict(x_train, y_train, x_eval, degree): ... def stack_weights(p_low, p_high, y): ... def bias_variance(preds, f_test): ... def bootstrap_stacking_experiment(x_train, y_train, x_test, f_test, low_degree, high_degree, n_boot, seed): ... ``` The functions must follow these rules: - `fit_poly_predict` does a least-squares fit (`np.linalg.lstsq`) on an increasing Vandermonde matrix with `degree + 1` columns, then predicts at `x_eval`. - `stack_weights` runs a no-intercept least squares of `y` on the columns `[p_low, p_high]`. It returns `[0.5, 0.5]` when there are fewer than 2 rows. - `bias_variance(preds, f_test)` takes `preds` of shape `(B, m)` and returns floats: `bias2` is the point-averaged squared gap between the mean prediction and `f_test`, `variance` is the point-averaged `ddof=0` variance, and `error` is the MSE over all entries. - The experiment creates `rng = np.random.default_rng(seed)`. Each replicate draws `idx = rng.integers(0, n, size=n)`, fits both degrees on the resample, fits blend weights on the out-of-bag points (base-model predictions against `y_train[oob]`), and sets the stack to `w[0]*low + w[1]*high` on `x_test`. - The experiment returns the keys `"low"`, `"high"` and `"stack"`, each mapped to a `bias_variance` dict, plus `"weights"`, an array of shape `(n_boot, 2)`. For example: ```python bias_variance(np.array([[1.0, 2.0], [3.0, 4.0]]), np.array([1.0, 1.0])) # {'bias2': 2.5, 'variance': 1.0, 'error': 3.5} fit_poly_predict([-1, 0, 0.5, 1], [-1, 1, 2, 3], [2.0, -3.0], 1) # array([ 5., -5.]) ``` ```hint Out-of-bag rows A bootstrap index array repeats some rows and misses others. Build the out-of-bag set from the rows that never appear in `idx`, and check what your code does when fewer than two of them are left. ``` ```hint A built-in test With `ddof=0` and the noiseless truth as the target, the three numbers `bias_variance` returns satisfy an exact relation. Use it to test your code. ``` ### Constraints & Assumptions - `x_train` and `y_train` are 1-D arrays of length `n`. `x_test` and `f_test` are 1-D arrays of length `m`. - `y_train` is noisy; `f_test` is the noiseless true function evaluated at `x_test`. - `low_degree` and `high_degree` are non-negative integers, the second one larger. - The generator is created once, and each replicate makes exactly one `rng.integers(0, n, size=n)` draw. ### What a Strong Answer Covers - A correct increasing Vandermonde fit with `degree + 1` columns, and what `lstsq` does when a resample has fewer distinct x values than coefficients. - An out-of-bag mask built from the rows missing from the resample, with the `[0.5, 0.5]` fallback when fewer than two rows remain. - Blend weights fit on out-of-bag predictions against `y_train[oob]` and applied to the same replicate's test predictions. - Bias, variance and error computed over the right axes, and why `error = bias2 + variance` holds against the noiseless truth. - The cost per replicate and the memory for the three prediction matrices. ### Follow-up Questions - Why fit the blend weights on out-of-bag points instead of the bootstrap sample? - How would you constrain the blend to be a convex combination? - Where does irreducible noise appear in this decomposition?

Overview: Measure the squared bias and variance of a low-degree polynomial, a high-degree polynomial and their linear stack over bootstrap resamples, with blend weights fit on out-of-bag points. It tests NumPy least squares, out-of-bag bookkeeping, and why the decomposition is exact against a noiseless target.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement the four starter functions below to measure the squared bias and variance of a low-degree polynomial, a high-degree polynomial and their linear stack. Run this over n_boot bootstrap resamples, scoring predictions on x_test against the noiseless truth f_test.

def fit_poly_predict(x_train, y_train, x_eval, degree): ...

def stack_weights(p_low, p_high, y): ...

def bias_variance(preds, f_test): ...

def bootstrap_stacking_experiment(x_train, y_train, x_test, f_test,
                                  low_degree, high_degree, n_boot, seed): ...

The functions must follow these rules:

  • fit_poly_predict does a least-squares fit ( np.linalg.lstsq ) on an increasing Vandermonde matrix with degree + 1 columns, then predicts at x_eval .
  • stack_weights runs a no-intercept least squares of y on the columns [p_low, p_high] . It returns [0.5, 0.5] when there are fewer than 2 rows.
  • bias_variance(preds, f_test) takes preds of shape (B, m) and returns floats: bias2 is the point-averaged squared gap between the mean prediction and f_test , variance is the point-averaged ddof=0 variance, and error is the MSE over all entries.
  • The experiment creates rng = np.random.default_rng(seed) . Each replicate draws idx = rng.integers(0, n, size=n) , fits both degrees on the resample, fits blend weights on the out-of-bag points (base-model predictions against y_train[oob] ), and sets the stack to w[0]*low + w[1]*high on x_test .
  • The experiment returns the keys "low" , "high" and "stack" , each mapped to a bias_variance dict, plus "weights" , an array of shape (n_boot, 2) .

For example:

bias_variance(np.array([[1.0, 2.0], [3.0, 4.0]]), np.array([1.0, 1.0]))
# {'bias2': 2.5, 'variance': 1.0, 'error': 3.5}

fit_poly_predict([-1, 0, 0.5, 1], [-1, 1, 2, 3], [2.0, -3.0], 1)
# array([ 5., -5.])

Constraints & Assumptions

  • x_train and y_train are 1-D arrays of length n . x_test and f_test are 1-D arrays of length m .
  • y_train is noisy; f_test is the noiseless true function evaluated at x_test .
  • low_degree and high_degree are non-negative integers, the second one larger.
  • The generator is created once, and each replicate makes exactly one rng.integers(0, n, size=n) draw.

What a Strong Answer Covers Guidance

  • A correct increasing Vandermonde fit with degree + 1 columns, and what lstsq does when a resample has fewer distinct x values than coefficients.
  • An out-of-bag mask built from the rows missing from the resample, with the [0.5, 0.5] fallback when fewer than two rows remain.
  • Blend weights fit on out-of-bag predictions against y_train[oob] and applied to the same replicate's test predictions.
  • Bias, variance and error computed over the right axes, and why error = bias2 + variance holds against the noiseless truth.
  • The cost per replicate and the memory for the three prediction matrices.

Follow-up Questions Guidance

  • Why fit the blend weights on out-of-bag points instead of the bootstrap sample?
  • How would you constrain the blend to be a convex combination?
  • Where does irreducible noise appear in this decomposition?