// I(X;Y) = H(X) + H(Y) - H(X,Y).
using System;
using System.Collections.Generic;
static class Information
{
public static double Mutuelle(Dictionary<(int, int), double> joint)
{
double hx = Entropie.Bits(Entropie.Marginale<int>(joint, 0));
double hy = Entropie.Bits(Entropie.Marginale<int>(joint, 1));
double hxy = Entropie.Bits(joint);
return hx + hy - hxy;
}
}
var cas = new (string nom, Dictionary<(int, int), double> joint)[]
{
("independantes (q=0.5)", CanalBinaire.De(0.5)),
("correlees (q=0.9)", CanalBinaire.De(0.9)),
("deterministes (q=1.0)", CanalBinaire.De(1.0)),
};
Console.WriteLine($"{"regime",-24} {"H(X,Y)",8} {"H(X)+H(Y)",10} {"borne",7} {"I(X;Y)",8}");
foreach (var (nom, joint) in cas)
{
double hx = Entropie.Bits(Entropie.Marginale<int>(joint, 0));
double hy = Entropie.Bits(Entropie.Marginale<int>(joint, 1));
double hxy = Entropie.Bits(joint);
bool borneOk = hxy <= hx + hy + 1e-12;
Console.WriteLine($"{nom,-24} {hxy,8:F4} {hx + hy,10:F4} {borneOk,7} {Information.Mutuelle(joint),8:F4}");
}
Console.WriteLine();
// Loi marginale de la jointe a trois variables, en gardant les indices `keep`.
static class Marginale3
{
public static Dictionary<(int, int), double> De(Dictionary<(int, int, int), double> joint3, int[] keep)
{
var outp = new Dictionary<(int, int), double>();
foreach (var kv in joint3)
{
var (x, y, z) = kv.Key;
int a = keep[0] == 0 ? x : (keep[0] == 1 ? y : z);
int b = keep[1] == 0 ? x : (keep[1] == 1 ? y : z);
var key = (a, b);
outp[key] = outp.GetValueOrDefault(key) + kv.Value;
}
return outp;
}
public static Dictionary<int, double> De1(Dictionary<(int, int, int), double> joint3, int keep)
{
var outp = new Dictionary<int, double>();
foreach (var kv in joint3)
{
var (x, y, z) = kv.Key;
int k = keep == 0 ? x : (keep == 1 ? y : z);
outp[k] = outp.GetValueOrDefault(k) + kv.Value;
}
return outp;
}
}
// Sous-modularite a trois variables : H(X,Z) + H(Y,Z) >= H(X,Y,Z) + H(Z)
Console.WriteLine("Sous-modularite, verifiee sur le canal q=0.9 avec Z = X xor Y :");
var joint3 = new Dictionary<(int, int, int), double>();
foreach (var kv in CanalBinaire.De(0.9))
joint3[(kv.Key.Item1, kv.Key.Item2, kv.Key.Item1 ^ kv.Key.Item2)] = kv.Value;
double hXyz = Entropie.Bits(joint3);
double hXz = Entropie.Bits(Marginale3.De(joint3, new[] { 0, 2 }));
double hYz = Entropie.Bits(Marginale3.De(joint3, new[] { 1, 2 }));
double hZ = Entropie.Bits(Marginale3.De1(joint3, 2));
double gauche = hXz + hYz;
double droite = hXyz + hZ;
Console.WriteLine($" H(X,Z) + H(Y,Z) = {hXz:F4} + {hYz:F4} = {gauche:F4}");
Console.WriteLine($" H(X,Y,Z) + H(Z) = {hXyz:F4} + {hZ:F4} = {droite:F4}");
Console.WriteLine($" inegalite tenue : {gauche >= droite - 1e-12} exces = {gauche - droite:F4} bit");