Likelihood-Ratio Test for a Logistic Regression Fit in statsmodels

Quick Overview

Implement a likelihood-ratio test for a logistic regression fitted with statsmodels, returning the statistic, degrees of freedom and chi-squared p-value against the intercept-only model. It tests knowledge of log-likelihoods, Wilks' theorem, and exact handling of fit settings and return types.

Likelihood-Ratio Test for a Logistic Regression Fit in statsmodels

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `llr_test(X: np.ndarray, y: np.ndarray) -> tuple[float, int, float]`. `X` is a 2-D float design matrix of shape `(n, p)` with no intercept column, and `y` is a 1-D integer target of 0s and 1s. Fit the model with `sm.Logit(y, sm.add_constant(X)).fit(method='newton', maxiter=200, tol=1e-10, disp=0)` and return `(llr_stat, df, p_value)` for the likelihood-ratio test of the full model against the intercept-only null. The returned values must meet these requirements: - `llr_stat` equals `-2 * (res.llnull - res.llf)`, which matches `res.llr`. - `df` is the number of non-intercept predictors `p`. The intercept is not counted. - `p_value` equals `scipy.stats.chi2.sf(llr_stat, df)`, which matches `res.llr_pvalue`. - All three are native Python scalars. `isinstance(df, int)` must hold, so an `np.int64` fails the check. ```hint Where the null model comes from Look at what the fitted results object already stores before you write a second fit for the intercept-only model. ``` ```hint Check what you return Counts and statistics read from the fitted results are not always plain Python integers. Check the type of each value you return against the requirements. ``` ### Constraints & Assumptions - The grader builds a fixed, non-separable dataset in-process with `n = 80` rows and `p = 2` predictors. - `statsmodels` and `scipy` are available. - `llr_stat` and `p_value` are checked with `assert_allclose(rtol=1e-6)`, so the fit settings must be the ones given above. ### What a Strong Answer Covers - Fitting the specified model exactly: constant added, Newton's method, the given tolerance and iteration cap. - Building the statistic from the full and intercept-only log-likelihoods, and why it follows a chi-squared distribution under the null. - The degrees of freedom, with the intercept excluded and the reason it is excluded. - Returning native Python scalars of the required types. - When the asymptotic test is unreliable: separation, non-convergence, small samples. ### Follow-up Questions - How would you run a likelihood-ratio test between two nested models when neither of them is the intercept-only null? - Why can perfect separation break this test, and what would you do about it?

Overview: Implement a likelihood-ratio test for a logistic regression fitted with statsmodels, returning the statistic, degrees of freedom and chi-squared p-value against the intercept-only model. It tests knowledge of log-likelihoods, Wilks' theorem, and exact handling of fit settings and return types.

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

Implement llr_test(X: np.ndarray, y: np.ndarray) -> tuple[float, int, float]. X is a 2-D float design matrix of shape (n, p) with no intercept column, and y is a 1-D integer target of 0s and 1s. Fit the model with sm.Logit(y, sm.add_constant(X)).fit(method='newton', maxiter=200, tol=1e-10, disp=0) and return (llr_stat, df, p_value) for the likelihood-ratio test of the full model against the intercept-only null.

The returned values must meet these requirements:

  • llr_stat equals -2 * (res.llnull - res.llf) , which matches res.llr .
  • df is the number of non-intercept predictors p . The intercept is not counted.
  • p_value equals scipy.stats.chi2.sf(llr_stat, df) , which matches res.llr_pvalue .
  • All three are native Python scalars. isinstance(df, int) must hold, so an np.int64 fails the check.

Constraints & Assumptions

  • The grader builds a fixed, non-separable dataset in-process with n = 80 rows and p = 2 predictors.
  • statsmodels and scipy are available.
  • llr_stat and p_value are checked with assert_allclose(rtol=1e-6) , so the fit settings must be the ones given above.

What a Strong Answer Covers Guidance

  • Fitting the specified model exactly: constant added, Newton's method, the given tolerance and iteration cap.
  • Building the statistic from the full and intercept-only log-likelihoods, and why it follows a chi-squared distribution under the null.
  • The degrees of freedom, with the intercept excluded and the reason it is excluded.
  • Returning native Python scalars of the required types.
  • When the asymptotic test is unreliable: separation, non-convergence, small samples.

Follow-up Questions Guidance

  • How would you run a likelihood-ratio test between two nested models when neither of them is the intercept-only null?
  • Why can perfect separation break this test, and what would you do about it?