Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
MaxLikelihoodEstimateTestStat.h
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: Kyle Cranmer June 2010
3 /*************************************************************************
4  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 #ifndef ROOSTATS_MaxLikelihoodEstimateTestStat
12 #define ROOSTATS_MaxLikelihoodEstimateTestStat
13 
14 
15 
16 
17 #include "Rtypes.h"
18 
19 #include "RooNLLVar.h"
20 
21 #include "RooFitResult.h"
22 #include "RooStats/TestStatistic.h"
23 #include "RooAbsPdf.h"
24 #include "RooRealVar.h"
25 #include "RooMinimizer.h"
26 #include "Math/MinimizerOptions.h"
27 #include "RooStats/RooStatsUtils.h"
28 
29 
30 
31 namespace RooStats {
32 
33 /** \class MaxLikelihoodEstimateTestStat
34  \ingroup Roostats
35 MaxLikelihoodEstimateTestStat: TestStatistic that returns maximum likelihood
36 estimate of a specified parameter.
37 */
38 
39 class MaxLikelihoodEstimateTestStat: public TestStatistic {
40 
41  public:
42 
43  //__________________________________
44  MaxLikelihoodEstimateTestStat() :
45  fPdf(NULL),fParameter(NULL), fUpperLimit(true)
46  {
47  /// constructor
48  /// fPdf = pdf;
49  /// fParameter = parameter;
50 
51  fMinimizer=::ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
52  fStrategy=::ROOT::Math::MinimizerOptions::DefaultStrategy();
53  fPrintLevel=::ROOT::Math::MinimizerOptions::DefaultPrintLevel();
54 
55  }
56  //__________________________________
57  MaxLikelihoodEstimateTestStat(RooAbsPdf& pdf, RooRealVar& parameter) :
58  fPdf(&pdf),fParameter(&parameter), fUpperLimit(true)
59  {
60  // constructor
61  // fPdf = pdf;
62  // fParameter = parameter;
63  fMinimizer=::ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
64  fStrategy=::ROOT::Math::MinimizerOptions::DefaultStrategy();
65  fPrintLevel=::ROOT::Math::MinimizerOptions::DefaultPrintLevel();
66 
67  }
68 
69  //______________________________
70  virtual Double_t Evaluate(RooAbsData& data, RooArgSet& /*nullPOI*/) {
71 
72 
73  RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
74  RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
75 
76  /*
77  // this is more straight forward, but produces a lot of messages
78  RooFitResult* res = fPdf.fitTo(data, RooFit::CloneData(kFALSE),RooFit::Minos(0),RooFit::Hesse(false), RooFit::Save(1),RooFit::PrintLevel(-1),RooFit::PrintEvalErrors(0));
79  RooRealVar* mle = (RooRealVar*) res->floatParsFinal().find(fParameter.GetName());
80  double ret = mle->getVal();
81  delete res;
82  return ret;
83  */
84 
85  RooArgSet* allParams = fPdf->getParameters(data);
86  RooStats::RemoveConstantParameters(allParams);
87 
88  // need to call constrain for RooSimultaneous until stripDisconnected problem fixed
89  RooAbsReal* nll = fPdf->createNLL(data, RooFit::CloneData(kFALSE),RooFit::Constrain(*allParams),RooFit::ConditionalObservables(fConditionalObs));
90 
91  //RooAbsReal* nll = fPdf->createNLL(data, RooFit::CloneData(false));
92 
93  // RooAbsReal* profile = nll->createProfile(RooArgSet());
94  // profile->getVal();
95  // RooArgSet* vars = profile->getVariables();
96  // RooMsgService::instance().setGlobalKillBelow(msglevel);
97  // double ret = vars->getRealValue(fParameter->GetName());
98  // delete vars;
99  // delete nll;
100  // delete profile;
101  // return ret;
102 
103 
104  RooMinimizer minim(*nll);
105  minim.setStrategy(fStrategy);
106  //LM: RooMinimizer.setPrintLevel has +1 offset - so subtract here -1
107  minim.setPrintLevel(fPrintLevel-1);
108  int status = -1;
109  // minim.optimizeConst(true);
110  for (int tries = 0, maxtries = 4; tries <= maxtries; ++tries) {
111  // status = minim.minimize(fMinimizer, ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str());
112  status = minim.minimize(fMinimizer, "Minimize");
113  if (status == 0) {
114  break;
115  } else {
116  if (tries > 1) {
117  printf(" ----> Doing a re-scan first\n");
118  minim.minimize(fMinimizer,"Scan");
119  }
120  if (tries > 2) {
121  printf(" ----> trying with strategy = 1\n");
122  minim.setStrategy(1);
123  }
124  }
125  }
126  //std::cout << "BEST FIT values " << std::endl;
127  //allParams->Print("V");
128 
129  RooMsgService::instance().setGlobalKillBelow(msglevel);
130  delete nll;
131 
132  if (status != 0) return -1;
133  return fParameter->getVal();
134 
135 
136  }
137 
138  virtual const TString GetVarName() const {
139  TString varName = Form("Maximum Likelihood Estimate of %s",fParameter->GetName());
140  return varName;
141  }
142 
143 
144  virtual void PValueIsRightTail(bool isright) { fUpperLimit = isright; }
145  virtual bool PValueIsRightTail(void) const { return fUpperLimit; }
146 
147  // set the conditional observables which will be used when creating the NLL
148  // so the pdf's will not be normalized on the conditional observables when computing the NLL
149  virtual void SetConditionalObservables(const RooArgSet& set) {fConditionalObs.removeAll(); fConditionalObs.add(set);}
150 
151 
152  private:
153  RooAbsPdf *fPdf;
154  RooRealVar *fParameter;
155  RooArgSet fConditionalObs;
156  bool fUpperLimit;
157  TString fMinimizer;
158  Int_t fStrategy;
159  Int_t fPrintLevel;
160 
161 
162 
163  protected:
164  ClassDef(MaxLikelihoodEstimateTestStat,2)
165 };
166 
167 }
168 
169 
170 #endif