use crate::linalg::{
QrWorkspace, quad_form, weighted_least_squares_qr, weighted_least_squares_qr_beta_into,
weighted_least_squares_qr_with_workspace,
};
use crate::triangle::{cumulative_to_incremental, latest_diagonal};
use crate::types::TriangleView;
use crate::validate::{
MonotonicityPolicy, ValidationError, validate_monotone_cumulative,
validate_positive_finite_slice, validate_triangle,
};
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum GlmFamily {
Odp,
Gamma,
Tweedie {
power: f64,
},
LogNormal,
}
impl GlmFamily {
fn tweedie_power(self) -> Option<f64> {
match self {
Self::Odp => Some(1.0),
Self::Gamma => Some(2.0),
Self::Tweedie { power } => Some(power),
Self::LogNormal => None,
}
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum GlmDesign {
CrossClassified,
Hoerl {
per_origin_intercepts: bool,
},
HoerlWithCalendarSplines {
per_origin_intercepts: bool,
calendar_knots: Vec<f64>,
},
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct GlmOptions {
pub family: GlmFamily,
pub design: GlmDesign,
pub exposure: Option<Vec<f64>>,
pub max_iterations: usize,
pub tolerance: f64,
pub monotonicity_policy: MonotonicityPolicy,
}
impl Default for GlmOptions {
fn default() -> Self {
Self::odp()
}
}
impl GlmOptions {
pub fn new(family: GlmFamily, design: GlmDesign) -> Self {
Self {
family,
design,
exposure: None,
max_iterations: 100,
tolerance: 1e-10,
monotonicity_policy: MonotonicityPolicy::Strict,
}
}
pub fn odp() -> Self {
Self::new(GlmFamily::Odp, GlmDesign::CrossClassified)
}
pub fn gamma() -> Self {
Self::new(GlmFamily::Gamma, GlmDesign::CrossClassified)
}
pub fn tweedie(power: f64) -> Self {
Self::new(GlmFamily::Tweedie { power }, GlmDesign::CrossClassified)
}
pub fn log_normal() -> Self {
Self::new(GlmFamily::LogNormal, GlmDesign::CrossClassified)
}
pub fn with_design(mut self, design: GlmDesign) -> Self {
self.design = design;
self
}
pub fn with_exposure(mut self, exposure: impl Into<Vec<f64>>) -> Self {
self.exposure = Some(exposure.into());
self
}
pub const fn with_max_iterations(mut self, max_iterations: usize) -> Self {
self.max_iterations = max_iterations;
self
}
pub const fn with_tolerance(mut self, tolerance: f64) -> Self {
self.tolerance = tolerance;
self
}
pub const fn with_policy(mut self, policy: MonotonicityPolicy) -> Self {
self.monotonicity_policy = policy;
self
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct GlmReservingResult {
pub family: GlmFamily,
pub param_names: Vec<String>,
pub coefficients: Vec<f64>,
pub coefficient_se: Vec<f64>,
pub coefficient_covariance: Vec<f64>,
pub iterations: usize,
pub converged: bool,
pub n_observed: usize,
pub n_params: usize,
pub degrees_of_freedom: usize,
pub deviance: f64,
pub pearson_chi_sq: f64,
pub pearson_dispersion: f64,
pub deviance_dispersion: f64,
pub leverages: Vec<f64>,
pub fitted_incremental: Vec<f64>,
pub reserves: Vec<f64>,
pub ultimates: Vec<f64>,
pub process_se: Vec<f64>,
pub parameter_se: Vec<f64>,
pub prediction_error: Vec<f64>,
pub total_reserve: f64,
pub total_process_se: f64,
pub total_parameter_se: f64,
pub total_prediction_error: f64,
pub total_cv: f64,
}
pub fn fit_glm(
triangle: &TriangleView<'_>,
options: &GlmOptions,
) -> Result<GlmReservingResult, ValidationError> {
validate_triangle(triangle)?;
if options.monotonicity_policy == MonotonicityPolicy::Strict {
validate_monotone_cumulative(triangle)?;
}
let n = triangle.n_origins;
if n < 3 {
return Err(ValidationError::InvalidParameter(
"GLM reserving requires at least a 3x3 triangle",
));
}
if let GlmFamily::Tweedie { power } = options.family {
if !power.is_finite() || !(1.0..=2.0).contains(&power) {
return Err(ValidationError::InvalidParameter(
"Tweedie variance power must lie in [1.0, 2.0]",
));
}
}
if let Some(exp) = &options.exposure {
if exp.len() != n {
return Err(ValidationError::ShapeMismatch);
}
validate_positive_finite_slice(exp, "exposure")?;
}
if options.max_iterations == 0 || !options.tolerance.is_finite() || options.tolerance <= 0.0 {
return Err(ValidationError::InvalidParameter(
"invalid IRLS convergence settings",
));
}
let inc = cumulative_to_incremental(triangle);
let latest = latest_diagonal(triangle);
let schema = GlmDesignSchema::build(n, &options.design)?;
let q = schema.param_names.len();
let mut obs_cells: Vec<(usize, usize)> = Vec::new();
let mut x_obs: Vec<f64> = Vec::new();
let mut y_obs: Vec<f64> = Vec::new();
let mut offsets_obs: Vec<f64> = Vec::new();
let mut row_buf = vec![0.0; q];
for i in 0..n {
let log_exp = options.exposure.as_ref().map_or(0.0, |e| e[i].ln());
for j in 0..(n - i) {
let y = inc[i * n + j];
if !y.is_finite() {
return Err(ValidationError::NotFinite);
}
match options.family {
GlmFamily::Gamma | GlmFamily::LogNormal => {
if y <= 0.0 {
return Err(ValidationError::InvalidParameter(
"Gamma and LogNormal GLMs require strictly positive observed increments",
));
}
}
GlmFamily::Tweedie { power } if power > 1.0 => {
if y < 0.0 {
return Err(ValidationError::Negative);
}
}
_ => {
if y < 0.0 && options.monotonicity_policy == MonotonicityPolicy::Strict {
return Err(ValidationError::Negative);
}
}
}
schema.fill_row(i, j, &mut row_buf);
x_obs.extend_from_slice(&row_buf);
y_obs.push(y);
offsets_obs.push(log_exp);
obs_cells.push((i, j));
}
}
let n_obs = obs_cells.len();
if n_obs <= q {
return Err(ValidationError::InvalidParameter(
"GLM model has non-positive residual degrees of freedom",
));
}
let dof = n_obs - q;
if options.family == GlmFamily::LogNormal {
return fit_log_normal_glm(
n,
&schema,
&x_obs,
&y_obs,
&offsets_obs,
&latest,
options.exposure.as_deref(),
n_obs,
q,
dof,
);
}
let power = options.family.tweedie_power().expect("Tweedie family");
let positive_mean = y_obs.iter().copied().filter(|&v| v > 0.0).sum::<f64>()
/ (y_obs.iter().filter(|&&v| v > 0.0).count().max(1) as f64);
let y_floor = (positive_mean * 1e-4).max(1e-8);
let mut working_z: Vec<f64> = y_obs
.iter()
.zip(offsets_obs.iter())
.map(|(&y, &off)| y.max(y_floor).ln() - off)
.collect();
let mut weights = vec![1.0; n_obs];
let mut qr_ws = QrWorkspace::with_capacity(n_obs, q);
let mut beta = vec![0.0; q];
weighted_least_squares_qr_beta_into(
&x_obs, &working_z, &weights, n_obs, q, &mut qr_ws, &mut beta,
)?;
let mut eta = vec![0.0; n_obs];
let mut mu = vec![0.0; n_obs];
eval_linear_predictor_and_mu(&x_obs, &offsets_obs, &beta, n_obs, q, &mut eta, &mut mu);
let mut cur_obj = total_tweedie_objective(&y_obs, &mu, power);
let mut converged = false;
let mut iterations = 0usize;
let is_odp = (power - 1.0).abs() < 1e-12;
let is_gamma = (power - 2.0).abs() < 1e-12;
let mut beta_target = vec![0.0; q];
let mut delta = vec![0.0; q];
let mut next_beta = vec![0.0; q];
let mut next_eta = vec![0.0; n_obs];
let mut next_mu = vec![0.0; n_obs];
for iter in 1..=options.max_iterations {
iterations = iter;
for k in 0..n_obs {
let mk = mu[k];
weights[k] = if is_odp {
mk
} else if is_gamma {
1.0
} else {
mk.powf(2.0 - power)
};
working_z[k] = (eta[k] - offsets_obs[k]) + (y_obs[k] - mk) / mk;
}
weighted_least_squares_qr_beta_into(
&x_obs,
&working_z,
&weights,
n_obs,
q,
&mut qr_ws,
&mut beta_target,
)?;
for j in 0..q {
delta[j] = beta_target[j] - beta[j];
next_beta[j] = beta_target[j];
}
let mut step_len = 1.0_f64;
eval_linear_predictor_and_mu(
&x_obs,
&offsets_obs,
&next_beta,
n_obs,
q,
&mut next_eta,
&mut next_mu,
);
let mut next_obj = total_tweedie_objective(&y_obs, &next_mu, power);
let mut halvings = 0usize;
while (!next_obj.is_finite() || next_obj > cur_obj + 1e-9 * (1.0 + cur_obj.abs()))
&& halvings < 25
{
step_len *= 0.5;
halvings += 1;
for j in 0..q {
next_beta[j] = beta[j] + step_len * delta[j];
}
eval_linear_predictor_and_mu(
&x_obs,
&offsets_obs,
&next_beta,
n_obs,
q,
&mut next_eta,
&mut next_mu,
);
next_obj = total_tweedie_objective(&y_obs, &next_mu, power);
}
let max_rel_change = next_beta
.iter()
.zip(beta.iter())
.map(|(&bn, &bo)| (bn - bo).abs() / (1.0 + bo.abs()))
.fold(0.0_f64, f64::max);
let rel_obj_change = (cur_obj - next_obj).abs() / (1.0 + cur_obj.abs());
std::mem::swap(&mut beta, &mut next_beta);
std::mem::swap(&mut eta, &mut next_eta);
std::mem::swap(&mut mu, &mut next_mu);
cur_obj = next_obj;
if max_rel_change < options.tolerance && rel_obj_change < options.tolerance {
converged = true;
break;
}
}
let cur_dev = total_tweedie_deviance(&y_obs, &mu, power);
for k in 0..n_obs {
let mk = mu[k];
weights[k] = if is_odp {
mk
} else if is_gamma {
1.0
} else {
mk.powf(2.0 - power)
};
working_z[k] = eta[k] - offsets_obs[k];
}
let final_sol = weighted_least_squares_qr_with_workspace(
&x_obs, &working_z, &weights, n_obs, q, &mut qr_ws,
)?;
let final_xtwx_inv = final_sol.xtwx_inv;
let final_leverages = final_sol.leverages;
let mut pearson_chi_sq = 0.0;
for k in 0..n_obs {
let diff = y_obs[k] - mu[k];
let v_mu = if is_odp {
mu[k]
} else if is_gamma {
mu[k] * mu[k]
} else {
mu[k].powf(power)
};
pearson_chi_sq += (diff * diff) / v_mu;
}
let pearson_dispersion = (pearson_chi_sq / dof as f64).max(0.0);
let deviance_dispersion = (cur_dev / dof as f64).max(0.0);
let cov: Vec<f64> = final_xtwx_inv
.iter()
.map(|&v| pearson_dispersion * v)
.collect();
let coefficient_se: Vec<f64> = (0..q).map(|k| cov[k * q + k].max(0.0).sqrt()).collect();
let mut fitted_incremental = vec![0.0; n * n];
let mut reserves = vec![0.0; n];
let mut process_se = vec![0.0; n];
let mut parameter_se = vec![0.0; n];
let mut prediction_error = vec![0.0; n];
let mut grad_total = vec![0.0; q];
let mut grad_origin = vec![0.0; q];
let mut total_proc_var = 0.0;
let mut x_cell = vec![0.0; q];
for i in 0..n {
let log_exp = options.exposure.as_ref().map_or(0.0, |e| e[i].ln());
grad_origin.fill(0.0);
let mut proc_var_i = 0.0;
let mut r_i = 0.0;
for j in 0..n {
schema.fill_row(i, j, &mut x_cell);
let mut e_ij = log_exp;
for (&xc, &bc) in x_cell.iter().zip(beta.iter()) {
e_ij += xc * bc;
}
let mu_ij = e_ij.clamp(-50.0, 50.0).exp();
fitted_incremental[i * n + j] = mu_ij;
if i + j >= n {
r_i += mu_ij;
let v_mu = if is_odp {
mu_ij
} else if is_gamma {
mu_ij * mu_ij
} else {
mu_ij.powf(power)
};
proc_var_i += pearson_dispersion * v_mu;
for col in 0..q {
let g = mu_ij * x_cell[col];
grad_origin[col] += g;
grad_total[col] += g;
}
}
}
reserves[i] = r_i;
total_proc_var += proc_var_i;
let param_var_i = quad_form(&grad_origin, &cov, q).max(0.0);
process_se[i] = proc_var_i.max(0.0).sqrt();
parameter_se[i] = param_var_i.sqrt();
prediction_error[i] = (proc_var_i.max(0.0) + param_var_i).sqrt();
}
let ultimates: Vec<f64> = latest
.iter()
.zip(reserves.iter())
.map(|(&l, &r)| l + r)
.collect();
let total_reserve: f64 = reserves.iter().sum();
let total_param_var = quad_form(&grad_total, &cov, q).max(0.0);
let total_proc_var = total_proc_var.max(0.0);
let total_prediction_error = (total_proc_var + total_param_var).sqrt();
let total_cv = if total_reserve > 0.0 {
total_prediction_error / total_reserve
} else {
0.0
};
Ok(GlmReservingResult {
family: options.family,
param_names: schema.param_names,
coefficients: beta,
coefficient_se,
coefficient_covariance: cov,
iterations,
converged,
n_observed: n_obs,
n_params: q,
degrees_of_freedom: dof,
deviance: cur_dev,
pearson_chi_sq,
pearson_dispersion,
deviance_dispersion,
leverages: final_leverages,
fitted_incremental,
reserves,
ultimates,
process_se,
parameter_se,
prediction_error,
total_reserve,
total_process_se: total_proc_var.sqrt(),
total_parameter_se: total_param_var.sqrt(),
total_prediction_error,
total_cv,
})
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct TweediePowerCandidate {
pub power: f64,
pub eql_score: f64,
pub pearson_dispersion: f64,
pub total_reserve: f64,
pub total_prediction_error: f64,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct TweediePowerSelection {
pub best_power: f64,
pub best_fit: GlmReservingResult,
pub candidates: Vec<TweediePowerCandidate>,
}
pub fn select_tweedie_power(
triangle: &TriangleView<'_>,
design: GlmDesign,
p_grid: &[f64],
) -> Result<TweediePowerSelection, ValidationError> {
if p_grid.is_empty() {
return Err(ValidationError::InvalidParameter(
"p_grid must contain at least one power in [1, 2]",
));
}
let inc = cumulative_to_incremental(triangle);
let n = triangle.n_origins;
let mut sum_log_y = 0.0;
let mut n_pos = 0usize;
for i in 0..n {
for j in 0..(n - i) {
let y = inc[i * n + j];
if y.is_finite() && y > 0.0 {
sum_log_y += y.ln();
n_pos += 1;
}
}
}
if n_pos == 0 {
return Err(ValidationError::ShapeMismatch);
}
let mut candidates = Vec::with_capacity(p_grid.len());
let mut best_score = f64::INFINITY;
let mut best_power = f64::NAN;
let mut best_fit: Option<GlmReservingResult> = None;
for &p in p_grid {
if !p.is_finite() || !(1.0..=2.0).contains(&p) {
return Err(ValidationError::InvalidParameter(
"Tweedie power must lie in [1.0, 2.0]",
));
}
let opts = GlmOptions::tweedie(p).with_design(design.clone());
if let Ok(fit) = fit_glm(triangle, &opts) {
let phi = fit.deviance_dispersion.max(1e-12);
let n_obs = fit.n_observed as f64;
let eql_score = 0.5
* (n_obs * (2.0 * std::f64::consts::PI * phi).ln()
+ p * sum_log_y
+ fit.deviance / phi);
candidates.push(TweediePowerCandidate {
power: p,
eql_score,
pearson_dispersion: fit.pearson_dispersion,
total_reserve: fit.total_reserve,
total_prediction_error: fit.total_prediction_error,
});
if eql_score < best_score {
best_score = eql_score;
best_power = p;
best_fit = Some(fit);
}
}
}
let best_fit = best_fit.ok_or(ValidationError::NotFinite)?;
Ok(TweediePowerSelection {
best_power,
best_fit,
candidates,
})
}
#[allow(clippy::too_many_arguments)]
fn fit_log_normal_glm(
n: usize,
schema: &GlmDesignSchema,
x_obs: &[f64],
y_obs: &[f64],
offsets_obs: &[f64],
latest: &[f64],
exposure: Option<&[f64]>,
n_obs: usize,
q: usize,
dof: usize,
) -> Result<GlmReservingResult, ValidationError> {
let z_obs: Vec<f64> = y_obs
.iter()
.zip(offsets_obs.iter())
.map(|(&y, &off)| y.ln() - off)
.collect();
let ones = vec![1.0; n_obs];
let sol = weighted_least_squares_qr(x_obs, &z_obs, &ones, n_obs, q)?;
let mut rss = 0.0;
for k in 0..n_obs {
let mut eta = 0.0;
for col in 0..q {
eta += x_obs[k * q + col] * sol.beta[col];
}
let d = z_obs[k] - eta;
rss += d * d;
}
let sigma_sq = (rss / dof as f64).max(0.0);
let cov: Vec<f64> = sol.xtwx_inv.iter().map(|&v| sigma_sq * v).collect();
let coefficient_se: Vec<f64> = (0..q).map(|k| cov[k * q + k].max(0.0).sqrt()).collect();
let mut fitted_incremental = vec![0.0; n * n];
let mut future_cells: Vec<(usize, f64, Vec<f64>)> = Vec::new();
let mut reserves = vec![0.0; n];
for i in 0..n {
let log_exp = exposure.map_or(0.0, |e| e[i].ln());
for j in 0..n {
let mut x_cell = vec![0.0; q];
schema.fill_row(i, j, &mut x_cell);
let mut eta = log_exp;
for col in 0..q {
eta += x_cell[col] * sol.beta[col];
}
let mu = (eta + 0.5 * sigma_sq).exp();
if !mu.is_finite() {
return Err(ValidationError::NotFinite);
}
fitted_incremental[i * n + j] = mu;
if i + j >= n {
reserves[i] += mu;
future_cells.push((i, mu, x_cell));
}
}
}
let mut proc_var_origin = vec![0.0; n];
let mut param_var_origin = vec![0.0; n];
let mut total_proc_var = 0.0;
let mut total_param_var = 0.0;
let n_fut = future_cells.len();
let mut cov_x: Vec<Vec<f64>> = Vec::with_capacity(n_fut);
for (_, _, xb) in &future_cells {
let mut v = vec![0.0; q];
for r in 0..q {
let mut acc = 0.0;
for c in 0..q {
acc += cov[r * q + c] * xb[c];
}
v[r] = acc;
}
cov_x.push(v);
}
for a in 0..n_fut {
let (ia, mu_a, ref xa) = future_cells[a];
let mut v_est_a = 0.0;
let cva = &cov_x[a];
for r in 0..q {
v_est_a += xa[r] * cva[r];
}
let v_est_a = v_est_a.max(0.0);
let pv = mu_a * mu_a * sigma_sq.exp_m1();
let tot_a = mu_a * mu_a * (sigma_sq + v_est_a).exp_m1();
let pmv = (tot_a - pv).max(0.0);
proc_var_origin[ia] += pv;
param_var_origin[ia] += pmv;
total_proc_var += pv;
total_param_var += pmv;
for b in (a + 1)..n_fut {
let (ib, mu_b, _) = future_cells[b];
let mut cov_eta = 0.0;
let cvb = &cov_x[b];
for r in 0..q {
cov_eta += xa[r] * cvb[r];
}
let cov_ab = mu_a * mu_b * cov_eta.exp_m1();
total_param_var += 2.0 * cov_ab;
if ia == ib {
param_var_origin[ia] += 2.0 * cov_ab;
}
}
}
let process_se: Vec<f64> = proc_var_origin.iter().map(|v| v.max(0.0).sqrt()).collect();
let parameter_se: Vec<f64> = param_var_origin.iter().map(|v| v.max(0.0).sqrt()).collect();
let prediction_error: Vec<f64> = proc_var_origin
.iter()
.zip(param_var_origin.iter())
.map(|(&p1, &p2)| (p1 + p2).max(0.0).sqrt())
.collect();
let ultimates: Vec<f64> = latest
.iter()
.zip(reserves.iter())
.map(|(&l, &r)| l + r)
.collect();
let total_reserve: f64 = reserves.iter().sum();
let total_prediction_error = (total_proc_var + total_param_var).max(0.0).sqrt();
let total_cv = if total_reserve > 0.0 {
total_prediction_error / total_reserve
} else {
0.0
};
Ok(GlmReservingResult {
family: GlmFamily::LogNormal,
param_names: schema.param_names.clone(),
coefficients: sol.beta,
coefficient_se,
coefficient_covariance: cov,
iterations: 1,
converged: true,
n_observed: n_obs,
n_params: q,
degrees_of_freedom: dof,
deviance: rss,
pearson_chi_sq: rss,
pearson_dispersion: sigma_sq,
deviance_dispersion: sigma_sq,
leverages: sol.leverages,
fitted_incremental,
reserves,
ultimates,
process_se,
parameter_se,
prediction_error,
total_reserve,
total_process_se: total_proc_var.max(0.0).sqrt(),
total_parameter_se: total_param_var.max(0.0).sqrt(),
total_prediction_error,
total_cv,
})
}
#[inline]
fn eval_linear_predictor_and_mu(
x: &[f64],
offsets: &[f64],
beta: &[f64],
n_obs: usize,
q: usize,
eta_out: &mut [f64],
mu_out: &mut [f64],
) {
for k in 0..n_obs {
let mut s = offsets[k];
let x_row = &x[k * q..(k + 1) * q];
for (&xj, &bj) in x_row.iter().zip(beta.iter()) {
s += xj * bj;
}
eta_out[k] = s;
mu_out[k] = s.clamp(-50.0, 50.0).exp();
}
}
fn tweedie_unit_deviance(y: f64, mu: f64, p: f64) -> f64 {
if (p - 1.0).abs() < 1e-12 {
let term = if y > 0.0 { y * (y / mu).ln() } else { 0.0 };
2.0 * (term - (y - mu))
} else if (p - 2.0).abs() < 1e-12 {
2.0 * (-(y / mu).ln() + (y - mu) / mu)
} else {
let one_mp = 1.0 - p;
let two_mp = 2.0 - p;
if y > 0.0 {
let ln_ratio = (y / mu).ln();
let t1 = y * mu.powf(one_mp) * (one_mp * ln_ratio).exp_m1() / one_mp;
let t2 = mu.powf(two_mp) * (two_mp * ln_ratio).exp_m1() / two_mp;
2.0 * (t1 - t2)
} else {
2.0 * mu.powf(two_mp) / two_mp
}
}
}
fn tweedie_line_search_objective(y: f64, mu: f64, p: f64) -> f64 {
if (p - 1.0).abs() < 1e-12 && y < 0.0 {
2.0 * (mu - y * mu.ln())
} else {
tweedie_unit_deviance(y, mu, p)
}
}
fn total_tweedie_objective(y: &[f64], mu: &[f64], p: f64) -> f64 {
y.iter()
.zip(mu.iter())
.map(|(&yk, &mk)| tweedie_line_search_objective(yk, mk, p))
.sum()
}
fn total_tweedie_deviance(y: &[f64], mu: &[f64], p: f64) -> f64 {
y.iter()
.zip(mu.iter())
.map(|(&yk, &mk)| tweedie_unit_deviance(yk, mk, p))
.sum()
}
struct GlmDesignSchema {
n: usize,
design: GlmDesign,
param_names: Vec<String>,
}
impl GlmDesignSchema {
fn build(n: usize, design: &GlmDesign) -> Result<Self, ValidationError> {
let mut names = vec!["intercept".to_string()];
match design {
GlmDesign::CrossClassified => {
for i in 1..n {
names.push(format!("origin_{i}"));
}
for j in 1..n {
names.push(format!("dev_{j}"));
}
}
GlmDesign::Hoerl {
per_origin_intercepts,
} => {
if *per_origin_intercepts {
for i in 1..n {
names.push(format!("origin_{i}"));
}
}
names.push("hoerl_ln_j".to_string());
names.push("hoerl_j".to_string());
}
GlmDesign::HoerlWithCalendarSplines {
per_origin_intercepts,
calendar_knots,
} => {
let mut prev = 0.0_f64;
for &k in calendar_knots {
if !k.is_finite() || k <= 0.0 || k >= (n - 1) as f64 {
return Err(ValidationError::InvalidParameter(
"calendar spline knots must lie strictly inside (0, n - 1)",
));
}
if k <= prev {
return Err(ValidationError::InvalidParameter(
"calendar spline knots must be strictly increasing",
));
}
prev = k;
}
if *per_origin_intercepts {
for i in 1..n {
names.push(format!("origin_{i}"));
}
}
names.push("hoerl_ln_j".to_string());
names.push("hoerl_j".to_string());
if !*per_origin_intercepts {
names.push("cal_slope_0".to_string());
}
for &k in calendar_knots {
names.push(format!("cal_hinge_{k}"));
}
}
}
Ok(Self {
n,
design: design.clone(),
param_names: names,
})
}
fn fill_row(&self, i: usize, j: usize, out: &mut [f64]) {
out.fill(0.0);
out[0] = 1.0;
let d = (j + 1) as f64; let t = (i + j) as f64;
match &self.design {
GlmDesign::CrossClassified => {
if i > 0 {
out[i] = 1.0;
}
if j > 0 {
out[self.n + (j - 1)] = 1.0;
}
}
GlmDesign::Hoerl {
per_origin_intercepts,
} => {
let mut col = 1;
if *per_origin_intercepts {
if i > 0 {
out[i] = 1.0;
}
col = self.n;
}
out[col] = d.ln();
out[col + 1] = d;
}
GlmDesign::HoerlWithCalendarSplines {
per_origin_intercepts,
calendar_knots,
} => {
let mut col = 1;
if *per_origin_intercepts {
if i > 0 {
out[i] = 1.0;
}
col = self.n;
}
out[col] = d.ln();
out[col + 1] = d;
col += 2;
if !*per_origin_intercepts {
out[col] = t;
col += 1;
}
for &knot in calendar_knots {
out[col] = (t - knot).max(0.0);
col += 1;
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bootstrap_odp::fit_odp;
use crate::types::Triangle;
const NAN: f64 = f64::NAN;
fn sample_tri() -> Triangle {
#[rustfmt::skip]
let data = vec![
100.0, 220.0, 310.0, 360.0, 385.0,
120.0, 255.0, 365.0, 420.0, NAN,
135.0, 290.0, 410.0, NAN, NAN,
150.0, 318.0, NAN, NAN, NAN,
175.0, NAN, NAN, NAN, NAN,
];
Triangle::new(5, 5, data)
}
#[test]
fn cross_classified_odp_irls_matches_exact_fit_odp_to_machine_precision() {
let tri = sample_tri();
let odp_exact = fit_odp(&tri.view(), 1.0, MonotonicityPolicy::Strict).unwrap();
let glm = fit_glm(
&tri.view(),
&GlmOptions::new(GlmFamily::Odp, GlmDesign::CrossClassified),
)
.unwrap();
assert!(glm.converged);
assert!((glm.total_reserve - odp_exact.total_reserve).abs() < 1e-8);
assert!((glm.pearson_dispersion - odp_exact.dispersion).abs() < 1e-8);
assert!(
(glm.total_prediction_error - odp_exact.analytic_total_prediction_error).abs() < 1e-6
);
let h_sum: f64 = glm.leverages.iter().sum();
assert!((h_sum - glm.n_params as f64).abs() < 1e-10);
}
#[test]
fn hoerl_gamma_and_tweedie_recover_exact_synthetic_hoerl_triangle() {
let n = 7usize;
let b0 = 6.0;
let b1 = 1.25;
let b2 = -0.65;
let mut data = vec![NAN; n * n];
let mut true_reserve = 0.0;
for i in 0..n {
let alpha_i = 0.08 * i as f64;
let mut cum = 0.0;
for j in 0..n {
let d = (j + 1) as f64;
let inc = (b0 + alpha_i + b1 * d.ln() + b2 * d).exp();
if i + j < n {
cum += inc;
data[i * n + j] = cum;
} else {
true_reserve += inc;
}
}
}
let tri = Triangle::new(n, n, data);
for family in [
GlmFamily::Odp,
GlmFamily::Tweedie { power: 1.5 },
GlmFamily::Gamma,
GlmFamily::LogNormal,
] {
let res = fit_glm(
&tri.view(),
&GlmOptions::new(
family,
GlmDesign::Hoerl {
per_origin_intercepts: true,
},
),
)
.expect("hoerl fit");
assert!(res.converged);
assert!(
(res.total_reserve - true_reserve).abs() / true_reserve < 1e-8,
"family={family:?}: got {} expected {}",
res.total_reserve,
true_reserve
);
assert!(
res.deviance < 1e-12,
"family={family:?}: dev={}",
res.deviance
);
}
}
}