CMS 3D CMS Logo

confidence.cc

Go to the documentation of this file.
00001 #include <limits>
00002 #include <cmath>
00003 
00004 #include "Math/QuantFuncMathCore.h"
00005 #include "TGraphAsymmErrors.h"
00006 
00007 #include "confidence.h"
00008 
00009 /* 
00010 Confidence level estimators for a Binomial distribution.
00011 
00012 For a detailed discussion see:
00013     "Interval Estimation for a Binomial Proportion", 
00014     L. D. Brown, T. T. Cai and A. DasGupta, 
00015     2001, Statistical Sciences 16 (2) 101-133, 
00016     https://www-stat.wharton.upenn.edu/~tcai/paper/Binomial-StatSci.pdf
00017 
00018 The D0 implementation is detailed in the source code and documentation for the class TGraphAsymmErrors.
00019 
00020 Implementation by Andrea Bocci <andrea.bocci@cern.ch>
00021 Last modified on Wed Jun 25 19:23:52 CEST 2008
00022 
00023 This is free software, licenced under the GNU LGPL version 2.1, or (at your option) any later version.
00024 */
00025 
00026 namespace confidence {
00027 
00028 // default implementation
00029 interval confidence_binomial(unsigned int n, unsigned int k, double level) {
00030   return confidence_binomial_jeffreys(n, k, level);
00031 }
00032 
00033 
00034 // Clopper-Pearson "exact" interval
00035 interval confidence_binomial_clopper_pearson(unsigned int n, unsigned int k, double level) {
00036   double min = std::numeric_limits<double>::quiet_NaN();
00037   double max = std::numeric_limits<double>::quiet_NaN();
00038 
00039   if (k <= n) {
00040     double alpha = (1.0 - level) / 2;
00041     min = (k == 0) ? 0.0 : ROOT::Math::beta_quantile(      alpha, k,       n-k + 1.0);
00042     max = (k == n) ? 1.0 : ROOT::Math::beta_quantile(1.0 - alpha, k + 1.0, n-k);
00043   }
00044   return std::make_pair(min, max);
00045 }
00046 
00047 //#include <iostream>
00048 // normal approximation
00049 interval confidence_binomial_normal(unsigned int n, unsigned int k, double level) {
00050   double min = std::numeric_limits<double>::quiet_NaN();
00051   double max = std::numeric_limits<double>::quiet_NaN();
00052 
00053   if (k <= n) {
00054     double alpha = (1.0 - level) / 2;
00055     double average = (double) k / n;
00056     double sigma = std::sqrt(average * (1-average) / n);
00057     double delta = ROOT::Math::normal_quantile(1.0 - alpha, sigma);
00058     min = ((average - delta) < 0.) ? 0. : (average - delta);
00059     max = ((average + delta) > 1.) ? 1. : (average + delta);
00060   }
00061   return std::make_pair(min, max);
00062 }
00063 
00064 // Jeffreys prior interval
00065 interval confidence_binomial_jeffreys(unsigned int n, unsigned int k, double level) {
00066   double min = std::numeric_limits<double>::quiet_NaN();
00067   double max = std::numeric_limits<double>::quiet_NaN();
00068 
00069   if (k <= n) {
00070     double alpha = (1.0 - level) / 2;
00071     min = (k == 0) ? 0.0 : ROOT::Math::beta_quantile(      alpha, k + 0.5, n-k + 0.5);
00072     max = (k == n) ? 1.0 : ROOT::Math::beta_quantile(1.0 - alpha, k + 0.5, n-k + 0.5);
00073   }
00074   return std::make_pair(min, max);
00075 }
00076 
00077 // modified Jeffreys prior interval
00078 interval confidence_binomial_jeffreys_modified(unsigned int n, unsigned int k, double level) {
00079   double min = std::numeric_limits<double>::quiet_NaN();
00080   double max = std::numeric_limits<double>::quiet_NaN();
00081 
00082   if (k <= n) {
00083     double alpha = (1.0 - level) / 2;
00084     double correction = 1 - std::pow(alpha, 1./n);
00085     if (n == 0) {
00086       // (0, 0)
00087       min = 0.0;
00088       max = 1.0;
00089     } else if (k == 0) {
00090       // (N, 0)
00091       min = 0.0;
00092       max = correction;
00093     } else if (k == n) {
00094       // (N, N)
00095       min = 1.0 - correction;
00096       max = 1.0;
00097     } else {
00098       min = (k ==   1) ? 0.0 : ROOT::Math::beta_quantile(      alpha, k + 0.5, n-k + 0.5);
00099       max = (k == n-1) ? 1.0 : ROOT::Math::beta_quantile(1.0 - alpha, k + 0.5, n-k + 0.5);
00100     }
00101   }
00102   return std::make_pair(min, max);
00103 }
00104 
00105 interval confidence_binomial_d0(unsigned int n, unsigned int k, double level) {
00106   // hack to access the implementation used in TGraphAsymmErrors (develped by D0) 
00107   struct AccessTGAEEfficicency : private TGraphAsymmErrors {
00108     void confidence_binomial(unsigned int n, unsigned int k, double level, double & min, double & max) {
00109       double mode;
00110       TGraphAsymmErrors::Efficiency(k, n, level, mode, min, max);
00111     }
00112   };
00113 
00114   static AccessTGAEEfficicency d0;
00115   double min = std::numeric_limits<double>::quiet_NaN();
00116   double max = std::numeric_limits<double>::quiet_NaN();
00117 
00118   if (k <= n) {
00119     d0.confidence_binomial(n, k, level, min, max);
00120   }
00121   return std::make_pair(min, max);
00122 }
00123 
00124 } // namespace confidence

Generated on Tue Jun 9 17:38:00 2009 for CMSSW by  doxygen 1.5.4