Compute the simple OLS slope from correlation and standard deviations

Quick Overview

Implement the simple linear regression slope in NumPy by multiplying the Pearson correlation by the ratio of the two standard deviations. It tests the algebra that links correlation to the least-squares slope, consistent ddof handling, edge cases such as a constant response, and checking the result against an independent fit.

Compute the simple OLS slope from correlation and standard deviations

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `slope_from_correlation(x, y)`, which returns the simple OLS slope of `y` on `x` using the identity `beta1 = r * (s_y / s_x)`. Here `r` is the Pearson correlation between `x` and `y`, and `s_y` and `s_x` are their standard deviations. ```python def slope_from_correlation(x: np.ndarray, y: np.ndarray) -> float: ... ``` Requirements: - Do not form the covariance/variance ratio directly. - Use the same `ddof` for both standard deviations. - The result must match an independently computed OLS slope under `numpy.testing.assert_allclose`. - Return a Python `float`, not a 0-d array. ```hint Expand the correlation Write `r` in terms of the covariance and the two standard deviations, then simplify `r * (s_y / s_x)`. Also ask which inputs leave `r` undefined even though a least-squares line still exists. ``` ### Constraints & Assumptions - `x` and `y` are 1-D float arrays of the same shape `(n,)`. - `s_x` is nonzero, so `x` has at least two distinct values. ### What a Strong Answer Covers - An algebraic argument that `r * (s_y / s_x)` equals the least-squares slope. - Computing `r` and both standard deviations with one consistent `ddof`, and why that choice does not change the result. - Inputs where `y` is constant and `r` is undefined. - Returning a plain Python `float` and testing against an independent fit. ### Follow-up Questions - Why does the `ddof` choice cancel out in this identity? - How do you get the OLS intercept once you have the slope? - How is this slope related to the slope you get by regressing `x` on `y`?

Overview: Implement the simple linear regression slope in NumPy by multiplying the Pearson correlation by the ratio of the two standard deviations. It tests the algebra that links correlation to the least-squares slope, consistent ddof handling, edge cases such as a constant response, and checking the result against an independent fit.

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

Implement slope_from_correlation(x, y), which returns the simple OLS slope of y on x using the identity beta1 = r * (s_y / s_x). Here r is the Pearson correlation between x and y, and s_y and s_x are their standard deviations.

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

Requirements:

  • Do not form the covariance/variance ratio directly.
  • Use the same ddof for both standard deviations.
  • The result must match an independently computed OLS slope under numpy.testing.assert_allclose .
  • Return a Python float , not a 0-d array.

Constraints & Assumptions

  • x and y are 1-D float arrays of the same shape (n,) .
  • s_x is nonzero, so x has at least two distinct values.

What a Strong Answer Covers Guidance

  • An algebraic argument that r * (s_y / s_x) equals the least-squares slope.
  • Computing r and both standard deviations with one consistent ddof , and why that choice does not change the result.
  • Inputs where y is constant and r is undefined.
  • Returning a plain Python float and testing against an independent fit.

Follow-up Questions Guidance

  • Why does the ddof choice cancel out in this identity?
  • How do you get the OLS intercept once you have the slope?
  • How is this slope related to the slope you get by regressing x on y ?