Skip to main content

reserving_kernels/preprocessing/
triangle_algebra.rs

1//! Allocation-free numeric kernels for triangle algebra.
2//!
3//! These functions intentionally operate on flat slices and caller-provided
4//! output buffers. Public actuarial types can validate and label data before
5//! calling into this layer.
6
7/// Returns whether a cell is observed under standard right-triangle geometry.
8#[inline]
9pub const fn is_observed_right_triangle(
10    origin_idx: usize,
11    dev_idx: usize,
12    _n_origin: usize,
13    n_dev: usize,
14) -> bool {
15    origin_idx + dev_idx < n_dev
16}
17
18/// Convert one cumulative row to incremental values.
19#[inline]
20pub fn cumulative_to_incremental_row(cum: &[f64], out: &mut [f64]) {
21    debug_assert_eq!(cum.len(), out.len());
22    if cum.is_empty() {
23        return;
24    }
25
26    out[0] = cum[0];
27    for j in 1..cum.len() {
28        out[j] = cum[j] - cum[j - 1];
29    }
30}
31
32/// Convert one incremental row to cumulative values.
33#[inline]
34pub fn incremental_to_cumulative_row(incr: &[f64], out: &mut [f64]) {
35    debug_assert_eq!(incr.len(), out.len());
36
37    let mut acc = 0.0;
38    for (j, &value) in incr.iter().enumerate() {
39        acc += value;
40        out[j] = acc;
41    }
42}
43
44/// Convert a flat row-major cumulative triangle to incremental values.
45#[inline]
46pub fn cumulative_to_incremental_flat(cum: &[f64], n_origin: usize, n_dev: usize, out: &mut [f64]) {
47    debug_assert_eq!(cum.len(), n_origin * n_dev);
48    debug_assert_eq!(out.len(), cum.len());
49
50    for i in 0..n_origin {
51        let start = i * n_dev;
52        let end = start + n_dev;
53        cumulative_to_incremental_row(&cum[start..end], &mut out[start..end]);
54    }
55}
56
57/// Convert a flat row-major incremental triangle to cumulative values.
58#[inline]
59pub fn incremental_to_cumulative_flat(
60    incr: &[f64],
61    n_origin: usize,
62    n_dev: usize,
63    out: &mut [f64],
64) {
65    debug_assert_eq!(incr.len(), n_origin * n_dev);
66    debug_assert_eq!(out.len(), incr.len());
67
68    for i in 0..n_origin {
69        let start = i * n_dev;
70        let end = start + n_dev;
71        incremental_to_cumulative_row(&incr[start..end], &mut out[start..end]);
72    }
73}
74
75/// Extract the latest observed value and development index for each origin.
76///
77/// This assumes standard right-triangle geometry.
78#[inline]
79pub fn latest_observed_by_origin(
80    values: &[f64],
81    n_origin: usize,
82    n_dev: usize,
83    latest_value: &mut [f64],
84    latest_dev: &mut [usize],
85) {
86    debug_assert_eq!(values.len(), n_origin * n_dev);
87    debug_assert_eq!(latest_value.len(), n_origin);
88    debug_assert_eq!(latest_dev.len(), n_origin);
89
90    if n_dev == 0 {
91        latest_dev.fill(0);
92        latest_value.fill(f64::NAN);
93        return;
94    }
95
96    for i in 0..n_origin {
97        if i >= n_dev {
98            latest_dev[i] = 0;
99            latest_value[i] = f64::NAN;
100            continue;
101        }
102        let last_j = n_dev - 1 - i;
103        latest_dev[i] = last_j;
104        latest_value[i] = values[i * n_dev + last_j];
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn row_cumulative_to_incremental() {
114        let cum = [100.0, 150.0, 170.0, 175.0];
115        let mut out = [0.0; 4];
116
117        cumulative_to_incremental_row(&cum, &mut out);
118
119        assert_eq!(out, [100.0, 50.0, 20.0, 5.0]);
120    }
121
122    #[test]
123    fn row_incremental_to_cumulative() {
124        let incr = [100.0, 50.0, 20.0, 5.0];
125        let mut out = [0.0; 4];
126
127        incremental_to_cumulative_row(&incr, &mut out);
128
129        assert_eq!(out, [100.0, 150.0, 170.0, 175.0]);
130    }
131
132    #[test]
133    fn flat_round_trip() {
134        let cum = [100.0, 150.0, 170.0, 120.0, 180.0, 0.0];
135        let mut incr = [0.0; 6];
136        let mut round_trip = [0.0; 6];
137
138        cumulative_to_incremental_flat(&cum, 2, 3, &mut incr);
139        incremental_to_cumulative_flat(&incr, 2, 3, &mut round_trip);
140
141        assert_eq!(round_trip, cum);
142    }
143
144    #[test]
145    fn right_triangle_observed_geometry() {
146        assert!(is_observed_right_triangle(0, 4, 5, 5));
147        assert!(is_observed_right_triangle(4, 0, 5, 5));
148        assert!(!is_observed_right_triangle(1, 4, 5, 5));
149        assert!(!is_observed_right_triangle(4, 1, 5, 5));
150    }
151
152    #[test]
153    fn latest_observed_right_triangle() {
154        let values = [
155            100.0, 150.0, 170.0, 175.0, 110.0, 165.0, 190.0, 0.0, 120.0, 180.0, 0.0, 0.0, 130.0,
156            0.0, 0.0, 0.0,
157        ];
158        let mut latest_value = [0.0; 4];
159        let mut latest_dev = [0; 4];
160
161        latest_observed_by_origin(&values, 4, 4, &mut latest_value, &mut latest_dev);
162
163        assert_eq!(latest_value, [175.0, 190.0, 180.0, 130.0]);
164        assert_eq!(latest_dev, [3, 2, 1, 0]);
165    }
166}