Skip to content

[BUG] Fitting a Triangle with a virtual column gives NaN for that column, with no warning #1607

Open

Description

@scks1

When a Triangle has a virtual column, fitting the whole Triangle gives NaN for that column with no warning, while fitting the column on its own works. On raa:

import chainladder as cl

raa = cl.load_sample("raa")
tri = raa.copy()
tri["half"] = raa["values"] * 0.5
tri["rest"] = lambda x: x["values"] - x["half"]

print(cl.Chainladder().fit(tri).ultimate_["rest"].to_frame().T)
print(cl.Chainladder().fit(tri["rest"]).ultimate_.sum())
      1981-01-01  1982-01-01  1983-01-01  ...  1988-01-01  1989-01-01  1990-01-01
2261         NaN         NaN         NaN  ...         NaN         NaN         NaN

[1 rows x 10 columns]
106561.11413060508

__setitem__ stores an all-NaN placeholder slice for a callable (slice.py L894–L895), and tri["rest"] evaluates the function. A fit of the whole Triangle uses the placeholder: Development().fit(tri).ldf_["rest"] and tri.link_ratio["rest"] are all NaN, and MackChainladder gives the same NaN ultimate. ultimate_ and ldf_ list no virtual columns (methods/base.py L43 clears them on the ultimate), so only X_.virtual_columns shows that rest was virtual. I checked the default numpy backend only. On the sparse backend, __setitem__ creates no placeholder.

Expected: either the column is evaluated before fitting,

import numpy as np

assert not np.isnan(cl.Chainladder().fit(tri).ultimate_["rest"].values).any()

or fit raises or warns that virtual columns aren't supported. The user guide says virtual columns defer the calculation "to when needed", so I'd lean toward evaluating them in fit. I can open a PR with whichever you prefer.

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

Activity

  1. henrydingliu commented on Nov 15, 2026

    @henrydingliu
    Member

    this feels pretty big. tri.age_to_age["rest"] looks funny too

  2. updated Priority High, Effort High and Scope Codebase on Nov 15, 2026
  3. added this to the v0.11.0 milestone on Nov 15, 2026
  4. scks1 commented on Nov 15, 2026

    @scks1
    ContributorAuthor

    Agreed. tri.age_to_age["rest"] is the same placeholder: on current main (46727b1) it is all NaN, while tri["rest"].age_to_age gives the correct ratios. It also means a fix has to evaluate the column on the cumulative triangle before link ratios are taken, since x["values"] - x["half"] on the ratio triangle gives 0 (both columns have the same ratios).

    Given the size, would a small first PR help, where fit raises (or warns) when the triangle has virtual columns, with evaluation before link_ratio and fit in a later PR? The sparse backend creates no placeholder, so it needs its own check either way. I'm happy to take either part.

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

  5. henrydingliu commented on Nov 15, 2026

    @henrydingliu
    Member

    good with a small first PR. fit doesn't feel like sufficiently upstream if age_to_age is failing. is values a better warning spot?

    we should also add a disclaimer into the docs that virtual columns can't be used further an any property or estimation.

  6. scks1 commented on Nov 16, 2026

    @scks1
    ContributorAuthor

    Agreed on a small first PR. values is a plain attribute that the library itself reads and assigns in many places (common.py, pandas.py, slice.py, triangle.py), so a warning there would also fire on internal reads. I'd warn where a calculation starts instead: in link_ratio (which age_to_age aliases) and in estimator fit, whenever the triangle carries virtual columns. I'll add the disclaimer next to the "should only reference other columns" note in the Virtual Columns section of docs/user_guide/triangle.ipynb.

    With the disclaimer in place, do you still want a later PR that evaluates virtual columns before fitting, or should they stay for display and export only?

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

  7. henrydingliu commented on Nov 16, 2026

    @henrydingliu
    Member

    ah, right, we never shimmed values. in theory, if we refactor values into a shim, with an eval on all virtual columns, does that get us most of the way there?

    we can start with a docs-only PR that warns virtual columns only have limited support.

  8. scks1 commented on Nov 16, 2026

    @scks1
    ContributorAuthor

    Mostly, I think: reads would see evaluated numbers, so age_to_age, link_ratio and fit should work on virtual columns. A shim has to handle three things:

    • In-place writes. Some code writes through .values (munich.py, incremental.py, methods/base.py, clark.py, triangle_weight.py, and values.shape = ... in slice.py and triangle.py). If the getter returns a freshly evaluated array, those writes are lost.
    • Recursion. A virtual column's lambda reads other columns through __getitem__, which reads values, so the getter needs a re-entrancy guard.
    • Cost. The library reads .values constantly, so running every lambda on every read needs a cache that is cleared when a column changes.

    to_frame already evaluates virtual columns its own way (row by row, after fillna(0)), so the shim should match it or replace that path.

    I'll start with the docs-only PR. I can prototype the shim afterwards and run the full test suite before proposing it.

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

  9. henrydingliu commented on Nov 16, 2026

    @henrydingliu
    Member

    some of the prior conversations are coming back to me now. at one point we were discussing a migration to polars. but that haven't been a high priority for a while. so there's no rush to make the package fully virtual column compatible.

  10. scks1 commented on Nov 16, 2026

    @scks1
    ContributorAuthor

    Makes sense. I'll hold off on the values shim prototype and keep #1632 to the docs warning.

    I can leave #1607 open to track full virtual column support, or close it once #1632 merges, whichever you prefer.

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

  11. henrydingliu commented on Nov 17, 2026

    @henrydingliu
    Member

    let's keep this issue around. i expect to get more inquiries regarding virtual columns as pd.col gets more traction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    Low

    Scope

    Codebase

    Effort

    High

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions