reserving-kernels 0.2.1

Pure Rust numeric kernels for P&C actuarial reserving and capital modeling.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Brosius least-squares development.
//!
//! For each development link `j → j+1`, Brosius fits the best linear
//! predictor of `C_{i, j+1}` given `C_{i, j}`:
//!
//! `C_{i, j+1} ≈ a_j + b_j × C_{i, j}`.
//!
//! OLS gives `b_j = cov(x, y) / var(x)` and `a_j = mean(y) − b_j × mean(x)`,
//! where `(x, y) = (C_{·,j}, C_{·,j+1})` over origins `i = 0..n−j−2`.
//!
//! Brosius is a credibility-weighted estimator: `b_j = 0` recovers a pure
//! budget method (expected next value is constant), `b_j = f_j` with
//! `a_j = 0` recovers chain-ladder. Real triangles typically fall between.
//!
//! Returns `(a, b, next_projection)` per link, and per-origin ultimates by
//! chaining projections forward from each origin's latest observation.

use crate::triangle::latest_diagonal;
use crate::types::TriangleView;
use crate::validate::{
    MonotonicityPolicy, ValidationError, validate_finite_slice, validate_monotone_cumulative,
    validate_observed_count_inputs, validate_tail, validate_triangle,
};

/// Brosius projection result.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Brosius {
    /// Intercept per link: `a_j`, length `n − 1`.
    pub intercept: Vec<f64>,
    /// Slope per link: `b_j`, length `n − 1`.
    pub slope: Vec<f64>,
    /// Estimated ultimate per origin.
    pub ultimate: Vec<f64>,
    /// Unpaid reserve per origin.
    pub reserve: Vec<f64>,
    /// Latest observed cumulative value per origin.
    pub latest: Vec<f64>,
    /// OLS fit diagnostics per development link.
    pub diagnostics: Vec<BrosiusLinkDiagnostic>,
}

impl Brosius {
    /// Credibility weight `Z_j = b_j * mean_x / mean_y` per link (Brosius 1993).
    ///
    /// Expresses the OLS predictor `a_j + b_j * x` in credibility form
    /// `Z_j * (f_j * x) + (1 - Z_j) * mean_y` where `f_j = mean_y / mean_x`
    /// is the volume-weighted link ratio.
    pub fn credibility_weights(&self) -> Vec<f64> {
        self.diagnostics
            .iter()
            .map(|d| {
                if d.mean_y.is_finite() && d.mean_y > 0.0 && d.mean_x.is_finite() {
                    d.slope * d.mean_x / d.mean_y
                } else {
                    f64::NAN
                }
            })
            .collect()
    }
}

/// Brosius OLS fit diagnostics for one development link.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BrosiusLinkDiagnostic {
    /// Development link start index.
    pub dev: usize,
    /// Number of observed pairs used.
    pub n: usize,
    /// Mean prior cumulative value.
    pub mean_x: f64,
    /// Mean next cumulative value.
    pub mean_y: f64,
    /// Population variance of prior cumulative values.
    pub var_x: f64,
    /// Population covariance between prior and next cumulative values.
    pub cov_xy: f64,
    /// Fitted intercept.
    pub intercept: f64,
    /// Fitted slope.
    pub slope: f64,
    /// Whether the single-pair chain-ladder-ratio fallback was used.
    pub fallback_used: bool,
}

