Compare Out-of-Sample R-Squared Against the Mean Baseline

Quick Overview

Implement a function that compares a regression model's R-squared on held-out data with the R-squared of a constant mean predictor. It tests why a mean baseline scores zero, why out-of-sample R-squared can drop below zero, and how to keep both scores on one shared denominator.

Compare Out-of-Sample R-Squared Against the Mean Baseline

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `baseline_beats_model(y_true, y_pred)`. Given true values and a model's predictions (for example on held-out data), it computes the model's $R^2$, the $R^2$ of the constant mean predictor (which predicts `y_true.mean()` for every row), and a flag that says whether the model does worse than that baseline. Return the tuple `(r2_model, r2_baseline, model_is_worse)`. Requirements: - `RSS_model = sum((y_true - y_pred)**2)` and `TSS = sum((y_true - y_true.mean())**2)`. - `r2_model = 1 - RSS_model/TSS`, returned as a float. - `r2_baseline` is exactly `0.0`, returned as a float. - `model_is_worse` is a Python `bool` that is `True` when `r2_model < r2_baseline`. Example: ```python y_true = np.array([1.0, 2.0, 3.0, 4.0]) y_pred = np.array([4.0, 3.0, 2.0, 1.0]) r2_model, r2_baseline, model_is_worse = baseline_beats_model(y_true, y_pred) # r2_model == -3.0 (below 0.0), r2_baseline == 0.0, model_is_worse == True ``` ```hint Write out the baseline's residuals List the residuals of the mean predictor and compare them, term by term, with the terms that make up TSS. ``` ```hint Training data versus new data For the conceptual part, think about which simple model an OLS fit can always fall back to on its own training data, and whether that fallback still bounds the error on new data. ``` ### Constraints & Assumptions - `y_true` and `y_pred` are 1-D arrays of equal length. - TSS is non-zero, so `y_true` is not constant. ### What a Strong Answer Covers - One shared TSS, centered on the mean of `y_true`, used for both scores - Why the mean predictor's $R^2$ is exactly 0.0 and not just close to it - The return types: two floats and a Python `bool` - Why out-of-sample $R^2$ can fall below zero while in-sample $R^2$ of OLS with an intercept cannot ### Follow-up Questions - Why can out-of-sample $R^2$ be negative when in-sample $R^2$ cannot? - What TSS convention should the baseline use so that the comparison stays meaningful? - If the baseline predicted the training-set mean instead of the test-set mean, what would its $R^2$ on the test data be?

Overview: Implement a function that compares a regression model's R-squared on held-out data with the R-squared of a constant mean predictor. It tests why a mean baseline scores zero, why out-of-sample R-squared can drop below zero, and how to keep both scores on one shared denominator.

General logo
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement baseline_beats_model(y_true, y_pred). Given true values and a model's predictions (for example on held-out data), it computes the model's R2R^2, the R2R^2 of the constant mean predictor (which predicts y_true.mean() for every row), and a flag that says whether the model does worse than that baseline. Return the tuple (r2_model, r2_baseline, model_is_worse).

Requirements:

  • RSS_model = sum((y_true - y_pred)**2) and TSS = sum((y_true - y_true.mean())**2) .
  • r2_model = 1 - RSS_model/TSS , returned as a float.
  • r2_baseline is exactly 0.0 , returned as a float.
  • model_is_worse is a Python bool that is True when r2_model < r2_baseline .

Example:

y_true = np.array([1.0, 2.0, 3.0, 4.0])
y_pred = np.array([4.0, 3.0, 2.0, 1.0])

r2_model, r2_baseline, model_is_worse = baseline_beats_model(y_true, y_pred)
# r2_model == -3.0 (below 0.0), r2_baseline == 0.0, model_is_worse == True

Constraints & Assumptions

  • y_true and y_pred are 1-D arrays of equal length.
  • TSS is non-zero, so y_true is not constant.

What a Strong Answer Covers Guidance

  • One shared TSS, centered on the mean of y_true , used for both scores
  • Why the mean predictor's R2R^2 is exactly 0.0 and not just close to it
  • The return types: two floats and a Python bool
  • Why out-of-sample R2R^2 can fall below zero while in-sample R2R^2 of OLS with an intercept cannot

Follow-up Questions Guidance

  • Why can out-of-sample R2R^2 be negative when in-sample R2R^2 cannot?
  • What TSS convention should the baseline use so that the comparison stays meaningful?
  • If the baseline predicted the training-set mean instead of the test-set mean, what would its R2R^2 on the test data be?