multiobjective_search/correlation.rs
1//! Edge-level correlation between objectives, the measure the benchmark
2//! suite of Peer et al. (arXiv 2603.24084, Section 2) uses to describe how
3//! redundant two objectives are, after Halle et al. 2025.
4
5use search_core::{BigUint, ExactArith, ExactNumber};
6
7/// The Pearson correlation of two objectives over a set of edges, kept
8/// exact: its sign and its square as a fraction.
9///
10/// `ρ = cov / sqrt(var_i · var_j)`, so `ρ² = cov² / (var_i · var_j)`, all
11/// computed from the integer sums `n Σxy - Σx Σy`, `n Σx² - (Σx)²`
12/// without rounding.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct Correlation {
15 /// `true` for `ρ < 0`.
16 pub negative: bool,
17 /// `cov²` (times `n²`).
18 pub numerator: BigUint,
19 /// `var_i · var_j` (times `n²`); zero when an objective is constant,
20 /// where the correlation is undefined.
21 pub denominator: BigUint,
22}
23
24impl Correlation {
25 /// Returns `true` if `|ρ| >= num / den`; `false` when undefined.
26 #[must_use]
27 pub fn at_least(&self, num: u64, den: u64) -> bool {
28 if self.denominator == BigUint::zero() {
29 return false;
30 }
31
32 let (num, den) = (
33 BigUint::from(u128::from(num)),
34 BigUint::from(u128::from(den)),
35 );
36 self.numerator.clone() * den.clone() * den >= num.clone() * num * self.denominator.clone()
37 }
38}
39
40/// The correlation of objectives `a` and `b` over the cost vectors of
41/// `edges`, for integer and decimal costs.
42///
43/// Correlation detection only: the survey of Salzman et al. and the
44/// benchmark paper do not define how correlated objectives would be
45/// merged, so no merging step is offered.
46///
47/// # Examples
48///
49/// ```
50/// use multiobjective_search::objective_correlation;
51///
52/// let edges = [[1_u32, 2, 5], [2, 4, 4], [3, 6, 1]];
53/// let r = objective_correlation(edges.iter().copied(), 0, 1);
54/// assert!(!r.negative && r.at_least(1, 1)); // perfectly correlated
55/// let r = objective_correlation(edges.iter().copied(), 0, 2);
56/// assert!(r.negative && r.at_least(9, 10));
57/// ```
58///
59/// # Panics
60///
61/// Panics if `a` or `b` is out of range.
62pub fn objective_correlation<C: ExactArith<Exact = BigUint>, const N: usize>(
63 edges: impl IntoIterator<Item = [C; N]>,
64 a: usize,
65 b: usize,
66) -> Correlation {
67 let zero = BigUint::zero;
68 let (mut count, mut sx, mut sy, mut sxy, mut sxx, mut syy) =
69 (0_u128, zero(), zero(), zero(), zero(), zero());
70 for cost in edges {
71 let (first, second) = (cost[a].exact(), cost[b].exact());
72 count += 1;
73 sxy += &first * &second;
74 sxx += &first * &first;
75 syy += &second * &second;
76 sx += first;
77 sy += second;
78 }
79
80 let n = BigUint::from(count);
81 let (products, means) = (n.clone() * sxy, sx.clone() * sy.clone());
82 let cov = products.abs_diff(&means);
83 let var_x = (n.clone() * sxx).abs_diff(&(sx.clone() * sx));
84 let var_y = (n * syy).abs_diff(&(sy.clone() * sy));
85
86 Correlation {
87 negative: products < means,
88 numerator: cov.clone() * cov,
89 denominator: var_x * var_y,
90 }
91}