casact / chainladder-python

Actuarial reserving in Python
https://chainladder-python.readthedocs.io/en/latest/
Mozilla Public License 2.0
192 stars 71 forks source link

[BUG] #411

Closed henrydingliu closed 1 year ago

henrydingliu commented 1 year ago

Describe the bug applying a cl estimator to another triangle doesn't always work

To Reproduce

import chainladder as cl
clrd_total = cl.load_sample('clrd').sum()[["CumPaidLoss"]]
clrd = cl.load_sample('clrd').groupby("LOB").sum()[["CumPaidLoss"]]
est = cl.Chainladder().fit(clrd_total)
try:
    est_other = est.predict(clrd[clrd["LOB"]=="comauto"])
except:
    print("this doesn't work")
est = cl.Chainladder().fit(clrd[clrd["LOB"]=="comauto"])
est_other = est.predict(clrd_total)
print(est_other.ultimate_)
print("but this works")

Expected behavior the code inside the try should work

Desktop (please complete the following information):

jbogaardt commented 1 year ago

Thanks for finding. This is a case where broadcasting should take precendence over index alignment. That is if X.shape[0] == 1 or est.cdf_.shape[0] == 1, then these should multiply together like numpy arrays. Basic arithmetic works:

ultimate = (
    clrd[clrd["LOB"]=="comauto"] * 
    est.cdf_.iloc[..., :-1]
).latest_diagonal

So I think there is an issue in intersection method of the Chainladder model that is not allowing broadcasting when it should.