use std::f64::consts::PI;
use crate::bootstrap_odp::{
DeterministicRng, OdpBootstrapOptions, OdpBootstrapScratch, OdpFit, ResidualAdjustment,
build_adjusted_residual_pool, cholesky_lower, fit_odp, run_odp_iteration_with_scratch,
};
use crate::reserve_distribution::{ReserveDistributionSummary, summarize_with_scratch};
use crate::types::TriangleView;
use crate::validate::ValidationError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum CorrelationType {
#[default]
LatentGaussian,
SpearmanTarget,
KendallTarget,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ImanConoverResult {
pub n_scenarios: usize,
pub n_variables: usize,
pub permutations: Vec<Vec<usize>>,
pub reordered_columns: Vec<Vec<f64>>,
pub effective_latent_correlation: Vec<f64>,
pub achieved_spearman: Vec<f64>,
pub achieved_pearson: Vec<f64>,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct SynchronizedBootstrapResult {
pub line_fits: Vec<OdpFit>,
pub line_totals: Vec<Vec<f64>>,
pub aggregate_totals: Vec<f64>,
pub line_summaries: Vec<ReserveDistributionSummary>,
pub aggregate_summary: ReserveDistributionSummary,
pub std_dev_diversification_benefit: f64,
pub achieved_spearman: Vec<f64>,
pub achieved_pearson: Vec<f64>,
pub failed_iterations: usize,
}
pub(crate) fn inv_normal_cdf(p: f64) -> f64 {
const A: [f64; 6] = [
-3.969_683_028_665_376e1,
2.209_460_984_245_205e2,
-2.759_285_104_469_687e2,
1.383_577_518_672_69e2,
-3.066_479_806_614_716e1,
2.506_628_277_459_239e0,
];
const B: [f64; 5] = [
-5.447_609_879_822_406e1,
1.615_858_368_580_409e2,
-1.556_989_798_598_866e2,
6.680_131_188_771_972e1,
-1.328_068_155_288_572e1,
];
const C: [f64; 6] = [
-7.784_894_002_430_293e-3,
-3.223_964_580_411_365e-1,
-2.400_758_277_161_838e0,
-2.549_732_539_343_734e0,
4.374_664_141_464_968e0,
2.938_163_982_698_783e0,
];
const D: [f64; 4] = [
7.784_695_709_041_462e-3,
3.224_671_290_700_398e-1,
2.445_134_137_142_996e0,
3.754_408_661_907_416e0,
];
const P_LOW: f64 = 0.02425;
const P_HIGH: f64 = 1.0 - P_LOW;
if p < P_LOW {
let q = (-2.0 * p.ln()).sqrt();
(((((C[0] * q + C[1]) * q + C[2]) * q + C[3]) * q + C[4]) * q + C[5])
/ ((((D[0] * q + D[1]) * q + D[2]) * q + D[3]) * q + 1.0)
} else if p <= P_HIGH {
let q = p - 0.5;
let r = q * q;
(((((A[0] * r + A[1]) * r + A[2]) * r + A[3]) * r + A[4]) * r + A[5]) * q
/ (((((B[0] * r + B[1]) * r + B[2]) * r + B[3]) * r + B[4]) * r + 1.0)
} else {
let q = (-2.0 * (1.0 - p).ln()).sqrt();
-(((((C[0] * q + C[1]) * q + C[2]) * q + C[3]) * q + C[4]) * q + C[5])
/ ((((D[0] * q + D[1]) * q + D[2]) * q + D[3]) * q + 1.0)
}
}
pub(crate) fn average_ranks(values: &[f64]) -> Vec<f64> {
let n = values.len();
let mut order: Vec<usize> = (0..n).collect();
order.sort_unstable_by(|&a, &b| values[a].total_cmp(&values[b]));
let mut ranks = vec![0.0; n];
let mut i = 0;
while i < n {
let mut j = i + 1;
while j < n && values[order[j]] == values[order[i]] {
j += 1;
}
let avg_rank = 0.5 * ((i + 1) as f64 + j as f64);
for &idx in &order[i..j] {
ranks[idx] = avg_rank;
}
i = j;
}
ranks
}
pub fn pearson_correlation(x: &[f64], y: &[f64]) -> f64 {
let n = x.len();
if n < 2 || n != y.len() {
return f64::NAN;
}
let nf = n as f64;
let mx = x.iter().sum::<f64>() / nf;
let my = y.iter().sum::<f64>() / nf;
let mut sxx = 0.0;
let mut syy = 0.0;
let mut sxy = 0.0;
for (&a, &b) in x.iter().zip(y.iter()) {
let dx = a - mx;
let dy = b - my;
sxx += dx * dx;
syy += dy * dy;
sxy += dx * dy;
}
let denom = (sxx * syy).sqrt();
if denom > 0.0 {
(sxy / denom).clamp(-1.0, 1.0)
} else {
f64::NAN
}
}
pub fn spearman_correlation(x: &[f64], y: &[f64]) -> f64 {
if x.len() < 2 || x.len() != y.len() {
return f64::NAN;
}
if x.iter().any(|v| v.is_nan()) || y.iter().any(|v| v.is_nan()) {
return f64::NAN;
}
let rx = average_ranks(x);
let ry = average_ranks(y);
pearson_correlation(&rx, &ry)
}
fn correlation_matrix(columns: &[Vec<f64>], spearman: bool) -> Vec<f64> {
let k = columns.len();
let mut out = vec![0.0; k * k];
let ranked: Vec<Vec<f64>> = if spearman {
columns.iter().map(|c| average_ranks(c)).collect()
} else {
Vec::new()
};
let slice_at = |idx: usize| -> &[f64] {
if spearman {
&ranked[idx]
} else {
&columns[idx]
}
};
let mut means = vec![0.0; k];
let mut inv_norms = vec![0.0; k];
for i in 0..k {
let col = slice_at(i);
let nf = col.len() as f64;
let m = col.iter().sum::<f64>() / nf;
means[i] = m;
let ss: f64 = col.iter().map(|&v| (v - m) * (v - m)).sum();
inv_norms[i] = if ss > 0.0 { 1.0 / ss.sqrt() } else { f64::NAN };
}
for i in 0..k {
out[i * k + i] = 1.0;
let ci = slice_at(i);
let mi = means[i];
let ni = inv_norms[i];
for j in (i + 1)..k {
let cj = slice_at(j);
let mj = means[j];
let nj = inv_norms[j];
let r = if ni.is_finite() && nj.is_finite() {
let mut sxy = 0.0;
for (&a, &b) in ci.iter().zip(cj.iter()) {
sxy += (a - mi) * (b - mj);
}
(sxy * ni * nj).clamp(-1.0, 1.0)
} else {
f64::NAN
};
out[i * k + j] = r;
out[j * k + i] = r;
}
}
out
}
fn solve_upper_from_lower_transpose(l: &[f64], dim: usize, b: &[f64]) -> Vec<f64> {
let mut x = vec![0.0; dim];
for i in (0..dim).rev() {
let mut sum = b[i];
for j in (i + 1)..dim {
sum -= l[j * dim + i] * x[j];
}
x[i] = sum / l[i * dim + i];
}
x
}
fn validate_and_convert_correlation_matrix(
target_corr: &[f64],
k: usize,
corr_type: CorrelationType,
) -> Result<Vec<f64>, ValidationError> {
if target_corr.len() != k * k {
return Err(ValidationError::ShapeMismatch);
}
let mut s = vec![0.0; k * k];
for i in 0..k {
for j in 0..k {
let v = target_corr[i * k + j];
if !v.is_finite() {
return Err(ValidationError::NotFinite);
}
if i == j {
if (v - 1.0).abs() > 1e-10 {
return Err(ValidationError::InvalidParameter(
"correlation matrix diagonal entries must equal 1.0",
));
}
s[i * k + j] = 1.0;
} else {
let sym = target_corr[j * k + i];
if (v - sym).abs() > 1e-10 {
return Err(ValidationError::InvalidParameter(
"correlation matrix must be symmetric",
));
}
if !(-1.0..=1.0).contains(&v) {
return Err(ValidationError::InvalidParameter(
"correlation matrix entries must lie in [-1, 1]",
));
}
s[i * k + j] = match corr_type {
CorrelationType::LatentGaussian => v,
CorrelationType::SpearmanTarget => 2.0 * (PI * v / 6.0).sin(),
CorrelationType::KendallTarget => (PI * v / 2.0).sin(),
};
}
}
}
Ok(s)
}
pub fn iman_conover(
columns: &[&[f64]],
target_corr: &[f64],
corr_type: CorrelationType,
seed: u64,
) -> Result<ImanConoverResult, ValidationError> {
let k = columns.len();
if k < 2 {
return Err(ValidationError::InvalidParameter(
"Iman-Conover requires at least 2 variables",
));
}
let n = columns[0].len();
if n <= k {
return Err(ValidationError::InvalidParameter(
"Iman-Conover requires more scenarios than variables (N > K)",
));
}
for col in columns {
if col.len() != n {
return Err(ValidationError::ShapeMismatch);
}
if col.iter().any(|v| !v.is_finite()) {
return Err(ValidationError::NotFinite);
}
}
let s = validate_and_convert_correlation_matrix(target_corr, k, corr_type)?;
let l_s = cholesky_lower(&s, k)?;
let mut base_scores: Vec<f64> = (0..n)
.map(|r| inv_normal_cdf((r as f64 + 1.0) / (n as f64 + 1.0)))
.collect();
let mean_z = base_scores.iter().sum::<f64>() / n as f64;
let var_z = base_scores
.iter()
.map(|z| (z - mean_z).powi(2))
.sum::<f64>()
/ (n as f64 - 1.0);
let sd_z = var_z.sqrt();
for z in &mut base_scores {
*z = (*z - mean_z) / sd_z;
}
let mut rng = DeterministicRng::from_seed(seed);
let mut m_cols: Vec<Vec<f64>> = Vec::with_capacity(k);
let mut l_e_opt = None;
for _attempt in 0..16 {
m_cols.clear();
for _ in 0..k {
let mut col = base_scores.clone();
for i in (1..n).rev() {
let j = rng.next_index(i + 1);
col.swap(i, j);
}
m_cols.push(col);
}
let e = correlation_matrix(&m_cols, false);
if let Ok(l_e) = cholesky_lower(&e, k) {
l_e_opt = Some(l_e);
break;
}
}
let l_e = l_e_opt.ok_or(ValidationError::InvalidParameter(
"failed to generate full-rank normal score matrix E",
))?;
let mut a_cols: Vec<Vec<f64>> = Vec::with_capacity(k);
for col_j in 0..k {
let mut c_col = vec![0.0; k];
for row_i in 0..=col_j {
c_col[row_i] = l_s[col_j * k + row_i];
}
a_cols.push(solve_upper_from_lower_transpose(&l_e, k, &c_col));
}
let mut permutations = Vec::with_capacity(k);
let mut reordered_columns = Vec::with_capacity(k);
let mut t_col = vec![0.0; n];
let mut t_order: Vec<usize> = (0..n).collect();
let mut val_order: Vec<usize> = (0..n).collect();
for col_j in 0..k {
let a_col = &a_cols[col_j];
t_col.fill(0.0);
for p in 0..k {
let ap = a_col[p];
if ap != 0.0 {
for (dst, &mp) in t_col.iter_mut().zip(m_cols[p].iter()) {
*dst += mp * ap;
}
}
}
for (idx, slot) in t_order.iter_mut().enumerate() {
*slot = idx;
}
t_order.sort_unstable_by(|&a, &b| t_col[a].total_cmp(&t_col[b]));
let col_vals = columns[col_j];
for (idx, slot) in val_order.iter_mut().enumerate() {
*slot = idx;
}
val_order.sort_unstable_by(|&a, &b| col_vals[a].total_cmp(&col_vals[b]));
let mut tie_start = 0usize;
while tie_start < n {
let v0 = col_vals[val_order[tie_start]];
let mut tie_end = tie_start + 1;
while tie_end < n && col_vals[val_order[tie_end]] == v0 {
tie_end += 1;
}
if tie_end - tie_start > 1 {
for m in (tie_start + 1..tie_end).rev() {
let swap_idx = tie_start + rng.next_index(m - tie_start + 1);
val_order.swap(m, swap_idx);
}
}
tie_start = tie_end;
}
let mut perm = vec![0usize; n];
let mut reordered = vec![0.0; n];
for rank_idx in 0..n {
let dst_row = t_order[rank_idx];
let src_row = val_order[rank_idx];
perm[dst_row] = src_row;
reordered[dst_row] = col_vals[src_row];
}
permutations.push(perm);
reordered_columns.push(reordered);
}
let achieved_spearman = correlation_matrix(&reordered_columns, true);
let achieved_pearson = correlation_matrix(&reordered_columns, false);
Ok(ImanConoverResult {
n_scenarios: n,
n_variables: k,
permutations,
reordered_columns,
effective_latent_correlation: s,
achieved_spearman,
achieved_pearson,
})
}
pub fn synchronized_odp_bootstrap(
triangles: &[TriangleView<'_>],
tails: &[f64],
options: OdpBootstrapOptions,
) -> Result<SynchronizedBootstrapResult, ValidationError> {
let k = triangles.len();
if k < 2 {
return Err(ValidationError::InvalidParameter(
"synchronized ODP bootstrap requires at least 2 triangles",
));
}
if tails.len() != k || options.iterations == 0 {
return Err(ValidationError::ShapeMismatch);
}
let n0 = triangles[0].n_origins;
for t in triangles {
if t.n_origins != n0 || t.n_devs != n0 {
return Err(ValidationError::ShapeMismatch);
}
}
let mut line_fits = Vec::with_capacity(k);
let mut pools = Vec::with_capacity(k);
let mut coord_signature: Option<Vec<(usize, usize)>> = None;
for (t, &tail) in triangles.iter().zip(tails.iter()) {
let fit = fit_odp(t, tail, options.monotonicity_policy)?;
let coords: Vec<(usize, usize)> = match options.residual_adjustment {
ResidualAdjustment::DegreesOfFreedom => {
fit.residuals.iter().map(|r| (r.origin, r.dev)).collect()
}
ResidualAdjustment::HatMatrixLeverage => fit
.residuals
.iter()
.filter(|r| !r.saturated)
.map(|r| (r.origin, r.dev))
.collect(),
};
if let Some(ref expected) = coord_signature {
if &coords != expected {
return Err(ValidationError::InvalidParameter(
"synchronized bootstrap requires identical eligible residual coordinates across lines",
));
}
} else {
coord_signature = Some(coords);
}
let (pool, _) = build_adjusted_residual_pool(
&fit,
options.residual_adjustment,
options.center_residuals,
)?;
line_fits.push(fit);
pools.push(pool);
}
let pool_len = pools[0].len();
let n_obs = line_fits[0].n_observed;
let mut rng = DeterministicRng::from_seed(options.seed);
let mut line_totals: Vec<Vec<f64>> = (0..k)
.map(|_| Vec::with_capacity(options.iterations))
.collect();
let mut aggregate_totals = Vec::with_capacity(options.iterations);
let mut shared_indices = vec![0usize; n_obs];
let mut line_residuals = vec![0.0; n_obs];
let mut scratches: Vec<OdpBootstrapScratch> =
line_fits.iter().map(OdpBootstrapScratch::for_fit).collect();
let mut draw_totals = vec![0.0; k];
let mut failed_iterations = 0usize;
for _ in 0..options.iterations {
for slot in &mut shared_indices {
*slot = rng.next_index(pool_len);
}
let mut all_ok = true;
for line in 0..k {
for (dst, &pool_idx) in line_residuals.iter_mut().zip(shared_indices.iter()) {
*dst = pools[line][pool_idx];
}
match run_odp_iteration_with_scratch(
&line_fits[line],
&line_residuals,
options.process_distribution,
&mut rng,
&mut scratches[line],
) {
Some(origin_res) => {
let tot: f64 = origin_res.iter().sum();
if tot.is_finite() {
draw_totals[line] = tot;
} else {
all_ok = false;
break;
}
}
None => {
all_ok = false;
break;
}
}
}
if all_ok {
let agg: f64 = draw_totals.iter().sum();
aggregate_totals.push(agg);
for (line, &tot) in draw_totals.iter().enumerate() {
line_totals[line].push(tot);
}
} else {
failed_iterations += 1;
}
}
if aggregate_totals.is_empty() {
return Err(ValidationError::NotFinite);
}
let mut summary_buf = Vec::with_capacity(aggregate_totals.len());
let mut line_summaries = Vec::with_capacity(k);
for series in &line_totals {
line_summaries.push(summarize_with_scratch(series, &mut summary_buf, true)?);
}
let aggregate_summary = summarize_with_scratch(&aggregate_totals, &mut summary_buf, true)?;
let sum_standalone_sd: f64 = line_summaries.iter().map(|s| s.std_dev).sum();
let std_dev_diversification_benefit = sum_standalone_sd - aggregate_summary.std_dev;
let achieved_spearman = correlation_matrix(&line_totals, true);
let achieved_pearson = correlation_matrix(&line_totals, false);
Ok(SynchronizedBootstrapResult {
line_fits,
line_totals,
aggregate_totals,
line_summaries,
aggregate_summary,
std_dev_diversification_benefit,
achieved_spearman,
achieved_pearson,
failed_iterations,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::Triangle;
#[test]
fn iman_conover_preserves_marginals_and_induces_target_rank_correlation() {
let mut rng = DeterministicRng::from_seed(42069);
let n = 4_000usize;
let x: Vec<f64> = (0..n)
.map(|_| (10.0 + 0.8 * rng.next_standard_normal()).exp())
.collect();
let y: Vec<f64> = (0..n)
.map(|_| (11.0 + 1.1 * rng.next_standard_normal()).exp())
.collect();
let z: Vec<f64> = (0..n)
.map(|_| (9.5 + 0.5 * rng.next_standard_normal()).exp())
.collect();
#[rustfmt::skip]
let target = [
1.0, 0.65, -0.30,
0.65, 1.0, 0.20,
-0.30, 0.20, 1.0,
];
let res = iman_conover(
&[&x, &y, &z],
&target,
CorrelationType::SpearmanTarget,
12345,
)
.expect("iman_conover");
for (orig, reord) in [&x, &y, &z].iter().zip(res.reordered_columns.iter()) {
let mut a = (*orig).clone();
let mut b = reord.clone();
a.sort_by(f64::total_cmp);
b.sort_by(f64::total_cmp);
assert_eq!(a, b);
}
for (col, orig) in [&x, &y, &z].iter().enumerate() {
for r in 0..n {
assert_eq!(
res.reordered_columns[col][r],
orig[res.permutations[col][r]]
);
}
}
assert!(
(res.achieved_spearman[1] - 0.65).abs() < 0.025,
"rho_01={}",
res.achieved_spearman[1]
);
assert!(
(res.achieved_spearman[2] - (-0.30)).abs() < 0.025,
"rho_02={}",
res.achieved_spearman[2]
);
assert!(
(res.achieved_spearman[5] - 0.20).abs() < 0.025,
"rho_12={}",
res.achieved_spearman[5]
);
}
#[test]
fn iman_conover_rejects_non_positive_definite_matrix() {
let a = [1.0, 2.0, 3.0, 4.0, 5.0];
let b = [2.0, 3.0, 4.0, 5.0, 6.0];
let c = [3.0, 4.0, 5.0, 6.0, 7.0];
#[rustfmt::skip]
let bad = [
1.0, 0.9, 0.9,
0.9, 1.0, -0.9,
0.9, -0.9, 1.0,
];
assert!(matches!(
iman_conover(&[&a, &b, &c], &bad, CorrelationType::LatentGaussian, 1),
Err(ValidationError::InvalidParameter(_))
));
}
#[test]
fn synchronized_bootstrap_preserves_positive_dependence_on_comonotonic_lines() {
const NAN: f64 = f64::NAN;
#[rustfmt::skip]
let t1 = Triangle::new(5, 5, vec![
1000.0, 1520.0, 1730.0, 1820.0, 1860.0,
1150.0, 1690.0, 1980.0, 2060.0, NAN,
1320.0, 2090.0, 2320.0, NAN, NAN,
1480.0, 2110.0, NAN, NAN, NAN,
1650.0, NAN, NAN, NAN, NAN,
]);
let t2 = Triangle::new(
5,
5,
t1.data
.iter()
.map(|v| if v.is_finite() { v * 2.0 } else { NAN })
.collect(),
);
let opts = OdpBootstrapOptions::shapland(400, 42069);
let sync = synchronized_odp_bootstrap(&[t1.view(), t2.view()], &[1.0, 1.0], opts)
.expect("synchronized bootstrap");
assert!(
sync.achieved_spearman[1] > 0.30,
"spearman={}",
sync.achieved_spearman[1]
);
assert!(sync.std_dev_diversification_benefit >= 0.0);
}
}