/// Fit Brosius OLS parameters per link and project ultimates without validation.
///
/// `tail` is the multiplicative tail factor applied at development age
/// `n − 1` (pass `1.0` for fully-developed).
///
/// Prefer [`project`] at external ingress boundaries.
pub fn project_unchecked(t: &TriangleView<'_>, tail: f64) -> Brosius {
    let n = t.n_origins.min(t.n_devs);
    let latest = latest_diagonal(t);
    let mut intercept = vec![0.0; n.saturating_sub(1)];
    let mut slope = vec![0.0; n.saturating_sub(1)];
    let mut diagnostics = Vec::with_capacity(n.saturating_sub(1));

    for j in 0..n.saturating_sub(1) {
        let k = n - j - 1; // number of origins with both (i, j) and (i, j+1)
        let mut sx = 0.0;
        let mut sy = 0.0;
        for i in 0..k {
            sx += t.get(i, j);
            sy += t.get(i, j + 1);
        }
        let kf = k as f64;
        let mut mean_x = f64::NAN;
        let mut mean_y = f64::NAN;
        let mut var_x = f64::NAN;
        let mut cov_xy = f64::NAN;
        let mut fallback_used = false;
        if k >= 2 {
            mean_x = sx / kf;
            mean_y = sy / kf;
            // Two-pass centered sums prevent catastrophic cancellation on large loss amounts.
            let mut sum_dx2 = 0.0;
            let mut sum_dxdy = 0.0;
            for i in 0..k {
                let dx = t.get(i, j) - mean_x;
                let dy = t.get(i, j + 1) - mean_y;
                sum_dx2 += dx * dx;
                sum_dxdy += dx * dy;
            }
            var_x = sum_dx2 / kf;
            cov_xy = sum_dxdy / kf;
            let var_tol = 1e-12 * mean_x * mean_x;
            let b = if var_x > var_tol { cov_xy / var_x } else { 0.0 };
            let a = mean_y - b * mean_x;
            intercept[j] = a;
            slope[j] = b;
        } else if k == 1 {
            // Degenerate: single pair, use chain-ladder ratio as slope, no
            // intercept (a = 0).
            let x = t.get(0, j);
            let y = t.get(0, j + 1);
            intercept[j] = 0.0;
            slope[j] = if x > 0.0 { y / x } else { f64::NAN };
            mean_x = x;
            mean_y = y;
            var_x = 0.0;
            cov_xy = 0.0;
            fallback_used = true;
        } else {
            intercept[j] = f64::NAN;
            slope[j] = f64::NAN;
        }
        diagnostics.push(BrosiusLinkDiagnostic {
            dev: j,
            n: k,
            mean_x,
            mean_y,
            var_x,
            cov_xy,
            intercept: intercept[j],
            slope: slope[j],
            fallback_used,
        });
    }

    let mut ultimate = vec![0.0; n];
    let mut reserve = vec![0.0; n];
    for i in 0..n {
        let mut c = latest[i];
        let start = n - 1 - i;
        for j in start..n.saturating_sub(1) {
            c = intercept[j] + slope[j] * c;
        }
        c *= tail;
        ultimate[i] = c;
        reserve[i] = c - latest[i];
    }
    Brosius {
        intercept,
        slope,
        ultimate,
        reserve,
        latest,
        diagnostics,
    }
}

/// Fit Brosius OLS parameters per link and project ultimates from observed row widths.
///
/// `observed_counts[i]` is the number of observed development periods in row
/// `i`. Each link fit uses rows where both `dev` and `dev + 1` are observed.
pub fn project_observed_counts(
    t: &TriangleView<'_>,
    observed_counts: &[usize],
    tail: f64,
) -> Result<Brosius, ValidationError> {
    validate_observed_count_inputs(t, observed_counts, tail)?;
    let n_origin = t.n_origins;
    let n_dev = t.n_devs;
    let mut latest = vec![0.0; n_origin];
    for i in 0..n_origin {
        latest[i] = t.get(i, observed_counts[i] - 1);
    }

    let mut intercept = vec![0.0; n_dev.saturating_sub(1)];
    let mut slope = vec![0.0; n_dev.saturating_sub(1)];
    let mut diagnostics = Vec::with_capacity(n_dev.saturating_sub(1));
    for dev in 0..n_dev.saturating_sub(1) {
        let mut k = 0usize;
        let mut first_origin = 0usize;
        let mut sx = 0.0;
        let mut sy = 0.0;
        for (origin, &count) in observed_counts.iter().enumerate() {
            if count > dev + 1 {
                if k == 0 {
                    first_origin = origin;
                }
                k += 1;
                sx += t.get(origin, dev);
                sy += t.get(origin, dev + 1);
            }
        }

        let kf = k as f64;
        let mut mean_x = f64::NAN;
        let mut mean_y = f64::NAN;
        let mut var_x = f64::NAN;
        let mut cov_xy = f64::NAN;
        let mut fallback_used = false;
        if k >= 2 {
            mean_x = sx / kf;
            mean_y = sy / kf;
            let mut sum_dx2 = 0.0;
            let mut sum_dxdy = 0.0;
            for (origin, &count) in observed_counts.iter().enumerate() {
                if count > dev + 1 {
                    let dx = t.get(origin, dev) - mean_x;
                    let dy = t.get(origin, dev + 1) - mean_y;
                    sum_dx2 += dx * dx;
                    sum_dxdy += dx * dy;
                }
            }
            var_x = sum_dx2 / kf;
            cov_xy = sum_dxdy / kf;
            let var_tol = 1e-12 * mean_x * mean_x;
            let b = if var_x > var_tol { cov_xy / var_x } else { 0.0 };
            let a = mean_y - b * mean_x;
            intercept[dev] = a;
            slope[dev] = b;
        } else if k == 1 {
            let x = t.get(first_origin, dev);
            let y = t.get(first_origin, dev + 1);
            intercept[dev] = 0.0;
            slope[dev] = if x > 0.0 { y / x } else { f64::NAN };
            mean_x = x;
            mean_y = y;
            var_x = 0.0;
            cov_xy = 0.0;
            fallback_used = true;
        } else {
            intercept[dev] = f64::NAN;
            slope[dev] = f64::NAN;
        }
        diagnostics.push(BrosiusLinkDiagnostic {
            dev,
            n: k,
            mean_x,
            mean_y,
            var_x,
            cov_xy,
            intercept: intercept[dev],
            slope: slope[dev],
            fallback_used,
        });
    }

    let mut ultimate = vec![0.0; n_origin];
    let mut reserve = vec![0.0; n_origin];
    for i in 0..n_origin {
        let mut c = latest[i];
        for dev in (observed_counts[i] - 1)..n_dev.saturating_sub(1) {
            c = intercept[dev] + slope[dev] * c;
        }
        c *= tail;
        ultimate[i] = c;
        reserve[i] = c - latest[i];
    }

    let projection = Brosius {
        intercept,
        slope,
        ultimate,
        reserve,
        latest,
        diagnostics,
    };
    validate_finite_slice(&projection.intercept)?;
    validate_finite_slice(&projection.slope)?;
    validate_finite_slice(&projection.ultimate)?;
    validate_finite_slice(&projection.reserve)?;
    Ok(projection)
}

