[FIX] CapeCod.predict discards the fitted apriori when the prediction data has exactly one extra index level - #1275
Conversation
Pyright Type CompletenessView the full Project (full
Other symbols referenced but not exported by
Symbols without documentation:
Patch (exported symbols added or changed by this PR): 0.0% fully typed (0 / 1)
Patch symbol details
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1275 +/- ##
==========================================
- Coverage 91.69% 91.69% -0.01%
==========================================
Files 96 96
Lines 5457 5456 -1
Branches 702 702
==========================================
- Hits 5004 5003 -1
Misses 328 328
Partials 125 125
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
looks good! would you mind picking up the couple ruff linter fixes? |
… level is added predict() regroups the prediction data up to the grain the model was fit at, and decides whether to do so by counting the index levels sample_weight carries that apriori_ does not. The count required more than one, so the ordinary case of a single extra level fell through to the ungrouped branch and recomputed the apriori from the prediction data. The existing casact#400 regression test uses prism, which has five extra levels, so it exercises the grouped branch and never saw this. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
`ruff check --config lint.per-file-ignores={}` now passes on both. The numpy
import in capecod.py was unused; the rest is whitespace in existing tests.
`ruff format` is left alone: it would rewrite 105 of the 107 lines in
test_capecod.py, and six of the eight files in chainladder/methods are
unformatted on main, so that looks like a separate decision.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
45dd61e to
b46e378
Compare
The table's own comment says to remove entries as files are cleaned up. Both are dead now: ruff check passes on capecod.py and test_capecod.py with the project config as well as with per-file-ignores cleared. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Done. I left |
|
thanks again! |
Summary of Changes
CapeCod.predict()regroups the prediction data up to the grain the model was fit at, and decides whether to do so by counting the index levelssample_weightcarries thatapriori_does not. The count required more than one, so the ordinary case of a single extra level fell through to the ungrouped branch and recomputed the apriori from the prediction data instead of using the fitted one.The fitted comauto apriori is 0.5689995797. Before this change
predict()returned 1.2516635774, and all 775 rows differed from the value fitted for their LOB. After it, every row carries its fitted apriori andexpectation_is exactly premium times that apriori across all 775 rows.test_capecod_predict2already covers this code path, but it usesprism, whose triangle carries five index levels more than the fitted model, so it stays on the grouped branch. The added test usesclrd, which carries exactly one.Related GitHub Issue(s)
Fixes #1265. The API question that thread raised is split out into #1274.
Additional Context for Reviewers
pytest chainladderpasses: 1130 passed, 7 skipped.ruff checkandruff format --checkunder the CI flags report the same eight findings on these two files before and after the change, so the added lines introduce none of their own.predict()docstring example (ukmotor) is unaffected: it fits and predicts at the same grain, so the level difference is empty and neither branch changes. Its documented output still reproduces exactly.Declarations
I am adhering to the standards in the Governing Doc. I am a human contributor, not a bot, and I opened this because @henrydingliu asked for the
> 0fix in #1265.AI disclosure, per the AI Usage Policy: I used Claude Code on this. It reproduced the bug against
clrd, ran the before and after comparison, and drafted the regression test. The one-character fix itself is the one @henrydingliu and I settled on in #1265 before any code was written. I reviewed the diff and the test myself, and the suite was run locally on my machine.Checklist
I passed tests locally for both code (
uv run pytest) and documentation changes (uv run --directory docs jb build . --builder=custom --custom-builder=doctest)pytestlocally: 1130 passed, 7 skipped. There are no documentation changes in this PR, so I did not run the docs doctest build. The only docstring the change could reach is theCapeCod.predictukmotor example, and I ran that directly: it still reproduces its documented output exactly. CI's Doctest job passes on this branch.