reserving-kernels 0.2.2

Pure Rust numeric kernels for P&C actuarial reserving and reserve-risk 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Typed actuarial exhibit DTOs over the reserving kernels.
//!
//! This module reshapes checked kernel outputs into stable, serializable
//! structures for tables, heatmaps, and method-comparison charts. It does not
//! reimplement reserving math.

use serde::Serialize;

use crate::bf::{BornhuetterFergusonEngine, BornhuetterFergusonProjection};
use crate::brosius::{self, Brosius};
use crate::cape_cod::{CapeCodEngine, CapeCodProjection};
use crate::chain_ladder::ChainLadderEngine;
use crate::ldf::{DevelopmentFactorEngine, DevelopmentFactors};
use crate::mack::{self, Mack};
use crate::triangle::cumulative_to_incremental;
use crate::types::TriangleView;
use crate::validate::{ValidationError, validate_monotone_cumulative, validate_triangle};

/// Full exhibit payload for a visualization workbench.
#[derive(Debug, Clone, Serialize)]
pub struct ExhibitBundle {
    /// Cumulative triangle heatmap/table exhibit.
    pub triangle: TriangleExhibit,
    /// Incremental triangle heatmap/table exhibit.
    pub incremental_triangle: TriangleExhibit,
    /// Selected factor calculation trail.
    pub factors: FactorExhibit,
    /// Per-origin method comparison table.
    pub projections: ProjectionComparisonExhibit,
    /// Validation and governance messages for display.
    pub validation: ValidationExhibit,
    /// Mack uncertainty exhibit, when Mack calculation succeeds.
    pub mack: Option<MackExhibit>,
    /// Brosius regression exhibit, when Brosius calculation succeeds.
    pub brosius: Option<BrosiusExhibit>,
}

/// Validation status and display messages.
#[derive(Debug, Clone, Serialize)]
pub struct ValidationExhibit {
    /// Whether the exhibit builder accepted the inputs.
    pub ok: bool,
    /// Validation errors suitable for display.
    pub errors: Vec<String>,
    /// Non-blocking governance warnings.
    pub warnings: Vec<String>,
}

/// One triangle cell for heatmaps and dense tables.
#[derive(Debug, Clone, Serialize)]
pub struct TriangleCellDto {
    /// Origin row index.
    pub origin: usize,
    /// Development column index.
    pub dev: usize,
    /// Cell value; `None` represents unobserved/missing values.
    pub value: Option<f64>,
    /// Whether the cell is scheduled observed under square right-triangle geometry.
    pub observed: bool,
    /// Whether the cell is on the latest diagonal.
    pub latest: bool,
    /// Whether this value is a projection rather than observed data.
    pub projected: bool,
}

/// Triangle display payload.
#[derive(Debug, Clone, Serialize)]
pub struct TriangleExhibit {
    /// Origin row count.
    pub n_origins: usize,
    /// Development column count.
    pub n_devs: usize,
    /// Row-major display cells.
    pub cells: Vec<TriangleCellDto>,
}

/// One factor row tying factor selection, CDF, maturity, and credibility.
#[derive(Debug, Clone, Serialize)]
pub struct FactorExhibitRow {
    /// Development age `j` for the `j -> j + 1` link.
    pub dev: usize,
    /// Sum of observed `C[i, j + 1]`.
    pub numerator: f64,
    /// Sum of observed `C[i, j]`.
    pub denominator: f64,
    /// Selected LDF.
    pub selected_ldf: f64,
    /// CDF at development age `j`.
    pub cdf: f64,
    /// Percent mature/reported at development age `j`.
    pub maturity: f64,
    /// Number of rows included in the link ratio.
    pub included_rows: usize,
}

/// Factor exhibit payload.
#[derive(Debug, Clone, Serialize)]
pub struct FactorExhibit {
    /// Factor rows by development age.
    pub rows: Vec<FactorExhibitRow>,
    /// Tail factor used in the CDFs.
    pub tail: f64,
}

