// Information mutuelle I(X;Y) = H(X) + H(Y) - H(X,Y), borne sous-additive,
// puis sous-modularite a trois variables : H(X,Z) + H(Y,Z) >= H(X,Y,Z) + H(Z).
fn entropy_bits<K>(dist: &[(K, f64)]) -> f64 {
-dist.iter().filter(|(_, p)| *p > 0.0).map(|(_, p)| p * p.log2()).sum::<f64>() + 0.0
}
fn joint_from_conditional(px: &[(i64, f64)], py_given_x: impl Fn(i64) -> Vec<(i64, f64)>)
-> Vec<((i64, i64), f64)> {
let mut joint = Vec::new();
for (x, p) in px {
for (y, q) in py_given_x(*x) {
if p * q > 0.0 { joint.push(((*x, y), p * q)); } // strict > : skip les atomes nuls (q = 1.0)
}
}
joint
}
fn canal_binaire(q: f64) -> Vec<((i64, i64), f64)> {
let px = vec![(0i64, 0.5), (1, 0.5)];
joint_from_conditional(&px, |x| vec![(x, q), (1 - x, 1.0 - q)]) // ordre d'insertion du dict Python
}
fn marginal(joint: &[((i64, i64), f64)], index: usize) -> Vec<(i64, f64)> {
let mut out: Vec<(i64, f64)> = Vec::new(); // Counter : cle par premiere apparition
for ((x, y), p) in joint {
let k = if index == 0 { *x } else { *y };
match out.iter_mut().find(|(kk, _)| *kk == k) { Some(e) => e.1 += p, None => out.push((k, *p)) }
}
out
}
fn marginal_2(joint3: &[((i64, i64, i64), f64)], keep: [usize; 2]) -> Vec<((i64, i64), f64)> {
let composante = |t: (i64, i64, i64), i: usize| match i { 0 => t.0, 1 => t.1, _ => t.2 };
let mut out: Vec<((i64, i64), f64)> = Vec::new(); // Counter : cle par premiere apparition
for (cle, p) in joint3 {
let k = (composante(*cle, keep[0]), composante(*cle, keep[1]));
match out.iter_mut().find(|(kk, _)| *kk == k) { Some(e) => e.1 += p, None => out.push((k, *p)) }
}
out
}
fn marginal_1(joint3: &[((i64, i64, i64), f64)], keep: usize) -> Vec<(i64, f64)> {
let composante = |t: (i64, i64, i64), i: usize| match i { 0 => t.0, 1 => t.1, _ => t.2 };
let mut out: Vec<(i64, f64)> = Vec::new(); // Counter : cle par premiere apparition
for (cle, p) in joint3 {
let k = composante(*cle, keep);
match out.iter_mut().find(|(kk, _)| *kk == k) { Some(e) => e.1 += p, None => out.push((k, *p)) }
}
out
}
fn information_mutuelle(joint: &[((i64, i64), f64)]) -> f64 {
let hx = entropy_bits(&marginal(joint, 0));
let hy = entropy_bits(&marginal(joint, 1));
let hxy = entropy_bits(joint);
hx + hy - hxy
}
fn py_bool(b: bool) -> &'static str { if b { "True" } else { "False" } }
let cas: Vec<(&str, Vec<((i64, i64), f64)>)> = vec![
("independantes (q=0.5)", canal_binaire(0.5)),
("correlees (q=0.9)", canal_binaire(0.9)),
("deterministes (q=1.0)", canal_binaire(1.0)),
];
println!("{:<24} {:>8} {:>10} {:>7} {:>8}", "regime", "H(X,Y)", "H(X)+H(Y)", "borne", "I(X;Y)");
for (nom, joint) in &cas {
let hx = entropy_bits(&marginal(joint, 0));
let hy = entropy_bits(&marginal(joint, 1));
let hxy = entropy_bits(joint);
println!("{:<24} {:>8.4} {:>10.4} {:>7} {:>8.4}", nom, hxy, hx + hy, py_bool(hxy <= hx + hy + 1e-12), information_mutuelle(joint));
}
println!();
// Sous-modularite a trois variables : H(X,Z) + H(Y,Z) >= H(X,Y,Z) + H(Z)
println!("Sous-modularite, verifiee sur le canal q=0.9 avec Z = X xor Y :");
let joint3: Vec<((i64, i64, i64), f64)> = canal_binaire(0.9).into_iter().map(|((x, y), p)| ((x, y, x ^ y), p)).collect();
let h_xyz = entropy_bits(&joint3);
let h_xz = entropy_bits(&marginal_2(&joint3, [0, 2]));
let h_yz = entropy_bits(&marginal_2(&joint3, [1, 2]));
let h_z = entropy_bits(&marginal_1(&joint3, 2));
let (gauche, droite) = (h_xz + h_yz, h_xyz + h_z);
println!(" H(X,Z) + H(Y,Z) = {:.4} + {:.4} = {:.4}", h_xz, h_yz, gauche);
println!(" H(X,Y,Z) + H(Z) = {:.4} + {:.4} = {:.4}", h_xyz, h_z, droite);
println!(" inegalite tenue : {} exces = {:.4} bit", py_bool(gauche >= droite - 1e-12), gauche - droite);