00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "RooStats/HypoTestResult.h"
00040 #include "RooAbsReal.h"
00041
00042 #ifndef RooStats_RooStatsUtils
00043 #include "RooStats/RooStatsUtils.h"
00044 #endif
00045
00046 #include <limits>
00047 #define NaN numeric_limits<float>::quiet_NaN()
00048 #define IsNaN(a) isnan(a)
00049
00050 ClassImp(RooStats::HypoTestResult) ;
00051
00052 using namespace RooStats;
00053
00054
00055
00056 HypoTestResult::HypoTestResult(const char* name) :
00057 TNamed(name,name),
00058 fNullPValue(NaN), fAlternatePValue(NaN),
00059 fTestStatisticData(NaN),
00060 fNullDistr(NULL), fAltDistr(NULL),
00061 fPValueIsRightTail(kTRUE)
00062 {
00063
00064 }
00065
00066
00067
00068 HypoTestResult::HypoTestResult(const char* name, Double_t nullp, Double_t altp) :
00069 TNamed(name,name),
00070 fNullPValue(nullp), fAlternatePValue(altp),
00071 fTestStatisticData(NaN),
00072 fNullDistr(NULL), fAltDistr(NULL),
00073 fPValueIsRightTail(kTRUE)
00074 {
00075
00076 }
00077
00078
00079
00080 HypoTestResult::~HypoTestResult()
00081 {
00082
00083
00084 }
00085
00086
00087 void HypoTestResult::Append(const HypoTestResult* other) {
00088
00089
00090
00091
00092 if(fNullDistr)
00093 fNullDistr->Add(other->GetNullDistribution());
00094 else
00095 fNullDistr = other->GetNullDistribution();
00096
00097 if(fAltDistr)
00098 fAltDistr->Add(other->GetAltDistribution());
00099 else
00100 fAltDistr = other->GetAltDistribution();
00101
00102
00103 if(IsNaN(fTestStatisticData)) fTestStatisticData = other->GetTestStatisticData();
00104
00105 UpdatePValue(fNullDistr, &fNullPValue, fPValueIsRightTail);
00106 UpdatePValue(fAltDistr, &fAlternatePValue, !fPValueIsRightTail);
00107 }
00108
00109
00110
00111 void HypoTestResult::SetAltDistribution(SamplingDistribution *alt) {
00112 fAltDistr = alt;
00113 UpdatePValue(fAltDistr, &fAlternatePValue, !fPValueIsRightTail);
00114 }
00115
00116 void HypoTestResult::SetNullDistribution(SamplingDistribution *null) {
00117 fNullDistr = null;
00118 UpdatePValue(fNullDistr, &fNullPValue, fPValueIsRightTail);
00119 }
00120
00121 void HypoTestResult::SetTestStatisticData(const Double_t tsd) {
00122 fTestStatisticData = tsd;
00123
00124 UpdatePValue(fNullDistr, &fNullPValue, fPValueIsRightTail);
00125 UpdatePValue(fAltDistr, &fAlternatePValue, !fPValueIsRightTail);
00126 }
00127
00128 void HypoTestResult::SetPValueIsRightTail(Bool_t pr) {
00129 fPValueIsRightTail = pr;
00130
00131 UpdatePValue(fNullDistr, &fNullPValue, fPValueIsRightTail);
00132 UpdatePValue(fAltDistr, &fAlternatePValue, !fPValueIsRightTail);
00133 }
00134
00135
00136 Bool_t HypoTestResult::HasTestStatisticData(void) const {
00137 return !IsNaN(fTestStatisticData);
00138 }
00139
00140 Double_t HypoTestResult::NullPValueError() const {
00141
00142
00143 if(!fNullDistr || !HasTestStatisticData()) return 0.0;
00144
00145 double squares = 0.0;
00146
00147 const vector<Double_t> & weights = fNullDistr->GetSampleWeights();
00148 size_t entries = weights.size();
00149
00150
00151
00152 double sumw = 0.0;
00153 for(size_t i=0; i < entries; i++) {
00154 sumw += weights[i];
00155 squares += weights[i]*weights[i];
00156 }
00157 if (sumw == 0) return 0.0;
00158 double neff = sumw*sumw/squares;
00159
00160 return sqrt( NullPValue()*(1.- NullPValue() )/ neff );
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 }
00175
00176
00177 Double_t HypoTestResult::CLbError() const {
00178
00179
00180
00181
00182 if(!fNullDistr || !HasTestStatisticData()) return 0.0;
00183
00184 double sumw = 0.0;
00185 double squares = 0.0;
00186
00187 const vector<Double_t> & weights = fNullDistr->GetSampleWeights();
00188 size_t entries = weights.size();
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 for(size_t i=0; i < entries; i++) {
00201 sumw += weights[i];
00202 squares += weights[i]*weights[i];
00203 }
00204 if (sumw == 0) return 0.0;
00205 double neff = sumw*sumw/squares;
00206
00207 return sqrt( CLb()*(1.-CLb() )/ neff );
00208
00209 }
00210
00211
00212 Double_t HypoTestResult::CLsplusbError() const {
00213 if(!fAltDistr || !HasTestStatisticData()) return 0.0;
00214
00215 double squares = 0.0;
00216
00217 const vector<Double_t> & weights = fAltDistr->GetSampleWeights();
00218 size_t entries = weights.size();
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 double sumw = 0.0;
00235 for(size_t i=0; i < entries; i++) {
00236 sumw += weights[i];
00237 squares += weights[i]*weights[i];
00238 }
00239 if (sumw == 0) return 0.0;
00240 double neff = sumw*sumw/squares;
00241
00242 return sqrt( CLsplusb()*(1.-CLsplusb() )/ neff );
00243
00244 }
00245
00246
00247
00248 Double_t HypoTestResult::CLsError() const {
00249
00250
00251
00252
00253
00254
00255
00256 if(!fAltDistr || !fNullDistr) return 0.0;
00257
00258
00259
00260
00261 if (CLb() == 0 ) return numeric_limits<double>::infinity();
00262
00263 double cl_b_err2 = pow(CLbError(),2);
00264 double cl_sb_err2 = pow(CLsplusbError(),2);
00265
00266 return TMath::Sqrt(cl_sb_err2 + cl_b_err2 * pow(CLs(),2))/CLb();
00267 }
00268
00269
00270
00271
00272
00273 void HypoTestResult::UpdatePValue(const SamplingDistribution* distr, Double_t *pvalue, Bool_t pIsRightTail) {
00274
00275
00276 if(IsNaN(fTestStatisticData)) return;
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 if(distr) {
00289 if(pIsRightTail) {
00290 *pvalue = distr->Integral(fTestStatisticData, RooNumber::infinity(), kTRUE,
00291 pIsRightTail == fPValueIsRightTail ? kFALSE : kTRUE,
00292 kTRUE
00293 );
00294 }else{
00295 *pvalue = distr->Integral(-RooNumber::infinity(), fTestStatisticData, kTRUE,
00296 kTRUE,
00297 pIsRightTail == fPValueIsRightTail ? kFALSE : kTRUE
00298 );
00299 }
00300 }
00301 }
00302