diff --git a/chainladder/methods/base.py b/chainladder/methods/base.py index 543d8c815..1b8d37e0e 100644 --- a/chainladder/methods/base.py +++ b/chainladder/methods/base.py @@ -151,15 +151,38 @@ def fit_predict(self, X, y=None, sample_weight=None): self.fit(X, y, sample_weight) return self.predict(X, sample_weight) - def _include_process_variance(self): - if hasattr(self.X_, "_get_process_variance"): + def _include_process_variance(self, X=None): + """ + Add simulated process variance to an ultimate triangle. + + With no argument this rewrites ``self.ultimate_`` from the + triangle the model was fit on (the ``fit()`` path). Pass the + predicted triangle from ``predict()`` to rewrite its + ``ultimate_`` instead, leaving the fitted state untouched. + Returns the process variance triangle, or ``None`` when the + triangle carries no simulated process variance. The full + triangle is only built when process variance is present, so + predicting on a plain triangle costs nothing extra. + """ + if X is None: + if not hasattr(self.X_, "_get_process_variance"): + return None full = self.full_triangle_ obj = self.X_._get_process_variance(full) self.ultimate_.values = obj.values[..., -1:] - process_var = obj - full - else: - process_var = None - return process_var + return obj - full + if not hasattr(X, "_get_process_variance"): + return None + full = X.full_triangle_ + obj = X._get_process_variance(full) + if obj.values[..., -1:].shape != X.ultimate_.values.shape: + # e.g. a subset of simulations: the variance draw is bound to + # the full bootstrap, so it cannot apply here. Keep base + # behaviour (point estimates, correct shape) instead of + # corrupting ultimate_. + return None + X.ultimate_.values = obj.values[..., -1:] + return obj - full @staticmethod def validate_ldf(X: Triangle, ldf: Triangle) -> None: diff --git a/chainladder/methods/benktander.py b/chainladder/methods/benktander.py index add140083..a149a8ddb 100644 --- a/chainladder/methods/benktander.py +++ b/chainladder/methods/benktander.py @@ -216,7 +216,10 @@ def predict(self, X, sample_weight=None): Returns ------- X_new: Triangle - Loss data with Benktander ultimate applied + Loss data with Benktander ultimate applied. When ``X`` + carries simulated process variance (for example from + ``BootstrapODPSample``), the predicted ultimate includes it, + matching ``fit()``. Examples -------- @@ -255,6 +258,7 @@ def predict(self, X, sample_weight=None): X_new = super().predict(X, sample_weight) X_new.expectation_ = self._get_benktander_aprioris(X, sample_weight) X_new.ultimate_ = self._get_ultimate(X_new, X_new.expectation_) + X_new.process_variance_ = self._include_process_variance(X_new) X_new.n_iters = self.n_iters X_new.apriori = self.apriori return X_new diff --git a/chainladder/methods/chainladder.py b/chainladder/methods/chainladder.py index 92434406c..7a91cd51f 100644 --- a/chainladder/methods/chainladder.py +++ b/chainladder/methods/chainladder.py @@ -164,7 +164,10 @@ def predict(self, X, sample_weight=None): Returns ------- X_new: Triangle - Loss data with chainladder ultimate applied + Loss data with chainladder ultimate applied. When ``X`` + carries simulated process variance (for example from + ``BootstrapODPSample``), the predicted ultimate includes it, + matching ``fit()``. Examples -------- @@ -198,6 +201,7 @@ def predict(self, X, sample_weight=None): """ X_new = super().predict(X, sample_weight) X_new.ultimate_ = self._get_ultimate(X_new, sample_weight) + X_new.process_variance_ = self._include_process_variance(X_new) return X_new def _get_ultimate(self, X, sample_weight=None): diff --git a/chainladder/methods/tests/test_predict.py b/chainladder/methods/tests/test_predict.py index 85ff7a015..b186a0391 100644 --- a/chainladder/methods/tests/test_predict.py +++ b/chainladder/methods/tests/test_predict.py @@ -98,6 +98,72 @@ def test_bs_random_state_predict(clrd): ) +def test_predict_carries_process_variance(raa): + # https://github.com/casact/chainladder-python/issues/1639 + sims = cl.BootstrapODPSample(n_sims=2000, random_state=42).fit_transform(raa) + + def sd(ult): + return float(np.std((ult - sims.latest_diagonal).sum("origin").values)) + + model = cl.Chainladder().fit(sims) + fit_sd = sd(model.ultimate_) + fit_ult = model.ultimate_.values.copy() + pred = model.predict(sims) + # predict() must carry the same process variance as fit() + assert sd(pred.ultimate_) == pytest.approx(fit_sd, rel=1e-12) + assert np.allclose(pred.ultimate_.values, model.ultimate_.values, equal_nan=True) + # ... and must not mutate the fitted model + assert np.allclose(model.ultimate_.values, fit_ult, equal_nan=True) + # VotingChainladder inherits it through predict() + vot = cl.VotingChainladder([("a", cl.Chainladder()), ("b", cl.Chainladder())]).fit( + sims + ) + assert sd(vot.ultimate_) == pytest.approx(fit_sd, rel=1e-12) + # Control: a plain triangle has no process variance to carry, so + # predict() still matches fit() exactly. + plain = cl.Chainladder().fit(raa) + assert np.allclose( + plain.predict(raa).ultimate_.values, plain.ultimate_.values, equal_nan=True + ) + assert plain.predict(raa).process_variance_ is None + + +def test_predict_subset_of_simulations_keeps_shape(raa): + # #1639: predicting on a subset of simulations must keep the + # subset's shape and fall back to point estimates. + sims = cl.BootstrapODPSample(n_sims=2000, random_state=42).fit_transform(raa) + model = cl.Chainladder().fit(sims) + sub = sims.iloc[:5] + pred = model.predict(sub) + assert pred.ultimate_.shape == (5, 1, 10, 1) + assert len(pred.ultimate_.index) == 5 + assert pred.process_variance_ is None + from chainladder.methods.base import MethodBase + + X_new = MethodBase.predict(model, sub) + assert np.allclose( + pred.ultimate_.values, + model._get_ultimate(X_new).values, + equal_nan=True, + ) + + +def test_benktander_predict_carries_process_variance(genins): + # #1639: same missing-variance bug as Chainladder.predict + boot = cl.BootstrapODPSample(n_sims=2000, random_state=42).fit_transform(genins) + w = boot.latest_diagonal * 0 + 1 + + def sd(ult): + return float(np.std((ult - boot.latest_diagonal).sum("origin").values)) + + bk = cl.Benktander(apriori=1.0, n_iters=2).fit(boot, sample_weight=w) + fit_sd = sd(bk.ultimate_) + pred = bk.predict(boot, sample_weight=w) + assert sd(pred.ultimate_) == pytest.approx(fit_sd, rel=1e-12) + assert np.allclose(pred.ultimate_.values, bk.ultimate_.values, equal_nan=True) + assert pred.process_variance_ is not None + + def test_basic_transform(raa): cl.Development().fit_transform(raa) cl.ClarkLDF().fit_transform(raa)