Fit OLS With statsmodels add_constant

Quick Overview

Write a Python function that fits an ordinary least squares regression with statsmodels, adding an intercept column with add_constant and returning the intercept and three slopes in a fixed order. It tests the statsmodels OLS interface, how a design matrix is built, and how parameter order follows column order.

Fit OLS With statsmodels add_constant

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def fit_ols_statsmodels(X: np.ndarray, y: np.ndarray) -> np.ndarray` that fits OLS of `y` (shape `(n,)`) on `X` (shape `(n, p)`, n=40, p=3, no ones-column) with statsmodels and returns the full parameter vector including the intercept. Requirements: - Add a constant with `sm.add_constant(X)` (default `prepend=True`). - Fit with `sm.OLS(y, ...).fit()` and read `.params`. - Return a 1-D `np.ndarray` of length 4, ordered `[intercept, b1, b2, b3]` (slopes in column order). ```hint Argument order `sm.OLS` does not take its arguments in the order scikit-learn's `fit(X, y)` does, and it fits only the columns you pass. Check both before you read `.params`. ``` ### Constraints & Assumptions - `X` has shape `(40, 3)` and contains no column of ones. - `y` has shape `(40,)`. - `numpy` is imported as `np` and `statsmodels.api` as `sm`. ### What a Strong Answer Covers - Why `sm.OLS` needs an explicit constant column to estimate an intercept. - The `(y, X)` argument order of `sm.OLS`, and how the column order of the design matrix sets the order of `.params`. - Returning a plain 1-D float array of length 4 in the required order. - What happens when the input already contains a constant column or has missing values. ### Follow-up Questions - What changes if you pass `prepend=False` to `add_constant`? - How would you get standard errors and p-values for the coefficients? - How would you confirm the fitted parameters without statsmodels?

Overview: Write a Python function that fits an ordinary least squares regression with statsmodels, adding an intercept column with add_constant and returning the intercept and three slopes in a fixed order. It tests the statsmodels OLS interface, how a design matrix is built, and how parameter order follows column order.

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

Implement def fit_ols_statsmodels(X: np.ndarray, y: np.ndarray) -> np.ndarray that fits OLS of y (shape (n,)) on X (shape (n, p), n=40, p=3, no ones-column) with statsmodels and returns the full parameter vector including the intercept.

Requirements:

  • Add a constant with sm.add_constant(X) (default prepend=True ).
  • Fit with sm.OLS(y, ...).fit() and read .params .
  • Return a 1-D np.ndarray of length 4, ordered [intercept, b1, b2, b3] (slopes in column order).

Constraints & Assumptions

  • X has shape (40, 3) and contains no column of ones.
  • y has shape (40,) .
  • numpy is imported as np and statsmodels.api as sm .

What a Strong Answer Covers Guidance

  • Why sm.OLS needs an explicit constant column to estimate an intercept.
  • The (y, X) argument order of sm.OLS , and how the column order of the design matrix sets the order of .params .
  • Returning a plain 1-D float array of length 4 in the required order.
  • What happens when the input already contains a constant column or has missing values.

Follow-up Questions Guidance

  • What changes if you pass prepend=False to add_constant ?
  • How would you get standard errors and p-values for the coefficients?
  • How would you confirm the fitted parameters without statsmodels?