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
11 changes: 7 additions & 4 deletions chainladder/development/glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down
44 changes: 44 additions & 0 deletions chainladder/development/tests/test_glm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import numpy as np

import chainladder as cl


Expand All @@ -14,6 +16,48 @@ def test_basic_odp_cl(genins):
)


def test_drop_valuation_without_sample_weight(genins):
"""
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 = (
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):
"""
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]
assert np.round(dropped, 4).tolist() == [3.571, 1.755, 1.4606, 1.1748]


def test_sample_weight(genins):
assert (
abs(
Expand Down
Loading