00001 #include <limits>
00002 #include <cmath>
00003
00004 #include "Math/QuantFuncMathCore.h"
00005 #include "TGraphAsymmErrors.h"
00006
00007 #include "confidence.h"
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 namespace confidence {
00027
00028
00029 interval confidence_binomial(unsigned int n, unsigned int k, double level) {
00030 return confidence_binomial_jeffreys(n, k, level);
00031 }
00032
00033
00034
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
00048
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
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
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
00087 min = 0.0;
00088 max = 1.0;
00089 } else if (k == 0) {
00090
00091 min = 0.0;
00092 max = correction;
00093 } else if (k == n) {
00094
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
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 }