Build a Logistic Regression Inference Table With Wald Tests and Odds Ratios
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `inference_table(X: pd.DataFrame, y: np.ndarray) -> pd.DataFrame`. `X` has a default `RangeIndex` and named `float64` columns with no intercept, and `y` is a 1-D 0/1 int array. Fit `sm.Logit(y, sm.add_constant(X)).fit(method='newton', maxiter=200, tol=1e-10, disp=0)` and return its inference table.
Requirements:
- The columns are exactly `['coef', 'std_err', 'z', 'p_value', 'odds_ratio', 'or_ci_low', 'or_ci_high']`, all `float64` and unrounded.
- `coef` is `res.params`, `std_err` is `res.bse`, and `z` is `res.params / res.bse`.
- `p_value` is `2 * scipy.stats.norm.sf(np.abs(z))`.
- `odds_ratio` is `exp(coef)`. `or_ci_low` and `or_ci_high` are the exponentiated columns 0 and 1 of `res.conf_int(alpha=0.05)`. The check also confirms that every row with `p_value < 0.05` has an interval that excludes `1.0`, and every row with `p_value > 0.05` has one that contains it.
- The index holds the string parameter names in fit order (`'const'` first, then the `X` columns), with `index.name` set to `None`.
```hint Read it off the fit
Every column comes from attributes of the fitted result object or a simple function of them. Check what `params`, `bse` and `conf_int` return and how each one is indexed.
```
```hint Exact comparison
The comparison checks column order, dtypes and the index exactly, with a tight tolerance. Think about which steps could change a dtype, reorder rows, or move a value by more than that tolerance.
```
### Constraints & Assumptions
- The test builds a fixed, non-separable frame with `n = 80` rows and `p = 3` columns named `['math', 'female', 'read']`. The expected index is `['const', 'math', 'female', 'read']`.
- The result is compared with `pandas.testing.assert_frame_equal(result, expected, rtol=1e-5, check_dtype=True, check_like=False)`, so column order matters.
### What a Strong Answer Covers
- Adding the intercept and fitting with the exact call given, so `'const'` comes first
- Deriving each column from the fitted result: coefficients, standard errors, Wald z, the two-sided p-value, the odds ratio and the exponentiated interval bounds
- Why the p-value and the odds-ratio interval cannot disagree about significance at the 0.05 level
- Producing the exact frame: column order, `float64` dtypes, index labels in fit order, no index name and no rounding
### Follow-up Questions
- Why can the Wald p-value and the confidence interval never disagree here?
- When would you prefer profile-likelihood or likelihood-ratio intervals over Wald intervals?
- How would you handle quasi-separation that produces huge coefficients and standard errors?
Overview: Fit a logistic regression in statsmodels and return a pandas table of coefficients, standard errors, Wald z statistics, p-values, odds ratios and their confidence intervals. Tests reading a fitted model correctly, exact DataFrame output and why the p-value and the interval agree.