Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

33. Bias variance decomposition

medium
GeneralGeneral
senior

Bias–variance decomposition helps you explain why a machine learning algorithm makes prediction errors by splitting expected error into parts from underfitting and overfitting. In this question, you’ll compute bias, variance, and noise from repeated model predictions on the same inputs.

Requirements

Implement the function

python

Rules:

  • Use the squared error decomposition: E[(f^(x)−y)2]=Bias(f^(x))2+Var(f^(x))+σ2.\mathbb{E}[(\hat{f}(x)-y)^2] = \text{Bias}(\hat{f}(x))^2 + \text{Var}(\hat{f}(x)) + \sigma^2.
  • Treat each row in predictions as an independent run (e.g., different training set, bootstrap sample, or random seed) of the same ml_algorithm.
  • Return global bias_squared, variance, and noise as averages across all n inputs.
  • Use only NumPy (and Python built-ins); don’t use any ML libraries.

Example

python

Output:

python
Input Signature
ArgumentType
y_truenp.ndarray
predictionsnp.ndarray
Output Signature
Return NameType
valuelist

Constraints

  • Use only NumPy, no ML libraries

  • predictions shape must be (m, n)

  • Return global averages over all n inputs

Hint 1

Convert y_true and predictions to NumPy arrays and check shapes: y is (n,), preds is (m, n).

Hint 2

Compute the per-input mean prediction mu = preds.mean(axis=0), then bias² is mean((mu - y)**2) over inputs.

Hint 3

Variance is the average over inputs of mean((preds - mu)**2, axis=0). Compute global MSE as mean((preds - y)**2) (over runs and inputs), then noise = mse - bias2 - var.

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
bias-variance
numpy-vectorization
mean-squared-error
statistics
13 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit