From d4734023821d147f8de6b929a86d5f8d43961f76 Mon Sep 17 00:00:00 2001 From: "henrydingliu@gmail.com" Date: Thu, 28 May 2026 04:32:40 +0000 Subject: [PATCH] adding support for multi-triangle in mackchainladder --- chainladder/methods/mack.py | 2 +- chainladder/methods/tests/test_mack.py | 11 +- docs/friedland/chapter_7.rst | 157 ------------------------- 3 files changed, 10 insertions(+), 160 deletions(-) diff --git a/chainladder/methods/mack.py b/chainladder/methods/mack.py index cfcf71e0..15bf820e 100644 --- a/chainladder/methods/mack.py +++ b/chainladder/methods/mack.py @@ -242,7 +242,7 @@ def _get_full_std_err_(self, X=None): val = xp.broadcast_to(xp.array(avg + [avg[-1]]), X.shape) weight = xp.sqrt(full.values[..., : len(X.ddims)] ** (2 - val)) obj.values = X.sigma_.values / num_to_nan(weight) - w = lxp.concatenate((X.w_, lxp.ones((1, 1, val.shape[2], 1))), 3) + w = lxp.concatenate((X.w_, lxp.ones((val.shape[0], val.shape[1], val.shape[2], 1))), 3) w[xp.isnan(w)] = 1 obj.values = xp.nan_to_num(obj.values) * xp.array(w) obj.valuation_date = full.valuation_date diff --git a/chainladder/methods/tests/test_mack.py b/chainladder/methods/tests/test_mack.py index c9adc020..ac9bf6c5 100644 --- a/chainladder/methods/tests/test_mack.py +++ b/chainladder/methods/tests/test_mack.py @@ -1,4 +1,5 @@ import chainladder as cl +import numpy as np def test_mack_to_triangle(): assert ( @@ -14,10 +15,16 @@ def test_mack_to_triangle(): .summary_ ) - def test_mack_malformed(): a = cl.load_sample('raa') b = a.iloc[:, :, :-1] x = cl.MackChainladder().fit(a) y = cl.MackChainladder().fit(b) - assert x.process_risk_.iloc[:,:,:-1] == y.process_risk_ \ No newline at end of file + assert x.process_risk_.iloc[:,:,:-1] == y.process_risk_ + +def test_multi_triangle_mack(clrd,atol): + tri = clrd.loc['Agway Ins Co']['IncurLoss','CumPaidLoss'] + mack = cl.MackChainladder().fit(tri) + for i in range(len(tri.index)): + for j in range(len(tri.columns)): + assert np.all(abs(mack.full_std_err_.iloc[i,j].values-cl.MackChainladder().fit(tri.iloc[i,j]).full_std_err_.values) < atol) \ No newline at end of file diff --git a/docs/friedland/chapter_7.rst b/docs/friedland/chapter_7.rst index 64f6f1b1..9b2630a8 100644 --- a/docs/friedland/chapter_7.rst +++ b/docs/friedland/chapter_7.rst @@ -27,7 +27,6 @@ PART 1 - Data Triangle We have already imported the necessary packages loading the ``Triangle`` at the top of p106. Let's take a look at the ``Triangle`` we just loaded. .. doctest:: - :hide: >>> tri = cl.load_sample('friedland_us_industry_auto') >>> tri['Reported Claims'] @@ -43,162 +42,6 @@ We have already imported the necessary packages loading the ``Triangle`` at the 2006 46582684.0 54641339.0 NaN NaN NaN NaN NaN NaN NaN NaN 2007 48853563.0 NaN NaN NaN NaN NaN NaN NaN NaN NaN -.. code-block:: - - >>> tri = cl.load_sample('friedland_us_industry_auto') - -.. raw:: html - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1224364860728496108120
199837,017,48743,169,00945,568,91946,784,55847,337,31847,533,26447,634,41947,689,65547,724,67847,742,304
199938,954,48446,045,71848,882,92450,219,67250,729,29250,926,77951,069,28551,163,54051,185,767
200041,155,77649,371,47852,358,47653,780,32254,303,08654,582,95054,742,18854,837,929
200142,394,06950,584,11253,704,29655,150,11855,895,58356,156,72756,299,562
200244,755,24352,971,64356,102,31257,703,85158,363,56458,592,712
200345,163,10252,497,73155,468,55157,015,41157,565,344
200445,417,30952,640,32255,553,67356,976,657
200546,360,86953,790,06156,786,410
200646,582,68454,641,339
200748,853,563
- PART 2 - Age-to-Age Factors ----------------------------