You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Triangle arithmetic matches origins and ages by position when only some labels differ #1650
Yes, this bug occurs on the latest version. I ran the example on main at a59efc9 with chainladder 0.10.1, Python 3.12.15, numpy 2.3.3 and pandas 2.3.3. v0.10.1 gives the same output.
Describe the bug in words
_prep_origin_development (dunders.py L162–L172) unions the origin and development labels of two triangles when their shapes can't broadcast, or when every label differs. Two triangles of the same shape whose labels differ in only some places pass neither test. Their values are then combined position by position under the left triangle's labels, with no warning.
In the example, origins 1981–1984 minus origins 1981, 1982, 1983 and 1986 gives 4142 for 1984 at age 12. That is 1984's 5655 minus 1986's 1513. When all four labels differ, the same subtraction unions the labels and keeps each year on its own row. Ages behave the same way. Ages 12–48 minus ages 12, 24, 36 and 72 puts 1981's age 48 minus its age 72, −4376, under age 48.
How can the bug be reproduced?
importchainladderasclraa=cl.load_sample("raa")
x=raa[raa.origin<="1984"]
y=raa[(raa.origin<="1983") | (raa.origin=="1986")]
print((x-y).iloc[..., 0])
# When every origin label differs, the axes are unioned by label.z=raa[(raa.origin>="1985") & (raa.origin<="1988")]
print((x-z).iloc[..., 0])
# Development axis: ages 12-48 minus ages 12, 24, 36 and 72.d=raa[raa.development<=48]
e=raa[(raa.development<=36) | (raa.development==72)]
print((d-e).iloc[..., 0, :])
12
1981 NaN
1982 NaN
1983 NaN
1984 4142.0
12
1981 5012.0
1982 106.0
1983 3410.0
1984 5655.0
1985 -1092.0
1986 -1513.0
1987 -557.0
1988 -1351.0
12 24 36 48
1981 NaN NaN NaN -4376.0
What is the expected behavior?
Arithmetic should match labels whenever any label differs, as the union path already does. For x - y that means rows 1981–1984 and 1986, with 5655 for 1984 and −1513 for 1986, as in the x - z output.
Changing np.all to np.any at L169 and L171 isn't enough on its own. With that change, x - y raises ValueError: could not broadcast input array from shape (1,1,4,10) into shape (1,1,5,10), because the 1986 label leaves a gap in y's block (#1649). On #1461, henrydingliu suggested a union method in TriangleAxis. #1304 rewrites L169–L171 and keeps the every-label test, and #1467 proposes refactoring the block.
Would you be willing to contribute this ticket?
Yes, absolutely!
Yes, but I would like some help.
No.
(This issue was written with Árbol, an AI assistant, under my direction.)
I reproduced this on main a59efc9. The two orders differ because the union in _prep_origin_development runs only when every origin label differs, at L171 of dunders.py with np.all. Your two triangles share nine of ten origins, so the code skips the union and adds them row by row. The result then takes its origin labels from the left operand. raa + premium labels the rows 1981 to 1990, and premium + raa labels the same sums 1982 to 1991. Both orders put the premium labelled 1982 on raa's 1981 row, so 1981 age 12 reads 25012 = 5012 + 20000, though no premium carries the 1981 label.
raa + premium also shows an extra diagonal of premium alone, such as 22000 at 1982 age 120. L39 sets the result's valuation date to the later of the two, which is 1991 from the premium origins, so _arithmetic_cleanup keeps one more diagonal on raa's labels.
This is the same path as the scope question in #1648. Switching L171 to np.any alone is not enough, because the union then copies each triangle as one contiguous block and hits #1649. A fix needs both changes. I'm happy to do it in #1648 or separately, whichever you prefer.
(This comment was written with Árbol, an AI assistant, under my direction.)
i'd like to keep dunders separate from #1648. if the dunders fix for this issue naturally restore the commutative __add__, we can add it as a test. otherwise i can open a new issue for this commutative bug.
Activity
henrydingliu commentedon Nov 18, 2026
adding another symptom. right now
__add__isn't commutative when origins are slightly mismatchingscks1 commentedon Nov 18, 2026
I reproduced this on main a59efc9. The two orders differ because the union in
_prep_origin_developmentruns only when every origin label differs, at L171 of dunders.py withnp.all. Your two triangles share nine of ten origins, so the code skips the union and adds them row by row. The result then takes its origin labels from the left operand.raa + premiumlabels the rows 1981 to 1990, andpremium + raalabels the same sums 1982 to 1991. Both orders put the premium labelled 1982 on raa's 1981 row, so 1981 age 12 reads 25012 = 5012 + 20000, though no premium carries the 1981 label.raa + premiumalso shows an extra diagonal of premium alone, such as 22000 at 1982 age 120. L39 sets the result's valuation date to the later of the two, which is 1991 from the premium origins, so_arithmetic_cleanupkeeps one more diagonal on raa's labels.This is the same path as the scope question in #1648. Switching L171 to
np.anyalone is not enough, because the union then copies each triangle as one contiguous block and hits #1649. A fix needs both changes. I'm happy to do it in #1648 or separately, whichever you prefer.(This comment was written with Árbol, an AI assistant, under my direction.)
henrydingliu commentedon Nov 18, 2026
i'd like to keep dunders separate from #1648. if the dunders fix for this issue naturally restore the commutative
__add__, we can add it as a test. otherwise i can open a new issue for this commutative bug.