/// One origin row comparing reserving methods.
#[derive(Debug, Clone, Serialize)]
pub struct ProjectionRow {
    /// Origin row index.
    pub origin: usize,
    /// Latest observed development index.
    pub latest_dev: usize,
    /// Latest observed cumulative value.
    pub latest: f64,
    /// Percent mature/reported at latest observed development.
    pub maturity: Option<f64>,
    /// Chain-ladder ultimate.
    pub chain_ladder_ultimate: Option<f64>,
    /// Chain-ladder reserve.
    pub chain_ladder_reserve: Option<f64>,
    /// BF expected ultimate.
    pub bf_expected: Option<f64>,
    /// BF ultimate.
    pub bf_ultimate: Option<f64>,
    /// BF reserve.
    pub bf_reserve: Option<f64>,
    /// Cape Cod expected ultimate.
    pub cape_cod_expected: Option<f64>,
    /// Cape Cod ultimate.
    pub cape_cod_ultimate: Option<f64>,
    /// Cape Cod reserve.
    pub cape_cod_reserve: Option<f64>,
    /// Mack ultimate.
    pub mack_ultimate: Option<f64>,
    /// Mack reserve.
    pub mack_reserve: Option<f64>,
    /// Mack standard error.
    pub mack_se: Option<f64>,
    /// Mack coefficient of variation.
    pub mack_cv: Option<f64>,
    /// Brosius ultimate.
    pub brosius_ultimate: Option<f64>,
    /// Brosius reserve.
    pub brosius_reserve: Option<f64>,
}

/// Projection totals across methods.
#[derive(Debug, Clone, Serialize)]
pub struct ProjectionTotals {
    /// Total latest observed loss.
    pub latest: f64,
    /// Chain-ladder reserve total.
    pub chain_ladder_reserve: Option<f64>,
    /// BF reserve total.
    pub bf_reserve: Option<f64>,
    /// Cape Cod reserve total.
    pub cape_cod_reserve: Option<f64>,
    /// Mack reserve total.
    pub mack_reserve: Option<f64>,
    /// Brosius reserve total.
    pub brosius_reserve: Option<f64>,
}

/// Method comparison display payload.
#[derive(Debug, Clone, Serialize)]
pub struct ProjectionComparisonExhibit {
    /// Per-origin method rows.
    pub rows: Vec<ProjectionRow>,
    /// Portfolio totals.
    pub totals: ProjectionTotals,
}

/// Mack uncertainty display payload.
#[derive(Debug, Clone, Serialize)]
pub struct MackExhibit {
    /// Origin rows with reserve uncertainty.
    pub rows: Vec<MackOriginRiskRow>,
    /// Sigma squared by development link.
    pub sigma_rows: Vec<MackSigmaRow>,
    /// Total reserve.
    pub total_reserve: f64,
    /// Total standard error.
    pub total_standard_error: f64,
    /// Total coefficient of variation.
    pub total_cv: f64,
    /// Normal-approximation portfolio quantiles.
    pub quantiles: MackQuantiles,
    /// Tail uncertainty disclosure for display.
    pub tail_disclosure: String,
}

/// Per-origin Mack uncertainty row.
#[derive(Debug, Clone, Serialize)]
pub struct MackOriginRiskRow {
    /// Origin row index.
    pub origin: usize,
    /// Mack reserve.
    pub reserve: f64,
    /// Mack standard error.
    pub standard_error: f64,
    /// Mack coefficient of variation.
    pub cv: f64,
    /// Reserve minus one standard error.
    pub reserve_minus_1se: f64,
    /// Reserve plus one standard error.
    pub reserve_plus_1se: f64,
}

/// Mack sigma-squared row by development link.
#[derive(Debug, Clone, Serialize)]
pub struct MackSigmaRow {
    /// Development link start index.
    pub dev: usize,
    /// Mack sigma squared.
    pub sigma_sq: f64,
}

/// Normal-approximation Mack reserve quantiles.
#[derive(Debug, Clone, Serialize)]
pub struct MackQuantiles {
    /// 50th percentile.
    pub p50: f64,
    /// 75th percentile.
    pub p75: f64,
    /// 90th percentile.
    pub p90: f64,
    /// 95th percentile.
    pub p95: f64,
}

/// Brosius regression display payload.
#[derive(Debug, Clone, Serialize)]
pub struct BrosiusExhibit {
    /// Link fit diagnostics.
    pub links: Vec<BrosiusLinkExhibitRow>,
    /// Scatterplot source points by link.
    pub scatter_points: Vec<BrosiusScatterPoint>,
}

