From e30648c178762583a020f4c5b87fc54db15f9c72 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Mon, 16 Nov 2026 18:40:32 +0000 Subject: [PATCH 1/5] [FIX] Build DevelopmentML projected valuations from an integer index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _get_triangle_ml stepped projected valuations by adding dgrain / 12 in floating point and then looked them up in valuation_encoder_ by exact match. For monthly grain the sums drift from the encoder's values, so most projected cells became NaN and were dropped, and DevelopmentML and BarnettZehnwirth ultimates no longer matched chain ladder on exact data. Projected valuations now come from the integer valuation index, divided once in the same way as the encoder. The new test fits a Poisson GLM to a monthly triangle with an exact multiplicative pattern and checks the square is complete and the ultimates match Chainladder. Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/learning.py | 12 ++++- .../development/tests/test_learning.py | 46 ++++++++++++++++++- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/chainladder/development/learning.py b/chainladder/development/learning.py index c809f49bb..ce27be452 100644 --- a/chainladder/development/learning.py +++ b/chainladder/development/learning.py @@ -215,10 +215,18 @@ def _get_triangle_ml(self, df, preds=None): latest_filter = latest_filter == latest_filter.max() preds = pd.DataFrame(preds.copy())[latest_filter].values out = df.loc[latest_filter].copy() + # valuation_encoder_ maps the i-th valuation date to i / periods_per_year. + # Step the integer index i and divide once so that projected valuations + # match the encoder's values exactly at every grain. + periods_per_year = 12 // dgrain + val_index = { + v: i for i, v in enumerate(sorted(self.valuation_encoder_.values())) + } + latest_val_index = out["valuation"].map(val_index) dev_lags = df["development"].drop_duplicates().sort_values() - for d in dev_lags[1:]: + for step, d in enumerate(dev_lags[1:], start=1): out["development"] = out["development"] + dgrain - out["valuation"] = out["valuation"] + dgrain / 12 + out["valuation"] = (latest_val_index + step) / periods_per_year if len(preds.shape) == 1: preds = preds[:, None] if self.autoregressive: diff --git a/chainladder/development/tests/test_learning.py b/chainladder/development/tests/test_learning.py index a496393e1..6b189e543 100644 --- a/chainladder/development/tests/test_learning.py +++ b/chainladder/development/tests/test_learning.py @@ -1,8 +1,9 @@ import chainladder as cl -from sklearn.linear_model import LinearRegression +from sklearn.linear_model import LinearRegression, TweedieRegressor from sklearn.pipeline import Pipeline from chainladder.utils.utility_functions import PatsyFormula import pandas as pd +import numpy as np def test_incremental(genins): @@ -74,3 +75,46 @@ def test_grain(): ).fit(tri) assert grains[period] == len(model.triangle_ml_.development) assert grains[period] == len(model.triangle_ml_.origin) + + +def test_monthly_poisson_glm_matches_chainladder(): + # A cross-classified Poisson GLM reproduces chain ladder (Renshaw and + # Verrall, 1998), so on a monthly triangle with an exact multiplicative + # pattern the projected square must be complete and the ultimates equal. + n = 8 + origins = pd.date_range("2020-01-01", periods=n, freq="MS") + pattern = 1 - 0.5 ** np.arange(1, n + 1) + rows = [ + { + "origin": o, + "valuation": o + pd.DateOffset(months=k + 1) - pd.Timedelta(days=1), + "values": 1000.0 * (1 + 0.1 * a) * pattern[k], + } + for a, o in enumerate(origins) + for k in range(n - a) + ] + tri = cl.Triangle( + pd.DataFrame(rows), + origin="origin", + valuation="valuation", + columns="values", + cumulative=True, + ) + model = cl.DevelopmentML( + Pipeline( + steps=[ + ("design_matrix", PatsyFormula("C(development) + C(origin)")), + ( + "model", + TweedieRegressor( + power=1, link="log", fit_intercept=False, alpha=0, max_iter=1000 + ), + ), + ] + ), + y_ml="values", + ).fit(tri) + assert not np.isnan(model.triangle_ml_.values).any() + expected = cl.Chainladder().fit(tri).ultimate_.values + actual = cl.Chainladder().fit(model.transform(tri)).ultimate_.values + assert np.allclose(actual, expected, rtol=1e-4) From 25ca40c9e5bb03df2959374b3b4b720a34590c99 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Tue, 17 Nov 2026 15:39:51 +0000 Subject: [PATCH 2/5] [FIX] Find the DevelopmentML latest diagonal by valuation at every grain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _get_triangle_ml marked the latest diagonal with a formula in origin and development periods that holds only when the two grains match. At mixed grains such as OYDM it missed cells at the latest valuation, so those origins were not projected and future cells of triangle_ml_ stayed empty. The latest diagonal is now the set of cells at the latest encoded valuation. test_grain also checks that triangle_ml_ has no NaN, and a new test fits TweedieGLM to the monthly sample at ten grains and checks every future cell is filled. Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/learning.py | 9 ++++----- chainladder/development/tests/test_learning.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/chainladder/development/learning.py b/chainladder/development/learning.py index ce27be452..d4b071505 100644 --- a/chainladder/development/learning.py +++ b/chainladder/development/learning.py @@ -208,11 +208,10 @@ def _get_triangle_ml(self, df, preds=None): X_r = [df] y_r = [preds] dgrain = __grain_to_months__[self.development_grain_] - ograin = 12 // __grain_to_months__[self.origin_grain_] - latest_filter = (df["origin"] + 1) * ograin + ( - df["development"] - dgrain - ) / dgrain - latest_filter = latest_filter == latest_filter.max() + # The latest diagonal holds the cells at the latest valuation. Comparing + # encoded valuations finds it whether or not the origin and development + # grains match, as in OYDM. + latest_filter = df["valuation"] == df["valuation"].max() preds = pd.DataFrame(preds.copy())[latest_filter].values out = df.loc[latest_filter].copy() # valuation_encoder_ maps the i-th valuation date to i / periods_per_year. diff --git a/chainladder/development/tests/test_learning.py b/chainladder/development/tests/test_learning.py index 6b189e543..7cfd0dc79 100644 --- a/chainladder/development/tests/test_learning.py +++ b/chainladder/development/tests/test_learning.py @@ -1,4 +1,5 @@ import chainladder as cl +import pytest from sklearn.linear_model import LinearRegression, TweedieRegressor from sklearn.pipeline import Pipeline from chainladder.utils.utility_functions import PatsyFormula @@ -75,6 +76,7 @@ def test_grain(): ).fit(tri) assert grains[period] == len(model.triangle_ml_.development) assert grains[period] == len(model.triangle_ml_.origin) + assert not np.isnan(model.triangle_ml_.values).any() def test_monthly_poisson_glm_matches_chainladder(): @@ -118,3 +120,19 @@ def test_monthly_poisson_glm_matches_chainladder(): expected = cl.Chainladder().fit(tri).ultimate_.values actual = cl.Chainladder().fit(model.transform(tri)).ultimate_.values assert np.allclose(actual, expected, rtol=1e-4) + + +@pytest.mark.parametrize( + "grain", + ["OMDM", "OQDM", "OSDM", "OYDM", "OQDQ", "OSDQ", "OYDQ", "OSDS", "OYDS", "OYDY"], +) +def test_projection_fills_future_cells(monthly, grain): + # DevelopmentML projects each origin forward from the latest diagonal, so + # every future cell of triangle_ml_ must be filled at every grain, + # including grains whose origin and development periods differ. + tri = monthly["reportedCount"].grain(grain) + ml = cl.TweedieGLM(response="reportedCount").fit(tri).triangle_ml_ + values = ml.set_backend("numpy").values[0, 0] + future = np.isnan(tri.set_backend("numpy").nan_triangle) + assert values.shape == future.shape + assert not np.isnan(values[future]).any() From f7b58cbc4b7619d61944c1d952d092a7c97d2870 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Wed, 18 Nov 2026 03:34:49 -0500 Subject: [PATCH 3/5] [TST] Pin GLM ultimates at every grain and cite Renshaw and Verrall MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The exact-pattern monthly triangle is now the exact_monthly fixture, with 24 origins so yearly grains keep two. test_poisson_glm_matches_chainladder runs at the ten grains and checks the Poisson GLM's chain ladder ultimates against Chainladder on the same triangle. On main it fails at seven of the ten. references.bib gains renshaw1998, which the test comment names. Developed with an AI assistant (Árbol, running Muse). --- .../development/tests/test_learning.py | 51 +++++++++++++++---- docs/library/references.bib | 11 ++++ 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/chainladder/development/tests/test_learning.py b/chainladder/development/tests/test_learning.py index 7cfd0dc79..3da492974 100644 --- a/chainladder/development/tests/test_learning.py +++ b/chainladder/development/tests/test_learning.py @@ -79,11 +79,26 @@ def test_grain(): assert not np.isnan(model.triangle_ml_.values).any() -def test_monthly_poisson_glm_matches_chainladder(): - # A cross-classified Poisson GLM reproduces chain ladder (Renshaw and - # Verrall, 1998), so on a monthly triangle with an exact multiplicative - # pattern the projected square must be complete and the ultimates equal. - n = 8 +GRAINS = [ + "OMDM", + "OQDM", + "OSDM", + "OYDM", + "OQDQ", + "OSDQ", + "OYDQ", + "OSDS", + "OYDS", + "OYDY", +] + + +@pytest.fixture +def exact_monthly(): + # Two years of monthly origins whose cumulative values follow one + # multiplicative development pattern, so every grain has at least two + # origins and a full upper triangle. + n = 24 origins = pd.date_range("2020-01-01", periods=n, freq="MS") pattern = 1 - 0.5 ** np.arange(1, n + 1) rows = [ @@ -95,13 +110,26 @@ def test_monthly_poisson_glm_matches_chainladder(): for a, o in enumerate(origins) for k in range(n - a) ] - tri = cl.Triangle( + return cl.Triangle( pd.DataFrame(rows), origin="origin", valuation="valuation", columns="values", cumulative=True, ) + + +# newton-cholesky converges in about ten iterations here, where lbfgs needs +# thousands. Its gradient step emits a matmul RuntimeWarning, and the fit +# still matches chain ladder at the tolerance below. +@pytest.mark.filterwarnings("ignore:invalid value encountered in matmul:RuntimeWarning") +@pytest.mark.parametrize("grain", GRAINS) +def test_poisson_glm_matches_chainladder(exact_monthly, grain): + # A cross-classified Poisson GLM reproduces chain ladder (Renshaw and + # Verrall, 1998, renshaw1998 in docs/library/references.bib), so at every + # grain the projected square must be complete and its chain ladder + # ultimates must equal the ultimates of chain ladder on the triangle. + tri = exact_monthly.grain(grain) model = cl.DevelopmentML( Pipeline( steps=[ @@ -109,7 +137,11 @@ def test_monthly_poisson_glm_matches_chainladder(): ( "model", TweedieRegressor( - power=1, link="log", fit_intercept=False, alpha=0, max_iter=1000 + power=1, + link="log", + fit_intercept=False, + alpha=0, + solver="newton-cholesky", ), ), ] @@ -122,10 +154,7 @@ def test_monthly_poisson_glm_matches_chainladder(): assert np.allclose(actual, expected, rtol=1e-4) -@pytest.mark.parametrize( - "grain", - ["OMDM", "OQDM", "OSDM", "OYDM", "OQDQ", "OSDQ", "OYDQ", "OSDS", "OYDS", "OYDY"], -) +@pytest.mark.parametrize("grain", GRAINS) def test_projection_fills_future_cells(monthly, grain): # DevelopmentML projects each origin forward from the latest diagonal, so # every future cell of triangle_ml_ must be filled at every grain, diff --git a/docs/library/references.bib b/docs/library/references.bib index c3ca2ff67..e5ec9ce43 100644 --- a/docs/library/references.bib +++ b/docs/library/references.bib @@ -120,3 +120,14 @@ @article{merz2014 year = {2014}, url = {https://doi.org/10.2139/ssrn.2524352} } + +@article{renshaw1998, + author = {Renshaw, A. E. and Verrall, R. J.}, + title = {A {S}tochastic {M}odel {U}nderlying the {C}hain-{L}adder {T}echnique}, + journal = {British Actuarial Journal}, + volume = {4}, + number = {4}, + pages = {903--923}, + year = {1998}, + url = {https://doi.org/10.1017/S1357321700000222} +} From be30ac5f7f803c1b12262379f9687ee8736f3447 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Wed, 18 Nov 2026 07:38:41 -0500 Subject: [PATCH 4/5] [TST] Retrigger CI after a runner download timeout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 3.13 unit-test job on f7b58cbc failed in Set up job when the setup-uv action download timed out. This empty commit reruns CI. Developed with an AI assistant (Árbol, running Muse). From fbf1d2033f70e7c27388d12c1e50f933e5eb24f8 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Wed, 18 Nov 2026 17:23:58 +0000 Subject: [PATCH 5/5] [TST] Turn the leading test comments into docstrings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Developed with an AI assistant (Árbol, running Muse). --- .../development/tests/test_learning.py | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/chainladder/development/tests/test_learning.py b/chainladder/development/tests/test_learning.py index 3da492974..952dd53a4 100644 --- a/chainladder/development/tests/test_learning.py +++ b/chainladder/development/tests/test_learning.py @@ -95,9 +95,11 @@ def test_grain(): @pytest.fixture def exact_monthly(): - # Two years of monthly origins whose cumulative values follow one - # multiplicative development pattern, so every grain has at least two - # origins and a full upper triangle. + """ + Two years of monthly origins whose cumulative values follow one + multiplicative development pattern, so every grain has at least two + origins and a full upper triangle. + """ n = 24 origins = pd.date_range("2020-01-01", periods=n, freq="MS") pattern = 1 - 0.5 ** np.arange(1, n + 1) @@ -125,10 +127,12 @@ def exact_monthly(): @pytest.mark.filterwarnings("ignore:invalid value encountered in matmul:RuntimeWarning") @pytest.mark.parametrize("grain", GRAINS) def test_poisson_glm_matches_chainladder(exact_monthly, grain): - # A cross-classified Poisson GLM reproduces chain ladder (Renshaw and - # Verrall, 1998, renshaw1998 in docs/library/references.bib), so at every - # grain the projected square must be complete and its chain ladder - # ultimates must equal the ultimates of chain ladder on the triangle. + """ + A cross-classified Poisson GLM reproduces chain ladder (Renshaw and + Verrall, 1998, renshaw1998 in docs/library/references.bib), so at every + grain the projected square must be complete and its chain ladder + ultimates must equal the ultimates of chain ladder on the triangle. + """ tri = exact_monthly.grain(grain) model = cl.DevelopmentML( Pipeline( @@ -156,9 +160,11 @@ def test_poisson_glm_matches_chainladder(exact_monthly, grain): @pytest.mark.parametrize("grain", GRAINS) def test_projection_fills_future_cells(monthly, grain): - # DevelopmentML projects each origin forward from the latest diagonal, so - # every future cell of triangle_ml_ must be filled at every grain, - # including grains whose origin and development periods differ. + """ + DevelopmentML projects each origin forward from the latest diagonal, so + every future cell of triangle_ml_ must be filled at every grain, + including grains whose origin and development periods differ. + """ tri = monthly["reportedCount"].grain(grain) ml = cl.TweedieGLM(response="reportedCount").fit(tri).triangle_ml_ values = ml.set_backend("numpy").values[0, 0]