OLS Fitted Values With Intercept

Quick Overview

Implement a NumPy function that fits ordinary least squares with an intercept and returns the in-sample fitted values. It tests building the design matrix, choosing a stable least-squares solver, telling fitted values apart from residuals, and handling collinear features.

OLS Fitted Values With Intercept

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `compute_fitted_values(X, y)`. Given `X` of shape `(n, k)` (no intercept column) and `y` of shape `(n,)`, fit OLS with an intercept and return the in-sample fitted values `y_hat` (the same as statsmodels `results.fittedvalues`). Requirements: - Add an intercept term before fitting. - Return fitted values, not residuals and not predictions on new data. - The output is a 1-D float `np.ndarray` of shape `(n,)` that preserves row order. Examples: ```python X = np.array([[1.0], [2.0], [3.0]]) y = np.array([2.0, 4.0, 6.0]) compute_fitted_values(X, y) # -> array([2., 4., 6.]) X = np.array([[1.0], [2.0], [3.0], [4.0]]) y = np.array([2.0, 3.0, 5.0, 6.0]) compute_fitted_values(X, y) # -> array([1.9, 3.3, 4.7, 6.1]) up to floating-point rounding ``` ```hint Choosing the solver The fitted values come from the same design matrix you fit on. Pick a least-squares routine that does not require you to invert $X^\top X$ yourself. ``` ### What a Strong Answer Covers - Adding the intercept column before fitting and solving the least-squares problem with a numerically stable method. - Returning the in-sample fitted values, kept distinct from residuals and from predictions on new data. - A 1-D float array of shape `(n,)` in the original row order. - How the method behaves with collinear columns. - The time and space cost of the solve. ### Follow-up Questions - How do fitted values relate to the hat matrix `H`? - What if `X` has collinear columns? - What properties should the fitted values and residuals satisfy, and how would you use them to check your implementation?

Overview: Implement a NumPy function that fits ordinary least squares with an intercept and returns the in-sample fitted values. It tests building the design matrix, choosing a stable least-squares solver, telling fitted values apart from residuals, and handling collinear features.

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

Implement compute_fitted_values(X, y). Given X of shape (n, k) (no intercept column) and y of shape (n,), fit OLS with an intercept and return the in-sample fitted values y_hat (the same as statsmodels results.fittedvalues).

Requirements:

  • Add an intercept term before fitting.
  • Return fitted values, not residuals and not predictions on new data.
  • The output is a 1-D float np.ndarray of shape (n,) that preserves row order.

Examples:

X = np.array([[1.0], [2.0], [3.0]])
y = np.array([2.0, 4.0, 6.0])
compute_fitted_values(X, y)  # -> array([2., 4., 6.])

X = np.array([[1.0], [2.0], [3.0], [4.0]])
y = np.array([2.0, 3.0, 5.0, 6.0])
compute_fitted_values(X, y)  # -> array([1.9, 3.3, 4.7, 6.1]) up to floating-point rounding

What a Strong Answer Covers Guidance

  • Adding the intercept column before fitting and solving the least-squares problem with a numerically stable method.
  • Returning the in-sample fitted values, kept distinct from residuals and from predictions on new data.
  • A 1-D float array of shape (n,) in the original row order.
  • How the method behaves with collinear columns.
  • The time and space cost of the solve.

Follow-up Questions Guidance

  • How do fitted values relate to the hat matrix H ?
  • What if X has collinear columns?
  • What properties should the fitted values and residuals satisfy, and how would you use them to check your implementation?