Repository navigation
[FIX] Build DevelopmentML projected valuations from an integer index - #1634
Conversation
_get_triangle_ml stepped projected valuations by adding dgrain / 12 in floating point and then looked them up in valuation_encoder_ by exact match. For monthly grain the sums drift from the encoder's values, so most projected cells became NaN and were dropped, and DevelopmentML and BarnettZehnwirth ultimates no longer matched chain ladder on exact data. Projected valuations now come from the integer valuation index, divided once in the same way as the encoder. The new test fits a Poisson GLM to a monthly triangle with an exact multiplicative pattern and checks the square is complete and the ultimates match Chainladder. Developed with an AI assistant (Árbol, running Muse).
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): 50.0% fully typed (2 / 4)
Patch symbol details
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1634 +/- ##
==========================================
+ Coverage 95.00% 95.03% +0.03%
==========================================
Files 100 100
Lines 6461 6471 +10
Branches 820 819 -1
==========================================
+ Hits 6138 6150 +12
+ Misses 204 203 -1
+ Partials 119 118 -1
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:
|
|
_get_triangle_ml marked the latest diagonal with a formula in origin and development periods that holds only when the two grains match. At mixed grains such as OYDM it missed cells at the latest valuation, so those origins were not projected and future cells of triangle_ml_ stayed empty. The latest diagonal is now the set of cells at the latest encoded valuation. test_grain also checks that triangle_ml_ has no NaN, and a new test fits TweedieGLM to the monthly sample at ten grains and checks every future cell is filled. Developed with an AI assistant (Árbol, running Muse).
Merged with main at ec4fcd2, the development tests pass, 193 of them. (This comment was written with Árbol, an AI assistant, under my direction.) |
|
|
||
| def test_monthly_poisson_glm_matches_chainladder(): | ||
| # A cross-classified Poisson GLM reproduces chain ladder (Renshaw and | ||
| # Verrall, 1998), so on a monthly triangle with an exact multiplicative |
There was a problem hiding this comment.
do we need to add this paper to the biblio?
There was a problem hiding this comment.
Yes, added as renshaw1998 in docs/library/references.bib (British Actuarial Journal 4(4), 903–923, doi 10.1017/S1357321700000222), and the test comment now names the key. The references page lists only cited entries, so it won't show there until a page cites it with {cite}, the same as merz2014 today.
(This comment was written with Árbol, an AI assistant, under my direction.)
| "grain", | ||
| ["OMDM", "OQDM", "OSDM", "OYDM", "OQDQ", "OSDQ", "OYDQ", "OSDS", "OYDS", "OYDY"], | ||
| ) | ||
| def test_projection_fills_future_cells(monthly, grain): |
There was a problem hiding this comment.
if you fixturized the data from test_monthly_poisson_glm_matches_chainladder, we could have a stronger test that pins the actual ultimates through all the grains?
There was a problem hiding this comment.
Done. The exact-pattern data is now the exact_monthly fixture, extended from 8 to 24 monthly origins so the yearly grains keep two origins. With 8, OYDY collapses to a single cell. test_poisson_glm_matches_chainladder runs at all ten grains and checks the GLM's chain ladder ultimates against cl.Chainladder on the same triangle. On main it fails at seven of the ten, every grain except OQDQ, OSDS and OYDY, with NaN left in triangle_ml_ and a lower total. I kept test_projection_fills_future_cells on the monthly sample since it covers the TweedieGLM wrapper on real data. The new test fits with solver="newton-cholesky", which converges in about ten iterations. With lbfgs at max_iter=1000, three grains stopped at the cap with a ConvergenceWarning, and raising the cap until they converge took the file from about 3 to 6 minutes. The test filters the matmul RuntimeWarning that newton-cholesky emits, and the ultimates still match at rtol=1e-4.
(This comment was written with Árbol, an AI assistant, under my direction.)
The exact-pattern monthly triangle is now the exact_monthly fixture, with 24 origins so yearly grains keep two. test_poisson_glm_matches_chainladder runs at the ten grains and checks the Poisson GLM's chain ladder ultimates against Chainladder on the same triangle. On main it fails at seven of the ten. references.bib gains renshaw1998, which the test comment names. Developed with an AI assistant (Árbol, running Muse).
The 3.13 unit-test job on f7b58cb failed in Set up job when the setup-uv action download timed out. This empty commit reruns CI. Developed with an AI assistant (Árbol, running Muse).
henrydingliu
left a comment
There was a problem hiding this comment.
all the code changes look good. can you turn the comments
|
|
||
| @pytest.fixture | ||
| def exact_monthly(): | ||
| # Two years of monthly origins whose cumulative values follow one |
There was a problem hiding this comment.
can you turn the comment into docstring?
| @pytest.mark.filterwarnings("ignore:invalid value encountered in matmul:RuntimeWarning") | ||
| @pytest.mark.parametrize("grain", GRAINS) | ||
| def test_poisson_glm_matches_chainladder(exact_monthly, grain): | ||
| # A cross-classified Poisson GLM reproduces chain ladder (Renshaw and |
There was a problem hiding this comment.
can you turn the comment into docstring?
| @pytest.mark.parametrize("grain", GRAINS) | ||
| def test_projection_fills_future_cells(monthly, grain): | ||
| # DevelopmentML projects each origin forward from the latest diagonal, so | ||
| # every future cell of triangle_ml_ must be filled at every grain, |
There was a problem hiding this comment.
can you turn the comment into docstring?
Developed with an AI assistant (Árbol, running Muse).
|
Thanks. The three comments in test_learning.py are docstrings now, with the same wording, and nothing else changed. (This comment was written with Árbol, an AI assistant, under my direction.) |
Summary of Changes
DevelopmentML._get_triangle_mlnow builds projected valuations from the integer valuation index instead of addingdgrain / 12at each step. The repeated float addition drifted away fromvaluation_encoder_'s values on monthly triangles, so the exact lookup returned NaN and.dropna()removed most of the projected cells.BarnettZehnwirthuses the same function and is fixed too.test_monthly_poisson_glm_matches_chainladderfits a Poisson GLM with origin and development factors to an 8×8 monthly triangle with an exact multiplicative pattern, and checks thattriangle_ml_has no NaN and that the ultimates matchChainladder(Renshaw and Verrall, 1998). It fails on main at the NaN check and passes with the change.The second commit answers review.
_get_triangle_mlfound the latest diagonal with a formula in origin and development periods that holds only when the two grains match, so it now compares encoded valuations.test_projection_fills_future_cellsfitsTweedieGLMtomonthlyat all ten grains and checks that every future cell oftriangle_ml_is filled. It fails on the first commit at OQDM, OSDM, OYDM, OSDQ, OYDQ and OYDS.test_grainnow also asserts thattriangle_ml_has no NaN.Related GitHub Issue(s)
Closes #1633
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
DevelopmentMLBarnettZehnwirthThe
DevelopmentMLvalues left after the fix are the Tweedie solver's tolerance.genins'sldf_is unchanged.learning.pyorbarnzehn.py.Merged with main at ec4fcd2, the
chainladder/developmenttests pass (193 passed), and ruff check and format are clean on the changed files.Submitter's Checklist
[FIX],[FEAT],[DOCS],[TST],[CHORE], or[BRK]).Reviewer's Checklist