Logo ROOT   6.08/07
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 
11 /**
12 Does a frequentist hypothesis test. Nuisance parameters are fixed to their
13 MLEs.
14 */
15 
17 #include "RooStats/ToyMCSampler.h"
18 #include "RooStats/RooStatsUtils.h"
20 #include "RooMinimizer.h"
21 #include "RooMinuit.h"
22 #include "RooProfileLL.h"
23 
24 
26 
27 using namespace RooStats;
28 using namespace std;
29 
31  if (fFitInfo != NULL) {
32  delete fFitInfo;
33  fFitInfo = NULL;
34  }
35  if (fStoreFitInfo) {
36  fFitInfo = new RooArgSet();
37  }
38 }
39 
41 }
42 
43 int FrequentistCalculator::PreNullHook(RooArgSet *parameterPoint, double obsTestStat) const {
44 
45  // ****** any TestStatSampler ********
46 
47  // create profile keeping everything but nuisance parameters fixed
48  RooArgSet * allParams = fNullModel->GetPdf()->getParameters(*fData);
49  RemoveConstantParameters(allParams);
50 
51  // note: making nll or profile class variables can only be done in the constructor
52  // as all other hooks are const (which has to be because GetHypoTest is const). However,
53  // when setting it only in constructor, they would have to be changed every time SetNullModel
54  // or SetAltModel is called. Simply put, converting them into class variables breaks
55  // encapsulation.
56 
57  bool doProfile = true;
58  RooArgSet allButNuisance(*allParams);
59  if( fNullModel->GetNuisanceParameters() ) {
60  allButNuisance.remove(*fNullModel->GetNuisanceParameters());
61  if( fConditionalMLEsNull ) {
62  oocoutI((TObject*)0,InputArguments) << "Using given conditional MLEs for Null." << endl;
63  *allParams = *fConditionalMLEsNull;
64  // LM: fConditionalMLEsNull must be nuisance parameters otherwise an error message will be printed
65  allButNuisance.add( *fConditionalMLEsNull );
66  if (fNullModel->GetNuisanceParameters()) {
67  RooArgSet remain(*fNullModel->GetNuisanceParameters());
68  remain.remove(*fConditionalMLEsNull,true,true);
69  if( remain.getSize() == 0 ) doProfile = false;
70  }
71  }
72  }else{
73  doProfile = false;
74  }
75  if (doProfile) {
76  oocoutI((TObject*)0,InputArguments) << "Profiling conditional MLEs for Null." << endl;
79 
80  RooArgSet conditionalObs;
81  if (fNullModel->GetConditionalObservables()) conditionalObs.add(*fNullModel->GetConditionalObservables());
82 
83  RooAbsReal* nll = fNullModel->GetPdf()->createNLL(*const_cast<RooAbsData*>(fData), RooFit::CloneData(kFALSE), RooFit::Constrain(*allParams),
85  RooProfileLL* profile = dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance));
86  profile->getVal(); // this will do fit and set nuisance parameters to profiled values
87 
88  // Hack to extract a RooFitResult
89  if (fStoreFitInfo) {
90  RooFitResult *result = profile->minimizer()->save();
91  RooArgSet * detOutput = DetailedOutputAggregator::GetAsArgSet(result, "fitNull_");
92  fFitInfo->addOwned(*detOutput);
93  delete detOutput;
94  delete result;
95  }
96 
97  delete profile;
98  delete nll;
100  }
101 
102  // add nuisance parameters to parameter point
103  if(fNullModel->GetNuisanceParameters())
104  parameterPoint->add(*fNullModel->GetNuisanceParameters());
105 
106  delete allParams;
107 
108 
109 
110  // ***** ToyMCSampler specific *******
111 
112  // check whether TestStatSampler is a ToyMCSampler
113  ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
114  if(toymcs) {
115  oocoutI((TObject*)0,InputArguments) << "Using a ToyMCSampler. Now configuring for Null." << endl;
116 
117  // variable number of toys
118  if(fNToysNull >= 0) toymcs->SetNToys(fNToysNull);
119 
120  // set the global observables to be generated by the ToyMCSampler
121  toymcs->SetGlobalObservables(*fNullModel->GetGlobalObservables());
122 
123  // adaptive sampling
124  if(fNToysNullTail) {
125  oocoutI((TObject*)0,InputArguments) << "Adaptive Sampling" << endl;
126  if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
127  toymcs->SetToysRightTail(fNToysNullTail, obsTestStat);
128  }else{
129  toymcs->SetToysLeftTail(fNToysNullTail, obsTestStat);
130  }
131  }else{
132  toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
133  }
134 
135  GetNullModel()->LoadSnapshot();
136  }
137 
138  return 0;
139 }
140 
141 
142 int FrequentistCalculator::PreAltHook(RooArgSet *parameterPoint, double obsTestStat) const {
143 
144  // ****** any TestStatSampler ********
145 
146  // create profile keeping everything but nuisance parameters fixed
147  RooArgSet * allParams = fAltModel->GetPdf()->getParameters(*fData);
148  RemoveConstantParameters(allParams);
149 
150  bool doProfile = true;
151  RooArgSet allButNuisance(*allParams);
152  if( fAltModel->GetNuisanceParameters() ) {
153  allButNuisance.remove(*fAltModel->GetNuisanceParameters());
154  if( fConditionalMLEsAlt ) {
155  oocoutI((TObject*)0,InputArguments) << "Using given conditional MLEs for Alt." << endl;
156  *allParams = *fConditionalMLEsAlt;
157  // LM: fConditionalMLEsAlt must be nuisance parameters otherwise an error message will be printed
158  allButNuisance.add( *fConditionalMLEsAlt );
159  if (fAltModel->GetNuisanceParameters()) {
160  RooArgSet remain(*fAltModel->GetNuisanceParameters());
161  remain.remove(*fConditionalMLEsAlt,true,true);
162  if( remain.getSize() == 0 ) doProfile = false;
163  }
164  }
165  }else{
166  doProfile = false;
167  }
168  if (doProfile) {
169  oocoutI((TObject*)0,InputArguments) << "Profiling conditional MLEs for Alt." << endl;
172 
173  RooArgSet conditionalObs;
174  if (fAltModel->GetConditionalObservables()) conditionalObs.add(*fAltModel->GetConditionalObservables());
175 
176  RooAbsReal* nll = fAltModel->GetPdf()->createNLL(*const_cast<RooAbsData*>(fData), RooFit::CloneData(kFALSE), RooFit::Constrain(*allParams),
178 
179  RooProfileLL* profile = dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance));
180  profile->getVal(); // this will do fit and set nuisance parameters to profiled values
181 
182  // Hack to extract a RooFitResult
183  if (fStoreFitInfo) {
184  RooFitResult *result = profile->minimizer()->save();
185  RooArgSet * detOutput = DetailedOutputAggregator::GetAsArgSet(result, "fitAlt_");
186  fFitInfo->addOwned(*detOutput);
187  delete detOutput;
188  delete result;
189  }
190 
191  delete profile;
192  delete nll;
194  }
195 
196  // add nuisance parameters to parameter point
197  if(fAltModel->GetNuisanceParameters())
198  parameterPoint->add(*fAltModel->GetNuisanceParameters());
199 
200  delete allParams;
201 
202 
203 
204 
205  // ***** ToyMCSampler specific *******
206 
207  // check whether TestStatSampler is a ToyMCSampler
208  ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
209  if(toymcs) {
210  oocoutI((TObject*)0,InputArguments) << "Using a ToyMCSampler. Now configuring for Alt." << endl;
211 
212  // variable number of toys
213  if(fNToysAlt >= 0) toymcs->SetNToys(fNToysAlt);
214 
215  // set the global observables to be generated by the ToyMCSampler
216  toymcs->SetGlobalObservables(*fAltModel->GetGlobalObservables());
217 
218  // adaptive sampling
219  if(fNToysAltTail) {
220  oocoutI((TObject*)0,InputArguments) << "Adaptive Sampling" << endl;
221  if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
222  toymcs->SetToysLeftTail(fNToysAltTail, obsTestStat);
223  }else{
224  toymcs->SetToysRightTail(fNToysAltTail, obsTestStat);
225  }
226  }else{
227  toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
228  }
229 
230  }
231 
232  return 0;
233 }
234 
235 
236 
237 
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:246
virtual void SetGlobalObservables(const RooArgSet &o)
Definition: ToyMCSampler.h:205
RooCmdArg CloneData(Bool_t flag)
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
#define oocoutI(o, a)
Definition: RooMsgService.h:45
RooFit::MsgLevel globalKillBelow() const
MINIMIZER * minimizer()
Definition: RooProfileLL.h:38
const Bool_t kFALSE
Definition: Rtypes.h:92
static RooMsgService & instance()
Return reference to singleton instance.
STL namespace.
void SetToysRightTail(Double_t toys, Double_t threshold)
Definition: ToyMCSampler.h:251
void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold)
Definition: ToyMCSampler.h:256
virtual void SetNToys(const Int_t ntoy)
Definition: ToyMCSampler.h:176
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:99
Namespace for the RooStats classes.
Definition: Asimov.h:20
#define ClassImp(name)
Definition: Rtypes.h:279
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:73
virtual RooAbsReal * createProfile(const RooArgSet &paramsOfInterest)
Create a RooProfileLL object that eliminates all nuisance parameters in the present function...
Definition: RooAbsReal.cxx:463
Hypothesis Test Calculator using a full frequentist procedure for sampling the test statistic distrib...
#define NULL
Definition: Rtypes.h:82
static RooArgSet * GetAsArgSet(RooFitResult *result, TString prefix="", bool withErrorsAndPulls=false)
double result[121]
RooCmdArg ConditionalObservables(const RooArgSet &set)
RooCmdArg Constrain(const RooArgSet &params)