use crate::linalg::{invert_spd, quad_form};
use crate::types::TriangleView;
use crate::validate::{ValidationError, validate_positive_finite_slice, validate_triangle};
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct PicPrior {
pub phi_mean: Vec<f64>,
pub phi_var: Vec<f64>,
pub psi_mean: Vec<f64>,
pub psi_var: Vec<f64>,
}
#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, serde::Deserialize)]
pub struct PicOptions {
pub incurred_variances: Option<Vec<f64>>,
pub paid_variances: Option<Vec<f64>>,
pub prior: Option<PicPrior>,
pub variance_floor: Option<f64>,
}
impl PicOptions {
pub fn with_incurred_variances(mut self, variances: impl Into<Vec<f64>>) -> Self {
self.incurred_variances = Some(variances.into());
self
}
pub fn with_paid_variances(mut self, variances: impl Into<Vec<f64>>) -> Self {
self.paid_variances = Some(variances.into());
self
}
pub fn with_prior(mut self, prior: PicPrior) -> Self {
self.prior = Some(prior);
self
}
pub const fn with_variance_floor(mut self, floor: f64) -> Self {
self.variance_floor = Some(floor);
self
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct PaidIncurredChainResult {
pub posterior_phi: Vec<f64>,
pub posterior_psi: Vec<f64>,
pub posterior_closing_psi: f64,
pub incurred_variances: Vec<f64>,
pub paid_variances: Vec<f64>,
pub closing_variance: f64,
pub incurred_weights: Vec<f64>,
pub predictive_log_mean: Vec<f64>,
pub predictive_log_variance: Vec<f64>,
pub ultimates: Vec<f64>,
pub reserves: Vec<f64>,
pub msep: Vec<f64>,
pub standard_error: Vec<f64>,
pub cv: Vec<f64>,
pub total_ultimate: f64,
pub total_reserve: f64,
pub total_msep: f64,
pub total_standard_error: f64,
pub total_cv: f64,
}
pub fn analyze(
paid: &TriangleView<'_>,
incurred: &TriangleView<'_>,
options: &PicOptions,
) -> Result<PaidIncurredChainResult, ValidationError> {
validate_triangle(paid)?;
validate_triangle(incurred)?;
let n = paid.n_origins;
if n < 3 || incurred.n_origins != n {
return Err(ValidationError::ShapeMismatch);
}
for i in 0..n {
for j in 0..(n - i) {
if paid.get(i, j) <= 0.0 || incurred.get(i, j) <= 0.0 {
return Err(ValidationError::InvalidParameter(
"Paid-Incurred Chain requires strictly positive observed paid and incurred cells",
));
}
}
}
let j_max = n - 1;
let var_floor = match options.variance_floor {
Some(v) => {
if !v.is_finite() || v <= 0.0 {
return Err(ValidationError::InvalidParameter(
"variance_floor must be finite and strictly positive",
));
}
v.max(1e-14)
}
None => 1e-10,
};
let mut zeta = vec![f64::NAN; n * n];
let mut xi = vec![f64::NAN; n * n];
for i in 0..n {
let d_i = j_max - i;
zeta[i * n] = incurred.get(i, 0).ln();
for j in 1..=d_i {
zeta[i * n + j] = (incurred.get(i, j) / incurred.get(i, j - 1)).ln();
xi[i * n + j] = (paid.get(i, j) / paid.get(i, j - 1)).ln();
}
}
let sigma_sq = match &options.incurred_variances {
Some(v) => {
if v.len() != n {
return Err(ValidationError::ShapeMismatch);
}
validate_positive_finite_slice(v, "incurred_variances")?;
v.clone()
}
None => estimate_column_variances(&zeta, n, 0, var_floor),
};
let tau_sq = match &options.paid_variances {
Some(v) => {
if v.len() != n - 1 {
return Err(ValidationError::ShapeMismatch);
}
validate_positive_finite_slice(v, "paid_variances")?;
v.clone()
}
None => estimate_column_variances(&xi, n, 1, var_floor),
};
let closing_obs = (incurred.get(0, j_max) / paid.get(0, j_max)).ln();
let has_closing_bridge = closing_obs.abs() > 1e-12;
let tau_close_sq = if has_closing_bridge {
extrapolate_last_variance(&tau_sq, var_floor)
} else {
0.0
};
let p_dim = 2 * n - 1 + usize::from(has_closing_bridge);
let psi_offset = n; let close_idx = 2 * n - 1;
let mut prec = vec![0.0; p_dim * p_dim];
let mut rhs = vec![0.0; p_dim];
if let Some(prior) = &options.prior {
if prior.phi_mean.len() != n
|| prior.phi_var.len() != n
|| prior.psi_mean.len() != n - 1
|| prior.psi_var.len() != n - 1
{
return Err(ValidationError::ShapeMismatch);
}
validate_positive_finite_slice(&prior.phi_var, "prior.phi_var")?;
validate_positive_finite_slice(&prior.psi_var, "prior.psi_var")?;
for j in 0..n {
if !prior.phi_mean[j].is_finite() {
return Err(ValidationError::NotFinite);
}
let inv_v = 1.0 / prior.phi_var[j];
prec[j * p_dim + j] += inv_v;
rhs[j] += inv_v * prior.phi_mean[j];
}
for k in 0..(n - 1) {
if !prior.psi_mean[k].is_finite() {
return Err(ValidationError::NotFinite);
}
let inv_v = 1.0 / prior.psi_var[k];
let idx = psi_offset + k;
prec[idx * p_dim + idx] += inv_v;
rhs[idx] += inv_v * prior.psi_mean[k];
}
}
for j in 0..n {
let inv_v = 1.0 / sigma_sq[j];
for i in 0..(n - j) {
prec[j * p_dim + j] += inv_v;
rhs[j] += inv_v * zeta[i * n + j];
}
}
for j in 1..n {
let inv_v = 1.0 / tau_sq[j - 1];
let idx = psi_offset + (j - 1);
for i in 0..(n - j) {
prec[idx * p_dim + idx] += inv_v;
rhs[idx] += inv_v * xi[i * n + j];
}
}
let mut v_inc = vec![0.0; n];
let mut v_paid = vec![0.0; n];
let mut active_entries: Vec<(usize, f64)> = Vec::with_capacity(p_dim);
for i in 0..n {
let d_i = j_max - i;
let vi: f64 = sigma_sq[(d_i + 1)..n].iter().sum();
let vp: f64 = tau_sq[d_i..(n - 1)].iter().sum::<f64>() + tau_close_sq;
v_inc[i] = vi;
v_paid[i] = vp;
let total_v = vi + vp;
if total_v <= 0.0 {
continue;
}
let z_obs = (incurred.get(i, d_i) / paid.get(i, d_i)).ln();
active_entries.clear();
for k in (d_i + 1)..n {
active_entries.push((k, -1.0)); active_entries.push((psi_offset + (k - 1), 1.0)); }
if has_closing_bridge {
active_entries.push((close_idx, 1.0));
}
let inv_v = 1.0 / total_v;
for &(a, xa) in &active_entries {
rhs[a] += inv_v * xa * z_obs;
let row_a = a * p_dim;
let scale_a = inv_v * xa;
for &(b, xb) in &active_entries {
prec[row_a + b] += scale_a * xb;
}
}
}
let v_theta = invert_spd(&prec, p_dim)?;
let mut theta_post = vec![0.0; p_dim];
for a in 0..p_dim {
let mut acc = 0.0;
for b in 0..p_dim {
acc += v_theta[a * p_dim + b] * rhs[b];
}
theta_post[a] = acc;
}
let mut incurred_weights = vec![0.0; n];
let mut pred_log_mean = vec![0.0; n];
let mut pred_log_var = vec![0.0; n];
let mut c_vecs: Vec<Vec<f64>> = vec![vec![0.0; p_dim]; n];
for i in 0..n {
let d_i = j_max - i;
let vi = v_inc[i];
let vp = v_paid[i];
if vi == 0.0 {
incurred_weights[i] = 1.0;
pred_log_mean[i] = incurred.get(i, d_i).ln();
pred_log_var[i] = 0.0;
continue;
}
let w_i = vp / (vi + vp);
let s_i_sq = (vi * vp) / (vi + vp);
incurred_weights[i] = w_i;
for k in (d_i + 1)..n {
c_vecs[i][k] = w_i;
c_vecs[i][psi_offset + (k - 1)] = 1.0 - w_i;
}
if has_closing_bridge {
c_vecs[i][close_idx] = 1.0 - w_i;
}
let mut linear_mean = 0.0;
for a in 0..p_dim {
linear_mean += c_vecs[i][a] * theta_post[a];
}
pred_log_mean[i] =
w_i * incurred.get(i, d_i).ln() + (1.0 - w_i) * paid.get(i, d_i).ln() + linear_mean;
let param_var = quad_form(&c_vecs[i], &v_theta, p_dim).max(0.0);
pred_log_var[i] = s_i_sq + param_var;
}
let mut ultimates = vec![0.0; n];
let mut reserves = vec![0.0; n];
let mut msep = vec![0.0; n];
let mut standard_error = vec![0.0; n];
let mut cv = vec![0.0; n];
for i in 0..n {
let d_i = j_max - i;
let ult = (pred_log_mean[i] + 0.5 * pred_log_var[i]).exp();
let res = ult - paid.get(i, d_i);
let var_i = if pred_log_var[i] > 0.0 {
ult * ult * pred_log_var[i].exp_m1()
} else {
0.0
};
ultimates[i] = ult;
reserves[i] = res;
msep[i] = var_i.max(0.0);
standard_error[i] = var_i.max(0.0).sqrt();
cv[i] = if res > 0.0 {
standard_error[i] / res
} else {
0.0
};
}
let mut vc_vecs: Vec<Vec<f64>> = Vec::with_capacity(n);
for k in 0..n {
if pred_log_var[k] == 0.0 {
vc_vecs.push(Vec::new());
continue;
}
let mut vc = vec![0.0; p_dim];
for a in 0..p_dim {
let mut row_acc = 0.0;
for b in 0..p_dim {
row_acc += v_theta[a * p_dim + b] * c_vecs[k][b];
}
vc[a] = row_acc;
}
vc_vecs.push(vc);
}
let mut total_msep: f64 = msep.iter().sum();
for i in 0..n {
if pred_log_var[i] == 0.0 {
continue;
}
for k in (i + 1)..n {
if pred_log_var[k] == 0.0 {
continue;
}
let mut cov_log = 0.0;
let vck = &vc_vecs[k];
for a in 0..p_dim {
cov_log += c_vecs[i][a] * vck[a];
}
total_msep += 2.0 * ultimates[i] * ultimates[k] * cov_log.exp_m1();
}
}
let total_ultimate: f64 = ultimates.iter().sum();
let total_reserve: f64 = reserves.iter().sum();
let total_standard_error = total_msep.max(0.0).sqrt();
let total_cv = if total_reserve > 0.0 {
total_standard_error / total_reserve
} else {
0.0
};
Ok(PaidIncurredChainResult {
posterior_phi: theta_post[0..n].to_vec(),
posterior_psi: theta_post[psi_offset..(psi_offset + n - 1)].to_vec(),
posterior_closing_psi: if has_closing_bridge {
theta_post[close_idx]
} else {
0.0
},
incurred_variances: sigma_sq,
paid_variances: tau_sq,
closing_variance: tau_close_sq,
incurred_weights,
predictive_log_mean: pred_log_mean,
predictive_log_variance: pred_log_var,
ultimates,
reserves,
msep,
standard_error,
cv,
total_ultimate,
total_reserve,
total_msep: total_msep.max(0.0),
total_standard_error,
total_cv,
})
}
fn estimate_column_variances(mat: &[f64], n: usize, start_col: usize, floor: f64) -> Vec<f64> {
let count = n - start_col;
let mut vars = vec![f64::NAN; count];
for (idx, j) in (start_col..n).enumerate() {
let m_j = n - j;
if m_j >= 2 {
let mut sum = 0.0;
for i in 0..m_j {
sum += mat[i * n + j];
}
let mean = sum / m_j as f64;
let mut ss = 0.0;
for i in 0..m_j {
let d = mat[i * n + j] - mean;
ss += d * d;
}
vars[idx] = (ss / (m_j as f64 - 1.0)).max(floor);
}
}
if let Some(last) = vars.last_mut()
&& !last.is_finite()
{
let known = &vars[..count - 1];
let ext = extrapolate_last_variance(known, floor);
*vars.last_mut().expect("non-empty") = ext;
}
vars
}
fn extrapolate_last_variance(known: &[f64], floor: f64) -> f64 {
let m = known.len();
if m >= 2 {
let a = known[m - 1];
let b = known[m - 2];
if a > 0.0 && b > 0.0 {
return (a * a / b).min(a).min(b).max(floor);
}
}
if m >= 1 {
return known[m - 1].max(floor);
}
floor
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::Triangle;
const NAN: f64 = f64::NAN;
#[test]
fn pic_recovers_exact_ultimates_on_consistent_noiseless_triangles() {
let n = 5usize;
let ult = [1000.0, 1100.0, 1200.0, 1300.0, 1400.0];
let paid_pct = [0.25, 0.55, 0.80, 0.94, 1.00];
let inc_pct = [0.65, 0.85, 0.95, 0.99, 1.00];
let mut p_data = vec![NAN; n * n];
let mut i_data = vec![NAN; n * n];
for i in 0..n {
for j in 0..(n - i) {
p_data[i * n + j] = ult[i] * paid_pct[j];
i_data[i * n + j] = ult[i] * inc_pct[j];
}
}
let paid = Triangle::new(n, n, p_data);
let inc = Triangle::new(n, n, i_data);
let res = analyze(&paid.view(), &inc.view(), &PicOptions::default()).expect("pic");
for i in 0..n {
assert!(
(res.ultimates[i] - ult[i]).abs() < 1e-3,
"origin {i}: got {} expected {}",
res.ultimates[i],
ult[i]
);
}
assert!(res.posterior_closing_psi.abs() < 1e-12);
}
#[test]
fn pic_process_variance_strictly_below_individual_chains() {
#[rustfmt::skip]
let paid_data = vec![
100.0, 210.0, 285.0, 325.0, 340.0,
115.0, 235.0, 315.0, 358.0, NAN,
125.0, 260.0, 348.0, NAN, NAN,
135.0, 272.0, NAN, NAN, NAN,
150.0, NAN, NAN, NAN, NAN,
];
#[rustfmt::skip]
let inc_data = vec![
220.0, 290.0, 322.0, 338.0, 340.0,
245.0, 325.0, 355.0, 372.0, NAN,
270.0, 350.0, 392.0, NAN, NAN,
290.0, 378.0, NAN, NAN, NAN,
315.0, NAN, NAN, NAN, NAN,
];
let paid = Triangle::new(5, 5, paid_data);
let inc = Triangle::new(5, 5, inc_data);
let res = analyze(&paid.view(), &inc.view(), &PicOptions::default()).expect("pic");
for i in 1..5 {
assert!(res.incurred_weights[i] > 0.0 && res.incurred_weights[i] < 1.0);
assert!(res.standard_error[i] > 0.0);
}
assert!(res.total_standard_error > 0.0);
assert!(res.total_reserve > 0.0);
}
}