Repository navigation
Fix TweedieGLM ignoring drop/drop_valuation without sample_weight - #1637
Conversation
DevelopmentML builds the exclusion weights from drop/drop_valuation in _prep_w_ml, but only forwards them to the estimator when weighted_step is set. TweedieGLM only set weighted_step='model' when sample_weight was given, so the exclusions were silently ignored otherwise. Always pass weighted_step='model' (as BarnettZehnwirth does); with no exclusions the weights are all ones, so the unweighted fit is unchanged. Fixes casact#1619
Pyright Type CompletenessView the full Project (full
Other symbols referenced but not exported by
Symbols without documentation:
Patch (exported symbols added or changed by this PR): 0.0% fully typed (0 / 2)
Patch symbol details
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1637 +/- ##
==========================================
- Coverage 95.00% 95.00% -0.01%
==========================================
Files 100 100
Lines 6461 6460 -1
Branches 820 819 -1
==========================================
- Hits 6138 6137 -1
Misses 204 204
Partials 119 119
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@scks1 do you might giving this a review? |
scks1
left a comment
There was a problem hiding this comment.
Looks good to me. I checked it locally on genins against main at c113fd9.
- The plain fit is unchanged. Its coefficients and LDFs match main exactly, with a max abs difference of 0.0.
- On main,
drop_valuation="2005",drop=("2001", 12), the list forms of both, anddropwithdrop_valuationtogether all leave the LDFs at the plain fit. On this branch each one changes the fit and matches the same call with a triangle of ones assample_weightexactly. - With a triangle of twos as
sample_weight, the drops still apply and give the same LDFs as ones. - All 8 tests in
test_glm.pypass locally.
Thanks for picking this up!
(This comment was written with Árbol, an AI assistant, under my direction.)
|
|
||
| def test_drop_valuation_without_sample_weight(genins): | ||
| # https://github.com/casact/chainladder-python/issues/1619 | ||
| # drop_valuation must take effect even when sample_weight is None, and |
There was a problem hiding this comment.
please turn into docstring
|
|
||
|
|
||
| def test_drop_without_sample_weight(genins): | ||
| # https://github.com/casact/chainladder-python/issues/1619 |
Per henrydingliu's review on casact#1637, turn the leading comment of test_drop_valuation_without_sample_weight into a docstring and add a docstring to test_drop_without_sample_weight.
Fixes #1619
TweedieGLMaccepteddrop/drop_valuationbut silently ignored them unlesssample_weightwas also passed.Root cause
DevelopmentML._prep_w_mlalready builds the exclusion weights fromdrop/drop_valuation(viaTriangleWeight), butDevelopmentML.fitonly forwards those weights to the estimator whenweighted_stepis set (chainladder/development/learning.py:341-342).TweedieGLM.fitsetweighted_step="model"only whensample_weightwas given, andNoneotherwise (chainladder/development/glm.py:217-220), so the computed weights were discarded.Fix
Always pass
weighted_step="model"inTweedieGLM.fit, matchingBarnettZehnwirth(chainladder/development/barnzehn.py:179). With no exclusions the weights are all ones, so the unweighted fit is unchanged.Repro (base
main@c113fd9a)Before fix — all three identical:
After fix:
Control cases (unchanged behavior preserved)
0.0, LDF max abs diff0.0,n_iter_63/63) — passing all-ones weights is a no-op forTweedieRegressor.drop_valuation="2005"withoutsample_weightnow equals the same call with an all-onessample_weight([3.5057, 1.7861, 1.4546, 1.1711]), i.e. the value the issue reported as the working case.dropcomposes with a non-trivialsample_weight(all 2s): base unchanged, drop still changes the fit.drop_valuation=["2005", "2006"]gives[3.6441, 1.8049, 1.4452, 1.1695];fit_transformwith a drop works.Tests
New regression tests in
chainladder/development/tests/test_glm.py(test_drop_valuation_without_sample_weight,test_drop_without_sample_weight):Regression scope:
test_learning.py+test_barnzehn.py+test_development.py— 97 passed, 6 failed. The 6 failures are pre-existing on the clean tree (verified viagit stashbaseline): all intest_development.pydrop-warning tests forcl.Development, which this diff does not touch.ruff checkandruff format --checkpass on both changed files.Diff scope
chainladder/development/glm.py:TweedieGLM.fitalways passesweighted_step="model"(+7/-4 with comment).chainladder/development/tests/test_glm.py: two new regression tests.Deliberately unchanged:
DevelopmentMLused directly with a pipeline and noweighted_stepstill silently ignores drops — the issue body explicitly scopes that out ("Not checked"), so it is left for a follow-up, not this PR. #1584 (separateTweedieGLMbug) is untouched.