Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion chainladder/development/development.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion chainladder/development/outstanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 46 additions & 0 deletions chainladder/development/tests/test_development.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
63 changes: 63 additions & 0 deletions chainladder/development/tests/test_outstanding.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import chainladder as cl
import numpy as np
import pytest


def test_basic_case_outstanding():
Expand Down Expand Up @@ -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)
Loading