// Primitive 2 — Décomposition projection / fibres sur un cas concret
// On considère deux scénarios, qui testent des identités DIFFÉRENTES :
// Cas 1 : projection sur Y (cours). Y n'est PAS une fonction de X,
// donc on ne peut pas écrire H(X) = H(π(X)) + H(X | π(X))
// avec π(X) := Y. La chaîne correcte est H(X) = I(X;Y) + H(X|Y),
// et l'écart H(Y) − I(X;Y) > 0 mesure l'information que Y porte
// sur autre chose que X.
// Cas 2 : projection π(X) = 𝟙{X ≥ 15}. π EST une fonction de X,
// donc on peut écrire H(X) = H(π(X)) + H(X | π(X)) ET
// I(X ; π(X)) = H(π(X)) ≠ 0.
const string sectionProjection = "Projection / fibres — notes";
// La référence Python est arrondie au millième : un demi-millième au plus,
// avec vérification supplémentaire de l'affichage exact au format F3.
const double PythonF3Tolerance = 0.0005;
void CheckPythonF3(string section, string label, double actual, double cited)
{
CheckNear(section, label + " (référence Python citée)", actual, cited, PythonF3Tolerance);
string shown = actual.ToString("F3", CultureInfo.InvariantCulture);
string expected = cited.ToString("F3", CultureInfo.InvariantCulture);
if (!string.Equals(shown, expected, StringComparison.Ordinal))
throw new InvalidOperationException($"{section} / {label} : affiché {shown}, attendu {expected}.");
}
// Normaliser les effectifs d'un échantillon pour obtenir les masses de sa loi empirique.
double[] EmpiricalProbabilities<T>(IEnumerable<T> sample)
{
int[] counts = sample.GroupBy(value => value).Select(group => group.Count()).ToArray();
int total = counts.Sum();
return counts.Select(count => (double)count / total).ToArray();
}
double EmpiricalEntropy<T>(IEnumerable<T> sample) => EntropyBits(EmpiricalProbabilities(sample));
double ConditionalGradeEntropy<TKey>(IEnumerable<IGrouping<TKey, int>> fibres, int total) =>
fibres.Sum(fibre => (double)fibre.Count() / total * EmpiricalEntropy(fibre));
// Données citées : les 25 paires (cours, note) de la cellule 12 du carnet Python.
var gradeData = new (string Course, int Grade)[]
{
("Math", 14), ("Math", 16), ("Math", 12), ("Math", 18), ("Math", 15),
("Phys", 11), ("Phys", 13), ("Phys", 9), ("Phys", 12), ("Phys", 14),
("Info", 17), ("Info", 19), ("Info", 16), ("Info", 18), ("Info", 20),
("Bio", 10), ("Bio", 12), ("Bio", 11), ("Bio", 13), ("Bio", 9),
("Chim", 13), ("Chim", 15), ("Chim", 14), ("Chim", 16), ("Chim", 12)
};
int gradeCount = gradeData.Length;
double hGrade = EmpiricalEntropy(gradeData.Select(row => row.Grade));
// === Cas 1 — projection sur Y (cours). Y n'est PAS fonction de X. ===
var courseFibres = gradeData.GroupBy(row => row.Course, row => row.Grade).ToArray();
double hGradeGivenCourse = ConditionalGradeEntropy(courseFibres, gradeCount);
double hCourse = EmpiricalEntropy(gradeData.Select(row => row.Course));
double iGradeCourse = hGrade - hGradeGivenCourse;
double courseGap = hCourse - iGradeCourse;
double courseChain = hGradeGivenCourse + iGradeCourse;
bool courseIsFunctionOfGrade = gradeData.GroupBy(row => row.Grade)
.All(fibre => fibre.Select(row => row.Course).Distinct().Count() == 1);
Console.WriteLine($"=== Cas 1 : projection sur Y (cours). Y {(courseIsFunctionOfGrade ? "est" : "n'est PAS")} fonction de X. ===");
Console.WriteLine(Inv($" H(X) = {hGrade:F3} bits"));
Console.WriteLine(Inv($" H(X | Y) = {hGradeGivenCourse:F3} bits (variation inexpliquée par le cours)"));
Console.WriteLine(Inv($" H(Y) = {hCourse:F3} bits"));
Console.WriteLine(Inv($" I(X;Y) = H(X) - H(X|Y) = {iGradeCourse:F3} bits (info que le cours porte sur la note)"));
Console.WriteLine(Inv($" Écart H(Y) − I(X;Y) = {courseGap:F3} bits"));
Console.WriteLine(Inv($" → I(X;Y) = {iGradeCourse:F3} ≠ H(Y) = {hCourse:F3} : Y porte {courseGap:F3} bits"));
Console.WriteLine(" d'information qui ne concernent pas X (témoin numérique de l'écart).");
Console.WriteLine(" C'est précisément pour ça qu'on ne peut PAS écrire");
Console.WriteLine(" H(X) = H(Y) + H(X|Y) : ce serait supposer Y = π(X) (fonction de X).");
Console.WriteLine(Inv($" Chaîne vérifiée : H(X) = {hGrade:F3} ≈ H(X|Y) + I(X;Y) = {courseChain:F3}"));
// Valeurs attendues citées de la sortie Python, cellule 12 (cas 1).
CheckPythonF3(sectionProjection, "H(X)", hGrade, 3.433);
CheckPythonF3(sectionProjection, "H(X | Y)", hGradeGivenCourse, 2.322);
CheckPythonF3(sectionProjection, "H(Y)", hCourse, 2.322);
CheckPythonF3(sectionProjection, "I(X;Y)", iGradeCourse, 1.111);
CheckPythonF3(sectionProjection, "H(Y) − I(X;Y)", courseGap, 1.211);
CheckPythonF3(sectionProjection, "H(X | Y) + I(X;Y)", courseChain, 3.433);
CheckNear(sectionProjection, "Chaîne avec Y", courseChain, hGrade);
CheckCondition(sectionProjection, "Y est fonction de X", courseIsFunctionOfGrade, false);
CheckCondition(sectionProjection, "H(Y) − I(X;Y) > 0", courseGap > NumericalTolerance);
// === Cas 2 — projection π(X) = 𝟙{X ≥ 15}. π est fonction de X. ===
const int gradeThreshold = 15; // Seuil cité du carnet Python.
var projectedGrades = gradeData.Select(row => (row.Grade, P: row.Grade >= gradeThreshold ? 1 : 0)).ToArray();
var gradeFibres = projectedGrades.GroupBy(row => row.P, row => row.Grade).ToArray();
double hIndicator = EmpiricalEntropy(projectedGrades.Select(row => row.P));
double hGradeGivenIndicator = ConditionalGradeEntropy(gradeFibres, gradeCount);
double iGradeIndicator = hGrade - hGradeGivenIndicator;
double indicatorChain = hIndicator + hGradeGivenIndicator;
bool indicatorIdentity = Math.Abs(iGradeIndicator - hIndicator) < NumericalTolerance;
Console.WriteLine();
Console.WriteLine(Inv($"=== Cas 2 : projection π(X) = 𝟙{{X ≥ {gradeThreshold}}}. π est fonction de X. ==="));
Console.WriteLine(Inv($" H(π(X)) = {hIndicator:F3} bits (entropie de l'indicatrice 'note ≥ {gradeThreshold}')"));
Console.WriteLine(Inv($" H(X | π(X)) = {hGradeGivenIndicator:F3} bits (variation intra-fibre)"));
Console.WriteLine(Inv($" I(X ; π(X)) = H(X) - H(X | π(X)) = {iGradeIndicator:F3} bits"));
Console.WriteLine(Inv($" I(X ; π(X)) = H(π(X)) ? {indicatorIdentity} → I(X;π(X)) = {iGradeIndicator:F3}"));
Console.WriteLine(" ≢ 0 bits.");
Console.WriteLine(" Ici π(X) est une fonction de X, donc l'identité correcte est");
Console.WriteLine(" I(X ; π(X)) = H(π(X)) : la projection porte TOUTE son information");
Console.WriteLine(" de X, et l'écart à 0 mesure exactement le contenu informationnel de π.");
Console.WriteLine(Inv($" Chaîne vérifiée : H(X) = {hGrade:F3} ≈ H(π(X)) + H(X | π(X)) = {indicatorChain:F3}"));
// === Témoins directs des propriétés du résidu (section 2.1) ===
// Résidu r = X − π_num(X) où π_num = 1 si X ≥ 15, 0 sinon. La prose
// des sections 2.1 et 2.2 cite I(X−π(X); π(X)) ≈ 0.78 bit et Cov ≈ 0.98
// — on les calcule ici sur les 25 paires du carnet, qui sont la base
// de toutes les autres mesures (donc falsifiables par le lecteur).
// Ces deux témoins mesurent que π = 𝟙{X≥15} n'est PAS la projection L2 :
// l'orthogonalité E[X−π(X)|π(X)] = 0 est garantie pour la projection L2 (régression).
// Ici le résidu n'est ni indépendant (I ≠ 0) ni même décorrélé
// (Cov ≠ 0) — la projection binaire capte l'ordre mais pas la valeur.
var residualPairs = projectedGrades.Select(row => (R: row.Grade - row.P, row.P)).ToArray();
double hResidual = EmpiricalEntropy(residualPairs.Select(row => row.R));
double hResidualIndicator = EmpiricalEntropy(residualPairs);
double iResidualIndicator = hResidual + hIndicator - hResidualIndicator;
double meanResidual = residualPairs.Average(row => row.R);
double meanIndicator = residualPairs.Average(row => row.P);
double covResidualIndicator = residualPairs.Average(row => (row.R - meanResidual) * (row.P - meanIndicator));
Console.WriteLine(Inv($" I(X−π(X); π(X)) = {iResidualIndicator:F3} bits"));
Console.WriteLine(Inv($" Cov(X−π(X), π(X)) = {covResidualIndicator:F3}"));
// === Témoin E[X−π(X) | π(X)] = 0 ? Garantie pour la projection L2 M = 𝔼[X|·] ===
// Pour la projection P = 𝟙{X≥15} (Cas 2), l'orthogonalité NE TIENT PAS :
// E[X−P | P=high] et E[X−P | P=low] diffèrent et ne sont pas nulles.
// Ces moyennes sont calculées sur les mêmes paires que I et Cov.
double meanResidualHigh = residualPairs.Where(row => row.P == 1).Average(row => row.R);
double meanResidualLow = residualPairs.Where(row => row.P == 0).Average(row => row.R);
Console.WriteLine(Inv($" E[X−π(X) | π(X)=high] = {meanResidualHigh:F3} (témoin : ≠ 0)"));
Console.WriteLine(Inv($" E[X−π(X) | π(X)=low] = {meanResidualLow:F3} (témoin : ≠ 0)"));
Console.WriteLine(Inv($" → π = 𝟙{{X≥{gradeThreshold}}} n'est PAS la projection L2 M = 𝔼[X|·] :"));
Console.WriteLine(" l'orthogonalité 𝔼[X−M | M] = 0 est garantie pour M, pas pour une projection générale P.");
// Valeurs attendues citées de la sortie Python, cellule 12 (cas 2 et résidus).
CheckPythonF3(sectionProjection, "H(π(X))", hIndicator, 0.971);
CheckPythonF3(sectionProjection, "H(X | π(X))", hGradeGivenIndicator, 2.462);
CheckPythonF3(sectionProjection, "I(X;π(X))", iGradeIndicator, 0.971);
CheckPythonF3(sectionProjection, "H(π(X)) + H(X | π(X))", indicatorChain, 3.433);
CheckPythonF3(sectionProjection, "I(X−π(X);π(X))", iResidualIndicator, 0.777);
CheckPythonF3(sectionProjection, "Cov(X−π(X), π(X))", covResidualIndicator, 0.976);
CheckPythonF3(sectionProjection, "E[X−π(X) | π(X)=high]", meanResidualHigh, 16.000);
CheckPythonF3(sectionProjection, "E[X−π(X) | π(X)=low]", meanResidualLow, 11.933);
CheckNear(sectionProjection, "Chaîne avec π(X)", indicatorChain, hGrade);
CheckCondition(sectionProjection, "I(X;π(X)) = H(π(X))", indicatorIdentity);
CheckCondition(sectionProjection, "I(X;π(X)) ≠ 0", iGradeIndicator > NumericalTolerance);
CheckCondition(sectionProjection, "I(X−π(X);π(X)) ≠ 0", iResidualIndicator > NumericalTolerance);
CheckCondition(sectionProjection, "Cov(X−π(X), π(X)) ≠ 0", Math.Abs(covResidualIndicator) > NumericalTolerance);
CheckCondition(sectionProjection, "E[X−π(X) | π(X)=high] ≠ 0", Math.Abs(meanResidualHigh) > NumericalTolerance);
CheckCondition(sectionProjection, "E[X−π(X) | π(X)=low] ≠ 0", Math.Abs(meanResidualLow) > NumericalTolerance);