Repository navigation
Conversation
BornhuetterFerguson, Benktander, ExpectedLoss and CapeCod read sample_weight by position along the origin axis. A weight labelled with other origins paired each origin of X with another origin's exposure, and CapeCod raised KeyError. These methods now reindex the weight to the origins of X when both have more than one origin at the same grain. An origin of X with no weight gets no ultimate, origins found only in the weight are left out, and a warning names both. Closes casact#1646 Developed with an AI assistant (Árbol, running Muse).
Pyright Type CompletenessView the full Project (full
Other symbols referenced but not exported by
Symbols without documentation:
Patch (exported symbols added or changed by this PR): 0.0% fully typed (0 / 12)
Patch symbol details
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1648 +/- ##
==========================================
+ Coverage 95.03% 95.04% +0.01%
==========================================
Files 100 101 +1
Lines 6460 6524 +64
Branches 819 828 +9
==========================================
+ Hits 6139 6201 +62
- Misses 203 204 +1
- Partials 118 119 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
after an initial read, i'm wondering if you could route much of the new methods through existing functionality within dunders. |
|
Happy to route it through dunders. There's one snag first. The union path in Which would you prefer?
(This comment was written with Árbol, an AI assistant, under my direction.) |
|
the dunders bug may be very involved. we should forge ahead. i've been thinking about this some more. things that i believe are safe to assume about X and sample_weight
can we simply slice sample_weight with max of the earliest origins of both and the min of the latest origins of both? |
Following the review on casact#1648, replace the label matching with a slice. _slice_weight_origins raises when X and sample_weight have different origin grains, keeps the weight from the later of the two first origins to the earlier of the two last origins, and gives NaN weight to origins of X outside that range, which then get no ultimate. It raises a clear error when the ranges share no origin or when the origins in the shared range do not line up, as with the gapped slices of raa in casact#1649. Developed with an AI assistant (Árbol, running Muse).
sample_weight to X by origin label in BornhuetterFerguson, Benktander, ExpectedLoss and CapeCodsample_weight to the origins it shares with X in BornhuetterFerguson, Benktander, ExpectedLoss and CapeCod
|
That works. The new commit drops the label matching. One question remains. The helper relies on consecutive origins, and slices of (This comment was written with Árbol, an AI assistant, under my direction.) |
Summary of Changes
BornhuetterFerguson,Benktander,ExpectedLossandCapeCodnow slicesample_weightto the origin range it shares withX. Before this change they read the weight by position along the origin axis. Onraa, whose origins run from 1981 to 1990, a premium triangle labelled 1982 to 1991 paired each accident year's losses with the next year's premium, andCapeCodraisedKeyError: 1.MethodBase._slice_weight_originsfollows the review on this PR. It takes both triangles to have consecutive origins at the same origin grain and raises a ValueError when the grains differ. When both have more than one origin and the origins differ, it keeps the weight from the later of the two first origins to the earlier of the two last origins. An origin ofXoutside that range gets NaN weight and no ultimate, and a warning names it. Origins found only in the weight drop out with no warning. A weight with one origin still applies to every origin ofX. The helper also raises when the ranges share no origin, or when the origins in the shared range don't line up, as with the gapped slices ofraain #1649.MethodBase._drop_ultimatessets the ultimate to NaN for the origins the weight lacks, after each method computes it.The tests are in the new
chainladder/methods/tests/test_sample_weight.py.test_shifted_sample_weight_is_sliced_to_the_shared_originsfits each of the four methods toraawith the 1982 to 1991 weight. It checks that 1981 has no ultimate and thatfitandpredictgive the same 1982 to 1990 ultimates as a weight carrying the same premiums on raa's own origins.test_bornhuetter_ferguson_uses_the_premium_labelled_with_the_originchecks the 1990 ultimate of 21,241.54 that #1646 worked out by hand from the premium labelled 1990. The library gave 22,307.01 before this change. Other tests cover a weight labelled 1979 to 1992, a weight labelled 1983 to 1988, a quarterly weight, odd years inXagainst even years in the weight, the sparse backend and a weight with one origin. Ten of the eleven new tests fail on main. The one-origin test passes there too, since that case is unchanged.Related GitHub Issue(s)
Closes #1646
AI/LLM Usage
This change was made with Árbol, an AI assistant running on Muse, working under my direction. It found the bug, wrote the change and the tests, ran the checks below and drafted this description.
Additional Context for Reviewers
Xafter its valuation date, whichultimate_,ibnr_andfull_triangle_would all have to handle, so this PR leaves those origins out.BornhuetterFerguson,BenktanderandCapeCodgive such an origin its latest diagonal as the ultimate, andExpectedLossgives NaN.test_benktander_to_chainladderdepends on this, sinceChainladder().fit(tri).ibnr_used as a weight carries NaN for the fully developed origins. A first version of this change gave every NaN expectation a NaN ultimate and failed that test on all five samples, so the change now drops only the origins the weight lacks.ifinvalidate_Xis unchanged, since the review said it can wait for another PR.import warningsfrombase.py, so_slice_weight_originsimportswarningswhere it warns.random_state_, and handlerandom_stateas documented #1605 merges with this branch without conflicts.odimsthroughout the library. The two helpers useodimsin five lines, and whichever PR merges second will need to port them.On Python 3.12 with numpy 2.3.3 and pandas 2.3.3,
pytest chainladder/methodsgives 109 passed.ruff checkandruff format --checkpass on the changed files.Submitter's Checklist
[FIX],[FEAT],[DOCS],[TST],[CHORE], or[BRK]).Reviewer's Checklist