Average marginal effects of a logistic regression with statsmodels

Quick Overview

Fit a logistic regression with statsmodels and return the average marginal effect of each regressor on the predicted probability. It tests why logit coefficients are not probability effects, how per-row effects are averaged, and how the constant is handled.

Average marginal effects of a logistic regression with statsmodels

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement the function below. Fit a logistic regression and return the average marginal effects (AME) on the probability scale. ```python def average_marginal_effects(X: np.ndarray, y: np.ndarray) -> np.ndarray: ``` - `X` is a 2-D float array of shape `(n, p)` with no intercept column, and `y` is a 1-D array of 0/1 integers. - Fit `sm.Logit(y, sm.add_constant(X)).fit(method="newton", maxiter=200, tol=1e-10, disp=0)`. - Return `res.get_margeff(at="overall", method="dydx").margeff` as a 1-D `float64` array, passing both keywords explicitly. - The constant is excluded, so the shape is `(p,)`, not `(p+1,)`. ```hint Why a coefficient is not enough A logit coefficient is a change in log-odds. Write how the probability itself responds to one regressor and look at what else that expression depends on. ``` ### Constraints & Assumptions - The grader uses a fixed dataset with `n = 60` and `p = 2` that is not perfectly separable, so the maximum likelihood estimate exists. - It asserts `result.shape == (2,)` and compares values with `numpy.testing.assert_allclose(rtol=1e-6)`. ### What a Strong Answer Covers - The logit fit with an added constant and the pinned solver settings. - The `get_margeff` call with `at="overall"` and `method="dydx"`, returned as a float64 array of shape `(p,)` without slicing. - Why the effect on the probability varies across rows and has to be averaged, with the formula for the logit. - How to read the result in probability units and how it compares with the raw coefficient. ### Follow-up Questions - How does `at="overall"` differ from `at="mean"`? - How would you report uncertainty around the AME? - How could you check the AME without calling `get_margeff`?

Overview: Fit a logistic regression with statsmodels and return the average marginal effect of each regressor on the predicted probability. It tests why logit coefficients are not probability effects, how per-row effects are averaged, and how the constant is handled.

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

Implement the function below. Fit a logistic regression and return the average marginal effects (AME) on the probability scale.

def average_marginal_effects(X: np.ndarray, y: np.ndarray) -> np.ndarray:
  • X is a 2-D float array of shape (n, p) with no intercept column, and y is a 1-D array of 0/1 integers.
  • Fit sm.Logit(y, sm.add_constant(X)).fit(method="newton", maxiter=200, tol=1e-10, disp=0) .
  • Return res.get_margeff(at="overall", method="dydx").margeff as a 1-D float64 array, passing both keywords explicitly.
  • The constant is excluded, so the shape is (p,) , not (p+1,) .

Constraints & Assumptions

  • The grader uses a fixed dataset with n = 60 and p = 2 that is not perfectly separable, so the maximum likelihood estimate exists.
  • It asserts result.shape == (2,) and compares values with numpy.testing.assert_allclose(rtol=1e-6) .

What a Strong Answer Covers Guidance

  • The logit fit with an added constant and the pinned solver settings.
  • The get_margeff call with at="overall" and method="dydx" , returned as a float64 array of shape (p,) without slicing.
  • Why the effect on the probability varies across rows and has to be averaged, with the formula for the logit.
  • How to read the result in probability units and how it compares with the raw coefficient.

Follow-up Questions Guidance

  • How does at="overall" differ from at="mean" ?
  • How would you report uncertainty around the AME?
  • How could you check the AME without calling get_margeff ?