/// One Brosius link fit row.
#[derive(Debug, Clone, Serialize)]
pub struct BrosiusLinkExhibitRow {
    /// Development link start index.
    pub dev: usize,
    /// Number of observed pairs used.
    pub n: usize,
    /// Fitted intercept.
    pub intercept: f64,
    /// Fitted slope.
    pub slope: f64,
    /// Chain-ladder LDF for comparison.
    pub chain_ladder_ldf: Option<f64>,
    /// Whether the single-pair fallback was used.
    pub fallback_used: bool,
}

/// One observed point for a Brosius scatter/fitted-line chart.
#[derive(Debug, Clone, Serialize)]
pub struct BrosiusScatterPoint {
    /// Development link start index.
    pub dev: usize,
    /// Origin row index.
    pub origin: usize,
    /// Prior cumulative value.
    pub x: f64,
    /// Next cumulative value.
    pub y: f64,
    /// Fitted next value.
    pub fitted_y: f64,
}

/// Build a full exhibit bundle from a triangle and optional method inputs.
///
/// `premium` enables Cape Cod. `expected_ultimate` enables BF. Chain ladder,
/// Mack, and Brosius are always attempted after shared validation succeeds.
pub fn build_exhibit_bundle(
    triangle: &TriangleView<'_>,
    premium: Option<&[f64]>,
    expected_ultimate: Option<&[f64]>,
    tail: f64,
) -> Result<ExhibitBundle, ValidationError> {
    validate_triangle(triangle)?;
    validate_monotone_cumulative(triangle)?;

    let factors = DevelopmentFactorEngine::volume_weighted(triangle, tail)?;
    let factor_diagnostics = DevelopmentFactorEngine::volume_weighted_diagnostics(triangle, tail)?;
    let chain_ladder = ChainLadderEngine::project(triangle, &factors)?;
    let bf = match expected_ultimate {
        Some(expected) => Some(BornhuetterFergusonEngine::project_from_expected_ultimate(
            triangle, &factors, expected,
        )?),
        None => None,
    };
    let cape_cod = match premium {
        Some(premium) => Some(CapeCodEngine::project(triangle, &factors, premium)?),
        None => None,
    };
    let mack = mack::analyze(triangle, tail)?;
    let brosius = brosius::project(triangle, tail)?;

    let triangle_exhibit = triangle_exhibit(triangle);
    let incremental_triangle = incremental_triangle_exhibit(triangle);
    let factor_exhibit = factor_exhibit(&factors, &factor_diagnostics);
    let projections = projection_comparison_exhibit(
        &factors,
        &chain_ladder,
        bf.as_ref(),
        cape_cod.as_ref(),
        &mack,
        &brosius,
    );
    let mack_exhibit = mack_exhibit(&mack, tail);
    let brosius_exhibit = brosius_exhibit(triangle, &factors, &brosius);

    let mut warnings = Vec::new();
    if tail > 1.0 {
        warnings.push(
            "Tail factor exceeds 1.0; Mack standard errors do not include tail uncertainty."
                .to_string(),
        );
    }
    if premium.is_none() {
        warnings.push("Cape Cod exhibit omitted because premium was not supplied.".to_string());
    }
    if expected_ultimate.is_none() {
        warnings.push("BF exhibit omitted because expected ultimate was not supplied.".to_string());
    }

    Ok(ExhibitBundle {
        triangle: triangle_exhibit,
        incremental_triangle,
        factors: factor_exhibit,
        projections,
        validation: ValidationExhibit {
            ok: true,
            errors: Vec::new(),
            warnings,
        },
        mack: Some(mack_exhibit),
        brosius: Some(brosius_exhibit),
    })
}

fn triangle_exhibit(triangle: &TriangleView<'_>) -> TriangleExhibit {
    triangle_exhibit_from_data(triangle.n_origins, triangle.n_devs, triangle.data, false)
}

fn incremental_triangle_exhibit(triangle: &TriangleView<'_>) -> TriangleExhibit {
    let incremental = cumulative_to_incremental(triangle);
    triangle_exhibit_from_data(triangle.n_origins, triangle.n_devs, &incremental, false)
}

fn triangle_exhibit_from_data(
    n_origins: usize,
    n_devs: usize,
    data: &[f64],
    projected: bool,
) -> TriangleExhibit {
    let mut cells = Vec::with_capacity(n_origins * n_devs);
    for origin in 0..n_origins {
        for dev in 0..n_devs {
            let observed = origin + dev <= n_devs.saturating_sub(1);
            let latest = observed && origin + dev == n_devs.saturating_sub(1);
            let raw = data[origin * n_devs + dev];
            cells.push(TriangleCellDto {
                origin,
                dev,
                value: raw.is_finite().then_some(raw),
                observed,
                latest,
                projected,
            });
        }
    }

    TriangleExhibit {
        n_origins,
        n_devs,
        cells,
    }
}

