From 6a8176a9c3771dd5fd86bcc469b4bb31fee814cc Mon Sep 17 00:00:00 2001 From: Huyvux12 <152865675+Huyvux12@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:10:05 +0000 Subject: [PATCH 1/2] Fix TweedieGLM ignoring drop/drop_valuation without sample_weight 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 #1619 --- chainladder/development/glm.py | 11 ++++--- chainladder/development/tests/test_glm.py | 36 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/chainladder/development/glm.py b/chainladder/development/glm.py index 495a1c52b..a8bdd8e22 100644 --- a/chainladder/development/glm.py +++ b/chainladder/development/glm.py @@ -214,10 +214,13 @@ def __init__( def fit(self, X, y=None, sample_weight=None): response = X.columns[0] if not self.response else self.response - if sample_weight is None: - weight = None - else: - weight = "model" + # Always forward the weights: DevelopmentML builds them from + # drop/drop_valuation in _prep_w_ml, but only passes them to the + # estimator when weighted_step is set. Passing "model" unconditionally + # (as BarnettZehnwirth does) makes the exclusions take effect even + # when sample_weight is None; with no exclusions the weights are all + # ones, so the unweighted fit is unchanged. + weight = "model" self.model_ = DevelopmentML( Pipeline( steps=[ diff --git a/chainladder/development/tests/test_glm.py b/chainladder/development/tests/test_glm.py index 6e972b4a6..e3c2b9e62 100644 --- a/chainladder/development/tests/test_glm.py +++ b/chainladder/development/tests/test_glm.py @@ -1,3 +1,5 @@ +import numpy as np + import chainladder as cl @@ -14,6 +16,40 @@ def test_basic_odp_cl(genins): ) +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 + # must match the result of passing an all-ones sample_weight. The + # unweighted fit itself must be unchanged. + base = cl.TweedieGLM().fit(genins).ldf_.values[0, 0, 0, :4] + dropped = cl.TweedieGLM(drop_valuation="2005").fit(genins).ldf_.values[0, 0, 0, :4] + dropped_weighted = ( + cl + .TweedieGLM(drop_valuation="2005") + .fit(genins, sample_weight=genins * 0 + 1) + .ldf_.values[0, 0, 0, :4] + ) + assert np.round(base, 4).tolist() == [3.491, 1.7474, 1.4574, 1.1739] + assert np.round(dropped, 4).tolist() == [3.5057, 1.7861, 1.4546, 1.1711] + assert np.round(dropped_weighted, 4).tolist() == [3.5057, 1.7861, 1.4546, 1.1711] + # list form goes through the same TriangleWeight path + dropped_list = ( + cl + .TweedieGLM(drop_valuation=["2005", "2006"]) + .fit(genins) + .ldf_.values[0, 0, 0, :4] + ) + assert np.round(dropped_list, 4).tolist() == [3.6441, 1.8049, 1.4452, 1.1695] + + +def test_drop_without_sample_weight(genins): + # https://github.com/casact/chainladder-python/issues/1619 + base = cl.TweedieGLM().fit(genins).ldf_.values[0, 0, 0, :4] + dropped = cl.TweedieGLM(drop=("2001", 12)).fit(genins).ldf_.values[0, 0, 0, :4] + assert np.round(base, 4).tolist() == [3.491, 1.7474, 1.4574, 1.1739] + assert np.round(dropped, 4).tolist() == [3.571, 1.755, 1.4606, 1.1748] + + def test_sample_weight(genins): assert ( abs( From 49ccfaa72829a7da1f1f50f790bef575cbfb074e Mon Sep 17 00:00:00 2001 From: Huyvux12 <152865675+Huyvux12@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:04:54 +0000 Subject: [PATCH 2/2] Address review: docstrings for new TweedieGLM drop tests Per henrydingliu's review on #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. --- chainladder/development/tests/test_glm.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/chainladder/development/tests/test_glm.py b/chainladder/development/tests/test_glm.py index e3c2b9e62..ba624389b 100644 --- a/chainladder/development/tests/test_glm.py +++ b/chainladder/development/tests/test_glm.py @@ -17,10 +17,13 @@ def test_basic_odp_cl(genins): 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 - # must match the result of passing an all-ones sample_weight. The - # unweighted fit itself must be unchanged. + """ + drop_valuation must take effect even when sample_weight is None, and + must match the result of passing an all-ones sample_weight. The + unweighted fit itself must be unchanged. + + https://github.com/casact/chainladder-python/issues/1619 + """ base = cl.TweedieGLM().fit(genins).ldf_.values[0, 0, 0, :4] dropped = cl.TweedieGLM(drop_valuation="2005").fit(genins).ldf_.values[0, 0, 0, :4] dropped_weighted = ( @@ -43,7 +46,12 @@ def test_drop_valuation_without_sample_weight(genins): def test_drop_without_sample_weight(genins): - # https://github.com/casact/chainladder-python/issues/1619 + """ + drop must take effect even when sample_weight is None, while the + unweighted fit itself is unchanged. + + https://github.com/casact/chainladder-python/issues/1619 + """ base = cl.TweedieGLM().fit(genins).ldf_.values[0, 0, 0, :4] dropped = cl.TweedieGLM(drop=("2001", 12)).fit(genins).ldf_.values[0, 0, 0, :4] assert np.round(base, 4).tolist() == [3.491, 1.7474, 1.4574, 1.1739]