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

Help with BF #478

Closed andreaeddyricci closed 11 months ago

andreaeddyricci commented 11 months ago

I am using the BF method to predict ultimates. I want to use an all year weighted average, as well as a five year weighted average. Here is my code. The ultimate and ldfs shouldn't be the same.

image

Desktop (please complete the following information):

jbogaardt commented 11 months ago

Hello @andreaeddyricci, I'm not sure I understand the full issue. Do you think the 5-year average LDF and the all year average LDF should be indentical? If so, wouldn't that be true only if you have a triangle with 5 annual diagonals? I think I might need to know more on the structure of your reported_triangle to know whether this is an issue.

andreaeddyricci commented 11 months ago

Hi John,

Thanks for the quick reply. Here is a portion of my reported triangle. My 5-year average LDF and the all year average LDF are different, which is what I expected. When I put these into the BornhuetterFerguson model though, the LDFs become identical and thus the ibnr and ultimate are also identical, which is not what I expected. Let me know if you would like to see anything else to better clarify. Thanks again!

[image: image.png]

On Tue, Dec 5, 2023 at 7:42 AM John S Bogaardt @.***> wrote:

Hello @andreaeddyricci https://github.com/andreaeddyricci, I'm not sure I understand the full issue. Do you think the 5-year average LDF and the all year average LDF should be indentical? If so, wouldn't that be true only if you have a triangle with 5 annual diagonals? I think I might need to know more on the structure of your reported_triangle to know whether this is an issue.

— Reply to this email directly, view it on GitHub https://github.com/casact/chainladder-python/issues/478#issuecomment-1840720159, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZQLLNYHRQR6QYZDH2KSMY3YH4JBZAVCNFSM6AAAAABAGZW6UKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNBQG4ZDAMJVHE . You are receiving this because you were mentioned.Message ID: @.***>

jbogaardt commented 11 months ago

Thanks for the additional explanation. I missed it in the code.

You have both variable triangle_bf_model5yr and triangle_bf_modelALLyr pointing at the same BF instance bf_model. That is, they're sharing the same model and when you refit it, both get the same underlying model.  You can avoid this by creating separate instances and references the separate instances in your code:

bf_model_5yr = cl.BornhuetterFerguson(apriori=0.7)
bf_model_ALLyr = cl.BornhuetterFerguson(apriori=0.7)

Key here is that bf_model is one instance. These models inherit from sklearn and the same behavior happens there:

from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([[1, 2, 3, 4]]).T
y1 = np.array([2, 4, 5, 8])
y2 = np.array([3, 6, 9, 12])

lm = LinearRegression()

model1 = lm.fit(X, y1)
model2 = lm.fit(X, y2)
# Similar behavior from scikit-learn models
print(model1.coef_ == model2.coef_)

lm2 = LinearRegression()
model1 = lm.fit(X, y1)
model2 = lm2.fit(X, y2)

print(model1.coef_ == model2.coef_)
andreaeddyricci commented 11 months ago

Wow, thank you so much. I overlooked that. I thought since I put different triangles into the model, that I wouldn't need a different instance. I really appreciate your time and explanation. Thanks again!

On Tue, Dec 5, 2023 at 10:07 AM John S Bogaardt @.***> wrote:

Thanks for the additional explanation. I missed it in the code.

You have both variable triangle_bf_model5yr and triangle_bf_modelALLyr pointing at the same BF instance bf_model. That is, they're sharing the same model and when you refit it, both get the same underlying model. You can avoid this by creating separate instances and references the separate instances in your code:

bf_model_5yr = cl.BornhuetterFerguson(apriori=0.7)bf_model_ALLyr = cl.BornhuetterFerguson(apriori=0.7)

Key here is that bf_model is one instance. These models inherit from sklearn and the same behavior happens there:

from sklearn.linearmodel import LinearRegressionimport numpy as np X = np.array([[1, 2, 3, 4]]).Ty1 = np.array([2, 4, 5, 8])y2 = np.array([3, 6, 9, 12]) lm = LinearRegression() model1 = lm.fit(X, y1)model2 = lm.fit(X, y2)# Similar behavior from scikit-learn modelsprint(model1.coef == model2.coef) lm2 = LinearRegression()model1 = lm.fit(X, y1)model2 = lm2.fit(X, y2) print(model1.coef == model2.coef_)

— Reply to this email directly, view it on GitHub https://github.com/casact/chainladder-python/issues/478#issuecomment-1840980959, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZQLLN252N5BBTIAHX5N2ETYH42EVAVCNFSM6AAAAABAGZW6UKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNBQHE4DAOJVHE . You are receiving this because you were mentioned.Message ID: @.***>