Verify that R-squared equals squared correlation in simple linear regression

Quick Overview

Implement a Python function that fits a simple linear regression with an intercept, computes R-squared from the residuals, and returns it alongside the squared Pearson correlation of the inputs. It tests OLS fitting, sums of squares, and when this identity stops holding.

Verify that R-squared equals squared correlation in simple linear regression

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `r2_equals_corr_squared(x, y)` in Python. Fit the ordinary least squares line $y \approx b_0 + b_1 x$ with an intercept, compute `r2` as ${1 - SS_{res} / SS_{tot}}$ from the in-sample predictions, and compute `corr_sq` as the squared Pearson correlation of `x` and `y`. Return `(r2, corr_sq)` as two floats. The two values should be equal within floating-point tolerance. ```hint Where the identity comes from Write each fitted value as a deviation from the mean of y. With an intercept in the model, how do those deviations relate to the deviations of x from its mean? ``` ### Constraints & Assumptions - `x` and `y` are fixed 1-D float arrays of length $n = 15$. - The correlation between `x` and `y` is non-zero and not perfect. - You may use `np.polyfit`, `np.corrcoef`, or solve the normal equations by hand. ### What a Strong Answer Covers - Fitting the line with an intercept and forming the in-sample predictions. - Computing $SS_{res}$ from the residuals and $SS_{tot}$ about the mean of `y`. - Computing the Pearson correlation separately from the fit, then squaring it. - Comparing the two results within a floating-point tolerance. - Explaining why the two numbers match for one predictor with an intercept. ### Follow-up Questions - Why does this identity break once you add a second predictor? - What happens to the identity if you drop the intercept? - How would you prove the identity algebraically from the OLS slope formula?

Overview: Implement a Python function that fits a simple linear regression with an intercept, computes R-squared from the residuals, and returns it alongside the squared Pearson correlation of the inputs. It tests OLS fitting, sums of squares, and when this identity stops holding.

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

Implement r2_equals_corr_squared(x, y) in Python. Fit the ordinary least squares line y≈b0+b1xy \approx b_0 + b_1 x with an intercept, compute r2 as 1−SSres/SStot{1 - SS_{res} / SS_{tot}} from the in-sample predictions, and compute corr_sq as the squared Pearson correlation of x and y. Return (r2, corr_sq) as two floats. The two values should be equal within floating-point tolerance.

Constraints & Assumptions

  • x and y are fixed 1-D float arrays of length n=15n = 15 .
  • The correlation between x and y is non-zero and not perfect.
  • You may use np.polyfit , np.corrcoef , or solve the normal equations by hand.

What a Strong Answer Covers Guidance

  • Fitting the line with an intercept and forming the in-sample predictions.
  • Computing SSresSS_{res} from the residuals and SStotSS_{tot} about the mean of y .
  • Computing the Pearson correlation separately from the fit, then squaring it.
  • Comparing the two results within a floating-point tolerance.
  • Explaining why the two numbers match for one predictor with an intercept.

Follow-up Questions Guidance

  • Why does this identity break once you add a second predictor?
  • What happens to the identity if you drop the intercept?
  • How would you prove the identity algebraically from the OLS slope formula?