Why liblinear and lbfgs give different logistic regression intercepts on imbalanced data
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `def intercept_penalization_gap(X: np.ndarray, y: np.ndarray) -> tuple`. `X` is a standardized float64 matrix of shape `(n_samples, n_features)` and `y` is binary in `{0, 1}` with a strongly imbalanced base rate. Fit the same L2 logistic model with two solvers and return both intercepts.
Requirements:
- Fit two `LogisticRegression` models on the same data: `solver='liblinear'` and `solver='lbfgs'`.
- Both use exactly: `penalty='l2'`, `C=0.001`, `fit_intercept=True`, `intercept_scaling=1.0`, `max_iter=100000`, `tol=1e-10`, `random_state=0`.
- Return `(intercept_liblinear, intercept_lbfgs)`, each as `float(model.intercept_[0])` (plain Python `float`).
`X` is already standardized: do not rescale it.
```hint How each solver adds the intercept
Check how `liblinear` puts an intercept into its model, and whether that term passes through the L2 penalty the same way it does for `lbfgs`.
```
```hint Size of the base-rate term
Work out the log-odds of a rare positive class, for example a 5% rate, and ask what a strong penalty would do to a parameter of that size.
```
### What a Strong Answer Covers
- Two fits that differ only in the solver, with the exact shared hyperparameters and the intercepts returned as plain floats.
- How each solver treats the intercept with respect to the L2 penalty, and which way the gap between the two intercepts points.
- Why the strong class imbalance and the small `C` make the gap large.
- How the setup rules out under-convergence as the cause of the difference.
### Follow-up Questions
- How would you make `liblinear`'s intercept behave more like `lbfgs`'s?
- Why does penalizing the intercept especially distort results on imbalanced data?
- How does the gap change as `C` or the number of samples grows?
Overview: Fit the same strongly regularized L2 logistic regression with the liblinear and lbfgs solvers on imbalanced data and compare the two intercepts. It tests how each solver treats the intercept under the penalty, why a skewed base rate makes the gap large, and how intercept_scaling changes it.