fn factor_exhibit(
    factors: &DevelopmentFactors,
    diagnostics: &[crate::ldf::LinkRatioDiagnostic],
) -> FactorExhibit {
    let maturity = factors.maturity_percentages();
    let rows = diagnostics
        .iter()
        .map(|diagnostic| FactorExhibitRow {
            dev: diagnostic.dev,
            numerator: diagnostic.numerator,
            denominator: diagnostic.denominator,
            selected_ldf: diagnostic.selected_ldf,
            cdf: factors.cdfs[diagnostic.dev],
            maturity: maturity[diagnostic.dev],
            included_rows: diagnostic.included_rows,
        })
        .collect();

    FactorExhibit {
        rows,
        tail: factors.tail,
    }
}

fn projection_comparison_exhibit(
    factors: &DevelopmentFactors,
    chain_ladder: &crate::chain_ladder::ChainLadderProjection,
    bf: Option<&BornhuetterFergusonProjection>,
    cape_cod: Option<&CapeCodProjection>,
    mack: &Mack,
    brosius: &Brosius,
) -> ProjectionComparisonExhibit {
    let factor_maturity = factors.maturity_percentages();
    let rows = chain_ladder
        .latest
        .iter()
        .enumerate()
        .map(|(origin, &latest)| {
            let latest_dev = chain_ladder.latest_dev[origin];
            ProjectionRow {
                origin,
                latest_dev,
                latest,
                maturity: bf
                    .map(|projection| projection.maturity[origin])
                    .or_else(|| cape_cod.map(|projection| projection.reported_percentage[origin]))
                    .or_else(|| factor_maturity.get(latest_dev).copied()),
                chain_ladder_ultimate: Some(chain_ladder.ultimates[origin]),
                chain_ladder_reserve: Some(chain_ladder.reserves[origin]),
                bf_expected: bf.map(|projection| projection.expected_ultimate[origin]),
                bf_ultimate: bf.map(|projection| projection.ultimates[origin]),
                bf_reserve: bf.map(|projection| projection.reserves[origin]),
                cape_cod_expected: cape_cod.map(|projection| projection.expected_ultimate[origin]),
                cape_cod_ultimate: cape_cod
                    .map(|projection| projection.bf_projection.ultimates[origin]),
                cape_cod_reserve: cape_cod
                    .map(|projection| projection.bf_projection.reserves[origin]),
                mack_ultimate: Some(mack.ultimate[origin]),
                mack_reserve: Some(mack.reserve[origin]),
                mack_se: Some(mack.standard_error[origin]),
                mack_cv: Some(mack.cv[origin]),
                brosius_ultimate: Some(brosius.ultimate[origin]),
                brosius_reserve: Some(brosius.reserve[origin]),
            }
        })
        .collect();

    ProjectionComparisonExhibit {
        rows,
        totals: ProjectionTotals {
            latest: chain_ladder.total_latest,
            chain_ladder_reserve: Some(chain_ladder.total_reserve),
            bf_reserve: bf.map(|projection| projection.total_reserve),
            cape_cod_reserve: cape_cod.map(|projection| projection.bf_projection.total_reserve),
            mack_reserve: Some(mack.total_reserve),
            brosius_reserve: Some(brosius.reserve.iter().sum()),
        },
    }
}

fn mack_exhibit(mack: &Mack, tail: f64) -> MackExhibit {
    let rows = mack
        .reserve
        .iter()
        .enumerate()
        .map(|(origin, &reserve)| {
            let standard_error = mack.standard_error[origin];
            MackOriginRiskRow {
                origin,
                reserve,
                standard_error,
                cv: mack.cv[origin],
                reserve_minus_1se: reserve - standard_error,
                reserve_plus_1se: reserve + standard_error,
            }
        })
        .collect();
    let sigma_rows = mack
        .sigma_sq
        .iter()
        .enumerate()
        .map(|(dev, &sigma_sq)| MackSigmaRow { dev, sigma_sq })
        .collect();

    MackExhibit {
        rows,
        sigma_rows,
        total_reserve: mack.total_reserve,
        total_standard_error: mack.total_standard_error,
        total_cv: mack.total_cv,
        quantiles: MackQuantiles {
            p50: mack.total_reserve,
            p75: mack.total_reserve + 0.674_489_750_196_081_7 * mack.total_standard_error,
            p90: mack.total_reserve + 1.281_551_565_544_600_4 * mack.total_standard_error,
            p95: mack.total_reserve + 1.644_853_626_951_472_2 * mack.total_standard_error,
        },
        tail_disclosure: if tail > 1.0 {
            "Mack normal approximation; tail uncertainty is not modeled.".to_string()
        } else {
            "Mack normal approximation.".to_string()
        },
    }
}

