use crate::subannual::grid::stub_average_age;
use crate::validate::ValidationError;
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct StubExtrapolationResult {
pub stub_age_months: f64,
pub stub_cdf: f64,
pub stub_to_12_ldf: f64,
pub pct_reported: f64,
pub exponent: f64,
}
pub fn stub_plus12(
stub_age_months: f64,
cdf_12: f64,
cdf_24: f64,
) -> Result<StubExtrapolationResult, ValidationError> {
if !stub_age_months.is_finite() || !cdf_12.is_finite() || !cdf_24.is_finite() {
return Err(ValidationError::NotFinite);
}
if stub_age_months <= 0.0 || stub_age_months > 12.0 + 1e-9 {
return Err(ValidationError::InvalidParameter(
"stub_age_months must be in (0, 12]",
));
}
if cdf_12 <= 0.0 || cdf_24 <= 0.0 {
return Err(ValidationError::DivideByZero("annual CDF"));
}
let s = stub_age_months.min(12.0);
let f12_24 = cdf_12 / cdf_24;
let exponent = (24.0 / (s + 12.0)).ln() / (36.0_f64 / 24.0_f64).ln();
let stub_to_12_ldf = f12_24.powf(exponent);
let stub_cdf = cdf_12 * stub_to_12_ldf;
Ok(StubExtrapolationResult {
stub_age_months: s,
stub_cdf,
stub_to_12_ldf,
pct_reported: 1.0 / stub_cdf,
exponent,
})
}
pub fn stub_power_ratio(
stub_age_months: f64,
cdf_12: f64,
ldf_12_24: f64,
ldf_24_36: f64,
) -> Result<StubExtrapolationResult, ValidationError> {
if !stub_age_months.is_finite()
|| !cdf_12.is_finite()
|| !ldf_12_24.is_finite()
|| !ldf_24_36.is_finite()
{
return Err(ValidationError::NotFinite);
}
if stub_age_months <= 0.0 || stub_age_months > 12.0 + 1e-9 {
return Err(ValidationError::InvalidParameter(
"stub_age_months must be in (0, 12]",
));
}
if cdf_12 <= 0.0 || ldf_12_24 <= 0.0 || ldf_24_36 <= 0.0 {
return Err(ValidationError::DivideByZero("CDF or LDF"));
}
let s = stub_age_months.min(12.0);
let exponent = (12.0 - s) / 12.0;
let base_factor = ldf_12_24 * (ldf_12_24 / ldf_24_36);
let stub_to_12_ldf = base_factor.powf(exponent);
let stub_cdf = cdf_12 * stub_to_12_ldf;
Ok(StubExtrapolationResult {
stub_age_months: s,
stub_cdf,
stub_to_12_ldf,
pct_reported: 1.0 / stub_cdf,
exponent,
})
}
pub fn stub_natural_log(
stub_age_months: f64,
cdf_12: f64,
cdf_24: f64,
) -> Result<StubExtrapolationResult, ValidationError> {
if !stub_age_months.is_finite() || !cdf_12.is_finite() || !cdf_24.is_finite() {
return Err(ValidationError::NotFinite);
}
if stub_age_months <= 0.0 || stub_age_months > 12.0 + 1e-9 {
return Err(ValidationError::InvalidParameter(
"stub_age_months must be in (0, 12]",
));
}
if cdf_12 <= 0.0 || cdf_24 <= 0.0 {
return Err(ValidationError::DivideByZero("annual CDF"));
}
let s = stub_age_months.min(12.0);
let f12_24 = cdf_12 / cdf_24;
let exponent = (12.0 / s).ln() / 2.0_f64.ln();
let stub_to_12_ldf = f12_24.powf(exponent);
let stub_cdf = cdf_12 * stub_to_12_ldf;
Ok(StubExtrapolationResult {
stub_age_months: s,
stub_cdf,
stub_to_12_ldf,
pct_reported: 1.0 / stub_cdf,
exponent,
})
}
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct StubExposureScaleResult {
pub exposure_fraction: f64,
pub earned_expected_loss: f64,
pub pure_reporting_cdf: f64,
pub full_year_combined_cdf: f64,
pub earned_percent_reported: f64,
pub stub_average_age_months: f64,
}
pub fn stub_exposure_scale(
full_period_expected_loss: f64,
pure_reporting_cdf: f64,
stub_duration_months: f64,
full_period_months: f64,
) -> Result<StubExposureScaleResult, ValidationError> {
if !full_period_expected_loss.is_finite()
|| !pure_reporting_cdf.is_finite()
|| !stub_duration_months.is_finite()
|| !full_period_months.is_finite()
{
return Err(ValidationError::NotFinite);
}
if full_period_expected_loss < 0.0 {
return Err(ValidationError::Negative);
}
if pure_reporting_cdf <= 0.0 {
return Err(ValidationError::DivideByZero("pure_reporting_cdf"));
}
if full_period_months <= 0.0
|| stub_duration_months <= 0.0
|| stub_duration_months > full_period_months + 1e-9
{
return Err(ValidationError::InvalidParameter(
"stub_duration_months must be in (0, full_period_months]",
));
}
let d = stub_duration_months.min(full_period_months);
let exposure_fraction = d / full_period_months;
let earned_expected_loss = full_period_expected_loss * exposure_fraction;
let full_year_combined_cdf = pure_reporting_cdf / exposure_fraction;
let avg_age = stub_average_age(d, full_period_months)?;
Ok(StubExposureScaleResult {
exposure_fraction,
earned_expected_loss,
pure_reporting_cdf,
full_year_combined_cdf,
earned_percent_reported: 1.0 / pure_reporting_cdf,
stub_average_age_months: avg_age,
})
}
pub fn decompose_combined_stub_cdf(
full_period_expected_loss: f64,
combined_stub_cdf: f64,
stub_duration_months: f64,
full_period_months: f64,
) -> Result<StubExposureScaleResult, ValidationError> {
if !combined_stub_cdf.is_finite()
|| !stub_duration_months.is_finite()
|| !full_period_months.is_finite()
{
return Err(ValidationError::NotFinite);
}
if combined_stub_cdf <= 0.0 || full_period_months <= 0.0 || stub_duration_months <= 0.0 {
return Err(ValidationError::InvalidParameter(
"combined_stub_cdf, stub_duration_months, and full_period_months must be positive",
));
}
let exposure_fraction = (stub_duration_months.min(full_period_months)) / full_period_months;
let pure_reporting_cdf = combined_stub_cdf * exposure_fraction;
stub_exposure_scale(
full_period_expected_loss,
pure_reporting_cdf,
stub_duration_months,
full_period_months,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn stub_methods_match_cdf12_at_12_months() {
let cdf_12 = 2.40;
let cdf_24 = 1.50;
let ldf_12_24 = cdf_12 / cdf_24;
let ldf_24_36 = 1.25;
let p12 = stub_plus12(12.0, cdf_12, cdf_24).unwrap();
let pr = stub_power_ratio(12.0, cdf_12, ldf_12_24, ldf_24_36).unwrap();
let nl = stub_natural_log(12.0, cdf_12, cdf_24).unwrap();
assert!((p12.stub_cdf - cdf_12).abs() < 1e-12);
assert!((pr.stub_cdf - cdf_12).abs() < 1e-12);
assert!((nl.stub_cdf - cdf_12).abs() < 1e-12);
}
#[test]
fn natural_log_is_more_conservative_than_plus12() {
let cdf_12 = 2.40;
let cdf_24 = 1.50;
for s in [3.0, 6.0, 9.0] {
let p12 = stub_plus12(s, cdf_12, cdf_24).unwrap();
let nl = stub_natural_log(s, cdf_12, cdf_24).unwrap();
assert!(
nl.stub_cdf > p12.stub_cdf,
"at {s}m: natural_log ({}) should exceed plus12 ({})",
nl.stub_cdf,
p12.stub_cdf
);
}
}
#[test]
fn stub_exposure_scale_separates_exposure_and_reporting() {
let res = stub_exposure_scale(1_000.0, 2.0, 6.0, 12.0).unwrap();
assert!((res.exposure_fraction - 0.5).abs() < 1e-12);
assert!((res.earned_expected_loss - 500.0).abs() < 1e-12);
assert!((res.full_year_combined_cdf - 4.0).abs() < 1e-12);
assert!((res.stub_average_age_months - 3.0).abs() < 1e-12);
let roundtrip = decompose_combined_stub_cdf(1_000.0, 4.0, 6.0, 12.0).unwrap();
assert!((roundtrip.pure_reporting_cdf - 2.0).abs() < 1e-12);
}
}