OLS Coefficients Under Predictor Rescaling

Quick Overview

Implement a function that returns the OLS slope and intercept of y regressed on a linearly rescaled predictor a*x + b. It tests how a change of units reparameterizes a fitted line, why the fitted values and R-squared stay the same, and careful numerical implementation in NumPy.

OLS Coefficients Under Predictor Rescaling

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `rescaled_coefficients(x, y, a, b)`. It fits a simple OLS line (with intercept) of `y` on the rescaled predictor `x_new = a*x + b` and returns `(slope_new, intercept_new)`. If the fit of `y` on the original `x` gives `(slope, intercept)`, then `slope_new = slope / a` and `intercept_new = intercept - slope*b/a`. Requirements: - Return a tuple of plain Python floats, slope first. - The result must match both a direct refit on `a*x + b` and the analytic transform. Both are checked with `numpy.testing.assert_allclose`. - Do not rescale `y`. ```hint Precision with a large offset If you build the normal equations from raw sums, think about what a large `b` does to them. Computing the slope from centered sums keeps the arithmetic well conditioned. ``` ### Constraints & Assumptions - `a` is non-zero. - `x` and `y` are 1-D float arrays of shape `(n,)`. ### What a Strong Answer Covers - Why substituting `x = (x_new - b)/a` into the original line gives the stated formulas, so the fitted line is the same and only its coefficients change. - A correct OLS fit with an intercept, returned slope first as plain Python floats. - Why a direct refit on `a*x + b` agrees with the analytic transform, with a numerical check. - Edge cases: a negative `a`, a constant `x`, and precision loss when `b` is large. ### Follow-up Questions - What happens to the coefficients if you also rescale `y` to `c*y + d`? - Why is R-squared unchanged by the predictor rescaling while the coefficients change? - How do the standard errors and t-statistics of the slope and the intercept change under the rescaling?

Overview: Implement a function that returns the OLS slope and intercept of y regressed on a linearly rescaled predictor a*x + b. It tests how a change of units reparameterizes a fitted line, why the fitted values and R-squared stay the same, and careful numerical implementation in NumPy.

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

Implement rescaled_coefficients(x, y, a, b). It fits a simple OLS line (with intercept) of y on the rescaled predictor x_new = a*x + b and returns (slope_new, intercept_new). If the fit of y on the original x gives (slope, intercept), then slope_new = slope / a and intercept_new = intercept - slope*b/a.

Requirements:

  • Return a tuple of plain Python floats, slope first.
  • The result must match both a direct refit on a*x + b and the analytic transform. Both are checked with numpy.testing.assert_allclose .
  • Do not rescale y .

Constraints & Assumptions

  • a is non-zero.
  • x and y are 1-D float arrays of shape (n,) .

What a Strong Answer Covers Guidance

  • Why substituting x = (x_new - b)/a into the original line gives the stated formulas, so the fitted line is the same and only its coefficients change.
  • A correct OLS fit with an intercept, returned slope first as plain Python floats.
  • Why a direct refit on a*x + b agrees with the analytic transform, with a numerical check.
  • Edge cases: a negative a , a constant x , and precision loss when b is large.

Follow-up Questions Guidance

  • What happens to the coefficients if you also rescale y to c*y + d ?
  • Why is R-squared unchanged by the predictor rescaling while the coefficients change?
  • How do the standard errors and t-statistics of the slope and the intercept change under the rescaling?