Skip to main content

reserving_kernels/excess/
excess_development.rs

1//! Excess-of-loss and limited-loss development factors.
2//!
3//! Implements:
4//!
5//! * Pinto & Gogol (1987), *An Analysis of Excess Loss Development*, Proceedings
6//!   of the Casualty Actuarial Society (PCAS), Vol. LXXIV, pp. 227–255.
7//! * Siewert (1996), *A Model for Reserving Workers Compensation High
8//!   Deductibles*, CAS Forum (1996), pp. 217–244.
9//!
10//! Bridges ground-up loss development ([`crate::deterministic::ldf`]) and parametric severity
11//! excess ratios ([`crate::excess::severity`], [`crate::excess::excess_layers`]).
12//!
13//! Given ground-up cumulative development factors $\text{CDF}_j^{\text{GU}}$ and
14//! age-dependent excess ratios $R_j(L) = \frac{E[(X_j - L)_+]}{E\[X_j\]}$ (with
15//! ultimate excess ratio $R_\infty(L)$):
16//!
17//! $$\text{CDF}_j^{\text{xs}}(L) = \text{CDF}_j^{\text{GU}} \cdot \frac{R_\infty(L)}{R_j(L)}$$
18//!
19//! $$\text{CDF}_j^{\text{lim}}(L) = \text{CDF}_j^{\text{GU}} \cdot \frac{1 - R_\infty(L)}{1 - R_j(L)}$$
20//!
21//! For any retention $L$ and maturity $j$, these satisfy the exact conservation
22//! identity:
23//!
24//! $$C_j^{\text{lim}}(L) \cdot \text{CDF}_j^{\text{lim}}(L) + C_j^{\text{xs}}(L) \cdot \text{CDF}_j^{\text{xs}}(L) = C_j^{\text{GU}} \cdot \text{CDF}_j^{\text{GU}}$$
25//!
26//! whenever $C_j^{\text{lim}}(L) = (1 - R_j(L)) C_j^{\text{GU}}$ and
27//! $C_j^{\text{xs}}(L) = R_j(L) C_j^{\text{GU}}$.
28
29use crate::excess::severity::SeverityDistribution;
30use crate::validate::{ValidationError, validate_positive_finite_slice};
31
32/// Excess and limited development factors derived at a single retention $L$.
33#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
34pub struct ExcessDevelopmentFactors {
35    /// Retention / limit threshold $L$.
36    pub limit: f64,
37    /// Ground-up CDFs per development age, length `m`.
38    pub ground_up_cdf: Vec<f64>,
39    /// Excess ratios $R_j(L) \in (0, 1)$ per development age, length `m`.
40    pub excess_ratio: Vec<f64>,
41    /// Ultimate excess ratio $R_\infty(L) \in (0, 1)$.
42    pub ultimate_excess_ratio: f64,
43    /// Excess-of-$L$ CDFs $\text{CDF}_j^{\text{xs}}(L)$, length `m`.
44    pub excess_cdf: Vec<f64>,
45    /// Limited-to-$L$ CDFs $\text{CDF}_j^{\text{lim}}(L)$, length `m`.
46    pub limited_cdf: Vec<f64>,
47    /// Implied age-to-age excess LDFs (`excess_cdf[j] / excess_cdf[j+1]`), length `m - 1`.
48    pub excess_ldf: Vec<f64>,
49    /// Implied age-to-age limited LDFs (`limited_cdf[j] / limited_cdf[j+1]`), length `m - 1`.
50    pub limited_ldf: Vec<f64>,
51    /// Excess-to-ground-up relativity $R_\infty(L) / R_j(L)$ per age, length `m`.
52    pub excess_relativity: Vec<f64>,
53}
54
55/// Bounded layer $(A, A + W]$ development factors derived from two excess-ratio schedules.
56#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
57pub struct LayerDevelopmentFactors {
58    /// Lower attachment point $A$.
59    pub attachment: f64,
60    /// Layer width $W > 0$ (exhaustion $E = A + W$).
61    pub limit: f64,
62    /// Ground-up CDFs per development age, length `m`.
63    pub ground_up_cdf: Vec<f64>,
64    /// Layer loss share $R_j(A) - R_j(A+W)$ per development age, length `m`.
65    pub layer_share: Vec<f64>,
66    /// Ultimate layer loss share $R_\infty(A) - R_\infty(A+W)$.
67    pub ultimate_layer_share: f64,
68    /// Layer CDFs per development age, length `m`.
69    pub layer_cdf: Vec<f64>,
70    /// Implied age-to-age layer LDFs, length `m - 1`.
71    pub layer_ldf: Vec<f64>,
72}
73
74/// Projected ground-up, limited, and excess ultimates across origins.
75#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
76pub struct ExcessProjection {
77    /// Ground-up ultimate per origin.
78    pub ground_up_ultimate: Vec<f64>,
79    /// Limited-to-$L$ ultimate per origin.
80    pub limited_ultimate: Vec<f64>,
81    /// Excess-of-$L$ ultimate per origin.
82    pub excess_ultimate: Vec<f64>,
83    /// Limited-to-$L$ reserve per origin.
84    pub limited_reserve: Vec<f64>,
85    /// Excess-of-$L$ reserve per origin.
86    pub excess_reserve: Vec<f64>,
87    /// Maximum conservation discrepancy `|(lim_ult + xs_ult) - gu_ult|` across origins.
88    pub max_conservation_error: f64,
89}
90
91fn cdfs_to_ldfs(cdfs: &[f64]) -> Vec<f64> {
92    if cdfs.len() < 2 {
93        return Vec::new();
94    }
95    (0..cdfs.len() - 1).map(|j| cdfs[j] / cdfs[j + 1]).collect()
96}
97
98/// Derive excess-of-$L$ and limited-to-$L$ CDFs and LDFs from ground-up CDFs
99/// and age-dependent excess ratios (Siewert 1996 / Pinto & Gogol 1987).
100///
101/// `ground_up_cdf[j]` is the ground-up CDF at maturity index `j`,
102/// `excess_ratios[j]` is $R_j(L) \in (0, 1)$, and `ultimate_excess_ratio` is
103/// $R_\infty(L) \in (0, 1)$.
104pub fn from_excess_ratios(
105    limit: f64,
106    ground_up_cdf: &[f64],
107    excess_ratios: &[f64],
108    ultimate_excess_ratio: f64,
109) -> Result<ExcessDevelopmentFactors, ValidationError> {
110    if !limit.is_finite() || limit < 0.0 {
111        return Err(ValidationError::InvalidParameter(
112            "limit must be finite and non-negative",
113        ));
114    }
115    if ground_up_cdf.is_empty() || ground_up_cdf.len() != excess_ratios.len() {
116        return Err(ValidationError::ShapeMismatch);
117    }
118    validate_positive_finite_slice(ground_up_cdf, "ground_up_cdf")?;
119    if !ultimate_excess_ratio.is_finite()
120        || ultimate_excess_ratio <= 0.0
121        || ultimate_excess_ratio >= 1.0
122    {
123        return Err(ValidationError::InvalidParameter(
124            "ultimate_excess_ratio must be strictly in (0, 1)",
125        ));
126    }
127    for &r in excess_ratios {
128        if !r.is_finite() || r <= 0.0 || r >= 1.0 {
129            return Err(ValidationError::InvalidParameter(
130                "each excess ratio must be strictly in (0, 1)",
131            ));
132        }
133    }
134
135    let m = ground_up_cdf.len();
136    let mut excess_cdf = Vec::with_capacity(m);
137    let mut limited_cdf = Vec::with_capacity(m);
138    let mut excess_relativity = Vec::with_capacity(m);
139
140    for j in 0..m {
141        let rel = ultimate_excess_ratio / excess_ratios[j];
142        let lim_rel = (1.0 - ultimate_excess_ratio) / (1.0 - excess_ratios[j]);
143        excess_relativity.push(rel);
144        excess_cdf.push(ground_up_cdf[j] * rel);
145        limited_cdf.push(ground_up_cdf[j] * lim_rel);
146    }
147
148    let excess_ldf = cdfs_to_ldfs(&excess_cdf);
149    let limited_ldf = cdfs_to_ldfs(&limited_cdf);
150
151    Ok(ExcessDevelopmentFactors {
152        limit,
153        ground_up_cdf: ground_up_cdf.to_vec(),
154        excess_ratio: excess_ratios.to_vec(),
155        ultimate_excess_ratio,
156        excess_cdf,
157        limited_cdf,
158        excess_ldf,
159        limited_ldf,
160        excess_relativity,
161    })
162}
163
164/// Derive excess-of-$L$ and limited-to-$L$ development factors directly from
165/// age-indexed parametric [`SeverityDistribution`] models.
166///
167/// Computes $R_j(L) = \text{dist}_j.\text{excess\_ratio}(L)$ and
168/// $R_\infty(L) = \text{ultimate\_dist}.\text{excess\_ratio}(L)$.
169pub fn from_severity_schedule(
170    limit: f64,
171    ground_up_cdf: &[f64],
172    age_severities: &[SeverityDistribution],
173    ultimate_severity: &SeverityDistribution,
174) -> Result<ExcessDevelopmentFactors, ValidationError> {
175    if ground_up_cdf.len() != age_severities.len() {
176        return Err(ValidationError::ShapeMismatch);
177    }
178    let mut ratios = Vec::with_capacity(age_severities.len());
179    for dist in age_severities {
180        ratios.push(dist.excess_ratio(limit)?);
181    }
182    let ult_ratio = ultimate_severity.excess_ratio(limit)?;
183    from_excess_ratios(limit, ground_up_cdf, &ratios, ult_ratio)
184}
185
186/// Derive development factors for a bounded excess layer $(A, A + W]$ from
187/// age-dependent parametric [`SeverityDistribution`] models.
188pub fn layer_from_severity_schedule(
189    attachment: f64,
190    limit: f64,
191    ground_up_cdf: &[f64],
192    age_severities: &[SeverityDistribution],
193    ultimate_severity: &SeverityDistribution,
194) -> Result<LayerDevelopmentFactors, ValidationError> {
195    if !attachment.is_finite() || attachment < 0.0 || !limit.is_finite() || limit <= 0.0 {
196        return Err(ValidationError::InvalidParameter(
197            "attachment must be >= 0 and limit must be > 0",
198        ));
199    }
200    if ground_up_cdf.is_empty() || ground_up_cdf.len() != age_severities.len() {
201        return Err(ValidationError::ShapeMismatch);
202    }
203    validate_positive_finite_slice(ground_up_cdf, "ground_up_cdf")?;
204
205    let exhaustion = attachment + limit;
206    let ult_share =
207        ultimate_severity.excess_ratio(attachment)? - ultimate_severity.excess_ratio(exhaustion)?;
208    if !ult_share.is_finite() || ult_share <= 0.0 {
209        return Err(ValidationError::InvalidParameter(
210            "ultimate layer share must be strictly positive",
211        ));
212    }
213
214    let mut layer_share = Vec::with_capacity(age_severities.len());
215    let mut layer_cdf = Vec::with_capacity(age_severities.len());
216    for (j, dist) in age_severities.iter().enumerate() {
217        let share = dist.excess_ratio(attachment)? - dist.excess_ratio(exhaustion)?;
218        if !share.is_finite() || share <= 0.0 {
219            return Err(ValidationError::InvalidParameter(
220                "age layer share must be strictly positive",
221            ));
222        }
223        layer_share.push(share);
224        layer_cdf.push(ground_up_cdf[j] * (ult_share / share));
225    }
226    let layer_ldf = cdfs_to_ldfs(&layer_cdf);
227
228    Ok(LayerDevelopmentFactors {
229        attachment,
230        limit,
231        ground_up_cdf: ground_up_cdf.to_vec(),
232        layer_share,
233        ultimate_layer_share: ult_share,
234        layer_cdf,
235        layer_ldf,
236    })
237}
238
239/// Project limited and excess ultimates from origin-level latest limited and
240/// excess losses using [`ExcessDevelopmentFactors`].
241///
242/// `latest_limited[i]` and `latest_excess[i]` correspond to origin `i` whose
243/// maturity index is `n - 1 - i` (standard square-triangle convention where
244/// origin 0 is the oldest / most mature).
245pub fn project_split_losses(
246    latest_limited: &[f64],
247    latest_excess: &[f64],
248    factors: &ExcessDevelopmentFactors,
249) -> Result<ExcessProjection, ValidationError> {
250    let n = latest_limited.len();
251    if n == 0 || latest_excess.len() != n || factors.ground_up_cdf.len() != n {
252        return Err(ValidationError::ShapeMismatch);
253    }
254
255    let mut ground_up_ultimate = Vec::with_capacity(n);
256    let mut limited_ultimate = Vec::with_capacity(n);
257    let mut excess_ultimate = Vec::with_capacity(n);
258    let mut limited_reserve = Vec::with_capacity(n);
259    let mut excess_reserve = Vec::with_capacity(n);
260    let mut max_err = 0.0_f64;
261
262    for i in 0..n {
263        let age = n - 1 - i;
264        let lim_lat = latest_limited[i];
265        let xs_lat = latest_excess[i];
266        if !lim_lat.is_finite() || !xs_lat.is_finite() || lim_lat < 0.0 || xs_lat < 0.0 {
267            return Err(ValidationError::InvalidParameter(
268                "latest losses must be finite and non-negative",
269            ));
270        }
271        let gu_lat = lim_lat + xs_lat;
272        let gu_ult = gu_lat * factors.ground_up_cdf[age];
273        let lim_ult = lim_lat * factors.limited_cdf[age];
274        let xs_ult = xs_lat * factors.excess_cdf[age];
275        max_err = max_err.max(((lim_ult + xs_ult) - gu_ult).abs());
276
277        ground_up_ultimate.push(gu_ult);
278        limited_ultimate.push(lim_ult);
279        excess_ultimate.push(xs_ult);
280        limited_reserve.push(lim_ult - lim_lat);
281        excess_reserve.push(xs_ult - xs_lat);
282    }
283
284    Ok(ExcessProjection {
285        ground_up_ultimate,
286        limited_ultimate,
287        excess_ultimate,
288        limited_reserve,
289        excess_reserve,
290        max_conservation_error: max_err,
291    })
292}
293
294/// Pinto & Gogol (1987) power-law excess LDF scaling model:
295///
296/// Given a ground-up LDF $f_j^{\text{GU}} \ge 1$ and excess leverage exponent
297/// $\gamma \ge 1$ (or retention-dependent leverage factor), computes
298/// $\text{LDF}_j^{\text{xs}} = 1 + (f_j^{\text{GU}} - 1) \cdot \text{leverage}_j$,
299/// returning the resulting excess CDFs (accumulated from tail backward).
300pub fn pinto_gogol_power_scale(
301    ground_up_ldf: &[f64],
302    leverage: &[f64],
303    excess_tail: f64,
304) -> Result<Vec<f64>, ValidationError> {
305    if ground_up_ldf.len() != leverage.len() {
306        return Err(ValidationError::ShapeMismatch);
307    }
308    if !excess_tail.is_finite() || excess_tail < 1.0 {
309        return Err(ValidationError::InvalidTail { tail: excess_tail });
310    }
311    let m = ground_up_ldf.len();
312    let mut excess_ldf = Vec::with_capacity(m);
313    for j in 0..m {
314        let f = ground_up_ldf[j];
315        let lev = leverage[j];
316        if !f.is_finite() || f < 1.0 || !lev.is_finite() || lev < 0.0 {
317            return Err(ValidationError::InvalidParameter(
318                "ground_up_ldf must be >= 1 and leverage must be >= 0",
319            ));
320        }
321        excess_ldf.push(1.0 + (f - 1.0) * lev);
322    }
323    let mut cdfs = vec![excess_tail; m + 1];
324    for j in (0..m).rev() {
325        cdfs[j] = cdfs[j + 1] * excess_ldf[j];
326    }
327    Ok(cdfs)
328}
329
330#[cfg(test)]
331mod tests {
332    use super::*;
333
334    #[test]
335    fn exact_conservation_identity_across_all_maturities() {
336        let gu_cdfs = [2.40, 1.65, 1.25, 1.08, 1.00];
337        let excess_ratios = [0.15, 0.22, 0.28, 0.32, 0.35];
338        let ult_ratio = 0.35;
339        let limit = 250_000.0;
340
341        let factors = from_excess_ratios(limit, &gu_cdfs, &excess_ratios, ult_ratio).unwrap();
342
343        // Excess CDFs must exceed ground-up CDFs when excess ratio increases with age.
344        for j in 0..4 {
345            assert!(factors.excess_cdf[j] > factors.ground_up_cdf[j]);
346            assert!(factors.limited_cdf[j] < factors.ground_up_cdf[j]);
347        }
348
349        // Exact conservation: Lim_Ult + XS_Ult == GU_Ult at every origin.
350        let gu_latest = [1_000_000.0, 950_000.0, 900_000.0, 800_000.0, 700_000.0];
351        let mut lim_latest = Vec::new();
352        let mut xs_latest = Vec::new();
353        for i in 0..5 {
354            let age = 4 - i;
355            let r = excess_ratios[age];
356            lim_latest.push(gu_latest[i] * (1.0 - r));
357            xs_latest.push(gu_latest[i] * r);
358        }
359
360        let proj = project_split_losses(&lim_latest, &xs_latest, &factors).unwrap();
361        assert!(proj.max_conservation_error < 1e-8);
362        for i in 0..5 {
363            let sum = proj.limited_ultimate[i] + proj.excess_ultimate[i];
364            assert!((sum - proj.ground_up_ultimate[i]).abs() < 1e-8);
365        }
366    }
367
368    #[test]
369    fn severity_schedule_and_layer_cdfs_are_consistent() {
370        let gu_cdfs = [2.0, 1.4, 1.1, 1.0];
371        // Severity grows with maturity (typical WC/liability claim-size emergence).
372        let dists = [
373            SeverityDistribution::Lognormal {
374                mu: 9.8,
375                sigma: 1.2,
376            },
377            SeverityDistribution::Lognormal {
378                mu: 10.1,
379                sigma: 1.25,
380            },
381            SeverityDistribution::Lognormal {
382                mu: 10.3,
383                sigma: 1.28,
384            },
385            SeverityDistribution::Lognormal {
386                mu: 10.4,
387                sigma: 1.3,
388            },
389        ];
390        let ult = &dists[3];
391
392        let xs = from_severity_schedule(100_000.0, &gu_cdfs, &dists, ult).unwrap();
393        assert!(xs.excess_cdf[0] > gu_cdfs[0]);
394        assert!((xs.excess_cdf[3] - 1.0).abs() < 1e-12);
395
396        let layer =
397            layer_from_severity_schedule(100_000.0, 400_000.0, &gu_cdfs, &dists, ult).unwrap();
398        assert!(layer.layer_cdf[0] > gu_cdfs[0]);
399        assert!((layer.layer_cdf[3] - 1.0).abs() < 1e-12);
400    }
401}