Skip to content

Comments

Fix operator precedence bug in PaidIncurredChain and improve code quality#98

Merged
mages merged 1 commit intomasterfrom
fix/pic-v2-operator-precedence
Feb 18, 2026
Merged

Fix operator precedence bug in PaidIncurredChain and improve code quality#98
mages merged 1 commit intomasterfrom
fix/pic-v2-operator-precedence

Conversation

@fabioconcina
Copy link
Collaborator

Summary

  • Fix operator precedence bug in PaidIncurredChain() that inflated reserves by ~2.9%
  • Improve code quality: rename variables shadowing base R, pre-allocate vectors, use idiomatic R, remove dead code
  • Add regression test

Bug details

R's : operator binds tighter than -, so tau2.hat[i:J-1] evaluates as tau2.hat[(i:J)-1] instead of the intended tau2.hat[i:(J-1)]:

# i=1: indices (1:J)-1 = 0:(J-1), index 0 silently dropped → correct by accident
# i=2: indices (2:J)-1 = 1:(J-1) → includes tau2.hat[1], should start at [2]

This caused v2[1] == v2[2], violating the strictly decreasing sequence required by Proposition 2.2 in Merz & Wüthrich (2010). Fixed with a vectorized cumsum:

v2 <- sum(sigma2.hat) + c(rev(cumsum(rev(tau2.hat))), 0)

Impact on USAA example data

Metric Before After Delta
Total reserve 1,643,953 1,596,954 -2.9%
s.e. 113,940 110,977 -2.6%

Code quality improvements

  • Renamed cc_par, diag/diag2diag_I/diag_P (shadowed base R functions)
  • Pre-allocated vectors instead of growing with c()
  • Replaced variance loops with apply(), cumulative sum loop with cumsum()
  • Adopted checkTriangle() and getLatestCumulative() (consistent with rest of package)
  • Vectorized fP and fI triangle construction
  • Simplified output to single list() call
  • Removed 257 lines of commented-out Bayesian model code (preserved in git history)

Test plan

  • New test.PaidIncurredChain passes (6 checks)
  • Full test suite: 70 tests, 0 failures (3 pre-existing errors unrelated)

@fabioconcina fabioconcina requested a review from mages February 17, 2026 12:09
@mages mages merged commit 84de08b into master Feb 18, 2026
6 checks passed
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.

2 participants