Skip to content

Util weight#940

Merged
henrydingliu merged 5 commits into
casact:#932_triangle_weightfrom
henrydingliu:util_weight
Jun 8, 2026
Merged

Util weight#940
henrydingliu merged 5 commits into
casact:#932_triangle_weightfrom
henrydingliu:util_weight

Conversation

@henrydingliu

@henrydingliu henrydingliu commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

Summary of Changes

Related GitHub Issue(s)

Additional Context for Reviewers

  • I passed tests locally for both code (uv run pytest) and documentation changes (uv run jb build docs --builder=custom --custom-builder=doctest)

Note

Medium Risk
New public API and ~500 lines of weight logic that must stay consistent with Development; wrong ordering or preserve behavior would change LDFs once integrated, though production Development paths are unchanged in this diff.

Overview
Introduces TriangleWeight, a new sklearn-style utility that builds a weight triangle from the same exclusion knobs as Development (n_periods, drop, drop_valuation, drop_above/drop_below, drop_high/drop_low, preserve). It is exported from chainladder.utils and the top-level chainladder package, with EXPECTED_PUBLIC_API updated accordingly.

The implementation lives in chainladder/utils/triangle_weight.py and documents a different drop order than Development: threshold drops (drop_above/drop_below) run before rank-based drop_high/drop_low (the docstring notes steps 4 and 5 are reversed relative to Development). fit/transform set w_ on triangles and can compose with existing w_ weights.

Tests in test_development.py are expanded with _FutureDevelopment, a TriangleWeight subclass that recomputes LDFs via weighted regression, and assertions that its LDFs match Development across drop scenarios—exercising the new utility without wiring it into Development yet.

Reviewed by Cursor Bugbot for commit 60cabb2. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread chainladder/development/tests/test_development.py Outdated
@codecov

codecov Bot commented Jun 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.53%. Comparing base (1dd5f1d) to head (60cabb2).

Files with missing lines Patch % Lines
chainladder/utils/triangle_weight.py 89.20% 9 Missing and 6 partials ⚠️
chainladder/development/tests/test_development.py 95.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@                   Coverage Diff                    @@
##           #932_triangle_weight     #940      +/-   ##
========================================================
+ Coverage                 88.30%   88.53%   +0.22%     
========================================================
  Files                        88       89       +1     
  Lines                      5029     5189     +160     
  Branches                    642      658      +16     
========================================================
+ Hits                       4441     4594     +153     
+ Misses                      443      442       -1     
- Partials                    145      153       +8     
Flag Coverage Δ
unittests 88.53% <90.00%> (+0.22%) ⬆️

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.

Comment thread chainladder/utils/triangle_weight.py

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

There are 3 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ca2f50d. Configure here.

if hasattr(X, "w_"):
self.w_ = self._set_weight_func(
X=X * X.w_,
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prior weights use wrong attribute

Medium Severity

When prior weights exist, TriangleWeight.fit multiplies the input triangle by X.w_ and passes that triangle into _set_weight_func. Development.fit instead checks w_v2_ and combines prior weights on obj.age_to_age. After Development, w_ and w_v2_ can differ, so chained weighting no longer matches development’s v2 path.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit ca2f50d. Configure here.

else:
self.w_ = self._set_weight_func(
X=X,
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ratio drops need age-to-age

Medium Severity

TriangleWeight.fit feeds the caller’s triangle straight into _set_weight_func, so _drop_x_func and _drop_n_func rank and threshold X.values as given. Those steps expect age-to-age link ratios; Development always builds v2 weights from obj.age_to_age. The same cumulative triangle passed to Development.fit can get incorrect exclusions here.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit ca2f50d. Configure here.

@henrydingliu henrydingliu marked this pull request as ready for review June 8, 2026 01:01
@henrydingliu henrydingliu merged commit 9a9a796 into casact:#932_triangle_weight Jun 8, 2026
10 checks passed
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.

1 participant