SimpleLikelihoodRatioTestStat.h

Go to the documentation of this file.
00001 // @(#)root/roostats:$Id: SimpleLikelihoodRatioTestStat.h 36602 2010-11-11 16:52:13Z moneta $
00002 // Author: Kyle Cranmer and Sven Kreiss    June 2010
00003 /*************************************************************************
00004  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00005  * All rights reserved.                                                  *
00006  *                                                                       *
00007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00009  *************************************************************************/
00010 
00011 #ifndef ROOSTATS_SimpleLikelihoodRatioTestStat
00012 #define ROOSTATS_SimpleLikelihoodRatioTestStat
00013 
00014 //_________________________________________________
00015 /*
00016  BEGIN_HTML
00017  <p>
00018  SimpleLikelihoodRatioTestStat: TestStatistic that returns -log(L[null] / L[alt]) where
00019  L is the likelihood.
00020  </p>
00021  END_HTML
00022  */
00023 //
00024 
00025 #ifndef ROOT_Rtypes
00026 #include "Rtypes.h"
00027 #endif
00028 
00029 #ifndef ROO_NLL_VAR
00030 #include "RooNLLVar.h"
00031 #endif
00032 
00033 #include "RooStats/TestStatistic.h"
00034 
00035 namespace RooStats {
00036 
00037 class SimpleLikelihoodRatioTestStat : public TestStatistic {
00038 
00039    public:
00040 
00041       //__________________________________
00042       SimpleLikelihoodRatioTestStat() :
00043          fNullPdf(NULL), fAltPdf(NULL)
00044       {
00045          // Constructor for proof. Do not use.
00046          fFirstEval = true;
00047          fNullParameters = NULL;
00048          fAltParameters = NULL;
00049       }
00050 
00051       //__________________________________
00052       SimpleLikelihoodRatioTestStat(
00053          RooAbsPdf& nullPdf,
00054          RooAbsPdf& altPdf
00055       ) :
00056          fFirstEval(true)
00057       {
00058          // Takes null and alternate parameters from PDF. Can be overridden.
00059 
00060          RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
00061          RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR);
00062          w.import(nullPdf, RooFit::RecycleConflictNodes());
00063          w.import(altPdf, RooFit::RecycleConflictNodes());
00064          RooMsgService::instance().setGlobalKillBelow(msglevel);
00065 
00066          fNullPdf = w.pdf(nullPdf.GetName());
00067          fAltPdf = w.pdf(altPdf.GetName());
00068 
00069          fNullParameters = (RooArgSet*) fNullPdf->getVariables()->snapshot();
00070          fAltParameters = (RooArgSet*) fAltPdf->getVariables()->snapshot();
00071       }
00072       //__________________________________
00073       SimpleLikelihoodRatioTestStat(
00074          RooAbsPdf& nullPdf,
00075          RooAbsPdf& altPdf,
00076          const RooArgSet& nullParameters,
00077          const RooArgSet& altParameters
00078       ) :
00079          fFirstEval(true)
00080       {
00081          // Takes null and alternate parameters from values in nullParameters
00082          // and altParameters. Can be overridden.
00083 
00084          RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
00085          RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR);
00086          w.import(nullPdf, RooFit::RecycleConflictNodes());
00087          w.import(altPdf, RooFit::RecycleConflictNodes());
00088          RooMsgService::instance().setGlobalKillBelow(msglevel);
00089 
00090          fNullPdf = w.pdf(nullPdf.GetName());
00091          fAltPdf = w.pdf(altPdf.GetName());
00092 
00093          fNullParameters = (RooArgSet*) nullParameters.snapshot();
00094          fAltParameters = (RooArgSet*) altParameters.snapshot();
00095       }
00096 
00097       //______________________________
00098       virtual ~SimpleLikelihoodRatioTestStat() {
00099          if (fNullParameters) delete fNullParameters;
00100          if (fAltParameters) delete fAltParameters;
00101       }
00102 
00103       //_________________________________________
00104       void SetNullParameters(const RooArgSet& nullParameters) {
00105          if (fNullParameters) delete fNullParameters;
00106          fFirstEval = true;
00107          //      if(fNullParameters) delete fNullParameters;
00108          fNullParameters = (RooArgSet*) nullParameters.snapshot();
00109       }
00110 
00111       //_________________________________________
00112       void SetAltParameters(const RooArgSet& altParameters) {
00113          if (fAltParameters) delete fAltParameters;
00114          fFirstEval = true;
00115          //      if(fAltParameters) delete fAltParameters;
00116          fAltParameters = (RooArgSet*) altParameters.snapshot();
00117       }
00118 
00119       //______________________________
00120       bool ParamsAreEqual() {
00121          // this should be possible with RooAbsCollection
00122          if (!fNullParameters->equals(*fAltParameters)) return false;
00123 
00124          RooAbsReal* null;
00125          RooAbsReal* alt;
00126 
00127          TIterator* nullIt = fNullParameters->createIterator();
00128          TIterator* altIt = fAltParameters->createIterator();
00129          bool ret = true;
00130          while ((null = (RooAbsReal*) nullIt->Next()) && (alt = (RooAbsReal*) altIt->Next())) {
00131             if (null->getVal() != alt->getVal()) ret = false;
00132          }
00133          delete nullIt;
00134          delete altIt;
00135          return ret;
00136       }
00137 
00138       //______________________________
00139       virtual Double_t Evaluate(RooAbsData& data, RooArgSet& nullPOI) {
00140 
00141          if (fFirstEval && ParamsAreEqual()) {
00142             oocoutW(fNullParameters,InputArguments)
00143                << "Same RooArgSet used for null and alternate, so you must explicitly SetNullParameters and SetAlternateParameters or the likelihood ratio will always be 1."
00144                << endl;
00145          }
00146          fFirstEval = false;
00147 
00148          RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
00149          RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
00150 
00151          RooAbsReal *nll;
00152          nll = fNullPdf->createNLL(data, RooFit::CloneData(kFALSE));
00153          // make sure we set the variables attached to this nll
00154          RooArgSet* attachedSet = nll->getVariables();
00155          *attachedSet = *fNullParameters;
00156          *attachedSet = nullPOI;
00157          double nullNLL = nll->getVal();
00158          delete nll;
00159          delete attachedSet;
00160 
00161          nll = fAltPdf->createNLL(data, RooFit::CloneData(kFALSE));
00162          // make sure we set the variables attached to this nll
00163          attachedSet = nll->getVariables();
00164          *attachedSet = *fAltParameters;
00165          double altNLL = nll->getVal();
00166          delete nll;
00167          delete attachedSet;
00168 
00169          RooMsgService::instance().setGlobalKillBelow(msglevel);
00170          return nullNLL - altNLL;
00171       }
00172 
00173       virtual const TString GetVarName() const {
00174          return "log(L(#mu_{1}) / L(#mu_{0}))";
00175       }
00176 
00177    private:
00178       RooWorkspace w;
00179 
00180       RooAbsPdf* fNullPdf;
00181       RooAbsPdf* fAltPdf;
00182       RooArgSet* fNullParameters;
00183       RooArgSet* fAltParameters;
00184       bool fFirstEval;
00185 
00186    protected:
00187    ClassDef(SimpleLikelihoodRatioTestStat,1)
00188 };
00189 
00190 }
00191 
00192 #endif

Generated on Tue Jul 5 14:26:26 2011 for ROOT_528-00b_version by  doxygen 1.5.1