Skip to content

[FIX] Slice sample_weight to the origins it shares with X in BornhuetterFerguson, Benktander, ExpectedLoss and CapeCod - #1648

Open
scks1 wants to merge 2 commits into
casact:mainfrom
scks1:#1646-sample-weight-origin-labels
Open

scks1 wants to merge 2 commits into
casact:mainfrom
scks1:#1646-sample-weight-origin-labels

Conversation

@scks1

@scks1 scks1 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

BornhuetterFerguson, Benktander, ExpectedLoss and CapeCod now slice sample_weight to the origin range it shares with X. Before this change they read the weight by position along the origin axis. On raa, 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, and CapeCod raised KeyError: 1.

MethodBase._slice_weight_origins follows 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 of X outside 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 of X. 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 of raa in #1649. MethodBase._drop_ultimates sets 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_origins fits each of the four methods to raa with the 1982 to 1991 weight. It checks that 1981 has no ultimate and that fit and predict give 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_origin checks 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 in X against 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

  1. The review on [BUG] BornhuetterFerguson matches sample_weight to origins by position, so a premium labelled 1982–1991 runs on raa without an error #1646 suggested letting origins found only in the weight, such as 1991, carry through to the result where convenient. That needs a row in X after its valuation date, which ultimate_, ibnr_ and full_triangle_ would all have to handle, so this PR leaves those origins out.
  2. A NaN inside a weight that covers the origin keeps its current meaning. On main, BornhuetterFerguson, Benktander and CapeCod give such an origin its latest diagonal as the ultimate, and ExpectedLoss gives NaN. test_benktander_to_chainladder depends on this, since Chainladder().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.
  3. The if in validate_X is unchanged, since the review said it can wait for another PR.
  4. Open PRs that touch these files:

On Python 3.12 with numpy 2.3.3 and pandas 2.3.3, pytest chainladder/methods gives 109 passed. ruff check and ruff format --check pass on the changed files.

Submitter's Checklist

  • I have reviewed and am adhering to the standards outlined in the project Governing Doc.
  • The PR subject title summarizes the changes, with one proper prefix ([FIX], [FEAT], [DOCS], [TST], [CHORE], or [BRK]).
  • I am a human (not a bot), and this PR form is written by a human.

Reviewer's Checklist

  • The implementation addresses the associated issue(s).
  • The implementation is appropriate, maintainable, and follows ARCHITECTURE.md.
  • PR subject title has the proper prefix and the subject is appropriate.
  • Relevant issue(s) are linked.
  • AI/LLM usage is disclosed and appropriate.
  • Documentation and tests are appropriate.
  • CI tests passed, or any failures are acceptable.
  • Leave a comment with the final recommendation (e.g. approve as is, request a secondary review, or flag an area for more review).

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).
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Pyright Type Completeness

View the full pyright --verifytypes output for this commit

Project (full chainladder package, at this PR's head): 17.9% of exported symbols fully typed (303 / 1689)

Known Ambiguous Unknown Total
Project (head) 303 97 1289 1689

Other symbols referenced but not exported by chainladder: 24

Known Ambiguous Unknown Total
Other (head) 11 1 12 24

Symbols without documentation:

  • Functions without docstring: 330
  • Functions without default param: 0
  • Classes without docstring: 10

Patch (exported symbols added or changed by this PR): 0.0% fully typed (0 / 12)

Known Ambiguous Unknown Total
Patch 0 1 11 12
Patch symbol details
Symbol Status Change
chainladder.methods.capecod.CapeCod.process_variance_ ❌ unknown new
chainladder.methods.capecod.CapeCod.ultimate_ ❌ unknown new
chainladder.methods.tests.test_sample_weight.raa ❌ unknown new
chainladder.methods.tests.test_sample_weight.test_bornhuetter_ferguson_uses_the_premium_labelled_with_the_origin ❌ unknown new
chainladder.methods.tests.test_sample_weight.test_longer_sample_weight_is_sliced_to_the_origins_of_x ❌ unknown new
chainladder.methods.tests.test_sample_weight.test_origins_that_do_not_line_up_raise ❌ unknown new
chainladder.methods.tests.test_sample_weight.test_sample_weight_slicing_works_on_the_sparse_backend ❌ unknown new
chainladder.methods.tests.test_sample_weight.test_sample_weight_with_another_origin_grain_raises ❌ unknown new
chainladder.methods.tests.test_sample_weight.test_shifted_sample_weight_is_sliced_to_the_shared_origins ❌ unknown new
chainladder.methods.tests.test_sample_weight.test_shorter_sample_weight_leaves_origins_without_weight_with_no_ultimate ❌ unknown new
chainladder.methods.tests.test_sample_weight.test_single_origin_sample_weight_broadcasts ❌ unknown new
chainladder.methods.tests.test_sample_weight.years ⚠️ ambiguous new

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.87500% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.04%. Comparing base (a59efc9) to head (730866c).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
chainladder/methods/base.py 95.00% 1 Missing and 1 partial ⚠️
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     
Flag Coverage Δ
unittests 95.04% <96.87%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@henrydingliu

Copy link
Copy Markdown
Member

after an initial read, i'm wondering if you could route much of the new methods through existing functionality within dunders.

@scks1

scks1 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Happy to route it through dunders. There's one snag first. The union path in _prep_origin_development fails on the #1646 pair today. On main a59efc9, with S1 as a premium triangle labelled 1982–1991, both raa.latest_diagonal + S1 and raa.latest_diagonal * S1 raise KeyError: '1', the same error CapeCod hits since it already sends the weight through that arithmetic. The same code path also has #1649 (a gap in the labels raises a broadcast error) and #1650 (same-shape triangles with partly different labels match by position).

Which would you prefer?

  1. Fix the dunders union in this PR, covering the KeyError, [BUG] Triangle arithmetic raises a raw broadcast error when origin labels interleave #1649 and [BUG] Triangle arithmetic matches origins and ages by position when only some labels differ #1650, and have the methods call it. That also lets weight-only origins such as 1991 flow through, as you suggested.
  2. Keep the helper here for now and switch to dunders once separate PRs fix those.

(This comment was written with Árbol, an AI assistant, under my direction.)

@henrydingliu

Copy link
Copy Markdown
Member

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

  • X and sample_weight are both well-formed triangles, i.e. origins are consecutive
  • X and sample_weight have the same ograin (should error if they don't)

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).
@scks1 scks1 changed the title [FIX] Match sample_weight to X by origin label in BornhuetterFerguson, Benktander, ExpectedLoss and CapeCod [FIX] Slice sample_weight to the origins it shares with X in BornhuetterFerguson, Benktander, ExpectedLoss and CapeCod Nov 18, 2026
@scks1

scks1 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

That works. The new commit drops the label matching. _slice_weight_origins raises when the origin grains differ and slices sample_weight to the range from the later first origin to the earlier last origin. On raa with the #1646 premium labelled 1982 to 1991, the four methods use 1982 to 1990, and BornhuetterFerguson(apriori=0.6) gives the 1990 ultimate of 21,241.54 that #1646 worked out by hand. An origin of X outside the shared range still gets no ultimate, and the warning names it. A weight that covers all of X and more raises no warning.

One question remains. The helper relies on consecutive origins, and slices of raa like those in #1649 still reach these methods. Right now it raises a clear error when the origins in the shared range don't line up, for example odd years in X and even years in the weight. Would you rather keep that check, or drop it and assume well-formed triangles as you suggested?

(This comment was written with Árbol, an AI assistant, under my direction.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] BornhuetterFerguson matches sample_weight to origins by position, so a premium labelled 1982–1991 runs on raa without an error

2 participants