Fit a logistic regression with statsmodels and compute McFadden's pseudo R-squared from the fitted and intercept-only log-likelihoods. Tests what the log-likelihood ratio measures, why it stays between 0 and 1, and how separable data breaks the fit.
Implement `mcfadden_pseudo_r2(X: np.ndarray, y: np.ndarray) -> float`. `X` is an `(n, p)` float matrix with no intercept column, and `y` is a 1-D 0/1 int array. Fit a logistic model and return McFadden's pseudo-R-squared as a plain Python `float`.
Requirements:
- Fit `sm.Logit(y, sm.add_constant(X)).fit(method='newton', maxiter=200, tol=1e-10, disp=0)`.
- Compute `1 - (llf / llnull)` by hand from `res.llf` and `res.llnull` (the intercept-only model). Do not just return `res.prsquared`.
- The result must match `res.prsquared` to `rtol=1e-6` and satisfy `0.0 < result < 1.0`.
The grader uses a fixed non-separable dataset with n = 60 and p = 2, and compares with `numpy.testing.assert_allclose(rtol=1e-6)`.
```hint What the null model predicts
The intercept-only model gives every row the same probability. Work out which probability maximizes its likelihood, and you can check `res.llnull` without statsmodels.
```
```hint Signs
Both log-likelihoods are negative. Check which way the ratio moves as the fitted model improves.
```
### Constraints & Assumptions
- `X` has shape `(n, p)`, contains floats and has no constant column. `y` is a 1-D array of 0s and 1s.
- The grading data has n = 60, p = 2 and is not separable.
- `statsmodels` and NumPy are available.
### What a Strong Answer Covers
- Adding the intercept before fitting, and what `llnull` measures.
- The McFadden formula and why it falls between 0 and 1 for a model with an intercept.
- Returning a plain Python `float` that matches `res.prsquared`.
- What happens when the data are separable or the fit does not converge.
- How to read the value and how it differs from the R-squared of linear regression.
### Follow-up Questions
- Why can McFadden's pseudo-R-squared never reach exactly 1?
- How would separable data break this computation?
- When would you prefer an adjusted or alternative pseudo-R-squared, such as Cox-Snell or Nagelkerke?
Overview: Fit a logistic regression with statsmodels and compute McFadden's pseudo R-squared from the fitted and intercept-only log-likelihoods. Tests what the log-likelihood ratio measures, why it stays between 0 and 1, and how separable data breaks the fit.
Implement mcfadden_pseudo_r2(X: np.ndarray, y: np.ndarray) -> float. X is an (n, p) float matrix with no intercept column, and y is a 1-D 0/1 int array. Fit a logistic model and return McFadden's pseudo-R-squared as a plain Python float.
Requirements:
Fit
sm.Logit(y, sm.add_constant(X)).fit(method='newton', maxiter=200, tol=1e-10, disp=0)
.
Compute
1 - (llf / llnull)
by hand from
res.llf
and
res.llnull
(the intercept-only model). Do not just return
res.prsquared
.
The result must match
res.prsquared
to
rtol=1e-6
and satisfy
0.0 < result < 1.0
.
The grader uses a fixed non-separable dataset with n = 60 and p = 2, and compares with numpy.testing.assert_allclose(rtol=1e-6).
Constraints & Assumptions
X
has shape
(n, p)
, contains floats and has no constant column.
y
is a 1-D array of 0s and 1s.
The grading data has n = 60, p = 2 and is not separable.
statsmodels
and NumPy are available.
What a Strong Answer Covers Guidance
Adding the intercept before fitting, and what
llnull
measures.
The McFadden formula and why it falls between 0 and 1 for a model with an intercept.
Returning a plain Python
float
that matches
res.prsquared
.
What happens when the data are separable or the fit does not converge.
How to read the value and how it differs from the R-squared of linear regression.
Follow-up Questions Guidance
Why can McFadden's pseudo-R-squared never reach exactly 1?
How would separable data break this computation?
When would you prefer an adjusted or alternative pseudo-R-squared, such as Cox-Snell or Nagelkerke?