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 != nullptr) {
39 delete fFitInfo;
40 fFitInfo = nullptr;
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 std::unique_ptr<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(nullptr,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.empty() ) doProfile = false;
81 }
82 }
83 }else{
84 doProfile = false;
85 }
86 if (doProfile) {
87 oocoutI(nullptr,InputArguments) << "Profiling conditional MLEs for Null." << endl;
90
91 RooArgSet conditionalObs;
93 RooArgSet globalObs;
95
96 auto& config = GetGlobalRooStatsConfig();
97 std::unique_ptr<RooAbsReal> nll{fNullModel->GetPdf()->createNLL(*const_cast<RooAbsData*>(fData), RooFit::CloneData(false), RooFit::Constrain(*allParams),
99 RooFit::ConditionalObservables(conditionalObs),
100 RooFit::Offset(config.useLikelihoodOffset))};
101 std::unique_ptr<RooProfileLL> profile{dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance))};
102 // set minimier options
104 profile->getVal(); // this will do fit and set nuisance parameters to profiled values
105
106 // Hack to extract a RooFitResult
107 if (fStoreFitInfo) {
108 std::unique_ptr<RooFitResult> result {profile->minimizer()->save()};
109 std::unique_ptr<RooArgSet> detOutput {DetailedOutputAggregator::GetAsArgSet(result.get(), "fitNull_")};
110 fFitInfo->addOwned(*detOutput);
111 }
112
114
115 // set in test statistics conditional and global observables
116 // (needed to get correct model likelihood)
117 TestStatistic * testStatistic = nullptr;
118 auto testStatSampler = GetTestStatSampler();
119 if (testStatSampler) testStatistic = testStatSampler->GetTestStatistic();
120 if (testStatistic) {
121 testStatistic->SetConditionalObservables(conditionalObs);
122 testStatistic->SetGlobalObservables(globalObs);
123 }
124
125 }
126
127 // add nuisance parameters to parameter point
129 parameterPoint->add(*fNullModel->GetNuisanceParameters());
130
131
132 // ***** ToyMCSampler specific *******
133
134 // check whether TestStatSampler is a ToyMCSampler
135 ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
136 if(toymcs) {
137 oocoutI(nullptr,InputArguments) << "Using a ToyMCSampler. Now configuring for Null." << endl;
138
139 // variable number of toys
140 if(fNToysNull >= 0) toymcs->SetNToys(fNToysNull);
141
142 // set the global observables to be generated by the ToyMCSampler
144
145 // adaptive sampling
146 if(fNToysNullTail) {
147 oocoutI(nullptr,InputArguments) << "Adaptive Sampling" << endl;
148 if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
149 toymcs->SetToysRightTail(fNToysNullTail, obsTestStat);
150 }else{
151 toymcs->SetToysLeftTail(fNToysNullTail, obsTestStat);
152 }
153 }else{
154 toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
155 }
156
158 }
159
160 return 0;
161}
162
163////////////////////////////////////////////////////////////////////////////////
164
165int FrequentistCalculator::PreAltHook(RooArgSet *parameterPoint, double obsTestStat) const {
166
167 // ****** any TestStatSampler ********
168
169 // create profile keeping everything but nuisance parameters fixed
170 std::unique_ptr<RooArgSet> allParams{fAltModel->GetPdf()->getParameters(*fData)};
171 RemoveConstantParameters(&*allParams);
172
173 bool doProfile = true;
174 RooArgSet allButNuisance(*allParams);
176 allButNuisance.remove(*fAltModel->GetNuisanceParameters());
177 if( fConditionalMLEsAlt ) {
178 oocoutI(nullptr,InputArguments) << "Using given conditional MLEs for Alt." << endl;
179 allParams->assign(*fConditionalMLEsAlt);
180 // LM: fConditionalMLEsAlt must be nuisance parameters otherwise an error message will be printed
181 allButNuisance.add( *fConditionalMLEsAlt );
184 remain.remove(*fConditionalMLEsAlt,true,true);
185 if( remain.empty() ) doProfile = false;
186 }
187 }
188 }else{
189 doProfile = false;
190 }
191 if (doProfile) {
192 oocoutI(nullptr,InputArguments) << "Profiling conditional MLEs for Alt." << endl;
195
196 RooArgSet conditionalObs;
198 RooArgSet globalObs;
200
201 const auto& config = GetGlobalRooStatsConfig();
202 std::unique_ptr<RooAbsReal> nll{fAltModel->GetPdf()->createNLL(*const_cast<RooAbsData*>(fData), RooFit::CloneData(false), RooFit::Constrain(*allParams),
203 RooFit::GlobalObservables(globalObs),
204 RooFit::ConditionalObservables(conditionalObs),
205 RooFit::Offset(config.useLikelihoodOffset))};
206
207 std::unique_ptr<RooProfileLL> profile{dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance))};
208 // set minimizer options
209 profile->minimizer()->setPrintLevel(ROOT::Math::MinimizerOptions::DefaultPrintLevel()-1); // use -1 to make more silent
210 profile->getVal(); // this will do fit and set nuisance parameters to profiled values
211
212 // Hack to extract a RooFitResult
213 if (fStoreFitInfo) {
214 std::unique_ptr<RooFitResult> result {profile->minimizer()->save()};
215 std::unique_ptr<RooArgSet> detOutput {DetailedOutputAggregator::GetAsArgSet(result.get(), "fitAlt_")};
216 fFitInfo->addOwned(*detOutput);
217 }
218
220
221 // set in test statistics conditional and global observables
222 // (needed to get correct model likelihood)
223 TestStatistic * testStatistic = nullptr;
224 auto testStatSampler = GetTestStatSampler();
225 if (testStatSampler) testStatistic = testStatSampler->GetTestStatistic();
226 if (testStatistic) {
227 testStatistic->SetConditionalObservables(conditionalObs);
228 testStatistic->SetGlobalObservables(globalObs);
229 }
230
231 }
232
233 // add nuisance parameters to parameter point
235 parameterPoint->add(*fAltModel->GetNuisanceParameters());
236
237 // ***** ToyMCSampler specific *******
238
239 // check whether TestStatSampler is a ToyMCSampler
240 ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
241 if(toymcs) {
242 oocoutI(nullptr,InputArguments) << "Using a ToyMCSampler. Now configuring for Alt." << endl;
243
244 // variable number of toys
245 if(fNToysAlt >= 0) toymcs->SetNToys(fNToysAlt);
246
247 // set the global observables to be generated by the ToyMCSampler
249
250 // adaptive sampling
251 if(fNToysAltTail) {
252 oocoutI(nullptr,InputArguments) << "Adaptive Sampling" << endl;
253 if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
254 toymcs->SetToysLeftTail(fNToysAltTail, obsTestStat);
255 }else{
256 toymcs->SetToysRightTail(fNToysAltTail, obsTestStat);
257 }
258 }else{
259 toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
260 }
261
262 }
263
264 return 0;
265}
#define oocoutI(o, a)
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
RooFit::OwningPtr< 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...
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:59
virtual RooFit::OwningPtr< RooAbsReal > createNLL(RooAbsData &data, const RooLinkedList &cmdList={})
Construct representation of -log(L) of PDF with given dataset.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
void setPrintLevel(int 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)
Translate the given fit result to a RooArgSet in a generic way.
Does a frequentist hypothesis test.
int PreNullHook(RooArgSet *parameterPoint, double obsTestStat) const override
configure TestStatSampler for the Null run
int PreAltHook(RooArgSet *parameterPoint, double obsTestStat) const override
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 nullptr if not existing)
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return nullptr if not existing)
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return nullptr if not existing)
void LoadSnapshot() const
load the snapshot from ws if it exists
RooAbsPdf * GetPdf() const
get model PDF (return nullptr 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 toys, double low_threshold, double high_threshold)
void SetToysRightTail(double toys, double threshold)
void SetToysLeftTail(double toys, double threshold)
void SetGlobalObservables(const RooArgSet &o) override
specify the conditional observables
RooCmdArg Offset(std::string const &mode)
RooCmdArg Constrain(const RooArgSet &params)
RooCmdArg GlobalObservables(Args_t &&... argsOrArgSet)
RooCmdArg CloneData(bool flag)
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...