From a89beb7d42c7e298f20de10f0219902aefbdf5e5 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Wed, 11 Nov 2026 11:49:00 +0000 Subject: [PATCH 1/3] [FIX] Pass alpha from TweedieGLM to TweedieRegressor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TweedieGLM stored alpha but built its TweedieRegressor without it, so every fit used scikit-learn's default penalty of 1.0. With alpha passed, TweedieGLM(alpha=0) on ukmotor reproduces the chain ladder ultimates. Closes #1584. Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/glm.py | 1 + chainladder/development/tests/test_glm.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/chainladder/development/glm.py b/chainladder/development/glm.py index a8bdd8e22..f99ac9283 100644 --- a/chainladder/development/glm.py +++ b/chainladder/development/glm.py @@ -230,6 +230,7 @@ def fit(self, X, y=None, sample_weight=None): TweedieRegressor( link=self.link, power=self.power, + alpha=self.alpha, max_iter=self.max_iter, tol=self.tol, warm_start=self.warm_start, diff --git a/chainladder/development/tests/test_glm.py b/chainladder/development/tests/test_glm.py index 5a09a57c9..6d945869a 100644 --- a/chainladder/development/tests/test_glm.py +++ b/chainladder/development/tests/test_glm.py @@ -98,3 +98,26 @@ def test_incremental_triangle_with_empty_cells(monthly): # Dropping 2017Q2 must change the fit. Without this check, the comparison # above would also pass if both fits ignored drop_valuation (#1619). assert not np.allclose(coef["incremental", None], coef["incremental", "2017Q2"]) + + +def test_alpha_reaches_regressor(): + """ + TweedieGLM must pass alpha to its TweedieRegressor. Without the penalty, + the over-dispersed Poisson GLM reproduces chain ladder, so + TweedieGLM(alpha=0) on ukmotor must give the chain ladder ultimates. + + https://github.com/casact/chainladder-python/issues/1584 + """ + ukmotor = cl.load_sample("ukmotor") + glm = cl.TweedieGLM(alpha=0).fit(ukmotor) + assert glm.model_.estimator_ml.named_steps["model"].alpha == 0 + assert ( + abs( + ( + cl.Chainladder().fit(ukmotor).ultimate_ + - cl.Chainladder().fit(glm.transform(ukmotor)).ultimate_ + ) + / ukmotor.latest_diagonal + ).max() + < 1e-3 + ) From 337e1112d4dae52e272b90740d5096ed5a70fa72 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Wed, 18 Nov 2026 17:39:25 +0000 Subject: [PATCH 2/3] [DOCS] Set alpha=0 in the GLM LDF gallery example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TweedieGLM defaults to alpha=1.0, so the unpenalized ODP fit that matches chain ladder needs alpha=0 now that TweedieGLM passes alpha to TweedieRegressor. Developed with an AI assistant (Árbol, running Muse). --- docs/gallery/plot_glm_ldf.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/gallery/plot_glm_ldf.ipynb b/docs/gallery/plot_glm_ldf.ipynb index 971f7bbd5..fa8257ecb 100644 --- a/docs/gallery/plot_glm_ldf.ipynb +++ b/docs/gallery/plot_glm_ldf.ipynb @@ -53,7 +53,7 @@ "\n", "# Fit an ODP GLM\n", "dev = cl.TweedieGLM(\n", - " design_matrix=\"C(development) + C(origin)\", link=\"log\", power=1\n", + " design_matrix=\"C(development) + C(origin)\", link=\"log\", power=1, alpha=0\n", ").fit(genins)\n", "\n", "# Grab LDFs vs traditional approach\n", From 52b786ede4d4f5d513a8eb6de43624a7dd09b916 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Wed, 18 Nov 2026 18:38:10 +0000 Subject: [PATCH 3/3] [TST] Tighten TweedieGLM parity tests to assert_allclose MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit test_basic_odp_cl, test_sample_weight and test_alpha_reaches_regressor compared ultimates through max(|difference| / latest diagonal) with bounds of 1e-2 and 1e-3. Now that TweedieGLM passes alpha through, the three tests fit with alpha=0 and use np.testing.assert_allclose with rtol=1e-5 on the ultimates, and the genins and ukmotor tests also on ldf_ against Development. The largest measured relative differences are 5.7e-07 on genins and 1.4e-07 on ukmotor. On main all three fail. Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/tests/test_glm.py | 60 +++++++++++------------ 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/chainladder/development/tests/test_glm.py b/chainladder/development/tests/test_glm.py index 6d945869a..4560bd1b2 100644 --- a/chainladder/development/tests/test_glm.py +++ b/chainladder/development/tests/test_glm.py @@ -4,15 +4,20 @@ def test_basic_odp_cl(genins): - assert ( - abs( - ( - cl.Chainladder().fit(genins).ultimate_ - - cl.Chainladder().fit(cl.TweedieGLM().fit_transform(genins)).ultimate_ - ) - / genins.latest_diagonal - ).max() - < 1e-2 + """ + Without the penalty, the over-dispersed Poisson GLM reproduces chain + ladder, so TweedieGLM(alpha=0) must match its factors and ultimates. + """ + glm = cl.TweedieGLM(alpha=0).fit(genins) + np.testing.assert_allclose( + glm.ldf_.values[..., :1, :], + cl.Development().fit(genins).ldf_.values, + rtol=1e-5, + ) + np.testing.assert_allclose( + cl.Chainladder().fit(glm.transform(genins)).ultimate_.values, + cl.Chainladder().fit(genins).ultimate_.values, + rtol=1e-5, ) @@ -59,20 +64,11 @@ def test_drop_without_sample_weight(genins): def test_sample_weight(genins): - assert ( - abs( - ( - cl.Chainladder().fit(genins).ultimate_ - - cl - .Chainladder() - .fit( - cl.TweedieGLM().fit_transform(genins, sample_weight=genins / genins) - ) - .ultimate_ - ) - / genins.latest_diagonal - ).max() - < 1e-2 + glm = cl.TweedieGLM(alpha=0).fit(genins, sample_weight=genins / genins) + np.testing.assert_allclose( + cl.Chainladder().fit(glm.transform(genins)).ultimate_.values, + cl.Chainladder().fit(genins).ultimate_.values, + rtol=1e-5, ) @@ -111,13 +107,13 @@ def test_alpha_reaches_regressor(): ukmotor = cl.load_sample("ukmotor") glm = cl.TweedieGLM(alpha=0).fit(ukmotor) assert glm.model_.estimator_ml.named_steps["model"].alpha == 0 - assert ( - abs( - ( - cl.Chainladder().fit(ukmotor).ultimate_ - - cl.Chainladder().fit(glm.transform(ukmotor)).ultimate_ - ) - / ukmotor.latest_diagonal - ).max() - < 1e-3 + np.testing.assert_allclose( + glm.ldf_.values[..., :1, :], + cl.Development().fit(ukmotor).ldf_.values, + rtol=1e-5, + ) + np.testing.assert_allclose( + cl.Chainladder().fit(glm.transform(ukmotor)).ultimate_.values, + cl.Chainladder().fit(ukmotor).ultimate_.values, + rtol=1e-5, )