Logo ROOT   6.10/09
Reference Guide
FrequentistCalculator.cxx
Go to the documentation of this file.
1 // @(#)root/roostats:$Id: FrequentistCalculator.cxx 37084 2010-11-29 21:37:13Z moneta $
2 // Author: Kyle Cranmer, Sven Kreiss 23/05/10
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 
12 #include "RooStats/ToyMCSampler.h"
13 #include "RooStats/RooStatsUtils.h"
15 #include "RooMinimizer.h"
16 #include "RooMinuit.h"
17 #include "RooProfileLL.h"
18 
19 /** \class RooStats::FrequentistCalculator
20  \ingroup Roostats
21 
22 Does a frequentist hypothesis test.
23 
24 Hypothesis Test Calculator using a full frequentist procedure for sampling the
25 test statistic distribution.
26 The nuisance parameters are fixed to their MLEs.
27 The use of ToyMCSampler as the TestStatSampler is assumed.
28 
29 */
30 
32 
33 using namespace RooStats;
34 using namespace std;
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 
39  if (fFitInfo != NULL) {
40  delete fFitInfo;
41  fFitInfo = NULL;
42  }
43  if (fStoreFitInfo) {
44  fFitInfo = new RooArgSet();
45  }
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 
55 int FrequentistCalculator::PreNullHook(RooArgSet *parameterPoint, double obsTestStat) const {
56 
57  // ****** any TestStatSampler ********
58 
59  // create profile keeping everything but nuisance parameters fixed
60  RooArgSet * allParams = fNullModel->GetPdf()->getParameters(*fData);
61  RemoveConstantParameters(allParams);
62 
63  // note: making nll or profile class variables can only be done in the constructor
64  // as all other hooks are const (which has to be because GetHypoTest is const). However,
65  // when setting it only in constructor, they would have to be changed every time SetNullModel
66  // or SetAltModel is called. Simply put, converting them into class variables breaks
67  // encapsulation.
68 
69  bool doProfile = true;
70  RooArgSet allButNuisance(*allParams);
71  if( fNullModel->GetNuisanceParameters() ) {
72  allButNuisance.remove(*fNullModel->GetNuisanceParameters());
73  if( fConditionalMLEsNull ) {
74  oocoutI((TObject*)0,InputArguments) << "Using given conditional MLEs for Null." << endl;
75  *allParams = *fConditionalMLEsNull;
76  // LM: fConditionalMLEsNull must be nuisance parameters otherwise an error message will be printed
77  allButNuisance.add( *fConditionalMLEsNull );
78  if (fNullModel->GetNuisanceParameters()) {
79  RooArgSet remain(*fNullModel->GetNuisanceParameters());
80  remain.remove(*fConditionalMLEsNull,true,true);
81  if( remain.getSize() == 0 ) doProfile = false;
82  }
83  }
84  }else{
85  doProfile = false;
86  }
87  if (doProfile) {
88  oocoutI((TObject*)0,InputArguments) << "Profiling conditional MLEs for Null." << endl;
91 
92  RooArgSet conditionalObs;
93  if (fNullModel->GetConditionalObservables()) conditionalObs.add(*fNullModel->GetConditionalObservables());
94 
95  RooAbsReal* nll = fNullModel->GetPdf()->createNLL(*const_cast<RooAbsData*>(fData), RooFit::CloneData(kFALSE), RooFit::Constrain(*allParams),
97  RooProfileLL* profile = dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance));
98  profile->getVal(); // this will do fit and set nuisance parameters to profiled values
99 
100  // Hack to extract a RooFitResult
101  if (fStoreFitInfo) {
102  RooFitResult *result = profile->minimizer()->save();
103  RooArgSet * detOutput = DetailedOutputAggregator::GetAsArgSet(result, "fitNull_");
104  fFitInfo->addOwned(*detOutput);
105  delete detOutput;
106  delete result;
107  }
108 
109  delete profile;
110  delete nll;
112  }
113 
114  // add nuisance parameters to parameter point
115  if(fNullModel->GetNuisanceParameters())
116  parameterPoint->add(*fNullModel->GetNuisanceParameters());
117 
118  delete allParams;
119 
120 
121 
122  // ***** ToyMCSampler specific *******
123 
124  // check whether TestStatSampler is a ToyMCSampler
125  ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
126  if(toymcs) {
127  oocoutI((TObject*)0,InputArguments) << "Using a ToyMCSampler. Now configuring for Null." << endl;
128 
129  // variable number of toys
130  if(fNToysNull >= 0) toymcs->SetNToys(fNToysNull);
131 
132  // set the global observables to be generated by the ToyMCSampler
133  toymcs->SetGlobalObservables(*fNullModel->GetGlobalObservables());
134 
135  // adaptive sampling
136  if(fNToysNullTail) {
137  oocoutI((TObject*)0,InputArguments) << "Adaptive Sampling" << endl;
138  if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
139  toymcs->SetToysRightTail(fNToysNullTail, obsTestStat);
140  }else{
141  toymcs->SetToysLeftTail(fNToysNullTail, obsTestStat);
142  }
143  }else{
144  toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
145  }
146 
147  GetNullModel()->LoadSnapshot();
148  }
149 
150  return 0;
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 
155 int FrequentistCalculator::PreAltHook(RooArgSet *parameterPoint, double obsTestStat) const {
156 
157  // ****** any TestStatSampler ********
158 
159  // create profile keeping everything but nuisance parameters fixed
160  RooArgSet * allParams = fAltModel->GetPdf()->getParameters(*fData);
161  RemoveConstantParameters(allParams);
162 
163  bool doProfile = true;
164  RooArgSet allButNuisance(*allParams);
165  if( fAltModel->GetNuisanceParameters() ) {
166  allButNuisance.remove(*fAltModel->GetNuisanceParameters());
167  if( fConditionalMLEsAlt ) {
168  oocoutI((TObject*)0,InputArguments) << "Using given conditional MLEs for Alt." << endl;
169  *allParams = *fConditionalMLEsAlt;
170  // LM: fConditionalMLEsAlt must be nuisance parameters otherwise an error message will be printed
171  allButNuisance.add( *fConditionalMLEsAlt );
172  if (fAltModel->GetNuisanceParameters()) {
173  RooArgSet remain(*fAltModel->GetNuisanceParameters());
174  remain.remove(*fConditionalMLEsAlt,true,true);
175  if( remain.getSize() == 0 ) doProfile = false;
176  }
177  }
178  }else{
179  doProfile = false;
180  }
181  if (doProfile) {
182  oocoutI((TObject*)0,InputArguments) << "Profiling conditional MLEs for Alt." << endl;
185 
186  RooArgSet conditionalObs;
187  if (fAltModel->GetConditionalObservables()) conditionalObs.add(*fAltModel->GetConditionalObservables());
188 
189  RooAbsReal* nll = fAltModel->GetPdf()->createNLL(*const_cast<RooAbsData*>(fData), RooFit::CloneData(kFALSE), RooFit::Constrain(*allParams),
191 
192  RooProfileLL* profile = dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance));
193  profile->getVal(); // this will do fit and set nuisance parameters to profiled values
194 
195  // Hack to extract a RooFitResult
196  if (fStoreFitInfo) {
197  RooFitResult *result = profile->minimizer()->save();
198  RooArgSet * detOutput = DetailedOutputAggregator::GetAsArgSet(result, "fitAlt_");
199  fFitInfo->addOwned(*detOutput);
200  delete detOutput;
201  delete result;
202  }
203 
204  delete profile;
205  delete nll;
207  }
208 
209  // add nuisance parameters to parameter point
210  if(fAltModel->GetNuisanceParameters())
211  parameterPoint->add(*fAltModel->GetNuisanceParameters());
212 
213  delete allParams;
214 
215 
216 
217 
218  // ***** ToyMCSampler specific *******
219 
220  // check whether TestStatSampler is a ToyMCSampler
221  ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
222  if(toymcs) {
223  oocoutI((TObject*)0,InputArguments) << "Using a ToyMCSampler. Now configuring for Alt." << endl;
224 
225  // variable number of toys
226  if(fNToysAlt >= 0) toymcs->SetNToys(fNToysAlt);
227 
228  // set the global observables to be generated by the ToyMCSampler
229  toymcs->SetGlobalObservables(*fAltModel->GetGlobalObservables());
230 
231  // adaptive sampling
232  if(fNToysAltTail) {
233  oocoutI((TObject*)0,InputArguments) << "Adaptive Sampling" << endl;
234  if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
235  toymcs->SetToysLeftTail(fNToysAltTail, obsTestStat);
236  }else{
237  toymcs->SetToysRightTail(fNToysAltTail, obsTestStat);
238  }
239  }else{
240  toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
241  }
242 
243  }
244 
245  return 0;
246 }
RooCmdArg Offset(Bool_t flag=kTRUE)
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:86
Class RooProfileLL implements the profile likelihood estimator for a given likelihood and set of para...
Definition: RooProfileLL.h:26
void SetToysLeftTail(Double_t toys, Double_t threshold)
Definition: ToyMCSampler.h:216
virtual void SetGlobalObservables(const RooArgSet &o)
Definition: ToyMCSampler.h:175
RooCmdArg CloneData(Bool_t flag)
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
#define oocoutI(o, a)
Definition: RooMsgService.h:44
RooFit::MsgLevel globalKillBelow() const
MINIMIZER * minimizer()
Definition: RooProfileLL.h:38
static RooMsgService & instance()
Return reference to singleton instance.
STL namespace.
#define NULL
Definition: RtypesCore.h:88
void SetToysRightTail(Double_t toys, Double_t threshold)
Definition: ToyMCSampler.h:221
void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold)
Definition: ToyMCSampler.h:226
virtual void SetNToys(const Int_t ntoy)
Definition: ToyMCSampler.h:146
int PreNullHook(RooArgSet *parameterPoint, double obsTestStat) const
configure TestStatSampler for the Null run
int PreAltHook(RooArgSet *parameterPoint, double obsTestStat) const
configure TestStatSampler for the Alt run
void setGlobalKillBelow(RooFit::MsgLevel level)
ToyMCSampler is an implementation of the TestStatSampler interface.
Definition: ToyMCSampler.h:71
const Bool_t kFALSE
Definition: RtypesCore.h:92
Namespace for the RooStats classes.
Definition: Asimov.h:20
#define ClassImp(name)
Definition: Rtypes.h:336
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
bool IsNLLOffset()
Mother of all ROOT objects.
Definition: TObject.h:37
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
void RemoveConstantParameters(RooArgSet *set)
Definition: RooStatsUtils.h:62
virtual RooAbsReal * createProfile(const RooArgSet &paramsOfInterest)
Create a RooProfileLL object that eliminates all nuisance parameters in the present function...
Definition: RooAbsReal.cxx:461
Does a frequentist hypothesis test.
static RooArgSet * GetAsArgSet(RooFitResult *result, TString prefix="", bool withErrorsAndPulls=false)
static function to translate the given fit result to a RooArgSet in a generic way.
double result[121]
RooCmdArg ConditionalObservables(const RooArgSet &set)
RooCmdArg Constrain(const RooArgSet &params)