k-NN Noise Floor Estimator: Compare Test MSE with Irreducible Error

Quick Overview

Estimate the irreducible noise variance of a regression dataset from k-nearest-neighbor residuals, then compare a model's test MSE against that floor and label the result. It tests the k/(k+1) correction, tie-breaking with duplicate rows, and when a noise estimate overstates the floor.

k-NN Noise Floor Estimator: Compare Test MSE with Irreducible Error

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `knn_noise_variance(X, y, k)`: for each row `i`, find its `k` nearest other rows (Euclidean distance, the row itself excluded), compute `r_i = y_i - mean(y[neighbors])`, and return `k/(k+1) * mean(r_i**2)` as a `float`. Then implement `diagnose_noise_floor(X_train, y_train, y_test, y_pred, k=5, tol=0.1)`, which compares a model's test MSE against that estimate. The estimate approximates the irreducible noise variance in `y`, so the comparison tells you whether a model still has reducible error left. Example: ```python knn_noise_variance([0., 1., 2., 3.], [0., 1., 0., 1.], k=1) # neighbors (lower-index ties): 1, 0, 1, 2 -> residuals -1, 1, -1, 1 # Output: 0.5 # (1/2) * mean([1, 1, 1, 1]) X = [0., 0., 5., 5.]; y = [1., 3., 10., 14.] diagnose_noise_floor(X, y, [3, 3, 3, 0, 0], [0, 0, 0, 0, 0], k=1, tol=0.1) # {'noise_variance': 5.0, 'test_mse': 5.4, 'excess_error': 0.4, 'label': 'at_noise_floor'} ``` In the first call, row 1 is equally far from rows 0 and 2, and row 2 is equally far from rows 1 and 3, so the lower index wins each time. In the second, the duplicate rows pair up, the squared residuals are 4, 4, 16 and 16, and half their mean is 5.0. The test MSE is 27 / 5 = 5.4, which is within 5.0 · 1.1 = 5.5. ```hint Self-exclusion and ties Find a way to stop a row from selecting itself that does not disturb the index order used for ties. ``` ```hint Where k/(k+1) comes from Split `r_i` into a signal difference and a noise difference, and work out the variance of the noise part when every row's noise is independent. ``` ### Constraints & Assumptions - A 1-D `X` is a single feature column. Distance ties break toward the lower row index. - Raise `ValueError` if `k` is outside `1 <= k <= n - 1` or the row counts of `X` and `y` differ. - `diagnose_noise_floor` returns a dict with keys `noise_variance`, `test_mse`, `excess_error` (`test_mse - noise_variance`) and `label`. - `label` is `"at_noise_floor"` if `test_mse <= noise_variance * (1 + tol)`, else `"above_noise_floor"`. - Raise `ValueError` if `tol < 0` or the shapes of `y_test` and `y_pred` differ. ### What a Strong Answer Covers - Excluding each row from its own neighbor set while keeping the lower-index tie rule, including when rows are duplicated. - A derivation of the `k/(k+1)` factor and the assumption it depends on. - The labeling rule, `excess_error`, and every stated `ValueError`. - The time and memory cost of the pairwise distances, and how to reduce them. - When the estimate is biased: a signal that changes within neighborhoods, unscaled features, or high dimension. ### Follow-up Questions - How does the choice of `k` trade bias against variance in this estimator? - What happens to the estimate when the features are on very different scales or the dimension is high? - If the noise variance depends on `x`, what does this estimator measure? - A model is labeled `"above_noise_floor"`. How would you tell whether the gap comes from bias or from variance?

Overview: Estimate the irreducible noise variance of a regression dataset from k-nearest-neighbor residuals, then compare a model's test MSE against that floor and label the result. It tests the k/(k+1) correction, tie-breaking with duplicate rows, and when a noise estimate overstates the floor.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement knn_noise_variance(X, y, k): for each row i, find its k nearest other rows (Euclidean distance, the row itself excluded), compute r_i = y_i - mean(y[neighbors]), and return k/(k+1) * mean(r_i**2) as a float. Then implement diagnose_noise_floor(X_train, y_train, y_test, y_pred, k=5, tol=0.1), which compares a model's test MSE against that estimate.

The estimate approximates the irreducible noise variance in y, so the comparison tells you whether a model still has reducible error left.

Example:

knn_noise_variance([0., 1., 2., 3.], [0., 1., 0., 1.], k=1)
# neighbors (lower-index ties): 1, 0, 1, 2 -> residuals -1, 1, -1, 1
# Output: 0.5   # (1/2) * mean([1, 1, 1, 1])

X = [0., 0., 5., 5.]; y = [1., 3., 10., 14.]
diagnose_noise_floor(X, y, [3, 3, 3, 0, 0], [0, 0, 0, 0, 0], k=1, tol=0.1)
# {'noise_variance': 5.0, 'test_mse': 5.4, 'excess_error': 0.4, 'label': 'at_noise_floor'}

In the first call, row 1 is equally far from rows 0 and 2, and row 2 is equally far from rows 1 and 3, so the lower index wins each time. In the second, the duplicate rows pair up, the squared residuals are 4, 4, 16 and 16, and half their mean is 5.0. The test MSE is 27 / 5 = 5.4, which is within 5.0 · 1.1 = 5.5.

Constraints & Assumptions

  • A 1-D X is a single feature column. Distance ties break toward the lower row index.
  • Raise ValueError if k is outside 1 <= k <= n - 1 or the row counts of X and y differ.
  • diagnose_noise_floor returns a dict with keys noise_variance , test_mse , excess_error ( test_mse - noise_variance ) and label .
  • label is "at_noise_floor" if test_mse <= noise_variance * (1 + tol) , else "above_noise_floor" .
  • Raise ValueError if tol < 0 or the shapes of y_test and y_pred differ.

What a Strong Answer Covers Guidance

  • Excluding each row from its own neighbor set while keeping the lower-index tie rule, including when rows are duplicated.
  • A derivation of the k/(k+1) factor and the assumption it depends on.
  • The labeling rule, excess_error , and every stated ValueError .
  • The time and memory cost of the pairwise distances, and how to reduce them.
  • When the estimate is biased: a signal that changes within neighborhoods, unscaled features, or high dimension.

Follow-up Questions Guidance

  • How does the choice of k trade bias against variance in this estimator?
  • What happens to the estimate when the features are on very different scales or the dimension is high?
  • If the noise variance depends on x , what does this estimator measure?
  • A model is labeled "above_noise_floor" . How would you tell whether the gap comes from bias or from variance?