Commit fcd4b8a
Fix OLS prediction SE and logistic l1_ratio silent ignore (#180)
* Fix OLS prediction SE and logistic l1_ratio silent ignore
Two correctness bugs in microimpute/models/ols.py:
1. Prediction SE used residual std only (#6). _OLSModel scaled normal
quantiles by se = sqrt(model.scale), ignoring the leverage term. The
prediction SE for a new observation is
sqrt(scale * (1 + x'(X'X)^-1 x)) = sqrt(var_pred_mean + scale). Test
rows far from the training centroid were systematically
under-dispersed; at extreme quantiles (0.01, 0.99) the under-
dispersion is material. Switched to statsmodels'
model.get_prediction(X).var_pred_mean + model.scale to obtain
per-row prediction variance.
Additionally clipped the mean_quantile to (1e-6, 1-1e-6) so q=0 or
q=1 no longer produce ±inf via norm.ppf or a=inf in the beta
distribution alpha formula.
2. Logistic l1_ratio silently ignored (#8). LogisticRegression uses
l1_ratio only when penalty="elasticnet" (and solver supports it).
Previously the classifier passed l1_ratio through with the default
penalty="l2", and sklearn warned "l1_ratio parameter is only used
when penalty is 'elasticnet'". Callers tuning l1_ratio saw no
change. Now: when a non-zero l1_ratio is supplied, penalty is set
to "elasticnet" and solver defaults to "saga".
Tests
- test_ols_quantile_uses_full_prediction_se: widths of prediction
intervals must grow with leverage (centroid vs extrapolation).
- test_ols_quantile_clips_q_away_from_zero_and_one: q=0 and q=1
return finite predictions.
- test_logistic_l1_ratio_activates_elasticnet: l1_ratio=0.5 must
result in penalty="elasticnet" and solver="saga".
* Return indexed Series from OLS _predict_quantile to preserve test index
Followup to the prior commit. _predict_quantile returned a bare
ndarray for numeric targets. When OLSResults._predict assembled the
output DataFrame, assigning the first (numeric) column as an ndarray
caused pandas to anchor the DataFrame to a default RangeIndex (0..N).
A subsequent categorical prediction — a pd.Series indexed by the real
X_test index — then failed index alignment and all rows came back
NaN. Downstream sklearn.log_loss then raised "Input contains NaN".
The symptom was specific to mixed-type imputation (numeric + binary
or categorical) with a non-zero-based test index. CI on 3.14 runs
the full suite and caught it via
tests/test_metrics.py::test_compare_metrics_mixed_types; 3.12 only
runs smoke tests so it was hidden there.
Fix: _predict_quantile now returns pd.Series(values,
index=mean_preds.index, name=mean_preds.name). Added
test_ols_mixed_targets_preserve_test_index as a regression.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 5668e9f commit fcd4b8a
3 files changed
Lines changed: 193 additions & 23 deletions
File tree
- changelog.d
- microimpute/models
- tests/test_models
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
63 | | - | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
64 | 68 | | |
65 | | - | |
66 | 69 | | |
67 | 70 | | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
73 | 85 | | |
74 | 86 | | |
75 | 87 | | |
| |||
216 | 228 | | |
217 | 229 | | |
218 | 230 | | |
219 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
220 | 237 | | |
221 | | - | |
222 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
223 | 245 | | |
224 | | - | |
| 246 | + | |
225 | 247 | | |
226 | 248 | | |
227 | 249 | | |
| |||
362 | 384 | | |
363 | 385 | | |
364 | 386 | | |
365 | | - | |
| 387 | + | |
366 | 388 | | |
367 | 389 | | |
368 | 390 | | |
369 | | - | |
| 391 | + | |
370 | 392 | | |
371 | 393 | | |
372 | 394 | | |
373 | 395 | | |
374 | | - | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
375 | 399 | | |
376 | 400 | | |
377 | 401 | | |
378 | 402 | | |
379 | 403 | | |
380 | 404 | | |
381 | 405 | | |
382 | | - | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
383 | 412 | | |
384 | 413 | | |
385 | 414 | | |
386 | 415 | | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
387 | 421 | | |
388 | | - | |
| 422 | + | |
389 | 423 | | |
390 | | - | |
| 424 | + | |
391 | 425 | | |
392 | 426 | | |
393 | 427 | | |
394 | | - | |
395 | | - | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
396 | 431 | | |
397 | 432 | | |
398 | 433 | | |
| |||
406 | 441 | | |
407 | 442 | | |
408 | 443 | | |
409 | | - | |
410 | | - | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
411 | 447 | | |
412 | | - | |
413 | | - | |
414 | | - | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
415 | 453 | | |
416 | 454 | | |
417 | 455 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
258 | 258 | | |
259 | 259 | | |
260 | 260 | | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
0 commit comments