Skip to content
Open
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
5 changes: 4 additions & 1 deletion chainladder/development/learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ def _prep_X_ml(self, X): # noqa: N802
def _prep_w_ml(self, X, sample_weight=None):
# scikit-learn requires a dense sample_weight
backend = "cupy" if X.array_backend == "cupy" else "numpy"
obj = X.set_backend(backend)
# _prep_X_ml builds one row per cell of the cumulative triangle. An
# incremental triangle can leave some of those cells empty, so the
# weights come from the cumulative triangle to get the same rows.
obj = X.set_backend(backend).incr_to_cum()
weight_base = (~np.isnan(obj.values)).astype(float)
weight = weight_base.copy()
weight = (
Expand Down
24 changes: 24 additions & 0 deletions chainladder/development/tests/test_glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,27 @@ def test_sample_weight(genins):
).max()
< 1e-2
)


def test_incremental_triangle_with_empty_cells(monthly):
"""
An incremental triangle can leave a cell empty, such as a development
quarter with no records, where its cumulative triangle has a value.
TweedieGLM must fit the incremental triangle, with or without
drop_valuation, the same way it fits the cumulative one.

https://github.com/casact/chainladder-python/issues/1640
"""
incremental = monthly["reportedCount"].grain("OYDQ")
cumulative = incremental.incr_to_cum()
assert np.isnan(incremental.values).sum() > np.isnan(cumulative.values).sum()
coef = {}
for name, X in (("incremental", incremental), ("cumulative", cumulative)):
for drop in (None, "2017Q2"):
model = cl.TweedieGLM(drop_valuation=drop).fit(X)
coef[name, drop] = model.model_.estimator_ml["model"].coef_
for drop in (None, "2017Q2"):
np.testing.assert_allclose(coef["incremental", drop], coef["cumulative", drop])
# 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"])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be comparing coef["incremenetal", ...] and coef["cumulative", ...]?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop just above this line already does that. It compares the incremental and cumulative coefficients for both drop settings. This line checks that dropping 2017Q2 changes the fit, because the loop would also pass if both fits ignored drop_valuation, as in #1619. I pushed a comment above it that says so. Would a different check read better to you?

(This comment was written with Árbol, an AI assistant, under my direction.)

Loading