Skip to content

[FIX] Keep the simulated process-variance path in full_triangle_ (#1667) - #1710

Open
scks1 wants to merge 1 commit into
casact:mainfrom
scks1:fix-1667-full-triangle-process-variance
Open

scks1 wants to merge 1 commit into
casact:mainfrom
scks1:fix-1667-full-triangle-process-variance

Conversation

@scks1

@scks1 scks1 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Closes #1667.

After a fit on BootstrapODPSample triangles, full_triangle_ now returns the path the fit simulated. _include_process_variance keeps the noisy triangle it draws, and MethodBase.full_triangle_ returns a copy of it. Every future cell then carries its own process variance, as in Shapland and Leong. Before this change only the last column did.

ultimate_, ibnr_ and the observed cells stay the same. A fit with no process variance builds full_triangle_ from the expected pattern as before. A refit on a plain triangle drops the stored path.

The triangle that predict returns keeps today's full_triangle_. Storing the path on a Triangle would carry it through copies and arithmetic, which is the problem #1706 tracks. So this PR stays with the fitted model.

Three new tests in test_predict.py cover the simulated path, the plain fit and the refit. Two of them fail on main. The full suite gives 1829 passed and 9 skipped on this branch, against 1823 passed and 9 skipped on main at 0f4b230. ruff format and ruff check pass on both changed files.

Snippet, run on this branch
import numpy as np
import chainladder as cl

tri = cl.load_sample("genins")
sims = cl.BootstrapODPSample(n_sims=1000, random_state=42).fit_transform(tri)
model = cl.Chainladder().fit(sims)

no_noise = sims.copy()
del no_noise._get_process_variance
expected = cl.Chainladder().fit(no_noise)
path = expected.full_triangle_ + model.process_variance_

full = model.full_triangle_.values
n = sims.shape[-1]
observed = ~np.isnan(sims.values)
future = ~observed & ~np.isnan(full[..., :n])
ult = expected.ultimate_.values + np.nan_to_num(model.process_variance_.values[..., -1:])
print("future cells:", int(future.sum()))
print("full_triangle_ equals the simulated path:", np.allclose(full, path.values, equal_nan=True))
print("future cells that differ from the expected completion:", int((~np.isclose(full[..., :n], expected.full_triangle_.values[..., :n]))[future].sum()))
print("observed cells unchanged:", np.allclose(full[..., :n][observed], sims.values[observed]))
print("last column equals ultimate_:", np.allclose(full[..., -1:], model.ultimate_.values, equal_nan=True))
print("ultimate_ equals the expected ultimate plus its process variance:", np.allclose(model.ultimate_.values, ult, equal_nan=True))

plain = cl.Chainladder().fit(tri)
print("process_variance_ without bootstrap:", plain.process_variance_)
print("total genins ultimate without bootstrap:", f"{plain.ultimate_.sum().sum():,.0f}")

Output:

future cells: 45000
full_triangle_ equals the simulated path: True
future cells that differ from the expected completion: 44995
observed cells unchanged: True
last column equals ultimate_: True
ultimate_ equals the expected ultimate plus its process variance: True
process_variance_ without bootstrap: None
total genins ultimate without bootstrap: 53,038,946

This change was made with Árbol, an AI assistant running on Muse, working under my direction. It wrote the fix and the tests, ran the checks above and drafted this description.

…act#1667)

_include_process_variance draws gamma noise for every future cell but
kept only the last column as ultimate_. full_triangle_ then rebuilt the
interior cells from the expected pattern, so the cell-level noise from
Shapland and Leong was lost.

The fitted model now keeps the noisy triangle it draws and full_triangle_
returns a copy of it. ultimate_, ibnr_ and observed cells do not change.
A fit without process variance and a refit on a plain triangle fall back
to the expected completion.

Closes casact#1667.

Developed with an AI assistant (Árbol, running Muse).
@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.methods.base.MethodBase.full_triangle_ ❌ unknown new
chainladder.methods.tests.test_predict.test_full_triangle_keeps_the_simulated_path ❌ unknown new
chainladder.methods.tests.test_predict.test_full_triangle_without_bootstrap_is_unchanged ❌ unknown new
chainladder.methods.tests.test_predict.test_refit_drops_the_simulated_path ❌ 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 (5545153).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1710   +/-   ##
=======================================
  Coverage   95.21%   95.22%           
=======================================
  Files         100      100           
  Lines        6604     6611    +7     
  Branches      831      832    +1     
=======================================
+ Hits         6288     6295    +7     
  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] After a bootstrap fit, full_triangle_ rebuilds future cells from the ultimate and drops the simulated process-variance path

1 participant