Skip to content

Fix TweedieGLM ignoring drop/drop_valuation without sample_weight - #1637

Merged
henrydingliu merged 2 commits into
casact:mainfrom
Huyvux12:fix/issue-1619-tweedieglm-drop-weights
Nov 17, 2026
Merged

henrydingliu merged 2 commits into
casact:mainfrom
Huyvux12:fix/issue-1619-tweedieglm-drop-weights

Conversation

@Huyvux12

Copy link
Copy Markdown
Contributor

Fixes #1619

TweedieGLM accepted drop / drop_valuation but silently ignored them unless sample_weight was also passed.

Root cause

DevelopmentML._prep_w_ml already builds the exclusion weights from drop / drop_valuation (via TriangleWeight), but DevelopmentML.fit only forwards those weights to the estimator when weighted_step is set (chainladder/development/learning.py:341-342). TweedieGLM.fit set weighted_step="model" only when sample_weight was given, and None otherwise (chainladder/development/glm.py:217-220), so the computed weights were discarded.

Fix

Always pass weighted_step="model" in TweedieGLM.fit, matching BarnettZehnwirth (chainladder/development/barnzehn.py:179). With no exclusions the weights are all ones, so the unweighted fit is unchanged.

Repro (base main @ c113fd9a)

import chainladder as cl
genins = cl.load_sample("genins")
base = cl.TweedieGLM().fit(genins).ldf_.values[0, 0, 0, :4].round(4)
dv = cl.TweedieGLM(drop_valuation="2005").fit(genins).ldf_.values[0, 0, 0, :4].round(4)
dr = cl.TweedieGLM(drop=("2001", 12)).fit(genins).ldf_.values[0, 0, 0, :4].round(4)

Before fix — all three identical:

base            : [3.491  1.7474 1.4574 1.1739]
drop_valuation  : [3.491  1.7474 1.4574 1.1739]
drop            : [3.491  1.7474 1.4574 1.1739]

After fix:

base            : [3.491  1.7474 1.4574 1.1739]
drop_valuation  : [3.5057 1.7861 1.4546 1.1711]
drop            : [3.571  1.755  1.4606 1.1748]

Control cases (unchanged behavior preserved)

  • Unweighted base fit is bit-identical before/after (coef max abs diff 0.0, LDF max abs diff 0.0, n_iter_ 63/63) — passing all-ones weights is a no-op for TweedieRegressor.
  • drop_valuation="2005" without sample_weight now equals the same call with an all-ones sample_weight ([3.5057, 1.7861, 1.4546, 1.1711]), i.e. the value the issue reported as the working case.
  • drop composes with a non-trivial sample_weight (all 2s): base unchanged, drop still changes the fit.
  • List forms work: drop_valuation=["2005", "2006"] gives [3.6441, 1.8049, 1.4452, 1.1695]; fit_transform with a drop works.

Tests

New regression tests in chainladder/development/tests/test_glm.py (test_drop_valuation_without_sample_weight, test_drop_without_sample_weight):

pytest chainladder/development/tests/test_glm.py -q
8 passed

Regression scope: test_learning.py + test_barnzehn.py + test_development.py — 97 passed, 6 failed. The 6 failures are pre-existing on the clean tree (verified via git stash baseline): all in test_development.py drop-warning tests for cl.Development, which this diff does not touch.

ruff check and ruff format --check pass on both changed files.

Diff scope

  • chainladder/development/glm.py: TweedieGLM.fit always passes weighted_step="model" (+7/-4 with comment).
  • chainladder/development/tests/test_glm.py: two new regression tests.

Deliberately unchanged: DevelopmentML used directly with a pipeline and no weighted_step still silently ignores drops — the issue body explicitly scopes that out ("Not checked"), so it is left for a follow-up, not this PR. #1584 (separate TweedieGLM bug) is untouched.

DevelopmentML builds the exclusion weights from drop/drop_valuation
in _prep_w_ml, but only forwards them to the estimator when
weighted_step is set. TweedieGLM only set weighted_step='model' when
sample_weight was given, so the exclusions were silently ignored
otherwise. Always pass weighted_step='model' (as BarnettZehnwirth
does); with no exclusions the weights are all ones, so the
unweighted fit is unchanged.

Fixes casact#1619
@github-actions

github-actions Bot commented Nov 16, 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 / 1676)

Known Ambiguous Unknown Total
Project (head) 303 96 1277 1676

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

Known Ambiguous Unknown Total
Patch 0 0 2 2
Patch symbol details
Symbol Status Change
chainladder.development.tests.test_glm.test_drop_valuation_without_sample_weight ❌ unknown new
chainladder.development.tests.test_glm.test_drop_without_sample_weight ❌ unknown new

@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.00%. Comparing base (c113fd9) to head (49ccfaa).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1637      +/-   ##
==========================================
- Coverage   95.00%   95.00%   -0.01%     
==========================================
  Files         100      100              
  Lines        6461     6460       -1     
  Branches      820      819       -1     
==========================================
- Hits         6138     6137       -1     
  Misses        204      204              
  Partials      119      119              
Flag Coverage Δ
unittests 95.00% <100.00%> (-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

@scks1 do you might giving this a review?

@scks1 scks1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me. I checked it locally on genins against main at c113fd9.

  • The plain fit is unchanged. Its coefficients and LDFs match main exactly, with a max abs difference of 0.0.
  • On main, drop_valuation="2005", drop=("2001", 12), the list forms of both, and drop with drop_valuation together all leave the LDFs at the plain fit. On this branch each one changes the fit and matches the same call with a triangle of ones as sample_weight exactly.
  • With a triangle of twos as sample_weight, the drops still apply and give the same LDFs as ones.
  • All 8 tests in test_glm.py pass locally.

Thanks for picking this up!

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

@henrydingliu henrydingliu left a comment

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.

@scks1 thanks for the substantive review.

@Huyvux12 thanks for putting this in. i just got a couple of nitpicks.


def test_drop_valuation_without_sample_weight(genins):
# https://github.com/casact/chainladder-python/issues/1619
# drop_valuation must take effect even when sample_weight is None, and

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.

please turn into docstring



def test_drop_without_sample_weight(genins):
# https://github.com/casact/chainladder-python/issues/1619

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.

please add docstring

Per henrydingliu's review on casact#1637, turn the leading comment of
test_drop_valuation_without_sample_weight into a docstring and add a
docstring to test_drop_without_sample_weight.
@henrydingliu
henrydingliu merged commit 8d8f90c into casact:main Nov 17, 2026
11 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.

[BUG] TweedieGLM ignores drop and drop_valuation unless sample_weight is passed

3 participants