/// Fit Brosius OLS parameters per link and project ultimates after validation.
///
/// The checked path enforces square-triangle shape, finite non-negative
/// observed cells, monotone cumulative rows, `tail >= 1`, and finite projected
/// parameters and outputs.
pub fn project(t: &TriangleView<'_>, tail: f64) -> Result<Brosius, ValidationError> {
    project_with_policy(t, tail, MonotonicityPolicy::Strict)
}

/// Fit Brosius OLS parameters per link and project ultimates under an explicit
/// [`MonotonicityPolicy`].
pub fn project_with_policy(
    t: &TriangleView<'_>,
    tail: f64,
    policy: MonotonicityPolicy,
) -> Result<Brosius, ValidationError> {
    validate_triangle(t)?;
    if policy == MonotonicityPolicy::Strict {
        validate_monotone_cumulative(t)?;
    }
    validate_tail(tail)?;

    let projection = project_unchecked(t, tail);
    validate_finite_slice(&projection.intercept)?;
    validate_finite_slice(&projection.slope)?;
    validate_finite_slice(&projection.latest)?;
    validate_finite_slice(&projection.ultimate)?;
    validate_finite_slice(&projection.reserve)?;
    for diagnostic in &projection.diagnostics {
        if !diagnostic.mean_x.is_finite()
            || !diagnostic.mean_y.is_finite()
            || !diagnostic.var_x.is_finite()
            || !diagnostic.cov_xy.is_finite()
            || !diagnostic.intercept.is_finite()
            || !diagnostic.slope.is_finite()
        {
            return Err(ValidationError::NotFinite);
        }
    }
    Ok(projection)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::Triangle;

    #[test]
    fn project_rejects_non_cumulative_triangle() {
        #[rustfmt::skip]
        let triangle = Triangle::new(3, 3, vec![
            100.0, 150.0, 180.0,
            120.0, 110.0, f64::NAN,
            140.0, f64::NAN, f64::NAN,
        ]);

        assert!(matches!(
            project(&triangle.view(), 1.0),
            Err(ValidationError::NonMonotoneCumulative {
                origin: 1,
                dev: 1,
                previous: 120.0,
                value: 110.0,
            })
        ));
    }

    #[test]
    fn project_rejects_invalid_tail() {
        #[rustfmt::skip]
        let triangle = Triangle::new(3, 3, vec![
            100.0, 150.0, 180.0,
            120.0, 180.0, f64::NAN,
            140.0, f64::NAN, f64::NAN,
        ]);

        assert!(matches!(
            project(&triangle.view(), 0.99),
            Err(ValidationError::InvalidTail { tail: 0.99 })
        ));
    }

    #[test]
    fn project_returns_link_diagnostics() {
        #[rustfmt::skip]
        let triangle = Triangle::new(3, 3, vec![
            100.0, 150.0, 180.0,
            120.0, 180.0, f64::NAN,
            140.0, f64::NAN, f64::NAN,
        ]);

        let projection = project(&triangle.view(), 1.0).expect("brosius");

        assert_eq!(projection.diagnostics.len(), 2);
        assert_eq!(projection.diagnostics[0].n, 2);
        assert!(!projection.diagnostics[0].fallback_used);
        assert_eq!(projection.diagnostics[1].n, 1);
        assert!(projection.diagnostics[1].fallback_used);
    }

    #[test]
    fn project_observed_counts_projects_rectangular_triangle() {
        #[rustfmt::skip]
        let data = vec![
            100.0, 150.0, 180.0, 198.0,
            120.0, 180.0, 216.0, f64::NAN,
            140.0, 210.0, f64::NAN, f64::NAN,
        ];
        let view = TriangleView {
            n_origins: 3,
            n_devs: 4,
            data: &data,
        };

        let projection =
            project_observed_counts(&view, &[4, 3, 2], 1.0).expect("brosius rectangular");

        assert_eq!(projection.diagnostics.len(), 3);
        assert_eq!(projection.reserve.len(), 3);
        assert!(projection.ultimate[0].is_finite());
        assert!(projection.ultimate[2] > projection.latest[2]);
        assert!(projection.diagnostics[2].fallback_used);
    }
}