16 #ifndef TEFFiciencyHelper_h
17 #define TEFFiciencyHelper_h
29 class BinomialProbHelper {
31 BinomialProbHelper(
double rho,
int x,
int n)
32 : fRho(rho), fX(x), fN(n),
33 fRho_hat(double(x)/n),
34 fProb(ROOT::Math::binomial_pdf(x, rho, n)) {
37 fLRatio = pow(1 - rho, n);
39 fLRatio = pow(rho, n);
43 const double term1 = pow(rho/fRho_hat, x);
44 const double term2 = pow((1 - rho)/(1 - fRho_hat), n - x);
45 fLRatio = (term1 == 0. || term2 == 0.) ? 0. : term1 * term2;
49 double Rho ()
const {
return fRho; };
50 int X ()
const {
return fX; };
51 int N ()
const {
return fN; };
52 double Prob ()
const {
return fProb; };
53 double LRatio()
const {
return fLRatio; };
71 template <
typename Sorter>
72 class BinomialNeymanInterval {
75 BinomialNeymanInterval() :
81 void Init(
double alpha) {
87 bool Find_rho_set(
const double rho,
const int ntot,
int& x_l,
int& x_r)
const {
90 std::vector<BinomialProbHelper> probs;
91 for (
int i = 0; i <= ntot; ++i)
92 probs.emplace_back(BinomialProbHelper(rho, i, ntot));
93 std::sort(probs.begin(), probs.end(), fSorter);
100 const double target = 1 - fAlpha;
105 for (
int i = 0; i <= ntot && sum < target; ++i) {
106 sum += probs[i].Prob();
107 const int& x = probs[i].X();
108 if (x < x_l) x_l = x;
109 if (x > x_r) x_r = x;
117 bool Neyman(
const int ntot,
const int nrho,
double* rho,
double* x_l,
double* x_r) {
119 for (
int i = 0; i < nrho; ++i) {
120 rho[i] = double(i)/nrho;
121 Find_rho_set(rho[i], ntot, xL, xR);
130 void Calculate(
const double X,
const double n) {
133 const double tol = 1e-9;
134 double rho_min, rho_max, rho;
135 rho_min = rho_max = rho = 0.;
140 rho_min = 0; rho_max = 1;
141 while (std::abs(rho_max - rho_min) > tol) {
142 rho = (rho_min + rho_max)/2;
143 Find_rho_set(rho,
int(n), x_l, x_r);
153 rho_min = 0; rho_max = 1;
154 while (std::abs(rho_max - rho_min) > tol) {
155 rho = (rho_min + rho_max)/2;
156 Find_rho_set(rho,
int(n), x_l, x_r);
165 double Lower()
const {
return fLower; }
166 double Upper()
const {
return fUpper; }
176 void Set(
double l,
double u) { fLower = l; fUpper = u; }
183 struct FeldmanCousinsSorter {
184 bool operator()(
const BinomialProbHelper& l,
const BinomialProbHelper& r)
const {
185 return l.LRatio() > r.LRatio();
189 class FeldmanCousinsBinomialInterval :
public BinomialNeymanInterval<FeldmanCousinsSorter> {