Skip to content

[FIX] Build DevelopmentML weights from the cumulative triangle - #1641

Open
scks1 wants to merge 2 commits into
casact:mainfrom
scks1:fix-devml-weight-incremental
Open

scks1 wants to merge 2 commits into
casact:mainfrom
scks1:fix-devml-weight-incremental

Conversation

@scks1

@scks1 scks1 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

DevelopmentML._prep_w_ml now builds its weights from X.incr_to_cum(), both the base mask and the TriangleWeight exclusions. _prep_X_ml builds one row per cell of the cumulative triangle, so the weights now line up with those rows when an incremental triangle has empty cells inside its observed area. Since #1637 TweedieGLM passes the weights on every fit, so on such a triangle the plain fit failed with ValueError: sample_weight.shape == (203,), expected (220,)!.

test_incremental_triangle_with_empty_cells fits TweedieGLM to prism reported counts at OYDQ, incremental and cumulative, with and without drop_valuation="2017Q2", and checks that the incremental and cumulative coefficients match and that the drop changes them. It fails on main with the ValueError and passes with the change.

Related GitHub Issue(s)

Closes #1640

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 test, ran the checks below and drafted this description.

Additional Context for Reviewers

Fits on prism reported counts, summed over the index:

Grain Empty observed cells TweedieGLM on main BarnettZehnwirth on main Both with this PR
OYDY 0 fits fits fit
OYDS 3 ValueError ValueError fit
OSDS 13 ValueError ValueError fit
OYDQ 17 ValueError ValueError fit
OSDQ 50 ValueError ValueError fit
OYDM 98 ValueError ValueError fit
OQDQ 142 ValueError ValueError fit
OSDM 258 ValueError ValueError fit
OQDM 669 ValueError ValueError fit
OMDM 2,723 ValueError ValueError fit

At OYDQ the incremental fit with this change matches the cumulative fit and the plain incremental fit on c113fd9, before #1637, to 10 decimal places, with and without drop_valuation="2017Q2".

A first version changed only the base mask. It cleared the error but gave zero weight to the 17 empty cells, because TriangleWeight still marked them empty, and every coefficient moved (largest difference 0.467). The new test fails on that version.

The chainladder/development tests pass (172 passed), and ruff check and format are clean 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).

_prep_X_ml builds one row per cell of the cumulative triangle, but
_prep_w_ml built one weight per non-empty cell of the triangle it was
given. On an incremental triangle with empty cells in its observed area
the counts differ, and since casact#1637 TweedieGLM passes the weights on
every fit, so the plain fit raised a sample_weight shape error.

Fixes casact#1640

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): 18.1% of exported symbols fully typed (303 / 1677)

Known Ambiguous Unknown Total
Project (head) 303 96 1278 1677

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 / 1)

Known Ambiguous Unknown Total
Patch 0 0 1 1
Patch symbol details
Symbol Status Change
chainladder.development.tests.test_glm.test_incremental_triangle_with_empty_cells ❌ unknown new

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.03%. Comparing base (8d8f90c) to head (31e64f6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1641      +/-   ##
==========================================
+ Coverage   95.00%   95.03%   +0.03%     
==========================================
  Files         100      100              
  Lines        6460     6460              
  Branches      819      819              
==========================================
+ Hits         6137     6139       +2     
+ Misses        204      203       -1     
+ Partials      119      118       -1     
Flag Coverage Δ
unittests 95.03% <100.00%> (+0.03%) ⬆️

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.

coef[name, drop] = model.model_.estimator_ml["model"].coef_
for drop in (None, "2017Q2"):
np.testing.assert_allclose(coef["incremental", drop], coef["cumulative", drop])
assert not np.allclose(coef["incremental", None], coef["incremental", "2017Q2"])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be comparing coef["incremenetal", ...] and coef["cumulative", ...]?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop just above this line already does that. It compares the incremental and cumulative coefficients for both drop settings. This line checks that dropping 2017Q2 changes the fit, because the loop would also pass if both fits ignored drop_valuation, as in #1619. I pushed a comment above it that says so. Would a different check read better to you?

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

The last assertion checks that drop_valuation changes the fit, so the
incremental-to-cumulative comparison cannot pass with the drop ignored.

Developed with an AI assistant (Árbol, running Muse).
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] TweedieGLM fails on an incremental triangle with empty cells since #1637

2 participants