Skip to main content

reserving_kernels/
triangle_ops.rs

1//! Ergonomic reserving method facades on [`Triangle`] and [`TriangleView`].
2//!
3//! Kept separate from [`crate::types`] so `types` remains a pure data/geometry
4//! layer with zero upward dependencies on reserving methods.
5
6use crate::deterministic::benktander::{BenktanderEngine, BenktanderProjection};
7use crate::deterministic::bf::{BornhuetterFergusonEngine, BornhuetterFergusonProjection};
8use crate::deterministic::brosius::{self, Brosius};
9use crate::deterministic::cape_cod::{CapeCodEngine, CapeCodProjection};
10use crate::deterministic::chain_ladder::{ChainLadderEngine, ChainLadderProjection};
11use crate::deterministic::generalized_cape_cod::{
12    GeneralizedCapeCodEngine, GeneralizedCapeCodProjection,
13};
14use crate::deterministic::ldf::{DevelopmentFactorEngine, DevelopmentFactors};
15use crate::diagnostics::triangle_diagnostics::{self, TriangleDiagnostics};
16use crate::preprocessing::exhibit::{ExhibitBundle, build_exhibit_bundle};
17use crate::preprocessing::triangle::{cumulative_to_incremental, latest_diagonal};
18use crate::stochastic::bootstrap_odp::{
19    OdpBootstrapOptions, OdpBootstrapResult, OdpFit, bootstrap_odp, fit_odp,
20};
21use crate::stochastic::clark_mle::{self, ClarkMleResult, ClarkOptions};
22use crate::stochastic::glm_reserving::{self, GlmOptions, GlmReservingResult};
23use crate::stochastic::mack::{self, Mack};
24use crate::stochastic::merz_wuthrich::{self, MerzWuthrichResult};
25use crate::stochastic::ptf_regression::{self, PtfOptions, PtfResult};
26use crate::types::{Triangle, TriangleView};
27use crate::validate::{MonotonicityPolicy, ValidationError};
28
29impl Triangle {
30    /// Extract the latest-observed cumulative anti-diagonal.
31    pub fn latest_diagonal(&self) -> Vec<f64> {
32        latest_diagonal(&self.view())
33    }
34
35    /// Convert this cumulative triangle to an incremental [`Triangle`].
36    pub fn to_incremental(&self) -> Self {
37        let n = self.n();
38        Self::new(n, n, cumulative_to_incremental(&self.view()))
39    }
40
41    /// Volume-weighted development factors and CDFs (`tail = 1.0` for fully developed).
42    pub fn ldfs(&self, tail: f64) -> Result<DevelopmentFactors, ValidationError> {
43        self.view().ldfs(tail)
44    }
45
46    /// Volume-weighted Chain-Ladder projection (`tail = 1.0` for fully developed).
47    pub fn chain_ladder(&self, tail: f64) -> Result<ChainLadderProjection, ValidationError> {
48        self.view().chain_ladder(tail)
49    }
50
51    /// Mack (1993) distribution-free chain-ladder reserve & standard error analysis.
52    pub fn mack(&self, tail: f64) -> Result<Mack, ValidationError> {
53        self.view().mack(tail)
54    }
55
56    /// Mack (1993) analysis under an explicit [`MonotonicityPolicy`].
57    pub fn mack_with_policy(
58        &self,
59        tail: f64,
60        policy: MonotonicityPolicy,
61    ) -> Result<Mack, ValidationError> {
62        self.view().mack_with_policy(tail, policy)
63    }
64
65    /// Merz & Wüthrich (2008) 1-year Solvency II Claims Development Result (CDR) MSEP.
66    pub fn merz_wuthrich(&self) -> Result<MerzWuthrichResult, ValidationError> {
67        self.view().merz_wuthrich()
68    }
69
70    /// Merz & Wüthrich (2008) 1-year CDR MSEP under an explicit [`MonotonicityPolicy`].
71    pub fn merz_wuthrich_with_policy(
72        &self,
73        policy: MonotonicityPolicy,
74    ) -> Result<MerzWuthrichResult, ValidationError> {
75        self.view().merz_wuthrich_with_policy(policy)
76    }
77
78    /// Bornhuetter–Ferguson projection using volume-weighted LDFs.
79    pub fn bf(
80        &self,
81        expected_ultimate: &[f64],
82        tail: f64,
83    ) -> Result<BornhuetterFergusonProjection, ValidationError> {
84        self.view().bf(expected_ultimate, tail)
85    }
86
87    /// Cape Cod projection using volume-weighted LDFs.
88    pub fn cape_cod(
89        &self,
90        premium: &[f64],
91        tail: f64,
92    ) -> Result<CapeCodProjection, ValidationError> {
93        self.view().cape_cod(premium, tail)
94    }
95
96    /// Run England & Verrall / Shapland ODP bootstrap reserve simulation.
97    pub fn bootstrap_odp(
98        &self,
99        tail: f64,
100        options: OdpBootstrapOptions,
101    ) -> Result<OdpBootstrapResult, ValidationError> {
102        self.view().bootstrap_odp(tail, options)
103    }
104
105    /// Build a complete JSON-serializable reserving exhibit bundle.
106    pub fn exhibit(
107        &self,
108        premium: Option<&[f64]>,
109        expected_ultimate: Option<&[f64]>,
110        tail: f64,
111    ) -> Result<ExhibitBundle, ValidationError> {
112        self.view().exhibit(premium, expected_ultimate, tail)
113    }
114
115    /// Benktander (1976) / Hovinen credibility projection using volume-weighted LDFs.
116    pub fn benktander(
117        &self,
118        expected_ultimate: &[f64],
119        iterations: usize,
120        tail: f64,
121    ) -> Result<BenktanderProjection, ValidationError> {
122        self.view().benktander(expected_ultimate, iterations, tail)
123    }
124
125    /// Generalized Cape Cod (Gluck 1997) decay-weighted projection.
126    pub fn generalized_cape_cod(
127        &self,
128        premium: &[f64],
129        decay: f64,
130        tail: f64,
131    ) -> Result<GeneralizedCapeCodProjection, ValidationError> {
132        self.view().generalized_cape_cod(premium, decay, tail)
133    }
134
135    /// Brosius (1993) OLS credibility development projection.
136    pub fn brosius(&self, tail: f64) -> Result<Brosius, ValidationError> {
137        self.view().brosius(tail)
138    }
139
140    /// Fit the Over-Dispersed Poisson GLM (`fit_odp`) with strict monotonicity.
141    pub fn fit_odp(&self, tail: f64) -> Result<OdpFit, ValidationError> {
142        self.view().fit_odp(tail)
143    }
144
145    /// Fit a reserving GLM (`Odp`, `Gamma`, `Tweedie`, or `LogNormal`) via Householder QR IRLS.
146    pub fn glm(&self, options: &GlmOptions) -> Result<GlmReservingResult, ValidationError> {
147        self.view().glm(options)
148    }
149
150    /// Fit Clark (2003) LDF parametric growth-curve MLE.
151    pub fn clark_ldf(&self, options: &ClarkOptions) -> Result<ClarkMleResult, ValidationError> {
152        self.view().clark_ldf(options)
153    }
154
155    /// Fit Clark (2003) Cape Cod parametric growth-curve MLE.
156    pub fn clark_cape_cod(
157        &self,
158        premium: &[f64],
159        options: &ClarkOptions,
160    ) -> Result<ClarkMleResult, ValidationError> {
161        self.view().clark_cape_cod(premium, options)
162    }
163
164    /// Fit Barnett & Zehnwirth (2000) Probabilistic Trend Family log-linear regression.
165    pub fn ptf(&self, options: &PtfOptions) -> Result<PtfResult, ValidationError> {
166        self.view().ptf(options)
167    }
168
169    /// Structural triangle diagnostics (observed/missing counts, monotonicity, zero latest).
170    pub fn diagnostics(&self) -> TriangleDiagnostics {
171        self.view().diagnostics()
172    }
173}
174
175impl TriangleView<'_> {
176    /// Volume-weighted development factors and CDFs (`tail = 1.0` for fully developed).
177    pub fn ldfs(&self, tail: f64) -> Result<DevelopmentFactors, ValidationError> {
178        DevelopmentFactorEngine::volume_weighted(self, tail)
179    }
180
181    /// Volume-weighted Chain-Ladder projection (`tail = 1.0` for fully developed).
182    pub fn chain_ladder(&self, tail: f64) -> Result<ChainLadderProjection, ValidationError> {
183        let factors = DevelopmentFactorEngine::volume_weighted(self, tail)?;
184        ChainLadderEngine::project(self, &factors)
185    }
186
187    /// Mack (1993) distribution-free chain-ladder reserve & standard error analysis.
188    pub fn mack(&self, tail: f64) -> Result<Mack, ValidationError> {
189        mack::analyze(self, tail)
190    }
191
192    /// Mack (1993) analysis under an explicit [`MonotonicityPolicy`].
193    pub fn mack_with_policy(
194        &self,
195        tail: f64,
196        policy: MonotonicityPolicy,
197    ) -> Result<Mack, ValidationError> {
198        mack::analyze_with_policy(self, tail, policy)
199    }
200
201    /// Merz & Wüthrich (2008) 1-year Solvency II Claims Development Result (CDR) MSEP.
202    pub fn merz_wuthrich(&self) -> Result<MerzWuthrichResult, ValidationError> {
203        merz_wuthrich::analyze(self)
204    }
205
206    /// Merz & Wüthrich (2008) 1-year CDR MSEP under an explicit [`MonotonicityPolicy`].
207    pub fn merz_wuthrich_with_policy(
208        &self,
209        policy: MonotonicityPolicy,
210    ) -> Result<MerzWuthrichResult, ValidationError> {
211        merz_wuthrich::analyze_with_policy(self, policy)
212    }
213
214    /// Bornhuetter–Ferguson projection using volume-weighted LDFs.
215    pub fn bf(
216        &self,
217        expected_ultimate: &[f64],
218        tail: f64,
219    ) -> Result<BornhuetterFergusonProjection, ValidationError> {
220        let factors = DevelopmentFactorEngine::volume_weighted(self, tail)?;
221        BornhuetterFergusonEngine::project_from_expected_ultimate(self, &factors, expected_ultimate)
222    }
223
224    /// Cape Cod projection using volume-weighted LDFs.
225    pub fn cape_cod(
226        &self,
227        premium: &[f64],
228        tail: f64,
229    ) -> Result<CapeCodProjection, ValidationError> {
230        let factors = DevelopmentFactorEngine::volume_weighted(self, tail)?;
231        CapeCodEngine::project(self, &factors, premium)
232    }
233
234    /// Run England & Verrall / Shapland ODP bootstrap reserve simulation.
235    pub fn bootstrap_odp(
236        &self,
237        tail: f64,
238        options: OdpBootstrapOptions,
239    ) -> Result<OdpBootstrapResult, ValidationError> {
240        bootstrap_odp(self, tail, options)
241    }
242
243    /// Build a complete JSON-serializable reserving exhibit bundle.
244    pub fn exhibit(
245        &self,
246        premium: Option<&[f64]>,
247        expected_ultimate: Option<&[f64]>,
248        tail: f64,
249    ) -> Result<ExhibitBundle, ValidationError> {
250        build_exhibit_bundle(self, premium, expected_ultimate, tail)
251    }
252
253    /// Benktander (1976) / Hovinen credibility projection using volume-weighted LDFs.
254    pub fn benktander(
255        &self,
256        expected_ultimate: &[f64],
257        iterations: usize,
258        tail: f64,
259    ) -> Result<BenktanderProjection, ValidationError> {
260        let factors = DevelopmentFactorEngine::volume_weighted(self, tail)?;
261        BenktanderEngine::project_from_expected_ultimate(
262            self,
263            &factors,
264            expected_ultimate,
265            iterations,
266        )
267    }
268
269    /// Generalized Cape Cod (Gluck 1997) decay-weighted projection.
270    pub fn generalized_cape_cod(
271        &self,
272        premium: &[f64],
273        decay: f64,
274        tail: f64,
275    ) -> Result<GeneralizedCapeCodProjection, ValidationError> {
276        let factors = DevelopmentFactorEngine::volume_weighted(self, tail)?;
277        GeneralizedCapeCodEngine::project(self, &factors, premium, decay)
278    }
279
280    /// Brosius (1993) OLS credibility development projection.
281    pub fn brosius(&self, tail: f64) -> Result<Brosius, ValidationError> {
282        brosius::project(self, tail)
283    }
284
285    /// Fit the Over-Dispersed Poisson GLM (`fit_odp`) with strict monotonicity.
286    pub fn fit_odp(&self, tail: f64) -> Result<OdpFit, ValidationError> {
287        fit_odp(self, tail, MonotonicityPolicy::Strict)
288    }
289
290    /// Fit a reserving GLM (`Odp`, `Gamma`, `Tweedie`, or `LogNormal`) via Householder QR IRLS.
291    pub fn glm(&self, options: &GlmOptions) -> Result<GlmReservingResult, ValidationError> {
292        glm_reserving::fit_glm(self, options)
293    }
294
295    /// Fit Clark (2003) LDF parametric growth-curve MLE.
296    pub fn clark_ldf(&self, options: &ClarkOptions) -> Result<ClarkMleResult, ValidationError> {
297        clark_mle::fit_clark_ldf(self, options)
298    }
299
300    /// Fit Clark (2003) Cape Cod parametric growth-curve MLE.
301    pub fn clark_cape_cod(
302        &self,
303        premium: &[f64],
304        options: &ClarkOptions,
305    ) -> Result<ClarkMleResult, ValidationError> {
306        clark_mle::fit_clark_cape_cod(self, premium, options)
307    }
308
309    /// Fit Barnett & Zehnwirth (2000) Probabilistic Trend Family log-linear regression.
310    pub fn ptf(&self, options: &PtfOptions) -> Result<PtfResult, ValidationError> {
311        ptf_regression::fit_ptf(self, options)
312    }
313
314    /// Structural triangle diagnostics (observed/missing counts, monotonicity, zero latest).
315    pub fn diagnostics(&self) -> TriangleDiagnostics {
316        triangle_diagnostics::analyze(self)
317    }
318}