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 "RooProfileLL.h"
17
18/** \class RooStats::FrequentistCalculator
19 \ingroup Roostats
20
21Does a frequentist hypothesis test.
22
23Hypothesis Test Calculator using a full frequentist procedure for sampling the
24test statistic distribution.
25The nuisance parameters are fixed to their MLEs.
26The use of ToyMCSampler as the TestStatSampler is assumed.
27
28*/
29
31
32using namespace RooStats;
33using namespace std;
34
35////////////////////////////////////////////////////////////////////////////////
36
38 if (fFitInfo != NULL) {
39 delete fFitInfo;
40 fFitInfo = NULL;
41 }
42 if (fStoreFitInfo) {
43 fFitInfo = new RooArgSet();
44 }
45}
46
47////////////////////////////////////////////////////////////////////////////////
48
50}
51
52////////////////////////////////////////////////////////////////////////////////
53
54int FrequentistCalculator::PreNullHook(RooArgSet *parameterPoint, double obsTestStat) const {
55
56 // ****** any TestStatSampler ********
57
58 // create profile keeping everything but nuisance parameters fixed
59 RooArgSet * allParams = fNullModel->GetPdf()->getParameters(*fData);
60 RemoveConstantParameters(allParams);
61
62 // note: making nll or profile class variables can only be done in the constructor
63 // as all other hooks are const (which has to be because GetHypoTest is const). However,
64 // when setting it only in constructor, they would have to be changed every time SetNullModel
65 // or SetAltModel is called. Simply put, converting them into class variables breaks
66 // encapsulation.
67
68 bool doProfile = true;
69 RooArgSet allButNuisance(*allParams);
71 allButNuisance.remove(*fNullModel->GetNuisanceParameters());
73 oocoutI((TObject*)0,InputArguments) << "Using given conditional MLEs for Null." << endl;
74 allParams->assign(*fConditionalMLEsNull);
75 // LM: fConditionalMLEsNull must be nuisance parameters otherwise an error message will be printed
76 allButNuisance.add( *fConditionalMLEsNull );
79 remain.remove(*fConditionalMLEsNull,true,true);
80 if( remain.getSize() == 0 ) doProfile = false;
81 }
82 }
83 }else{
84 doProfile = false;
85 }
86 if (doProfile) {
87 oocoutI((TObject*)0,InputArguments) << "Profiling conditional MLEs for Null." << endl;
90
91 RooArgSet conditionalObs;
93 RooArgSet globalObs;
95
96 auto& config = GetGlobalRooStatsConfig();
99 RooFit::ConditionalObservables(conditionalObs),
100 RooFit::Offset(config.useLikelihoodOffset));
101 RooProfileLL* profile = dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance));
102 // set minimier options
105 profile->getVal(); // this will do fit and set nuisance parameters to profiled values
106
107 // Hack to extract a RooFitResult
108 if (fStoreFitInfo) {
109 RooFitResult *result = profile->minimizer()->save();
110 RooArgSet * detOutput = DetailedOutputAggregator::GetAsArgSet(result, "fitNull_");
111 fFitInfo->addOwned(*detOutput);
112 delete detOutput;
113 delete result;
114 }
115
116 delete profile;
117 delete nll;
119
120 // set in test statistics conditional and global observables
121 // (needed to get correct model likelihood)
122 TestStatistic * testStatistic = nullptr;
123 auto testStatSampler = GetTestStatSampler();
124 if (testStatSampler) testStatistic = testStatSampler->GetTestStatistic();
125 if (testStatistic) {
126 testStatistic->SetConditionalObservables(conditionalObs);
127 testStatistic->SetGlobalObservables(globalObs);
128 }
129
130 }
131
132 // add nuisance parameters to parameter point
134 parameterPoint->add(*fNullModel->GetNuisanceParameters());
135
136 delete allParams;
137
138
139 // ***** ToyMCSampler specific *******
140
141 // check whether TestStatSampler is a ToyMCSampler
142 ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
143 if(toymcs) {
144 oocoutI((TObject*)0,InputArguments) << "Using a ToyMCSampler. Now configuring for Null." << endl;
145
146 // variable number of toys
147 if(fNToysNull >= 0) toymcs->SetNToys(fNToysNull);
148
149 // set the global observables to be generated by the ToyMCSampler
151
152 // adaptive sampling
153 if(fNToysNullTail) {
154 oocoutI((TObject*)0,InputArguments) << "Adaptive Sampling" << endl;
155 if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
156 toymcs->SetToysRightTail(fNToysNullTail, obsTestStat);
157 }else{
158 toymcs->SetToysLeftTail(fNToysNullTail, obsTestStat);
159 }
160 }else{
161 toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
162 }
163
165 }
166
167 return 0;
168}
169
170////////////////////////////////////////////////////////////////////////////////
171
172int FrequentistCalculator::PreAltHook(RooArgSet *parameterPoint, double obsTestStat) const {
173
174 // ****** any TestStatSampler ********
175
176 // create profile keeping everything but nuisance parameters fixed
177 RooArgSet * allParams = fAltModel->GetPdf()->getParameters(*fData);
178 RemoveConstantParameters(allParams);
179
180 bool doProfile = true;
181 RooArgSet allButNuisance(*allParams);
183 allButNuisance.remove(*fAltModel->GetNuisanceParameters());
184 if( fConditionalMLEsAlt ) {
185 oocoutI((TObject*)0,InputArguments) << "Using given conditional MLEs for Alt." << endl;
186 allParams->assign(*fConditionalMLEsAlt);
187 // LM: fConditionalMLEsAlt must be nuisance parameters otherwise an error message will be printed
188 allButNuisance.add( *fConditionalMLEsAlt );
191 remain.remove(*fConditionalMLEsAlt,true,true);
192 if( remain.getSize() == 0 ) doProfile = false;
193 }
194 }
195 }else{
196 doProfile = false;
197 }
198 if (doProfile) {
199 oocoutI((TObject*)0,InputArguments) << "Profiling conditional MLEs for Alt." << endl;
202
203 RooArgSet conditionalObs;
205 RooArgSet globalObs;
207
208 const auto& config = GetGlobalRooStatsConfig();
210 RooFit::GlobalObservables(globalObs),
211 RooFit::ConditionalObservables(conditionalObs),
212 RooFit::Offset(config.useLikelihoodOffset));
213
214 RooProfileLL* profile = dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance));
215 // set minimizer options
217 profile->minimizer()->setPrintLevel(ROOT::Math::MinimizerOptions::DefaultPrintLevel()-1); // use -1 to make more silent
218 profile->getVal(); // this will do fit and set nuisance parameters to profiled values
219
220 // Hack to extract a RooFitResult
221 if (fStoreFitInfo) {
222 RooFitResult *result = profile->minimizer()->save();
223 RooArgSet * detOutput = DetailedOutputAggregator::GetAsArgSet(result, "fitAlt_");
224 fFitInfo->addOwned(*detOutput);
225 delete detOutput;
226 delete result;
227 }
228
229 delete profile;
230 delete nll;
232
233 // set in test statistics conditional and global observables
234 // (needed to get correct model likelihood)
235 TestStatistic * testStatistic = nullptr;
236 auto testStatSampler = GetTestStatSampler();
237 if (testStatSampler) testStatistic = testStatSampler->GetTestStatistic();
238 if (testStatistic) {
239 testStatistic->SetConditionalObservables(conditionalObs);
240 testStatistic->SetGlobalObservables(globalObs);
241 }
242
243 }
244
245 // add nuisance parameters to parameter point
247 parameterPoint->add(*fAltModel->GetNuisanceParameters());
248
249 delete allParams;
250
251 // ***** ToyMCSampler specific *******
252
253 // check whether TestStatSampler is a ToyMCSampler
254 ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
255 if(toymcs) {
256 oocoutI((TObject*)0,InputArguments) << "Using a ToyMCSampler. Now configuring for Alt." << endl;
257
258 // variable number of toys
259 if(fNToysAlt >= 0) toymcs->SetNToys(fNToysAlt);
260
261 // set the global observables to be generated by the ToyMCSampler
263
264 // adaptive sampling
265 if(fNToysAltTail) {
266 oocoutI((TObject*)0,InputArguments) << "Adaptive Sampling" << endl;
267 if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
268 toymcs->SetToysLeftTail(fNToysAltTail, obsTestStat);
269 }else{
270 toymcs->SetToysRightTail(fNToysAltTail, obsTestStat);
271 }
272 }else{
273 toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
274 }
275
276 }
277
278 return 0;
279}
#define oocoutI(o, a)
const Bool_t kFALSE
Definition RtypesCore.h:101
#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 add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
void assign(const RooAbsCollection &other) const
Sets the value, cache and constant attribute of any argument in our set that also appears in the othe...
virtual Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE)
Add an argument and transfer the ownership to the collection.
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:82
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:64
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:94
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:35
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
RooFitResult * save(const char *name=0, const char *title=0)
Save and return a RooFitResult snapshot of current minimizer status.
void setMinimizerType(const char *type)
Choose the minimizer algorithm.
Int_t setPrintLevel(Int_t newLevel)
Change the MINUIT internal printing level.
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...
RooMinimizer * 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:41
RooCmdArg Constrain(const RooArgSet &params)
RooCmdArg GlobalObservables(Args_t &&... argsOrArgSet)
RooCmdArg CloneData(Bool_t flag)
RooCmdArg Offset(Bool_t flag=kTRUE)
RooCmdArg ConditionalObservables(Args_t &&... argsOrArgSet)
Create a RooCmdArg to declare conditional observables.
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...