ENH: Add WeightedPoisson datafit with sample weights support - #341
ENH: Add WeightedPoisson datafit with sample weights support#341Vrinda12-tech wants to merge 2 commits into
Conversation
mathurinm
left a comment
There was a problem hiding this comment.
thanks a lot for the initiative !
There are lots of tests, do you think you could shorten them a bit for easier maintenance ? like factorize some aprts
also, can you have a single test where you have weights equal to 0, 1 or 2, and test that you get the same result as fitting with duplicated and removed samples (weights=2 or 0 respectively). I think this would remove the need for testing the lipschitz constant for example
| from .base import BaseDatafit, BaseMultitaskDatafit | ||
| from .single_task import (Quadratic, QuadraticSVC, Logistic, Huber, Poisson, Gamma, | ||
| Cox, WeightedQuadratic, QuadraticHessian,) | ||
| Cox, WeightedQuadratic, QuadraticHessian,WeightedPoisson) |
There was a problem hiding this comment.
you need spaces after the commas
| np.testing.assert_allclose(lasso.coef_, qpl1.coef_) | ||
| # check that it's not just because we got alpha too high and thus 0 coef | ||
| np.testing.assert_array_less(0.1, np.max(np.abs(qpl1.coef_))) | ||
| # ============================================================================= |
There was a problem hiding this comment.
this looks like a duplicate of what's 3 lines below
|
|
||
| def test_weighted_poisson_validation(): | ||
| """Test input validation for WeightedPoisson.""" | ||
| import numpy as np |
There was a problem hiding this comment.
you can put the imports at the top of the file
|
@Badr-MOUFAD at some point I wonder if it wouldn't be easier if every datafit had weights by default... but not that much time to think about it |
|
@mathurinm Thank you for the detailed review. I have addressed all the points:
All 4 tests ( Let me know if you have any other suggestions! |
Summary
This PR adds a weighted variant of the Poisson datafit, enabling sample weights support for Poisson regression in skglm.
Implementation
WeightedPoissoninskglm/datafits/single_task.pyWeightedQuadraticpatternnp.clip(Xw, -500, 500)preventsexpoverflowMathematical Derivation
The weighted Poisson loss is:
L = (1/Σw) * Σ( w_i * (exp((Xw)_i) - y_i * (Xw)_i) )
The weighted gradient:
∇L = (w * (exp(Xw) - y)) / Σw
The weighted Hessian:
H = (w * exp(Xw)) / Σw
Tests Added
All 7 tests are passing locally.
References
WeightedQuadraticimplementation patternPoissondatafit for unweighted version