Skip to content

[FIX] Warn on failed ClarkLDF fits and name the bad age in TailBondy (#1707) - #1709

Open
Huyvux12 wants to merge 1 commit into
casact:mainfrom
Huyvux12:fix/1707-clark-bondy-silent-fits
Open

Huyvux12 wants to merge 1 commit into
casact:mainfrom
Huyvux12:fix/1707-clark-bondy-silent-fits

Conversation

@Huyvux12

Copy link
Copy Markdown
Contributor

Closes #1707

What was wrong

  • ClarkLDF.fit kept minimize(...).x and never read success, so failed
    optimizer fits were kept silently with finite ldf_ and no warning.
  • TailBondy.fit took log of ldf_ at the fit ages and handed it to
    least_squares, so a NaN factor surfaced as scipy's generic
    ValueError: Initial guess is outside of provided bounds, which does not
    point at the data.

Fix

  • chainladder/development/clark.py: after each minimize call, warn
    (UserWarning) naming the triangle (index label) and column, plus the
    optimizer message and status, when success is False. The failed fit is
    still kept, as before; only the silence is fixed.
  • chainladder/tails/bondy.py: before calling least_squares, check the
    log-development factors for finiteness and raise a ValueError naming
    every fit age with no finite development factor and the column.

Repro (base SHA 0f4b230c, scipy 1.18.1, fresh venv)

Before the fix, the issue's snippets give:

After the fix:

  • ClarkLDF().fit(clrd.iloc[273]) warns: ClarkLDF optimization did not converge for triangle ('First Amer Ins Co', 'ppauto') (column 'CumPaidLoss'): ABNORMAL: (status 2). The fitted omega_ and theta_ may be unreliable. Same for Development lags could not be determined #683. A healthy genins fit stays silent.
  • TailBondy().fit(clrd.iloc[2]) raises: TailBondy cannot fit: no finite development factor at age(s) 108 for column 'CumPaidLoss'. ...
    With earliest_age=12 it lists age(s) 84, 96, 108.
    A healthy raa fit still works (tail 1.0092, as in the docstring).

Regression tests

  • chainladder/development/tests/test_clark.py:
    test_clark_ldf_warns_when_optimizer_fails (deterministic fake
    minimize returning success=False, asserts the warning names the
    triangle and carries the optimizer message) and
    test_clark_ldf_no_warning_when_optimizer_succeeds (control case).
  • chainladder/tails/tests/test_bondy.py:
    test_bondy_rejects_non_finite_development_factor and
    test_bondy_names_each_non_finite_fit_age.

Regression scope

  • pytest chainladder/development/tests/test_clark.py chainladder/tails/tests/test_bondy.py: 14 passed.
  • pytest chainladder/tails/tests/ chainladder/development/tests/test_clark.py chainladder/development/tests/test_development.py chainladder/methods/tests/test_predict.py: 208 passed, 6 failed.
    The 6 failures are pre-existing on the clean tree (same 6 fail with
    this PR stashed): test_development.py::test_drop_* trips a numpy 2.5
    DeprecationWarning in chainladder/core/base.py, untouched here.
  • ruff check and ruff format --check pass on all changed files.

Risks checked and excluded

  • The Bondy pre-check only changes the error for inputs that already
    failed: any non-finite factor made least_squares raise before (NaN
    residuals, or a NaN starting value), so no previously succeeding fit
    now fails. No existing test expects the old scipy message (none
    references it).
  • The Clark warning fires once per failed (triangle, column) fit and
    leaves fitted values untouched; the grouped-clrd docstring fit emits
    no warning and matches its published omega_ values.
  • Deliberately out of scope: open PR [FEAT] Pass minimizer_kwargs from ClarkLDF to scipy.optimize.minimize #1541 adds minimizer_kwargs and
    a minimizer_results_ attribute. This PR does not add that attribute,
    so it stays out of [FEAT] Pass minimizer_kwargs from ClarkLDF to scipy.optimize.minimize #1541's way; the warning reads the OptimizeResult
    directly instead.

@github-actions

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.5% of exported symbols fully typed (306 / 1748)

Known Ambiguous Unknown Total
Project (head) 306 102 1340 1748

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: 338
  • Functions without default param: 0
  • Classes without docstring: 10

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

Known Ambiguous Unknown Total
Patch 0 0 4 4
Patch symbol details
Symbol Status Change
chainladder.development.tests.test_clark.test_clark_ldf_no_warning_when_optimizer_succeeds ❌ unknown new
chainladder.development.tests.test_clark.test_clark_ldf_warns_when_optimizer_fails ❌ unknown new
chainladder.tails.tests.test_bondy.test_bondy_names_each_non_finite_fit_age ❌ unknown new
chainladder.tails.tests.test_bondy.test_bondy_rejects_non_finite_development_factor ❌ unknown new

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.22%. Comparing base (0f4b230) to head (c7fc0ab).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1709   +/-   ##
=======================================
  Coverage   95.21%   95.22%           
=======================================
  Files         100      100           
  Lines        6604     6616   +12     
  Branches      831      833    +2     
=======================================
+ Hits         6288     6300   +12     
  Misses        200      200           
  Partials      116      116           
Flag Coverage Δ
unittests 95.22% <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.

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] ClarkLDF keeps failed optimizer fits without a warning, and TailBondy passes NaN patterns to the optimizer

1 participant