Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
15#include "RooMinimizer.h"
16#include "RooMinuit.h"
17#include "RooProfileLL.h"
18
19/** \class RooStats::FrequentistCalculator
20 \ingroup Roostats
21
22Does a frequentist hypothesis test.
23
24Hypothesis Test Calculator using a full frequentist procedure for sampling the
25test statistic distribution.
26The nuisance parameters are fixed to their MLEs.
27The use of ToyMCSampler as the TestStatSampler is assumed.
28
29*/
30
32
33using namespace RooStats;
34using 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
55int 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);
72 allButNuisance.remove(*fNullModel->GetNuisanceParameters());
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 );
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;
94 RooArgSet globalObs;
96
97 auto& config = GetGlobalRooStatsConfig();
100 RooFit::ConditionalObservables(conditionalObs),
101 RooFit::Offset(config.useLikelihoodOffset));
102 RooProfileLL* profile = dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance));
103 // set minimier options
104 profile->minimizer()->setMinimizerType(ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str());
105 profile->minimizer()->setPrintLevel(ROOT::Math::MinimizerOptions::DefaultPrintLevel()-1);
106 profile->getVal(); // this will do fit and set nuisance parameters to profiled values
107
108 // Hack to extract a RooFitResult
109 if (fStoreFitInfo) {
110 RooFitResult *result = profile->minimizer()->save();
111 RooArgSet * detOutput = DetailedOutputAggregator::GetAsArgSet(result, "fitNull_");
112 fFitInfo->addOwned(*detOutput);
113 delete detOutput;
114 delete result;
115 }
116
117 delete profile;
118 delete nll;
120
121 // set in test statistics conditional and global observables
122 // (needed to get correct model likelihood)
123 TestStatistic * testStatistic = nullptr;
124 auto testStatSampler = GetTestStatSampler();
125 if (testStatSampler) testStatistic = testStatSampler->GetTestStatistic();
126 if (testStatistic) {
127 testStatistic->SetConditionalObservables(conditionalObs);
128 testStatistic->SetGlobalObservables(globalObs);
129 }
130
131 }
132
133 // add nuisance parameters to parameter point
135 parameterPoint->add(*fNullModel->GetNuisanceParameters());
136
137 delete allParams;
138
139
140 // ***** ToyMCSampler specific *******
141
142 // check whether TestStatSampler is a ToyMCSampler
143 ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
144 if(toymcs) {
145 oocoutI((TObject*)0,InputArguments) << "Using a ToyMCSampler. Now configuring for Null." << endl;
146
147 // variable number of toys
148 if(fNToysNull >= 0) toymcs->SetNToys(fNToysNull);
149
150 // set the global observables to be generated by the ToyMCSampler
152
153 // adaptive sampling
154 if(fNToysNullTail) {
155 oocoutI((TObject*)0,InputArguments) << "Adaptive Sampling" << endl;
156 if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
157 toymcs->SetToysRightTail(fNToysNullTail, obsTestStat);
158 }else{
159 toymcs->SetToysLeftTail(fNToysNullTail, obsTestStat);
160 }
161 }else{
162 toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
163 }
164
166 }
167
168 return 0;
169}
170
171////////////////////////////////////////////////////////////////////////////////
172
173int FrequentistCalculator::PreAltHook(RooArgSet *parameterPoint, double obsTestStat) const {
174
175 // ****** any TestStatSampler ********
176
177 // create profile keeping everything but nuisance parameters fixed
178 RooArgSet * allParams = fAltModel->GetPdf()->getParameters(*fData);
179 RemoveConstantParameters(allParams);
180
181 bool doProfile = true;
182 RooArgSet allButNuisance(*allParams);
184 allButNuisance.remove(*fAltModel->GetNuisanceParameters());
185 if( fConditionalMLEsAlt ) {
186 oocoutI((TObject*)0,InputArguments) << "Using given conditional MLEs for Alt." << endl;
187 *allParams = *fConditionalMLEsAlt;
188 // LM: fConditionalMLEsAlt must be nuisance parameters otherwise an error message will be printed
189 allButNuisance.add( *fConditionalMLEsAlt );
192 remain.remove(*fConditionalMLEsAlt,true,true);
193 if( remain.getSize() == 0 ) doProfile = false;
194 }
195 }
196 }else{
197 doProfile = false;
198 }
199 if (doProfile) {
200 oocoutI((TObject*)0,InputArguments) << "Profiling conditional MLEs for Alt." << endl;
203
204 RooArgSet conditionalObs;
206 RooArgSet globalObs;
208
209 const auto& config = GetGlobalRooStatsConfig();
211 RooFit::GlobalObservables(globalObs),
212 RooFit::ConditionalObservables(conditionalObs),
213 RooFit::Offset(config.useLikelihoodOffset));
214
215 RooProfileLL* profile = dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance));
216 // set minimizer options
217 profile->minimizer()->setMinimizerType(ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str());
218 profile->minimizer()->setPrintLevel(ROOT::Math::MinimizerOptions::DefaultPrintLevel()-1); // use -1 to make more silent
219 profile->getVal(); // this will do fit and set nuisance parameters to profiled values
220
221 // Hack to extract a RooFitResult
222 if (fStoreFitInfo) {
223 RooFitResult *result = profile->minimizer()->save();
224 RooArgSet * detOutput = DetailedOutputAggregator::GetAsArgSet(result, "fitAlt_");
225 fFitInfo->addOwned(*detOutput);
226 delete detOutput;
227 delete result;
228 }
229
230 delete profile;
231 delete nll;
233
234 // set in test statistics conditional and global observables
235 // (needed to get correct model likelihood)
236 TestStatistic * testStatistic = nullptr;
237 auto testStatSampler = GetTestStatSampler();
238 if (testStatSampler) testStatistic = testStatSampler->GetTestStatistic();
239 if (testStatistic) {
240 testStatistic->SetConditionalObservables(conditionalObs);
241 testStatistic->SetGlobalObservables(globalObs);
242 }
243
244 }
245
246 // add nuisance parameters to parameter point
248 parameterPoint->add(*fAltModel->GetNuisanceParameters());
249
250 delete allParams;
251
252 // ***** ToyMCSampler specific *******
253
254 // check whether TestStatSampler is a ToyMCSampler
255 ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
256 if(toymcs) {
257 oocoutI((TObject*)0,InputArguments) << "Using a ToyMCSampler. Now configuring for Alt." << endl;
258
259 // variable number of toys
260 if(fNToysAlt >= 0) toymcs->SetNToys(fNToysAlt);
261
262 // set the global observables to be generated by the ToyMCSampler
264
265 // adaptive sampling
266 if(fNToysAltTail) {
267 oocoutI((TObject*)0,InputArguments) << "Adaptive Sampling" << endl;
268 if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
269 toymcs->SetToysLeftTail(fNToysAltTail, obsTestStat);
270 }else{
271 toymcs->SetToysRightTail(fNToysAltTail, obsTestStat);
272 }
273 }else{
274 toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
275 }
276
277 }
278
279 return 0;
280}
#define oocoutI(o, a)
const Bool_t kFALSE
Definition RtypesCore.h:92
#define ClassImp(name)
Definition Rtypes.h:364
static const std::string & DefaultMinimizerType()
RooArgSet * getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
Int_t getSize() const
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:49
virtual RooAbsReal * createNLL(RooAbsData &data, const RooLinkedList &cmdList)
Construct representation of -log(L) of PDFwith given dataset.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:61
virtual RooAbsReal * createProfile(const RooArgSet &paramsOfInterest)
Create a RooProfileLL object that eliminates all nuisance parameters in the present function.
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:91
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:29
Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE) override
Add element to non-owning set.
Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE) override
Add element to an owning set.
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
static RooMsgService & instance()
Return reference to singleton instance.
void setGlobalKillBelow(RooFit::MsgLevel level)
RooFit::MsgLevel globalKillBelow() const
Class RooProfileLL implements the profile likelihood estimator for a given likelihood and set of para...
MINIMIZER * minimizer()
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.
Does a frequentist hypothesis test.
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
const ModelConfig * GetNullModel(void) const
TestStatSampler * GetTestStatSampler(void) const
Returns instance of TestStatSampler.
const RooArgSet * GetConditionalObservables() const
get RooArgSet for conditional observables (return NULL if not existing)
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return NULL if not existing)
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return NULL if not existing)
void LoadSnapshot() const
load the snapshot from ws if it exists
RooAbsPdf * GetPdf() const
get model PDF (return NULL if pdf has not been specified or does not exist)
TestStatistic is an interface class to provide a facility for construction test statistics distributi...
virtual void SetConditionalObservables(const RooArgSet &)
interface to set conditional observables. If a test statistics needs them it will re-implement this f...
virtual void SetGlobalObservables(const RooArgSet &)
interface to set global observables. If a test statistics needs them it will re-implement this functi...
ToyMCSampler is an implementation of the TestStatSampler interface.
virtual void SetNToys(const Int_t ntoy)
void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold)
void SetToysLeftTail(Double_t toys, Double_t threshold)
virtual void SetGlobalObservables(const RooArgSet &o)
void SetToysRightTail(Double_t toys, Double_t threshold)
Mother of all ROOT objects.
Definition TObject.h:37
RooCmdArg Constrain(const RooArgSet &params)
RooCmdArg GlobalObservables(const RooArgSet &globs)
RooCmdArg CloneData(Bool_t flag)
RooCmdArg ConditionalObservables(const RooArgSet &set)
RooCmdArg Offset(Bool_t flag=kTRUE)
MsgLevel
Verbosity level for RooMsgService::StreamConfig in RooMsgService.
Namespace for the RooStats classes.
Definition Asimov.h:19
void RemoveConstantParameters(RooArgSet *set)
RooStatsConfig & GetGlobalRooStatsConfig()
Retrieve the config object which can be used to set flags for things like offsetting the likelihood o...