fn brosius_exhibit(
    triangle: &TriangleView<'_>,
    factors: &DevelopmentFactors,
    brosius: &Brosius,
) -> BrosiusExhibit {
    let links = brosius
        .diagnostics
        .iter()
        .map(|diagnostic| BrosiusLinkExhibitRow {
            dev: diagnostic.dev,
            n: diagnostic.n,
            intercept: diagnostic.intercept,
            slope: diagnostic.slope,
            chain_ladder_ldf: factors.ldfs.get(diagnostic.dev).copied(),
            fallback_used: diagnostic.fallback_used,
        })
        .collect();

    let mut scatter_points = Vec::new();
    for diagnostic in &brosius.diagnostics {
        for origin in 0..diagnostic.n {
            let x = triangle.get(origin, diagnostic.dev);
            let y = triangle.get(origin, diagnostic.dev + 1);
            scatter_points.push(BrosiusScatterPoint {
                dev: diagnostic.dev,
                origin,
                x,
                y,
                fitted_y: diagnostic.intercept + diagnostic.slope * x,
            });
        }
    }

    BrosiusExhibit {
        links,
        scatter_points,
    }
}

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

    fn sample_triangle() -> Triangle {
        #[rustfmt::skip]
        let data = vec![
            1000.0, 1500.0, 1700.0, 1800.0,
            1200.0, 1800.0, 2000.0, f64::NAN,
            1500.0, 2100.0, f64::NAN, f64::NAN,
            1800.0, f64::NAN, f64::NAN, f64::NAN,
        ];
        Triangle::new(4, 4, data)
    }

    #[test]
    fn bundle_contains_core_exhibits() {
        let triangle = sample_triangle();
        let premium = [4000.0, 4500.0, 5000.0, 5500.0];
        let expected = [2500.0, 2750.0, 3000.0, 3250.0];

        let bundle = build_exhibit_bundle(&triangle.view(), Some(&premium), Some(&expected), 1.0)
            .expect("bundle");

        assert!(bundle.validation.ok);
        assert_eq!(bundle.triangle.cells.len(), 16);
        assert_eq!(bundle.incremental_triangle.cells.len(), 16);
        assert_eq!(bundle.factors.rows.len(), 3);
        assert_eq!(bundle.projections.rows.len(), 4);
        assert!(bundle.projections.totals.chain_ladder_reserve.is_some());
        assert!(bundle.projections.totals.bf_reserve.is_some());
        assert!(bundle.projections.totals.cape_cod_reserve.is_some());
        assert!(bundle.mack.is_some());
        assert!(bundle.brosius.is_some());
    }

    #[test]
    fn triangle_exhibit_marks_latest_and_unobserved_cells() {
        let triangle = sample_triangle();

        let exhibit = triangle_exhibit(&triangle.view());

        let latest_cells = exhibit.cells.iter().filter(|cell| cell.latest).count();
        let unobserved_cells = exhibit.cells.iter().filter(|cell| !cell.observed).count();
        assert_eq!(latest_cells, 4);
        assert_eq!(unobserved_cells, 6);
        assert_eq!(exhibit.cells[7].value, None);
    }

    #[test]
    fn bundle_allows_optional_bf_and_cape_cod_inputs() {
        let triangle = sample_triangle();

        let bundle = build_exhibit_bundle(&triangle.view(), None, None, 1.0).expect("bundle");

        assert!(
            bundle
                .projections
                .rows
                .iter()
                .all(|row| row.bf_reserve.is_none())
        );
        assert!(
            bundle
                .projections
                .rows
                .iter()
                .all(|row| row.cape_cod_reserve.is_none())
        );
        assert_eq!(bundle.validation.warnings.len(), 2);
    }
}