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
30
31using namespace RooStats;
32using std::endl;
33
34////////////////////////////////////////////////////////////////////////////////
35
37 if (fFitInfo != nullptr) {
38 delete fFitInfo;
39 fFitInfo = nullptr;
40 }
41 if (fStoreFitInfo) {
42 fFitInfo = new RooArgSet();
43 }
44}
45
46////////////////////////////////////////////////////////////////////////////////
47
50
51////////////////////////////////////////////////////////////////////////////////
52
54
55 // ****** any TestStatSampler ********
56
57 // create profile keeping everything but nuisance parameters fixed
58 std::unique_ptr<RooArgSet> allParams{fNullModel->GetPdf()->getParameters(*fData)};
59 RemoveConstantParameters(&*allParams);
60
61 // note: making nll or profile class variables can only be done in the constructor
62 // as all other hooks are const (which has to be because GetHypoTest is const). However,
63 // when setting it only in constructor, they would have to be changed every time SetNullModel
64 // or SetAltModel is called. Simply put, converting them into class variables breaks
65 // encapsulation.
66
67 bool doProfile = true;
68 RooArgSet allButNuisance(*allParams);
69 if( fNullModel->GetNuisanceParameters() ) {
70 allButNuisance.remove(*fNullModel->GetNuisanceParameters());
72 oocoutI(nullptr,InputArguments) << "Using given conditional MLEs for Null." << std::endl;
73 allParams->assign(*fConditionalMLEsNull);
74 // LM: fConditionalMLEsNull must be nuisance parameters otherwise an error message will be printed
76 if (fNullModel->GetNuisanceParameters()) {
77 RooArgSet remain(*fNullModel->GetNuisanceParameters());
78 remain.remove(*fConditionalMLEsNull,true,true);
79 if( remain.empty() ) doProfile = false;
80 }
81 }
82 }else{
83 doProfile = false;
84 }
85 if (doProfile) {
86 oocoutI(nullptr,InputArguments) << "Profiling conditional MLEs for Null." << std::endl;
88 RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
89
91 if (fNullModel->GetConditionalObservables()) conditionalObs.add(*fNullModel->GetConditionalObservables());
93 if (fNullModel->GetGlobalObservables()) globalObs.add(*fNullModel->GetGlobalObservables());
94
95 auto& config = GetGlobalRooStatsConfig();
96 std::unique_ptr<RooAbsReal> nll{fNullModel->GetPdf()->createNLL(*const_cast<RooAbsData*>(fData), RooFit::CloneData(false), RooFit::Constrain(*allParams),
99 RooFit::Offset(config.useLikelihoodOffset))};
100 std::unique_ptr<RooAbsArg> profileOwner{nll->createProfile(allButNuisance)};
101 auto profile = dynamic_cast<RooProfileLL*>(profileOwner.get());
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_")};
111 }
112
113 RooMsgService::instance().setGlobalKillBelow(msglevel);
114
115 // set in test statistics conditional and global observables
116 // (needed to get correct model likelihood)
117 TestStatistic * testStatistic = nullptr;
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
128 if(fNullModel->GetNuisanceParameters())
129 parameterPoint->add(*fNullModel->GetNuisanceParameters());
130
131
132 // ***** ToyMCSampler specific *******
133
134 // check whether TestStatSampler is a ToyMCSampler
136 if(toymcs) {
137 oocoutI(nullptr,InputArguments) << "Using a ToyMCSampler. Now configuring for Null." << std::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
143 toymcs->SetGlobalObservables(*fNullModel->GetGlobalObservables());
144
145 // adaptive sampling
146 if(fNToysNullTail) {
147 oocoutI(nullptr,InputArguments) << "Adaptive Sampling" << std::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
157 GetNullModel()->LoadSnapshot();
158 }
159
160 return 0;
161}
162
163////////////////////////////////////////////////////////////////////////////////
164
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);
175 if( fAltModel->GetNuisanceParameters() ) {
176 allButNuisance.remove(*fAltModel->GetNuisanceParameters());
177 if( fConditionalMLEsAlt ) {
178 oocoutI(nullptr,InputArguments) << "Using given conditional MLEs for Alt." << std::endl;
179 allParams->assign(*fConditionalMLEsAlt);
180 // LM: fConditionalMLEsAlt must be nuisance parameters otherwise an error message will be printed
182 if (fAltModel->GetNuisanceParameters()) {
183 RooArgSet remain(*fAltModel->GetNuisanceParameters());
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." << std::endl;
194 RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
195
197 if (fAltModel->GetConditionalObservables()) conditionalObs.add(*fAltModel->GetConditionalObservables());
199 if (fAltModel->GetGlobalObservables()) globalObs.add(*fAltModel->GetGlobalObservables());
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),
205 RooFit::Offset(config.useLikelihoodOffset))};
206
207 std::unique_ptr<RooAbsReal> profileOwner{nll->createProfile(allButNuisance)};
208 auto profile = dynamic_cast<RooProfileLL*>(profileOwner.get());
209 // set minimizer options
210 profile->minimizer()->setPrintLevel(ROOT::Math::MinimizerOptions::DefaultPrintLevel()-1); // use -1 to make more silent
211 profile->getVal(); // this will do fit and set nuisance parameters to profiled values
212
213 // Hack to extract a RooFitResult
214 if (fStoreFitInfo) {
215 std::unique_ptr<RooFitResult> result {profile->minimizer()->save()};
216 std::unique_ptr<RooArgSet> detOutput {DetailedOutputAggregator::GetAsArgSet(result.get(), "fitAlt_")};
218 }
219
220 RooMsgService::instance().setGlobalKillBelow(msglevel);
221
222 // set in test statistics conditional and global observables
223 // (needed to get correct model likelihood)
224 TestStatistic * testStatistic = nullptr;
226 if (testStatSampler) testStatistic = testStatSampler->GetTestStatistic();
227 if (testStatistic) {
228 testStatistic->SetConditionalObservables(conditionalObs);
229 testStatistic->SetGlobalObservables(globalObs);
230 }
231
232 }
233
234 // add nuisance parameters to parameter point
235 if(fAltModel->GetNuisanceParameters())
236 parameterPoint->add(*fAltModel->GetNuisanceParameters());
237
238 // ***** ToyMCSampler specific *******
239
240 // check whether TestStatSampler is a ToyMCSampler
242 if(toymcs) {
243 oocoutI(nullptr,InputArguments) << "Using a ToyMCSampler. Now configuring for Alt." << std::endl;
244
245 // variable number of toys
246 if(fNToysAlt >= 0) toymcs->SetNToys(fNToysAlt);
247
248 // set the global observables to be generated by the ToyMCSampler
249 toymcs->SetGlobalObservables(*fAltModel->GetGlobalObservables());
250
251 // adaptive sampling
252 if(fNToysAltTail) {
253 oocoutI(nullptr,InputArguments) << "Adaptive Sampling" << std::endl;
254 if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
255 toymcs->SetToysLeftTail(fNToysAltTail, obsTestStat);
256 }else{
257 toymcs->SetToysRightTail(fNToysAltTail, obsTestStat);
258 }
259 }else{
260 toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
261 }
262
263 }
264
265 return 0;
266}
#define oocoutI(o, a)
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:24
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.
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 CodegenImpl.h:58
void RemoveConstantParameters(RooArgSet *set)
RooStatsConfig & GetGlobalRooStatsConfig()
Retrieve the config object which can be used to set flags for things like offsetting the likelihood o...