Compute the simple OLS slope and intercept with the closed form in NumPy

Quick Overview

Write a NumPy function that fits a one-predictor linear regression with the covariance over variance closed form and returns the slope and intercept as plain floats. It tests the OLS formulas, return types, numerical care when centering, and how the slope relates to correlation and standard errors.

Compute the simple OLS slope and intercept with the closed form in NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `simple_ols_coefficients(x, y)` that fits `y = intercept + slope*x` on a single predictor using the covariance/variance closed form. Return `(slope, intercept)` (slope first), each a scalar float, not an array. Use these formulas: - `slope = sum((x - x.mean()) * (y - y.mean())) / sum((x - x.mean())**2)` - `intercept = y.mean() - slope * x.mean()` Examples: ```python x = np.array([1., 2., 3., 4., 5.]) y = np.array([2., 4., 6., 8., 10.]) simple_ols_coefficients(x, y) # (2.0, 0.0) y = np.array([2., 3., 5., 4., 6.]) simple_ols_coefficients(x, y) # about (0.9, 1.3) ``` ```hint Return type Compute the centered arrays once and reuse them. Then check what type `np.sum` gives you and what the task asks you to return. ``` ### Constraints & Assumptions - `x` and `y` are 1-D float arrays of shape `(n,)` with `n >= 5`. - `x` has non-zero spread, so the denominator is never 0. - The result is checked with `numpy.testing.assert_allclose`. ### What a Strong Answer Covers - Centers both arrays and applies the slope and intercept formulas correctly. - Returns a tuple of two plain floats in the order `(slope, intercept)`. - Explains why the intercept formula follows from the fitted line passing through the point of means. - States the time and space cost and says what would break if `x` had no spread. ### Follow-up Questions - Computing the slope as `np.cov(x, y)[0, 1] / np.var(x)` gives the wrong answer. Why, and how do you fix it? - How does the slope relate to the correlation between `x` and `y`? - How would you compute standard errors for the slope and the intercept?

Overview: Write a NumPy function that fits a one-predictor linear regression with the covariance over variance closed form and returns the slope and intercept as plain floats. It tests the OLS formulas, return types, numerical care when centering, and how the slope relates to correlation and standard errors.

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

Implement simple_ols_coefficients(x, y) that fits y = intercept + slope*x on a single predictor using the covariance/variance closed form. Return (slope, intercept) (slope first), each a scalar float, not an array.

Use these formulas:

  • slope = sum((x - x.mean()) * (y - y.mean())) / sum((x - x.mean())**2)
  • intercept = y.mean() - slope * x.mean()

Examples:

x = np.array([1., 2., 3., 4., 5.])
y = np.array([2., 4., 6., 8., 10.])
simple_ols_coefficients(x, y)  # (2.0, 0.0)

y = np.array([2., 3., 5., 4., 6.])
simple_ols_coefficients(x, y)  # about (0.9, 1.3)

Constraints & Assumptions

  • x and y are 1-D float arrays of shape (n,) with n >= 5 .
  • x has non-zero spread, so the denominator is never 0.
  • The result is checked with numpy.testing.assert_allclose .

What a Strong Answer Covers Guidance

  • Centers both arrays and applies the slope and intercept formulas correctly.
  • Returns a tuple of two plain floats in the order (slope, intercept) .
  • Explains why the intercept formula follows from the fitted line passing through the point of means.
  • States the time and space cost and says what would break if x had no spread.

Follow-up Questions Guidance

  • Computing the slope as np.cov(x, y)[0, 1] / np.var(x) gives the wrong answer. Why, and how do you fix it?
  • How does the slope relate to the correlation between x and y ?
  • How would you compute standard errors for the slope and the intercept?