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
12 changes: 10 additions & 2 deletions chainladder/development/learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
46 changes: 45 additions & 1 deletion chainladder/development/tests/test_learning.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Loading