Overall F-Test for OLS Using statsmodels

Quick Overview

Fit an ordinary least squares regression with an intercept in statsmodels and return the overall F-statistic and its p-value. It tests whether you know the null hypothesis of the overall F-test, why the intercept stays unrestricted, and how the test relates to R squared and the coefficient t-tests.

Overall F-Test for OLS Using statsmodels

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def overall_f_test(X: np.ndarray, y: np.ndarray) -> tuple[float, float]`: fit OLS of `y` on `X` with an intercept and run the overall F-test (null: all slopes zero, intercept unrestricted). Return `(f_statistic, f_pvalue)` as two Python floats, in that order. Requirements: - Fit with `sm.OLS(y, sm.add_constant(X)).fit()`. - Return `res.fvalue` and `res.f_pvalue`. Example: ```python import numpy as np X = np.array([[0.0], [1.0], [2.0], [3.0]]) y = np.array([1.0, 3.0, 2.0, 5.0]) overall_f_test(X, y) # -> approximately (4.4815, 0.1685) ``` The fitted line is y = 1.1 + 1.1x. With one slope and four rows the test has 1 and 2 degrees of freedom, and the small sample is why an R squared of about 0.69 still gives a p-value well above 0.05. ```hint Where the intercept comes from `sm.OLS` fits exactly the columns you give it. Think about how the unrestricted intercept gets into the design matrix. ``` ### Constraints & Assumptions - `X` is a 2-D array of shape (n, k) with k predictor columns and no constant column. `y` has length n. - The grader calls the function on fixed inputs with `X.shape == (25, 3)` and compares the returned pair with `numpy.testing.assert_allclose`. ### What a Strong Answer Covers - Fitting with an explicit intercept column and returning the F-statistic and p-value as plain floats, in the required order. - The null and alternative hypotheses of the overall F-test, and why the intercept is left out of the restriction. - How the statistic is built from explained and residual mean squares, with the right degrees of freedom. - What a significant or non-significant result says, and what it does not say about individual coefficients. ### Follow-up Questions - How does the overall F-test differ from the individual t-tests on each coefficient? - How would you compute the same F-statistic manually from the R² values? - What happens to `res.fvalue` if you forget `add_constant`, or if `X` already contains a constant column?

Overview: Fit an ordinary least squares regression with an intercept in statsmodels and return the overall F-statistic and its p-value. It tests whether you know the null hypothesis of the overall F-test, why the intercept stays unrestricted, and how the test relates to R squared and the coefficient t-tests.

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

Implement def overall_f_test(X: np.ndarray, y: np.ndarray) -> tuple[float, float]: fit OLS of y on X with an intercept and run the overall F-test (null: all slopes zero, intercept unrestricted). Return (f_statistic, f_pvalue) as two Python floats, in that order.

Requirements:

  • Fit with sm.OLS(y, sm.add_constant(X)).fit() .
  • Return res.fvalue and res.f_pvalue .

Example:

import numpy as np

X = np.array([[0.0], [1.0], [2.0], [3.0]])
y = np.array([1.0, 3.0, 2.0, 5.0])
overall_f_test(X, y)
# -> approximately (4.4815, 0.1685)

The fitted line is y = 1.1 + 1.1x. With one slope and four rows the test has 1 and 2 degrees of freedom, and the small sample is why an R squared of about 0.69 still gives a p-value well above 0.05.

Constraints & Assumptions

  • X is a 2-D array of shape (n, k) with k predictor columns and no constant column. y has length n.
  • The grader calls the function on fixed inputs with X.shape == (25, 3) and compares the returned pair with numpy.testing.assert_allclose .

What a Strong Answer Covers Guidance

  • Fitting with an explicit intercept column and returning the F-statistic and p-value as plain floats, in the required order.
  • The null and alternative hypotheses of the overall F-test, and why the intercept is left out of the restriction.
  • How the statistic is built from explained and residual mean squares, with the right degrees of freedom.
  • What a significant or non-significant result says, and what it does not say about individual coefficients.

Follow-up Questions Guidance

  • How does the overall F-test differ from the individual t-tests on each coefficient?
  • How would you compute the same F-statistic manually from the R² values?
  • What happens to res.fvalue if you forget add_constant , or if X already contains a constant column?