Bias-Variance Decomposition of Refit Models

Quick Overview

Implement a NumPy function that splits the squared error of models refit on different datasets into per-point squared bias and variance, with averages over test points. It tests array reductions along the right axis, input validation and the bias-variance decomposition of mean squared error.

Bias-Variance Decomposition of Refit Models

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `bias_variance_decomposition(predictions, y_true)`. `predictions` has shape `(n_models, n_points)` (one row per model refit on a different dataset) and `y_true` has shape `(n_points,)`. Return a dict with per-point squared bias, variance and MSE, plus their averages over test points. - `bias_sq`: the squared difference between the mean prediction across models and `y_true`, per point. - `variance`: the population variance across models (`ddof=0`), per point. - `mse`: the mean over models of `(prediction - y_true)**2`, per point. - The per-point values are arrays of shape `(n_points,)`. `avg_bias_sq`, `avg_variance` and `avg_mse` are Python `float`s. ```hint Pick the axis first Each column is one test point, so every per-point quantity is a reduction across models. Bias and variance are both measured from the same reference prediction. ``` ### Example ```text predictions = [[1.0, 2.0], [3.0, 4.0]] y_true = [2.0, 2.0] { "bias_sq": [0.0, 1.0], "variance": [1.0, 1.0], "mse": [1.0, 2.0], "avg_bias_sq": 0.5, "avg_variance": 1.0, "avg_mse": 1.5 } ``` At the second point the two models predict 2 and 4, so the mean prediction is 3, the squared bias is 1, the variance is 1 and the MSE is (0 + 4) / 2 = 2. ### Constraints & Assumptions - `n_models` is at least 1 (a single model yields zero variance). - Inputs may be lists or NumPy arrays of finite numbers. - Raise `ValueError` if `predictions` is not 2-D, if `y_true` is not 1-D, or if the number of columns does not equal `len(y_true)`. ### What a Strong Answer Covers - Reductions along the model axis that produce per-point arrays of shape `(n_points,)`, with the averages returned as Python floats. - Squared bias and variance both measured from the mean prediction, using the `ddof=0` convention. - Validation for each of the three shape errors. - Why MSE equals squared bias plus variance in this setup, and which convention that depends on. ### Follow-up Questions - Where does irreducible noise appear in this decomposition? - How would you build the `predictions` matrix in practice, and how does refitting on bootstrap resamples instead of independent datasets affect the variance you measure? - How do the average squared bias and variance move as you raise the degree of a polynomial regression or the depth of a decision tree? - Does an additive decomposition like this hold for 0-1 loss in classification?

Overview: Implement a NumPy function that splits the squared error of models refit on different datasets into per-point squared bias and variance, with averages over test points. It tests array reductions along the right axis, input validation and the bias-variance decomposition of mean squared error.

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

Implement bias_variance_decomposition(predictions, y_true). predictions has shape (n_models, n_points) (one row per model refit on a different dataset) and y_true has shape (n_points,). Return a dict with per-point squared bias, variance and MSE, plus their averages over test points.

  • bias_sq : the squared difference between the mean prediction across models and y_true , per point.
  • variance : the population variance across models ( ddof=0 ), per point.
  • mse : the mean over models of (prediction - y_true)**2 , per point.
  • The per-point values are arrays of shape (n_points,) . avg_bias_sq , avg_variance and avg_mse are Python float s.

Example

predictions = [[1.0, 2.0], [3.0, 4.0]]
y_true = [2.0, 2.0]

{
  "bias_sq": [0.0, 1.0], "variance": [1.0, 1.0], "mse": [1.0, 2.0],
  "avg_bias_sq": 0.5, "avg_variance": 1.0, "avg_mse": 1.5
}

At the second point the two models predict 2 and 4, so the mean prediction is 3, the squared bias is 1, the variance is 1 and the MSE is (0 + 4) / 2 = 2.

Constraints & Assumptions

  • n_models is at least 1 (a single model yields zero variance).
  • Inputs may be lists or NumPy arrays of finite numbers.
  • Raise ValueError if predictions is not 2-D, if y_true is not 1-D, or if the number of columns does not equal len(y_true) .

What a Strong Answer Covers Guidance

  • Reductions along the model axis that produce per-point arrays of shape (n_points,) , with the averages returned as Python floats.
  • Squared bias and variance both measured from the mean prediction, using the ddof=0 convention.
  • Validation for each of the three shape errors.
  • Why MSE equals squared bias plus variance in this setup, and which convention that depends on.

Follow-up Questions Guidance

  • Where does irreducible noise appear in this decomposition?
  • How would you build the predictions matrix in practice, and how does refitting on bootstrap resamples instead of independent datasets affect the variance you measure?
  • How do the average squared bias and variance move as you raise the degree of a polynomial regression or the depth of a decision tree?
  • Does an additive decomposition like this hold for 0-1 loss in classification?