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..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, ) @@ -98,3 +94,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 + 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, + ) 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",