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..5a09a57c9 100644 --- a/chainladder/development/tests/test_glm.py +++ b/chainladder/development/tests/test_glm.py @@ -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"])