Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chainladder/development/glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
65 changes: 42 additions & 23 deletions chainladder/development/tests/test_glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down Expand Up @@ -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,
)


Expand All @@ -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,
)
2 changes: 1 addition & 1 deletion docs/gallery/plot_glm_ldf.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading