From e30648c178762583a020f4c5b87fc54db15f9c72 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Mon, 16 Nov 2026 18:40:32 +0000 Subject: [PATCH] [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)