Skip to main content

reserving_kernels/preprocessing/
aggregate.rs

1//! Portfolio-level slice reducers.
2
3use crate::types::TriangleView;
4
5/// Sum of latest observed cumulative losses (total "paid to date" across
6/// origin years) without intermediate heap allocation.
7#[inline]
8pub fn total_paid(t: &TriangleView<'_>) -> f64 {
9    let n = t.n_origins.min(t.n_devs);
10    let mut sum = 0.0;
11    for i in 0..n {
12        sum += t.get(i, n - 1 - i);
13    }
14    sum
15}
16
17/// Sum of a per-origin ultimate vector.
18#[inline]
19pub fn total_ultimate(ultimate: &[f64]) -> f64 {
20    ultimate.iter().sum()
21}
22
23/// Sum of a per-origin reserve vector.
24#[inline]
25pub fn total_reserve(reserve: &[f64]) -> f64 {
26    reserve.iter().sum()
27}
28
29/// Portfolio paid ratio: total paid / total ultimate.
30#[inline]
31pub fn paid_ratio(t: &TriangleView<'_>, ultimate: &[f64]) -> f64 {
32    let u = total_ultimate(ultimate);
33    if u > 0.0 { total_paid(t) / u } else { f64::NAN }
34}
35
36/// Portfolio loss ratio: total ultimate / total premium.
37/// Returns `NaN` if slice lengths differ or total premium is non-positive.
38#[inline]
39pub fn portfolio_loss_ratio(ultimate: &[f64], premium: &[f64]) -> f64 {
40    if ultimate.len() != premium.len() {
41        return f64::NAN;
42    }
43    let mut num = 0.0;
44    let mut den = 0.0;
45    for (u, p) in ultimate.iter().zip(premium.iter()) {
46        num += *u;
47        den += *p;
48    }
49    if den > 0.0 { num / den } else { f64::NAN }
50}