Commit 14b001e
fix: correct IRLS Hessian for weighted logistic regression
Two bugs in _abessGLM::_IRLS_fit and abessLogistic::hessian_core
caused the weighted IRLS to give different results from fitting
on repeated data, breaking sklearn's check_sample_weight_equivalence.
Bug 1 (hessian_core): Pi*(1-Pi)*sw was truncated to [1e-3, 1] as a
whole. When a near-saturated sample (Pi*(1-Pi) < 1e-3) had sw > 1,
the lower clamp replaced the product with 1e-3, losing the sample
weight. This made X_new^T*X_full differ between the weighted (n=9,
sw=[3,4,...]) and repeated (n=27, all sw=1) cases at saturation.
Fix: truncate Pi*(1-Pi) BEFORE multiplying by sw, so near-saturated
rows keep their correct relative weight (1e-3 * sw_i vs 1e-3 * 1).
Bug 2 (safe D_bare division): The original D.cwiseQuotient(weights)
causes 0/0=NaN when sw_i=0 (even if D_i=0 after the hessian fix).
Since X_new[i,:]=0 for zero-weight rows, Z[i] can be any finite
value. Fix: use 1.0 as placeholder when sw_i=0 to avoid NaN
propagation in the matrix products.
Together these fixes ensure X_new^T*Z (gradient) and X_new^T*X_full
(Hessian) are numerically equivalent for weighted and repeated-data
fits, allowing sklearn's check_sample_weights_equivalence to pass.
Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>1 parent d24f340 commit 14b001e
1 file changed
Lines changed: 11 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
254 | 254 | | |
255 | 255 | | |
256 | 256 | | |
257 | | - | |
258 | | - | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
259 | 264 | | |
260 | 265 | | |
261 | 266 | | |
| |||
316 | 321 | | |
317 | 322 | | |
318 | 323 | | |
319 | | - | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
320 | 327 | | |
321 | | - | |
| 328 | + | |
322 | 329 | | |
323 | 330 | | |
324 | 331 | | |
| |||
0 commit comments