Skip to main content

reserving_kernels/
incremental_additive.rs

1//! Incremental Additive reserving kernel (Schmidt 2006 / Friedland).
2//!
3//! Projects unobserved future incremental losses additively from exposure-weighted
4//! incremental loss ratios:
5//!
6//! 1. Detrend observed incremental losses $Y_{i,j}$ to the latest origin period
7//!    using an annual severity/loss trend rate $\tau$:
8//!    $$\tilde{Y}_{i,j} = Y_{i,j} (1 + \tau)^{(n-1) - i}.$$
9//! 2. Compute the selected incremental-to-exposure factor $\hat{\zeta}_j$ at
10//!    each development age $j$:
11//!    $$\hat{\zeta}_j = \frac{\sum_{i \in \mathcal{O}_j} \tilde{Y}_{i,j}}{\sum_{i \in \mathcal{O}_j} E_i}$$
12//!    (or unweighted simple average of $\tilde{Y}_{i,j} / E_i$).
13//! 3. Project unobserved cells $(i, j)$ by re-trending back to origin $i$:
14//!    $$\hat{Y}_{i,j} = E_i \, \hat{\zeta}_j \, (1 + \tau)^{i - (n-1)}.$$
15
16use serde::{Deserialize, Serialize};
17
18use crate::{
19    triangle::{cumulative_to_incremental, latest_diagonal},
20    types::TriangleView,
21    validate::{
22        MonotonicityPolicy, ValidationError, validate_monotone_cumulative, validate_triangle,
23    },
24};
25
26/// Averaging rule for incremental-to-exposure ratios.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
28pub enum IncrementalRatioAverage {
29    /// Exposure-weighted average $\sum \tilde{Y}_{i,j} / \sum E_i$.
30    ExposureWeighted,
31    /// Unweighted arithmetic mean of $\tilde{Y}_{i,j} / E_i$.
32    Simple,
33}
34
35/// Configuration for [`project_incremental_additive`].
36#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
37pub struct IncrementalAdditiveOptions {
38    /// Per-origin-period trend rate $\tau$ (e.g. `0.03` for 3% annual trend;
39    /// `0.0` disables trending).
40    pub trend: f64,
41    /// Averaging rule for age-specific incremental-to-exposure factors.
42    pub average: IncrementalRatioAverage,
43    /// Optional trailing window length (`Some(k)` uses at most the latest `k`
44    /// observed origins per development age).
45    pub last_n: Option<usize>,
46    /// Monotonicity policy for validating the input cumulative triangle.
47    pub policy: MonotonicityPolicy,
48}
49
50impl Default for IncrementalAdditiveOptions {
51    fn default() -> Self {
52        Self {
53            trend: 0.0,
54            average: IncrementalRatioAverage::ExposureWeighted,
55            last_n: None,
56            policy: MonotonicityPolicy::AllowNegativeIncrements,
57        }
58    }
59}
60
61/// Output of the Incremental Additive reserving projection.
62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
63pub struct IncrementalAdditiveProjection {
64    /// Selected incremental-to-exposure factors $\hat{\zeta}_j$ (at latest origin trend level).
65    pub zeta: Vec<f64>,
66    /// Full completed incremental triangle (row-major `n_origins × n_devs`).
67    pub completed_incremental: Vec<f64>,
68    /// Full completed cumulative triangle (row-major `n_origins × n_devs`).
69    pub completed_cumulative: Vec<f64>,
70    /// Projected ultimate loss per origin.
71    pub ultimate: Vec<f64>,
72    /// Projected unpaid reserve (`ultimate - latest`) per origin.
73    pub reserve: Vec<f64>,
74    /// Sum of `reserve` across all origins.
75    pub total_reserve: f64,
76}
77
78/// Project ultimates and reserves using the Incremental Additive method from a
79/// cumulative loss triangle and per-origin `exposures`.
80pub fn project_incremental_additive(
81    cumulative: &TriangleView<'_>,
82    exposures: &[f64],
83    options: &IncrementalAdditiveOptions,
84) -> Result<IncrementalAdditiveProjection, ValidationError> {
85    validate_triangle(cumulative)?;
86    if options.policy == MonotonicityPolicy::Strict {
87        validate_monotone_cumulative(cumulative)?;
88    }
89    let n_origins = cumulative.n_origins;
90    let n_devs = cumulative.n_devs;
91    if exposures.len() != n_origins {
92        return Err(ValidationError::ShapeMismatch);
93    }
94    if !options.trend.is_finite() || options.trend <= -1.0 {
95        return Err(ValidationError::InvalidParameter(
96            "trend must be finite and greater than -1.0",
97        ));
98    }
99    for &e in exposures {
100        if !e.is_finite() || e <= 0.0 {
101            return Err(ValidationError::InvalidParameter(
102                "exposures must be finite and strictly positive",
103            ));
104        }
105    }
106
107    let inc = cumulative_to_incremental(cumulative);
108    let factor = 1.0 + options.trend;
109    let mut zeta = vec![0.0; n_devs];
110
111    for j in 0..n_devs {
112        let mut pairs: Vec<(f64, f64)> = Vec::with_capacity(n_origins);
113        for i in 0..n_origins {
114            let y = inc[i * n_devs + j];
115            if y.is_finite() {
116                let detrend = factor.powi((n_origins - 1 - i) as i32);
117                pairs.push((y * detrend, exposures[i]));
118            }
119        }
120        if pairs.is_empty() {
121            zeta[j] = 0.0;
122            continue;
123        }
124        let slice = match options.last_n {
125            Some(k) if k > 0 && k < pairs.len() => &pairs[pairs.len() - k..],
126            _ => &pairs[..],
127        };
128        zeta[j] = match options.average {
129            IncrementalRatioAverage::ExposureWeighted => {
130                let num: f64 = slice.iter().map(|(y, _)| *y).sum();
131                let den: f64 = slice.iter().map(|(_, e)| *e).sum();
132                if den > 0.0 { num / den } else { 0.0 }
133            }
134            IncrementalRatioAverage::Simple => {
135                let sum: f64 = slice.iter().map(|(y, e)| *y / *e).sum();
136                sum / slice.len() as f64
137            }
138        };
139    }
140
141    let mut completed_incremental = inc;
142    for i in 0..n_origins {
143        let retrend = factor.powi(i as i32 - (n_origins - 1) as i32);
144        for j in 0..n_devs {
145            let idx = i * n_devs + j;
146            if !completed_incremental[idx].is_finite() {
147                completed_incremental[idx] = exposures[i] * zeta[j] * retrend;
148            }
149        }
150    }
151
152    let mut completed_cumulative = vec![0.0; n_origins * n_devs];
153    let mut ultimate = Vec::with_capacity(n_origins);
154    let latest = latest_diagonal(cumulative);
155    let mut reserve = Vec::with_capacity(n_origins);
156
157    for i in 0..n_origins {
158        let mut acc = 0.0;
159        for j in 0..n_devs {
160            acc += completed_incremental[i * n_devs + j];
161            completed_cumulative[i * n_devs + j] = acc;
162        }
163        ultimate.push(acc);
164        let lp = if latest[i].is_finite() {
165            latest[i]
166        } else {
167            0.0
168        };
169        reserve.push(acc - lp);
170    }
171
172    let total_reserve = reserve.iter().sum();
173
174    Ok(IncrementalAdditiveProjection {
175        zeta,
176        completed_incremental,
177        completed_cumulative,
178        ultimate,
179        reserve,
180        total_reserve,
181    })
182}
183
184#[cfg(test)]
185mod tests {
186    use super::*;
187    use crate::types::Triangle;
188
189    #[test]
190    fn exact_recovery_with_trend() {
191        let n = 5;
192        let exposures = vec![1000.0, 1100.0, 1200.0, 1300.0, 1400.0];
193        let base_zeta = [0.30, 0.20, 0.10, 0.05, 0.02];
194        let trend = 0.05_f64;
195        let mut data = vec![f64::NAN; n * n];
196        let mut expected_res = 0.0;
197
198        for i in 0..n {
199            let tr = (1.0 + trend).powi(i as i32 - (n - 1) as i32);
200            let mut cum = 0.0;
201            for j in 0..n {
202                let y = exposures[i] * base_zeta[j] * tr;
203                if i + j < n {
204                    cum += y;
205                    data[i * n + j] = cum;
206                } else {
207                    expected_res += y;
208                }
209            }
210        }
211
212        let tri = Triangle::new(n, n, data);
213        let opts = IncrementalAdditiveOptions {
214            trend,
215            ..Default::default()
216        };
217        let res = project_incremental_additive(&tri.view(), &exposures, &opts).expect("inc add");
218        for (a, b) in res.zeta.iter().zip(base_zeta.iter()) {
219            assert!((a - b).abs() < 1e-12, "zeta mismatch: {a} vs {b}");
220        }
221        assert!(
222            (res.total_reserve - expected_res).abs() < 1e-8,
223            "reserve={} vs expected={}",
224            res.total_reserve,
225            expected_res
226        );
227    }
228}