Fit OLS with an intercept and verify that fitted values are orthogonal to residuals

Quick Overview

Implement a NumPy function that fits ordinary least squares with an intercept and returns the dot product of the fitted values and the residuals. It tests the projection view of linear regression, the normal equations, and how floating-point error shows up in an identity that is exactly zero on paper.

Fit OLS with an intercept and verify that fitted values are orthogonal to residuals

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement the function below. `X` has shape `(n, k)` with no intercept column and `y` has shape `(n,)`. Fit OLS with an intercept and return the dot product of the fitted values and the residuals, which is zero up to floating-point error. ```python import numpy as np def fitted_resid_dot(X: np.ndarray, y: np.ndarray) -> float: ... ``` The function must: - Add an intercept term before fitting. - Compute `y_hat` and `e = y - y_hat` from the fitted model. - Return the actual computed `float(y_hat @ e)` (around `1e-14` in magnitude). Do not hard-code `0.0`. ```hint Think geometrically OLS fitted values are a projection of `y`. Ask where the residual vector sits relative to the space that `y` is projected onto. ``` ### Constraints & Assumptions - `X` has shape `(n, k)`, contains no intercept column, and has full column rank. - `y` has shape `(n,)`. - `numpy` or `statsmodels` may be used. ### What a Strong Answer Covers - Builds a design matrix with a column of ones so the model includes an intercept. - Solves for the coefficients with a numerically stable method. - Forms the fitted values and residuals from the fitted coefficients and returns the computed dot product. - Explains why the dot product is zero in exact arithmetic and why the returned number is small but not exactly zero. ### Follow-up Questions - Does the sum of residuals also equal zero, and why? - Why compute the value instead of returning `0.0`? - If you drop the intercept, which of the two identities (fitted values orthogonal to residuals, residuals summing to zero) still holds? - Would the identity survive if `X` were rank deficient and you used `np.linalg.lstsq`?

Overview: Implement a NumPy function that fits ordinary least squares with an intercept and returns the dot product of the fitted values and the residuals. It tests the projection view of linear regression, the normal equations, and how floating-point error shows up in an identity that is exactly zero on paper.

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

Implement the function below. X has shape (n, k) with no intercept column and y has shape (n,). Fit OLS with an intercept and return the dot product of the fitted values and the residuals, which is zero up to floating-point error.

import numpy as np


def fitted_resid_dot(X: np.ndarray, y: np.ndarray) -> float:
    ...

The function must:

  • Add an intercept term before fitting.
  • Compute y_hat and e = y - y_hat from the fitted model.
  • Return the actual computed float(y_hat @ e) (around 1e-14 in magnitude). Do not hard-code 0.0 .

Constraints & Assumptions

  • X has shape (n, k) , contains no intercept column, and has full column rank.
  • y has shape (n,) .
  • numpy or statsmodels may be used.

What a Strong Answer Covers Guidance

  • Builds a design matrix with a column of ones so the model includes an intercept.
  • Solves for the coefficients with a numerically stable method.
  • Forms the fitted values and residuals from the fitted coefficients and returns the computed dot product.
  • Explains why the dot product is zero in exact arithmetic and why the returned number is small but not exactly zero.

Follow-up Questions Guidance

  • Does the sum of residuals also equal zero, and why?
  • Why compute the value instead of returning 0.0 ?
  • If you drop the intercept, which of the two identities (fitted values orthogonal to residuals, residuals summing to zero) still holds?
  • Would the identity survive if X were rank deficient and you used np.linalg.lstsq ?