R-squared for Simple OLS Fit

Quick Overview

Write a NumPy function that fits a simple linear regression with an intercept and returns R squared computed as one minus SSE over SST. It tests the sum-of-squares definitions, the link between R squared and the Pearson correlation for one predictor, and handling of degenerate inputs.

R-squared for Simple OLS Fit

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `r_squared_simple`, which fits a simple OLS line with an intercept on a single predictor and returns $R^2$ as `1 - SSE/SST`, where `SSE = sum((y - y_hat)**2)` and `SST = sum((y - y.mean())**2)`. ```python def r_squared_simple(x: np.ndarray, y: np.ndarray) -> float: ... ``` Requirements: - Fit the slope and intercept on `x` (`np.polyfit`, the normal equations, or `np.linalg.lstsq`). - Return a single `float` equal to `np.corrcoef(x, y)[0, 1] ** 2` within tolerance (checked with `numpy.testing.assert_allclose`). For example, with `x = [1.0, 2.0, 3.0, 4.0]` and `y = [2.0, 4.0, 5.0, 4.0]` the function returns about `0.5158`. ```hint Check against the grader Compare your result with `np.corrcoef(x, y)[0, 1] ** 2` on a small array, and confirm the order of the coefficients your fitting routine returns. ``` ### Constraints & Assumptions - Inputs are 1-D float arrays of shape `(n,)`. - The grader uses an imperfect fit, so $R^2$ is strictly between 0 and 1. ### What a Strong Answer Covers - Fitting the line with an intercept and computing the fitted values. - Computing SSE and SST from their definitions and returning `1 - SSE/SST` as a Python `float`. - Why the result equals the squared Pearson correlation for one predictor with an intercept. - Degenerate inputs such as a constant `y` or a constant `x`. ### Follow-up Questions - Why does the $R^2 = r^2$ identity break with more than one predictor? - How would you handle a degenerate case where `SST == 0`?

Overview: Write a NumPy function that fits a simple linear regression with an intercept and returns R squared computed as one minus SSE over SST. It tests the sum-of-squares definitions, the link between R squared and the Pearson correlation for one predictor, and handling of degenerate inputs.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement r_squared_simple, which fits a simple OLS line with an intercept on a single predictor and returns R2R^2 as 1 - SSE/SST, where SSE = sum((y - y_hat)**2) and SST = sum((y - y.mean())**2).

def r_squared_simple(x: np.ndarray, y: np.ndarray) -> float:
    ...

Requirements:

  • Fit the slope and intercept on x ( np.polyfit , the normal equations, or np.linalg.lstsq ).
  • Return a single float equal to np.corrcoef(x, y)[0, 1] ** 2 within tolerance (checked with numpy.testing.assert_allclose ).

For example, with x = [1.0, 2.0, 3.0, 4.0] and y = [2.0, 4.0, 5.0, 4.0] the function returns about 0.5158.

Constraints & Assumptions

  • Inputs are 1-D float arrays of shape (n,) .
  • The grader uses an imperfect fit, so R2R^2 is strictly between 0 and 1.

What a Strong Answer Covers Guidance

  • Fitting the line with an intercept and computing the fitted values.
  • Computing SSE and SST from their definitions and returning 1 - SSE/SST as a Python float .
  • Why the result equals the squared Pearson correlation for one predictor with an intercept.
  • Degenerate inputs such as a constant y or a constant x .

Follow-up Questions Guidance

  • Why does the R2=r2R^2 = r^2 identity break with more than one predictor?
  • How would you handle a degenerate case where SST == 0 ?