Compute the least-squares slope for a regression through the origin (no intercept)

Quick Overview

Implement a NumPy function that fits a least-squares line forced through the origin and returns its slope as a float. It tests deriving the no-intercept estimator, how it differs from the usual centered slope, and when dropping the intercept is justified.

Compute the least-squares slope for a regression through the origin (no intercept)

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `slope_through_origin(x, y)`, which fits the no-intercept model $y = \beta x$ on 1-D float arrays `x` and `y` of shape `(n,)`. Return $\beta$ as a single `float`. Compute `beta = sum(x * y) / sum(x**2)` from raw sums, with no mean-centering. Do not fit an intercept: the inputs have non-zero means, so a with-intercept formula gives a different slope. For example: ```python x = np.array([1.0, 2.0, 3.0, 4.0]) y = np.array([2.1, 3.9, 6.2, 7.8]) slope_through_origin(x, y) # -> 1.99 ``` Here `sum(x * y)` is 59.7 and `sum(x**2)` is 30, so the slope is 1.99. ```hint Where the formula comes from Write the squared error as a function of beta alone and set its derivative to zero. Then ask which input makes the denominator vanish. ``` ### Constraints & Assumptions - `x` and `y` are 1-D float arrays of the same length `n`. - The inputs have non-zero means. - The result is compared with `numpy.testing.assert_allclose`. ### What a Strong Answer Covers - Deriving the slope by minimizing the squared error with respect to $\beta$. - Using raw sums of products and squares, with no mean-centering. - Returning a plain float and handling an `x` that is all zeros. - Explaining how this slope differs from the with-intercept slope on the same data. ### Follow-up Questions - How does the no-intercept slope differ from the with-intercept slope, and when is it appropriate? - How would you extend this to multiple predictors without an intercept? - What changes about the residuals and $R^2$ when you drop the intercept?

Overview: Implement a NumPy function that fits a least-squares line forced through the origin and returns its slope as a float. It tests deriving the no-intercept estimator, how it differs from the usual centered slope, and when dropping the intercept is justified.

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

Implement slope_through_origin(x, y), which fits the no-intercept model y=βxy = \beta x on 1-D float arrays x and y of shape (n,). Return β\beta as a single float.

Compute beta = sum(x * y) / sum(x**2) from raw sums, with no mean-centering. Do not fit an intercept: the inputs have non-zero means, so a with-intercept formula gives a different slope.

For example:

x = np.array([1.0, 2.0, 3.0, 4.0])
y = np.array([2.1, 3.9, 6.2, 7.8])
slope_through_origin(x, y)  # -> 1.99

Here sum(x * y) is 59.7 and sum(x**2) is 30, so the slope is 1.99.

Constraints & Assumptions

  • x and y are 1-D float arrays of the same length n .
  • The inputs have non-zero means.
  • The result is compared with numpy.testing.assert_allclose .

What a Strong Answer Covers Guidance

  • Deriving the slope by minimizing the squared error with respect to β\beta .
  • Using raw sums of products and squares, with no mean-centering.
  • Returning a plain float and handling an x that is all zeros.
  • Explaining how this slope differs from the with-intercept slope on the same data.

Follow-up Questions Guidance

  • How does the no-intercept slope differ from the with-intercept slope, and when is it appropriate?
  • How would you extend this to multiple predictors without an intercept?
  • What changes about the residuals and R2R^2 when you drop the intercept?