Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 std::endl;
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
51
52////////////////////////////////////////////////////////////////////////////////
53
55
56 // ****** any TestStatSampler ********
57
58 // create profile keeping everything but nuisance parameters fixed
59 std::unique_ptr<RooArgSet> allParams{fNullModel->GetPdf()->getParameters(*fData)};
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;
70 if( fNullModel->GetNuisanceParameters() ) {
71 allButNuisance.remove(*fNullModel->GetNuisanceParameters());
73 oocoutI(nullptr,InputArguments) << "Using given conditional MLEs for Null." << endl;
75 // LM: fConditionalMLEsNull must be nuisance parameters otherwise an error message will be printed
77 if (fNullModel->GetNuisanceParameters()) {
78 RooArgSet remain(*fNullModel->GetNuisanceParameters());
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;
89 RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
90
92 if (fNullModel->GetConditionalObservables()) conditionalObs.add(*fNullModel->GetConditionalObservables());
94 if (fNullModel->GetGlobalObservables()) globalObs.add(*fNullModel->GetGlobalObservables());
95
96 auto& config = GetGlobalRooStatsConfig();
97 std::unique_ptr<RooAbsReal> nll{fNullModel->GetPdf()->createNLL(*const_cast<RooAbsData*>(fData), RooFit::CloneData(false), RooFit::Constrain(*allParams),
100 RooFit::Offset(config.useLikelihoodOffset))};
101 std::unique_ptr<RooAbsArg> profileOwner{nll->createProfile(allButNuisance)};
102 auto profile = dynamic_cast<RooProfileLL*>(profileOwner.get());
103 // 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 std::unique_ptr<RooFitResult> result {profile->minimizer()->save()};
110 std::unique_ptr<RooArgSet> detOutput {DetailedOutputAggregator::GetAsArgSet(result.get(), "fitNull_")};
112 }
113
114 RooMsgService::instance().setGlobalKillBelow(msglevel);
115
116 // set in test statistics conditional and global observables
117 // (needed to get correct model likelihood)
118 TestStatistic * testStatistic = nullptr;
120 if (testStatSampler) testStatistic = testStatSampler->GetTestStatistic();
121 if (testStatistic) {
122 testStatistic->SetConditionalObservables(conditionalObs);
123 testStatistic->SetGlobalObservables(globalObs);
124 }
125
126 }
127
128 // add nuisance parameters to parameter point
129 if(fNullModel->GetNuisanceParameters())
130 parameterPoint->add(*fNullModel->GetNuisanceParameters());
131
132
133 // ***** ToyMCSampler specific *******
134
135 // check whether TestStatSampler is a ToyMCSampler
137 if(toymcs) {
138 oocoutI(nullptr,InputArguments) << "Using a ToyMCSampler. Now configuring for Null." << endl;
139
140 // variable number of toys
141 if(fNToysNull >= 0) toymcs->SetNToys(fNToysNull);
142
143 // set the global observables to be generated by the ToyMCSampler
144 toymcs->SetGlobalObservables(*fNullModel->GetGlobalObservables());
145
146 // adaptive sampling
147 if(fNToysNullTail) {
148 oocoutI(nullptr,InputArguments) << "Adaptive Sampling" << endl;
149 if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
150 toymcs->SetToysRightTail(fNToysNullTail, obsTestStat);
151 }else{
152 toymcs->SetToysLeftTail(fNToysNullTail, obsTestStat);
153 }
154 }else{
155 toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
156 }
157
158 GetNullModel()->LoadSnapshot();
159 }
160
161 return 0;
162}
163
164////////////////////////////////////////////////////////////////////////////////
165
167
168 // ****** any TestStatSampler ********
169
170 // create profile keeping everything but nuisance parameters fixed
171 std::unique_ptr<RooArgSet> allParams{fAltModel->GetPdf()->getParameters(*fData)};
173
174 bool doProfile = true;
176 if( fAltModel->GetNuisanceParameters() ) {
177 allButNuisance.remove(*fAltModel->GetNuisanceParameters());
178 if( fConditionalMLEsAlt ) {
179 oocoutI(nullptr,InputArguments) << "Using given conditional MLEs for Alt." << endl;
181 // LM: fConditionalMLEsAlt must be nuisance parameters otherwise an error message will be printed
183 if (fAltModel->GetNuisanceParameters()) {
184 RooArgSet remain(*fAltModel->GetNuisanceParameters());
185 remain.remove(*fConditionalMLEsAlt,true,true);
186 if( remain.empty() ) doProfile = false;
187 }
188 }
189 }else{
190 doProfile = false;
191 }
192 if (doProfile) {
193 oocoutI(nullptr,InputArguments) << "Profiling conditional MLEs for Alt." << endl;
195 RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
196
198 if (fAltModel->GetConditionalObservables()) conditionalObs.add(*fAltModel->GetConditionalObservables());
200 if (fAltModel->GetGlobalObservables()) globalObs.add(*fAltModel->GetGlobalObservables());
201
202 const auto& config = GetGlobalRooStatsConfig();
203 std::unique_ptr<RooAbsReal> nll{fAltModel->GetPdf()->createNLL(*const_cast<RooAbsData*>(fData), RooFit::CloneData(false), RooFit::Constrain(*allParams),
206 RooFit::Offset(config.useLikelihoodOffset))};
207
208 std::unique_ptr<RooAbsReal> profileOwner{nll->createProfile(allButNuisance)};
209 auto profile = dynamic_cast<RooProfileLL*>(profileOwner.get());
210 // set minimizer options
211 profile->minimizer()->setPrintLevel(ROOT::Math::MinimizerOptions::DefaultPrintLevel()-1); // use -1 to make more silent
212 profile->getVal(); // this will do fit and set nuisance parameters to profiled values
213
214 // Hack to extract a RooFitResult
215 if (fStoreFitInfo) {
216 std::unique_ptr<RooFitResult> result {profile->minimizer()->save()};
217 std::unique_ptr<RooArgSet> detOutput {DetailedOutputAggregator::GetAsArgSet(result.get(), "fitAlt_")};
219 }
220
221 RooMsgService::instance().setGlobalKillBelow(msglevel);
222
223 // set in test statistics conditional and global observables
224 // (needed to get correct model likelihood)
225 TestStatistic * testStatistic = nullptr;
227 if (testStatSampler) testStatistic = testStatSampler->GetTestStatistic();
228 if (testStatistic) {
229 testStatistic->SetConditionalObservables(conditionalObs);
230 testStatistic->SetGlobalObservables(globalObs);
231 }
232
233 }
234
235 // add nuisance parameters to parameter point
236 if(fAltModel->GetNuisanceParameters())
237 parameterPoint->add(*fAltModel->GetNuisanceParameters());
238
239 // ***** ToyMCSampler specific *******
240
241 // check whether TestStatSampler is a ToyMCSampler
243 if(toymcs) {
244 oocoutI(nullptr,InputArguments) << "Using a ToyMCSampler. Now configuring for Alt." << endl;
245
246 // variable number of toys
247 if(fNToysAlt >= 0) toymcs->SetNToys(fNToysAlt);
248
249 // set the global observables to be generated by the ToyMCSampler
250 toymcs->SetGlobalObservables(*fAltModel->GetGlobalObservables());
251
252 // adaptive sampling
253 if(fNToysAltTail) {
254 oocoutI(nullptr,InputArguments) << "Adaptive Sampling" << endl;
255 if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
256 toymcs->SetToysLeftTail(fNToysAltTail, obsTestStat);
257 }else{
258 toymcs->SetToysRightTail(fNToysAltTail, obsTestStat);
259 }
260 }else{
261 toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
262 }
263
264 }
265
266 return 0;
267}
#define oocoutI(o, a)
#define ClassImp(name)
Definition Rtypes.h:377
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
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.
Implements the profile likelihood estimator for a given likelihood and set of parameters of interest.
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.
TestStatistic is an interface class to provide a facility for construction test statistics distributi...
ToyMCSampler is an implementation of the TestStatSampler interface.
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...