Skip to main content

reserving_kernels/
triangle.rs

1//! Triangle helpers: cumulative ↔ incremental conversion, latest-diagonal
2//! extraction, individual age-to-age ratios.
3//!
4//! All functions treat `NaN` as the unobserved sentinel and assume the input
5//! has been validated (see [`crate::validate`]).
6
7use crate::types::TriangleView;
8
9/// Extract the latest-observed cumulative value for each origin.
10///
11/// For a square triangle of size `n`, origin `i`'s latest observed
12/// development age is `n − 1 − i` (anti-diagonal). Returns a `Vec<f64>` of
13/// length `n`.
14pub fn latest_diagonal(t: &TriangleView<'_>) -> Vec<f64> {
15    let n = t.n_origins.min(t.n_devs);
16    let mut out = vec![0.0; n];
17    for i in 0..n {
18        out[i] = t.get(i, n - 1 - i);
19    }
20    out
21}
22
23/// Convert cumulative to incremental in-place semantics: returns a new
24/// triangle-shaped `Vec<f64>` whose observed cells are period deltas.
25/// Cell `(i, 0)` is copied as-is; cell `(i, j)` with `j > 0` is
26/// `C_{i,j} − C_{i,j−1}`. Unobserved cells become `NaN`.
27pub fn cumulative_to_incremental(t: &TriangleView<'_>) -> Vec<f64> {
28    let n = t.n_origins.min(t.n_devs);
29    let mut out = vec![f64::NAN; n * n];
30    for i in 0..n {
31        let row_len = n - i;
32        let in_row = &t.data[i * t.n_devs..i * t.n_devs + row_len];
33        let out_row = &mut out[i * n..i * n + row_len];
34        out_row[0] = in_row[0];
35        for j in 1..row_len {
36            out_row[j] = in_row[j] - in_row[j - 1];
37        }
38    }
39    out
40}
41
42/// Convert incremental to cumulative. Mirror of [`cumulative_to_incremental`].
43pub fn incremental_to_cumulative(t: &TriangleView<'_>) -> Vec<f64> {
44    let n = t.n_origins.min(t.n_devs);
45    let mut out = vec![f64::NAN; n * n];
46    for i in 0..n {
47        let row_len = n - i;
48        let in_row = &t.data[i * t.n_devs..i * t.n_devs + row_len];
49        let out_row = &mut out[i * n..i * n + row_len];
50        let mut acc = 0.0;
51        for j in 0..row_len {
52            acc += in_row[j];
53            out_row[j] = acc;
54        }
55    }
56    out
57}
58
59/// Individual age-to-age ratio `C_{i, j+1} / C_{i, j}`. Returns `NaN` where
60/// the ratio is undefined (pair unobserved or denominator ≤ 0).
61#[inline]
62pub fn age_to_age(t: &TriangleView<'_>, i: usize, j: usize) -> f64 {
63    let n = t.n_origins.min(t.n_devs);
64    if n < 2 || i >= n || j >= n || i.saturating_add(j).saturating_add(1) >= n {
65        return f64::NAN;
66    }
67    let a = t.get(i, j);
68    let b = t.get(i, j + 1);
69    if !(a > 0.0) || !b.is_finite() {
70        return f64::NAN;
71    }
72    b / a
73}
74
75/// Number of origins with both `(i, j)` and `(i, j+1)` observed (i.e. the
76/// count that feeds a development-`j → j+1` LDF).
77#[inline]
78pub fn link_count(n: usize, j: usize) -> usize {
79    // Need i + j + 1 ≤ n - 1, so i ≤ n - j - 2, giving n - j - 1 observations
80    // for j = 0..n-2.
81    if j + 2 > n { 0 } else { n - j - 1 }
82}