Compute the simple OLS slope three ways: closed form, np.polyfit and np.linalg.lstsq

Quick Overview

Compute the slope of a one-predictor linear regression three ways in NumPy: the centered closed form, np.polyfit, and np.linalg.lstsq on a design matrix with an intercept. It tests coefficient ordering in each library, floating-point agreement checks, and which route stays accurate on badly conditioned data.

Compute the simple OLS slope three ways: closed form, np.polyfit and np.linalg.lstsq

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `slope_three_ways(x, y)` that computes the simple OLS slope of `y` on `x` by three independent routes. Return a 1-D float `np.ndarray` of shape `(3,)` whose entries all equal the slope. Requirements: - Index 0: closed form `sum((x-x.mean())*(y-y.mean())) / sum((x-x.mean())**2)`. - Index 1: `np.polyfit(x, y, 1)[0]`. - Index 2: element 1 of `np.linalg.lstsq(A, y, rcond=None)[0]` with `A = np.column_stack([np.ones(len(x)), x])` (the vector is `[intercept, slope]`). - Return the values in exactly this order. All three must agree. Examples: ```python x = np.array([0., 1., 2., 3.]) y = np.array([1., 3., 5., 7.]) slope_three_ways(x, y) # array([2., 2., 2.]) x = np.array([0., 1., 2.]) y = np.array([1., 2., 4.]) slope_three_ways(x, y) # array([1.5, 1.5, 1.5]) ``` ```hint Coefficient order Each library returns its coefficients in its own order. Check the order for `np.polyfit` and for the design matrix you pass to `np.linalg.lstsq` before you index into either result. ``` ```hint Checking agreement Decide how you would test that the three entries agree. Ask whether exact equality is a fair test for three different floating-point computations. ``` ### What a Strong Answer Covers - Implements each route as specified and picks the slope out of each library's coefficient order correctly. - Returns a float array of shape `(3,)` in the required order. - Explains why the three routes must agree and why they can still differ in the last digits. - Names the input where the routes stop agreeing (no spread in `x`) and how to detect it. - Says which route stays accurate on badly conditioned data and what centering does to the problem. ### Follow-up Questions - Why can the three routes differ at the level of floating-point precision? - Which route is most numerically stable for ill-conditioned `x`, for example values near ${10^9}$ with a spread of a few units? - What does each route return when every `x` value is the same, and how would you detect that case?

Overview: Compute the slope of a one-predictor linear regression three ways in NumPy: the centered closed form, np.polyfit, and np.linalg.lstsq on a design matrix with an intercept. It tests coefficient ordering in each library, floating-point agreement checks, and which route stays accurate on badly conditioned data.

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

Implement slope_three_ways(x, y) that computes the simple OLS slope of y on x by three independent routes. Return a 1-D float np.ndarray of shape (3,) whose entries all equal the slope.

Requirements:

  • Index 0: closed form sum((x-x.mean())*(y-y.mean())) / sum((x-x.mean())**2) .
  • Index 1: np.polyfit(x, y, 1)[0] .
  • Index 2: element 1 of np.linalg.lstsq(A, y, rcond=None)[0] with A = np.column_stack([np.ones(len(x)), x]) (the vector is [intercept, slope] ).
  • Return the values in exactly this order. All three must agree.

Examples:

x = np.array([0., 1., 2., 3.])
y = np.array([1., 3., 5., 7.])
slope_three_ways(x, y)
# array([2., 2., 2.])

x = np.array([0., 1., 2.])
y = np.array([1., 2., 4.])
slope_three_ways(x, y)
# array([1.5, 1.5, 1.5])

What a Strong Answer Covers Guidance

  • Implements each route as specified and picks the slope out of each library's coefficient order correctly.
  • Returns a float array of shape (3,) in the required order.
  • Explains why the three routes must agree and why they can still differ in the last digits.
  • Names the input where the routes stop agreeing (no spread in x ) and how to detect it.
  • Says which route stays accurate on badly conditioned data and what centering does to the problem.

Follow-up Questions Guidance

  • Why can the three routes differ at the level of floating-point precision?
  • Which route is most numerically stable for ill-conditioned x , for example values near 109{10^9} with a spread of a few units?
  • What does each route return when every x value is the same, and how would you detect that case?