using System;
using System.Collections.Generic;
using System.Linq;
public static class Bornes
{
// eps_H : Hoeffding distribution-free pour Bernoulli dans {0, 1}, (b-a) = 1.
public static double HoeffdingEps(double n, double p, double epsP = 0.05)
{
double L = Math.Log(2.0 / epsP);
return Math.Sqrt(L / (2.0 * n));
}
// eps_C : Chernoff (approximation KL quadratique), variance reelle p*(1-p).
public static double ChernoffEps(double n, double p, double epsP = 0.05)
{
double L = Math.Log(2.0 / epsP);
return Math.Sqrt(2.0 * L * p * (1.0 - p) / n);
}
// eps_B : Bernstein, inversion quadratique de l'inegalite en eps.
public static double BernsteinEps(double n, double p, double epsP = 0.05)
{
double L = Math.Log(2.0 / epsP);
double disc = 4.0 * L * L / 9.0 + 8.0 * n * L * p * (1.0 - p);
return (2.0 * L / 3.0 + Math.Sqrt(disc)) / (2.0 * n);
}
}
var BSC_INSTANCES = new (string Label, int N, double P, double EpsP)[]
{
("BSC-01", 100, 0.10, 0.05), ("BSC-02", 100, 0.20, 0.05),
("BSC-03", 100, 0.30, 0.05), ("BSC-04", 100, 0.40, 0.05),
("BSC-05", 100, 0.50, 0.05), ("BSC-06", 500, 0.10, 0.05),
("BSC-07", 500, 0.30, 0.05), ("BSC-08", 500, 0.50, 0.05),
("BSC-09", 1000, 0.10, 0.05), ("BSC-10", 1000, 0.20, 0.05),
("BSC-11", 1000, 0.30, 0.05), ("BSC-12", 1000, 0.40, 0.05),
("BSC-13", 1000, 0.50, 0.05), ("BSC-14", 5000, 0.05, 0.01),
("BSC-15", 5000, 0.10, 0.01), ("BSC-16", 5000, 0.30, 0.01),
("BSC-17", 5000, 0.50, 0.01), ("BSC-18", 10000, 0.10, 0.05),
("BSC-19", 10000, 0.30, 0.05), ("BSC-20", 10000, 0.50, 0.05),
};
// Formatage via string.Format : pas de guillemet imbrique dans un trou d'interpolation.
Console.WriteLine(new string('=', 95));
Console.WriteLine("TABLEAU COMPARATIF BORNES BSC : Hoeffding vs Chernoff vs Bernstein");
Console.WriteLine(new string('=', 95));
Console.WriteLine(string.Format("{0,10} {1,6} {2,5} {3,6} | {4,9} {5,9} {6,9} | {7,6} {8,6} {9,6}",
"Instance", "N", "p", "eps_p", "eps_H", "eps_C", "eps_B", "H/B", "C/B", "H/C"));
Console.WriteLine(new string('-', 95));
var ratiosHB = new List<double>();
var ratiosCB = new List<double>();
var ratiosHC = new List<double>();
foreach (var (label, n, p, epsP) in BSC_INSTANCES)
{
double eH = Bornes.HoeffdingEps(n, p, epsP);
double eC = Bornes.ChernoffEps(n, p, epsP);
double eB = Bornes.BernsteinEps(n, p, epsP);
double rHB = eH / eB, rCB = eC / eB, rHC = eH / eC;
ratiosHB.Add(rHB); ratiosCB.Add(rCB); ratiosHC.Add(rHC);
Console.WriteLine(string.Format("{0,10} {1,6} {2,5:F2} {3,6:F3} | {4,9:F5} {5,9:F5} {6,9:F5} | {7,6:F3} {8,6:F3} {9,6:F3}",
label, n, p, epsP, eH, eC, eB, rHB, rCB, rHC));
}
Console.WriteLine(new string('-', 95));
double Mean(List<double> xs) => xs.Average();
double Median(List<double> xs) { var s = xs.OrderBy(v => v).ToList(); return s[s.Count / 2]; }
Console.WriteLine(string.Format("{0,10} {1,6} {2,5} {3,6} | {4,9} {5,9} {6,9} | {7,6:F3} {8,6:F3} {9,6:F3}",
"MOYENNE", "", "", "", "", "", "", Mean(ratiosHB), Mean(ratiosCB), Mean(ratiosHC)));
Console.WriteLine(string.Format("{0,10} {1,6} {2,5} {3,6} | {4,9} {5,9} {6,9} | {7,6:F3} {8,6:F3} {9,6:F3}",
"MEDIANE", "", "", "", "", "", "", Median(ratiosHB), Median(ratiosCB), Median(ratiosHC)));
Console.WriteLine();
int nBTighterH = ratiosHB.Count(r => r > 1);
int nCTighterB = ratiosCB.Count(r => r < 1);
Console.WriteLine($"Bernstein plus tight que Hoeffding : {nBTighterH}/20");
Console.WriteLine($"Chernoff plus tight que Bernstein : {nCTighterB}/20 (mgf exacte de Chernoff vs borne en mu/sigma^2 : c'est le cas general pour Bernoulli)");