From ee0101723d72329b1a365d1290f071d596509df2 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Mon, 16 Nov 2026 23:30:38 -0500 Subject: [PATCH 1/2] Build DevelopmentML weights from the cumulative triangle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _prep_X_ml builds one row per cell of the cumulative triangle, but _prep_w_ml built one weight per non-empty cell of the triangle it was given. On an incremental triangle with empty cells in its observed area the counts differ, and since #1637 TweedieGLM passes the weights on every fit, so the plain fit raised a sample_weight shape error. Fixes #1640 Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/learning.py | 5 ++++- chainladder/development/tests/test_glm.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/chainladder/development/learning.py b/chainladder/development/learning.py index c809f49bb..7e7625d4f 100644 --- a/chainladder/development/learning.py +++ b/chainladder/development/learning.py @@ -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 = ( diff --git a/chainladder/development/tests/test_glm.py b/chainladder/development/tests/test_glm.py index ba624389b..947550cbb 100644 --- a/chainladder/development/tests/test_glm.py +++ b/chainladder/development/tests/test_glm.py @@ -74,3 +74,25 @@ 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]) + assert not np.allclose(coef["incremental", None], coef["incremental", "2017Q2"]) From 31e64f669f439f4b9aad9480e1d16505b0175070 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Tue, 17 Nov 2026 05:09:20 +0000 Subject: [PATCH 2/2] Explain the drop check in the empty-cell test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The last assertion checks that drop_valuation changes the fit, so the incremental-to-cumulative comparison cannot pass with the drop ignored. Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/tests/test_glm.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/chainladder/development/tests/test_glm.py b/chainladder/development/tests/test_glm.py index 947550cbb..5a09a57c9 100644 --- a/chainladder/development/tests/test_glm.py +++ b/chainladder/development/tests/test_glm.py @@ -95,4 +95,6 @@ def test_incremental_triangle_with_empty_cells(monthly): 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"])