Fit a simple OLS regression with statsmodels and return the intercept and slope

Quick Overview

Write a Python function that fits a simple linear regression with statsmodels OLS and returns the intercept and slope in that order. It tests building a design matrix with add_constant, the endog and exog argument order, and reading the fitted parameters.

Fit a simple OLS regression with statsmodels and return the intercept and slope

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def statsmodels_ols_params(x: np.ndarray, y: np.ndarray) -> np.ndarray` that fits a simple OLS regression of `y` on `x` with `statsmodels.api.OLS` and returns `results.params` ordered `[intercept, slope]`. Requirements: - Build the design matrix with `sm.add_constant(x)` (default `prepend=True`: ones column first). - Fit with `sm.OLS(y, X)` (response first: `OLS(endog, exog)`), then call `.fit()`. - Return `results.params` as a 1-D float `np.ndarray` of shape `(2,)`. Example: ```python x = np.array([1.0, 2.0, 3.0, 4.0]) y = np.array([2.0, 4.0, 6.0, 8.0]) statsmodels_ols_params(x, y) # array([0.0, 2.0]) -> [intercept, slope] ``` ```hint Count the columns `sm.OLS` estimates one coefficient per column of the design matrix you pass and adds nothing on its own. Look at what that matrix holds before you fit. ``` ### Constraints & Assumptions - `x` and `y` are fixed 1-D float arrays of shape `(n,)`. - The output is checked with `numpy.testing.assert_allclose`. - `numpy` is imported as `np` and `statsmodels.api` as `sm`. ### What a Strong Answer Covers - Why the design matrix needs an explicit ones column for statsmodels to estimate an intercept. - The `OLS(endog, exog)` argument order and how it differs from scikit-learn's `fit(X, y)`. - How the column order of the design matrix fixes the order of `results.params`. - Returning a 1-D float array of shape `(2,)`, and what happens with degenerate input such as a constant `x`. ### Follow-up Questions - What if you called `sm.add_constant` with `prepend=False`? - How would you also return standard errors or confidence intervals? - How would you compute the same intercept and slope by hand from `x` and `y`?

Overview: Write a Python function that fits a simple linear regression with statsmodels OLS and returns the intercept and slope in that order. It tests building a design matrix with add_constant, the endog and exog argument order, and reading the fitted parameters.

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

Implement def statsmodels_ols_params(x: np.ndarray, y: np.ndarray) -> np.ndarray that fits a simple OLS regression of y on x with statsmodels.api.OLS and returns results.params ordered [intercept, slope].

Requirements:

  • Build the design matrix with sm.add_constant(x) (default prepend=True : ones column first).
  • Fit with sm.OLS(y, X) (response first: OLS(endog, exog) ), then call .fit() .
  • Return results.params as a 1-D float np.ndarray of shape (2,) .

Example:

x = np.array([1.0, 2.0, 3.0, 4.0])
y = np.array([2.0, 4.0, 6.0, 8.0])
statsmodels_ols_params(x, y)  # array([0.0, 2.0])  -> [intercept, slope]

Constraints & Assumptions

  • x and y are fixed 1-D float arrays of shape (n,) .
  • The output is checked with numpy.testing.assert_allclose .
  • numpy is imported as np and statsmodels.api as sm .

What a Strong Answer Covers Guidance

  • Why the design matrix needs an explicit ones column for statsmodels to estimate an intercept.
  • The OLS(endog, exog) argument order and how it differs from scikit-learn's fit(X, y) .
  • How the column order of the design matrix fixes the order of results.params .
  • Returning a 1-D float array of shape (2,) , and what happens with degenerate input such as a constant x .

Follow-up Questions Guidance

  • What if you called sm.add_constant with prepend=False ?
  • How would you also return standard errors or confidence intervals?
  • How would you compute the same intercept and slope by hand from x and y ?