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
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #ifndef RooStats_ProfileLikelihoodCalculator
00050 #include "RooStats/ProfileLikelihoodCalculator.h"
00051 #endif
00052
00053 #ifndef RooStats_RooStatsUtils
00054 #include "RooStats/RooStatsUtils.h"
00055 #endif
00056
00057 #include "RooStats/LikelihoodInterval.h"
00058 #include "RooStats/HypoTestResult.h"
00059
00060 #include "RooFitResult.h"
00061 #include "RooRealVar.h"
00062 #include "RooProfileLL.h"
00063 #include "RooNLLVar.h"
00064 #include "RooGlobalFunc.h"
00065
00066
00067 ClassImp(RooStats::ProfileLikelihoodCalculator) ;
00068
00069 using namespace RooFit;
00070 using namespace RooStats;
00071
00072
00073
00074 ProfileLikelihoodCalculator::ProfileLikelihoodCalculator() :
00075 CombinedCalculator(), fFitResult(0)
00076 {
00077
00078 }
00079
00080 ProfileLikelihoodCalculator::ProfileLikelihoodCalculator(RooAbsData& data, RooAbsPdf& pdf, const RooArgSet& paramsOfInterest,
00081 Double_t size, const RooArgSet* nullParams ) :
00082 CombinedCalculator(data,pdf, paramsOfInterest, size, nullParams ),
00083 fFitResult(0)
00084 {
00085
00086
00087 }
00088
00089 ProfileLikelihoodCalculator::ProfileLikelihoodCalculator(RooAbsData& data, ModelConfig& model, Double_t size) :
00090 CombinedCalculator(data, model, size),
00091 fFitResult(0)
00092 {
00093
00094
00095 assert(model.GetPdf() );
00096 }
00097
00098
00099
00100 ProfileLikelihoodCalculator::~ProfileLikelihoodCalculator(){
00101
00102
00103
00104
00105 if (fFitResult) delete fFitResult;
00106 }
00107
00108 void ProfileLikelihoodCalculator::DoReset() const {
00109
00110
00111 if (fFitResult) delete fFitResult;
00112 fFitResult = 0;
00113 }
00114
00115 void ProfileLikelihoodCalculator::DoGlobalFit() const {
00116
00117
00118
00119
00120 DoReset();
00121 RooAbsPdf * pdf = GetPdf();
00122 RooAbsData* data = GetData();
00123 if (!data || !pdf ) return;
00124
00125
00126 RooArgSet* constrainedParams = pdf->getParameters(*data);
00127 if (!constrainedParams) return ;
00128 RemoveConstantParameters(constrainedParams);
00129
00130
00131 RooFitResult* fit = pdf->fitTo(*data, Constrain(*constrainedParams),Strategy(1),Hesse(kTRUE),Save(kTRUE),PrintLevel(-1));
00132
00133
00134 fit->Print();
00135
00136 delete constrainedParams;
00137
00138 fFitResult = fit;
00139 if (fFitResult == 0)
00140 oocoutI((TObject*)0,Minimization) << "ProfileLikelihoodCalcultor::DoGlobalFit - Global fit failed " << std::endl;
00141
00142 }
00143
00144
00145 LikelihoodInterval* ProfileLikelihoodCalculator::GetInterval() const {
00146
00147
00148
00149
00150
00151 RooAbsPdf * pdf = GetPdf();
00152 RooAbsData* data = GetData();
00153 if (!data || !pdf || fPOI.getSize() == 0) return 0;
00154
00155 RooArgSet* constrainedParams = pdf->getParameters(*data);
00156 RemoveConstantParameters(constrainedParams);
00157
00158
00159
00160
00161
00162
00163
00164
00165 RooAbsReal* nll = pdf->createNLL(*data, CloneData(kTRUE), Constrain(*constrainedParams));
00166 RooAbsReal* profile = nll->createProfile(fPOI);
00167 profile->addOwnedComponents(*nll) ;
00168
00169
00170
00171
00172 if (!fFitResult) DoGlobalFit();
00173
00174 if (!fFitResult) return 0;
00175
00176
00177
00178 const RooArgList & fitParams = fFitResult->floatParsFinal();
00179 for (int i = 0; i < fitParams.getSize(); ++i) {
00180 RooRealVar & fitPar = (RooRealVar &) fitParams[i];
00181 RooRealVar * par = (RooRealVar*) fPOI.find( fitPar.GetName() );
00182 if (par) {
00183 par->setVal( fitPar.getVal() );
00184 par->setError( fitPar.getError() );
00185 }
00186 }
00187
00188
00189
00190 profile->getVal();
00191
00192
00193
00194 TString name = TString("LikelihoodInterval_");
00195
00196
00197
00198 TIter iter = fPOI.createIterator();
00199 RooArgSet fitParSet(fitParams);
00200 RooArgSet * bestPOI = new RooArgSet();
00201 while (RooAbsArg * arg = (RooAbsArg*) iter.Next() ) {
00202 RooAbsArg * p = fitParSet.find( arg->GetName() );
00203 if (p) bestPOI->addClone(*p);
00204 else bestPOI->addClone(*arg);
00205 }
00206
00207
00208 LikelihoodInterval* interval = new LikelihoodInterval(name, profile, &fPOI, bestPOI);
00209 interval->SetConfidenceLevel(1.-fSize);
00210 delete constrainedParams;
00211 return interval;
00212 }
00213
00214
00215 HypoTestResult* ProfileLikelihoodCalculator::GetHypoTest() const {
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 RooAbsPdf * pdf = GetPdf();
00226 RooAbsData* data = GetData();
00227
00228
00229 if (!data || !pdf) return 0;
00230
00231 if (fNullParams.getSize() == 0) return 0;
00232
00233
00234
00235 RooArgList poiList;
00236 poiList.addClone(fNullParams);
00237
00238
00239
00240 if (!fFitResult) DoGlobalFit();
00241 if (!fFitResult) return 0;
00242
00243 RooArgSet* constrainedParams = pdf->getParameters(*data);
00244 RemoveConstantParameters(constrainedParams);
00245
00246
00247 if (!fFitResult) DoGlobalFit();
00248 Double_t NLLatMLE= fFitResult->minNll();
00249
00250
00251 std::vector<double> oldValues(poiList.getSize() );
00252 for (unsigned int i = 0; i < oldValues.size(); ++i) {
00253 RooRealVar * mytarget = (RooRealVar*) constrainedParams->find(poiList[i].GetName());
00254 if (mytarget) {
00255 oldValues[i] = mytarget->getVal();
00256 mytarget->setVal( ( (RooRealVar&) poiList[i] ).getVal() );
00257 mytarget->setConstant(kTRUE);
00258 }
00259 }
00260
00261
00262
00263
00264
00265
00266 RooArgSet nuisParams(*constrainedParams);
00267
00268
00269 RemoveConstantParameters(&nuisParams);
00270
00271
00272 bool existVarParams = false;
00273 TIter it = nuisParams.createIterator();
00274 RooRealVar * myarg = 0;
00275 while ((myarg = (RooRealVar *)it.Next())) {
00276 if ( !myarg->isConstant() ) {
00277 existVarParams = true;
00278 break;
00279 }
00280 }
00281
00282 Double_t NLLatCondMLE = NLLatMLE;
00283 if (existVarParams) {
00284
00285 RooFitResult* fit2 = pdf->fitTo(*data,Constrain(*constrainedParams),Hesse(kFALSE),Strategy(0), Minos(kFALSE), Save(kTRUE),PrintLevel(-1));
00286
00287 NLLatCondMLE = fit2->minNll();
00288 fit2->Print();
00289 }
00290 else {
00291
00292 RooAbsReal* nll = pdf->createNLL(*data, CloneData(kTRUE), Constrain(*constrainedParams));
00293 NLLatCondMLE = nll->getVal();
00294 delete nll;
00295 }
00296
00297
00298 Double_t deltaNLL = std::max( NLLatCondMLE-NLLatMLE, 0.);
00299
00300 TString name = TString("ProfileLRHypoTestResult_");
00301 HypoTestResult* htr =
00302 new HypoTestResult(name, SignificanceToPValue(sqrt( 2*deltaNLL)), 0 );
00303
00304
00305
00306 for (unsigned int i = 0; i < oldValues.size(); ++i) {
00307 RooRealVar * mytarget = (RooRealVar*) constrainedParams->find(poiList[i].GetName());
00308 if (mytarget) {
00309 mytarget->setVal(oldValues[i] );
00310 mytarget->setConstant(false);
00311 }
00312 }
00313
00314 delete constrainedParams;
00315 return htr;
00316
00317 }
00318