From f85c9ae97423e1dcdd835259692232bb917545c6 Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Mon, 16 Nov 2026 19:32:06 +0000 Subject: [PATCH 1/2] [FIX] Label Development parameters with one age per link ratio MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Development._param_property took the age labels for ldf_, sigma_ and std_err_ from X.link_ratio.ddims. link_ratio drops link-ratio columns with no ratio before the valuation date, so a triangle with more development ages than origins, such as raa[raa.origin >= "1983"], got 9 parameter values and 7 labels, and every method failed in nan_triangle. The labels now come from X.ddims[:-1], one per link ratio. The new test checks the labels on three origin subsets and compares Chainladder's IBNR with a hand chain ladder (Mack, 1993). Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/development.py | 4 +- .../development/tests/test_development.py | 46 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/chainladder/development/development.py b/chainladder/development/development.py index a97dc5582..bdf46a475 100644 --- a/chainladder/development/development.py +++ b/chainladder/development/development.py @@ -565,7 +565,9 @@ def _param_property(self, X, params, idx): obj = X[X.origin == X.origin.min()] xp = X.get_array_module() obj.values = xp.ones(obj.shape)[..., :-1] * params[..., idx : idx + 1, :] - obj.ddims = X.link_ratio.ddims + # One label per link-ratio column. X.link_ratio can drop columns that + # have no ratio before the valuation date, so it cannot supply them. + obj.ddims = X.ddims[:-1] obj.valuation_date = pd.to_datetime(options.ULT_VAL) obj.is_pattern = True obj.is_cumulative = False diff --git a/chainladder/development/tests/test_development.py b/chainladder/development/tests/test_development.py index c6e8b78b9..bf9c12a17 100644 --- a/chainladder/development/tests/test_development.py +++ b/chainladder/development/tests/test_development.py @@ -1132,3 +1132,49 @@ def test_valid_params_pass_validation(raa): cl.Development(average=lambda age: "geometric", n_periods=[-1] * 9).fit(raa) dev = cl.Development(n_periods=4, average="simple") assert cl.read_json(dev.to_json()).get_params() == dev.get_params() + + +def _hand_chainladder_ibnr(values): + """ + Volume-weighted chain ladder IBNR from a 2D cumulative array with NaN in + unobserved cells. Each factor uses the origins observed at both ages, and a + factor with no such origin is 1. + """ + factors = np.ones(values.shape[1] - 1) + for j in range(values.shape[1] - 1): + both = ~np.isnan(values[:, j]) & ~np.isnan(values[:, j + 1]) + if both.any(): + factors[j] = values[both, j + 1].sum() / values[both, j].sum() + ibnr = 0.0 + for row in values: + last = np.flatnonzero(~np.isnan(row))[-1] + ibnr += row[last] * (np.prod(factors[last:]) - 1) + return ibnr + + +@pytest.mark.parametrize("backend", ["numpy", "sparse"]) +@pytest.mark.parametrize( + "sample, first_origin, expected", + [ + ("raa", "1982", 54901.61), + ("raa", "1983", 44554.44), + ("genins", "2004", 12013150.15), + ], +) +def test_origin_subset_with_more_ages_than_origins( + sample, first_origin, expected, backend +): + """ + Check that dropping the oldest origins leaves one age label per fitted + parameter, and that ``Chainladder`` then matches a hand chain ladder + (Mack, 1993) although no remaining origin reaches the last ages. + """ + tri = cl.load_sample(sample) + tri = tri[tri.origin >= first_origin].set_backend(backend) + dev = cl.Development().fit(tri) + for param in (dev.ldf_, dev.sigma_, dev.std_err_): + assert len(param.ddims) == param.shape[-1] + ibnr = float(cl.Chainladder().fit(tri).ibnr_.sum()) + hand = _hand_chainladder_ibnr(tri.set_backend("numpy").values[0, 0]) + assert np.isclose(ibnr, hand) + assert round(ibnr, 2) == expected From d03816d703d6a58e2b0b4a6b8b83a3629b8385ff Mon Sep 17 00:00:00 2001 From: Simon Koch-Sultan Date: Mon, 16 Nov 2026 19:59:15 +0000 Subject: [PATCH 2/2] [FIX] Label CaseOutstanding patterns with one age per link ratio MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CaseOutstanding._set_ldf took the age labels for ldf_ from X.link_ratio.ddims, which drops link-ratio columns with no ratio before the valuation date. On a triangle with more development ages than origins, such as comauto origins from 1992 in clrd, fit failed in nan_triangle (cannot reshape array of size 30 into shape (6,9)). The labels now come from X.ddims[:-1], as in Development. The new test checks the labels on three origin subsets and compares the paid IBNR with a hand case outstanding development (Friedland, 2010, ch. 12). Developed with an AI assistant (Árbol, running Muse). --- chainladder/development/outstanding.py | 4 +- .../development/tests/test_outstanding.py | 63 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/chainladder/development/outstanding.py b/chainladder/development/outstanding.py index 184a19fd3..258a3c0ca 100644 --- a/chainladder/development/outstanding.py +++ b/chainladder/development/outstanding.py @@ -254,7 +254,9 @@ def _set_ldf(self, X): # Convert the paid/incurred to multiplicative LDF dev = (dev.iloc[..., -1] / dev).iloc[..., :-1] dev.valuation_date = pd.Timestamp(options.ULT_VAL) - dev.ddims = X.link_ratio.ddims + # One label per link-ratio column. X.link_ratio can drop columns that + # have no ratio before the valuation date, so it cannot supply them. + dev.ddims = X.ddims[:-1] dev.is_pattern = True dev.is_cumulative = True diff --git a/chainladder/development/tests/test_outstanding.py b/chainladder/development/tests/test_outstanding.py index 656c75c0f..c0c667ccf 100644 --- a/chainladder/development/tests/test_outstanding.py +++ b/chainladder/development/tests/test_outstanding.py @@ -1,5 +1,6 @@ import chainladder as cl import numpy as np +import pytest def test_basic_case_outstanding(): @@ -345,3 +346,65 @@ def test_unweighted_ratios_preserved_when_n_periods_set(): assert np.allclose( m.case_to_prior_case_.values, m_full.case_to_prior_case_.values, equal_nan=True ) + + +def _hand_case_outstanding_unpaid(paid, incurred): + """ + Unpaid claims from the case outstanding development technique (Friedland, + 2010, ch. 12, Approach 1) on 2D cumulative arrays with NaN in unobserved + cells. Each ratio is the simple average over the origins observed at both + ages. An age with no such origin keeps case outstanding unchanged and adds + no payment. + """ + case = incurred - paid + n_ages = paid.shape[1] + case_ratio = np.ones(n_ages) + paid_ratio = np.zeros(n_ages) + for j in range(1, n_ages): + both = ~np.isnan(case[:, j]) & ~np.isnan(case[:, j - 1]) + if both.any(): + case_ratio[j] = np.mean(case[both, j] / case[both, j - 1]) + paid_ratio[j] = np.mean( + (paid[both, j] - paid[both, j - 1]) / case[both, j - 1] + ) + unpaid = 0.0 + for row in case: + last = np.flatnonzero(~np.isnan(row))[-1] + outstanding = row[last] + for j in range(last + 1, n_ages): + unpaid += outstanding * paid_ratio[j] + outstanding *= case_ratio[j] + return unpaid + + +@pytest.mark.parametrize( + "sample, columns, first_origin, expected", + [ + ("clrd", ("CumPaidLoss", "IncurLoss"), "1992", 1205826.14), + ("clrd", ("CumPaidLoss", "IncurLoss"), "1994", 849163.92), + ("usauto", ("paid", "incurred"), "2002", 61733801.04), + ], +) +def test_origin_subset_with_more_ages_than_origins( + sample, columns, first_origin, expected +): + """ + Check that dropping the oldest origins leaves one age label per pattern + value, and that the paid IBNR then matches a hand case outstanding + development (Friedland, 2010, ch. 12) although no remaining origin + reaches the last ages. + """ + tri = cl.load_sample(sample) + if sample == "clrd": + tri = tri.groupby("LOB").sum().loc["comauto"] + tri = tri[list(columns)] + tri = tri[tri.origin >= first_origin] + model = cl.CaseOutstanding(paid_to_incurred=columns).fit(tri) + for pattern in (model.ldf_, model.paid_ldf_, model.case_ldf_): + assert len(pattern.ddims) == pattern.shape[-1] + ibnr = float(cl.Chainladder().fit(model.transform(tri)).ibnr_[columns[0]].sum()) + hand = _hand_case_outstanding_unpaid( + tri[columns[0]].values[0, 0], tri[columns[1]].values[0, 0] + ) + assert np.isclose(ibnr, hand, rtol=1e-10) + assert np.isclose(ibnr, expected, atol=0.01)