Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-16 09:54:51

0001 // @(#)root/roostats:$Id$
0002 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
0003 // Additional Contributions: Giovanni Petrucciani
0004 /*************************************************************************
0005  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOSTATS_ProfileLikelihoodTestStat
0013 #define ROOSTATS_ProfileLikelihoodTestStat
0014 
0015 
0016 #include "Rtypes.h"
0017 
0018 #include "RooStats/TestStatistic.h"
0019 
0020 
0021 #include "RooRealVar.h"
0022 
0023 #include "Math/MinimizerOptions.h"
0024 
0025 #include "RooStats/RooStatsUtils.h"
0026 
0027 
0028 namespace RooStats {
0029 
0030   class ProfileLikelihoodTestStat : public TestStatistic{
0031 
0032      enum LimitType {twoSided, oneSided, oneSidedDiscovery};
0033 
0034    public:
0035      ProfileLikelihoodTestStat() {
0036         // Proof constructor. Do not use.
0037         fPdf = nullptr;
0038         fCachedBestFitParams = nullptr;
0039         fLastData = nullptr;
0040         fLimitType = twoSided;
0041         fSigned = false;
0042         fDetailedOutputWithErrorsAndPulls = false;
0043         fDetailedOutputEnabled = false;
0044         fDetailedOutput = nullptr;
0045         fLOffset = RooStats::IsNLLOffset() ;
0046 
0047         fVarName = "Profile Likelihood Ratio";
0048         fReuseNll = false;
0049         fStrategy=::ROOT::Math::MinimizerOptions::DefaultStrategy();
0050         fTolerance=TMath::Max(1.,::ROOT::Math::MinimizerOptions::DefaultTolerance());
0051         fPrintLevel=::ROOT::Math::MinimizerOptions::DefaultPrintLevel();
0052      }
0053 
0054      ProfileLikelihoodTestStat(RooAbsPdf& pdf) {
0055        fPdf = &pdf;
0056        fCachedBestFitParams = nullptr;
0057        fLastData = nullptr;
0058        fLimitType = twoSided;
0059        fSigned = false;
0060        fDetailedOutputWithErrorsAndPulls = false;
0061        fDetailedOutputEnabled = false;
0062        fDetailedOutput = nullptr;
0063        fLOffset = RooStats::IsNLLOffset() ;
0064 
0065        fVarName = "Profile Likelihood Ratio";
0066        fReuseNll = false;
0067        fStrategy=::ROOT::Math::MinimizerOptions::DefaultStrategy();
0068        // avoid default tolerance to be too small (1. is default in RooMinimizer)
0069        fTolerance=TMath::Max(1.,::ROOT::Math::MinimizerOptions::DefaultTolerance());
0070        fPrintLevel=::ROOT::Math::MinimizerOptions::DefaultPrintLevel();
0071      }
0072 
0073      ~ProfileLikelihoodTestStat() override {
0074        if(fCachedBestFitParams) delete fCachedBestFitParams;
0075        if(fDetailedOutput) delete fDetailedOutput;
0076      }
0077 
0078      void SetOneSided(bool flag=true) {fLimitType = (flag ? oneSided : twoSided);}
0079      void SetOneSidedDiscovery(bool flag=true) {fLimitType = (flag ? oneSidedDiscovery : twoSided);}
0080      void SetSigned(bool flag=true) {fSigned = flag;}  // +/- t_mu instead of t_mu>0 with one-sided settings
0081 
0082      bool IsTwoSided() const { return fLimitType == twoSided; }
0083      bool IsOneSidedDiscovery() const { return fLimitType == oneSidedDiscovery; }
0084 
0085      static void SetAlwaysReuseNLL(bool flag);
0086 
0087      void SetReuseNLL(bool flag) { fReuseNll = flag ; }
0088      void SetLOffset(bool flag=true) { fLOffset = flag ; }
0089 
0090      void SetMinimizer(const char* minimizer){ fMinimizer=minimizer;}
0091      void SetStrategy(Int_t strategy){fStrategy=strategy;}
0092      void SetTolerance(double tol){fTolerance=tol;}
0093      void SetPrintLevel(Int_t printlevel){fPrintLevel=printlevel;}
0094 
0095      /// Main interface to evaluate the test statistic on a dataset
0096      double Evaluate(RooAbsData& data, RooArgSet& paramsOfInterest) override {
0097         return EvaluateProfileLikelihood(0, data, paramsOfInterest);
0098      }
0099 
0100      /// evaluate  the profile likelihood ratio (type = 0) or the minimum of likelihood (type=1) or the conditional LL (type = 2)
0101      virtual double EvaluateProfileLikelihood(int type, RooAbsData &data, RooArgSet & paramsOfInterest);
0102 
0103      virtual void EnableDetailedOutput( bool e=true, bool withErrorsAndPulls=false ) {
0104         fDetailedOutputEnabled = e;
0105         fDetailedOutputWithErrorsAndPulls = withErrorsAndPulls;
0106         delete fDetailedOutput;
0107         fDetailedOutput = nullptr;
0108      }
0109      /// Returns detailed output. The value returned by this function is updated after each call to Evaluate().
0110      /// The returned RooArgSet contains the following:
0111      ///
0112      ///  - the minimum nll, fitstatus and convergence quality for each fit
0113      ///  - for each fit and for each non-constant parameter, the value, error and pull of the parameter are stored
0114      ///
0115      const RooArgSet* GetDetailedOutput(void) const override {
0116       return fDetailedOutput;
0117      }
0118 
0119      /// set the conditional observables which will be used when creating the NLL
0120      /// so the pdf's will not be normalized on the conditional observables when computing the NLL
0121      void SetConditionalObservables(const RooArgSet& set) override {fConditionalObs.removeAll(); fConditionalObs.add(set);}
0122 
0123      /// set the global observables which will be used when creating the NLL
0124      /// so the constraint pdf's will be normalized correctly on the global observables when computing the NLL
0125      void SetGlobalObservables(const RooArgSet& set) override {fGlobalObs.removeAll(); fGlobalObs.add(set);}
0126 
0127      virtual void SetVarName(const char* name) { fVarName = name; }
0128      const TString GetVarName() const override {return fVarName;}
0129 
0130      virtual RooAbsPdf * GetPdf() const { return fPdf; }
0131 
0132   private:
0133 
0134      std::unique_ptr<RooFitResult> GetMinNLL();
0135 
0136    private:
0137 
0138       RooAbsPdf* fPdf;
0139       std::unique_ptr<RooAbsReal> fNll; //!
0140       const RooArgSet* fCachedBestFitParams;
0141       RooAbsData* fLastData;
0142       //      double fLastMLE;
0143       LimitType fLimitType;
0144       bool fSigned;
0145 
0146       /// this will store a snapshot of the unconditional nuisance
0147       /// parameter fit.
0148       bool fDetailedOutputEnabled;
0149       bool fDetailedOutputWithErrorsAndPulls;
0150       RooArgSet* fDetailedOutput; ///<!
0151       RooArgSet fConditionalObs;  ///< conditional observables
0152       RooArgSet fGlobalObs;       ///< global observables
0153 
0154       TString fVarName;
0155 
0156       static bool fgAlwaysReuseNll ;
0157       bool fReuseNll ;
0158       TString fMinimizer;
0159       Int_t fStrategy;
0160       double fTolerance;
0161       Int_t fPrintLevel;
0162       bool fLOffset ;
0163 
0164    protected:
0165 
0166       ClassDefOverride(ProfileLikelihoodTestStat,10)   // implements the profile likelihood ratio as a test statistic to be used with several tools
0167    };
0168 }
0169 
0170 
0171 #endif