ROOT  6.06/09
Reference Guide
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 
11 /**
12 Does a frequentist hypothesis test. Nuisance parameters are fixed to their
13 MLEs.
14 */
15 
17 #include "RooStats/ToyMCSampler.h"
18 #include "RooMinimizer.h"
19 #include "RooMinuit.h"
20 #include "RooProfileLL.h"
21 
22 
24 
25 using namespace RooStats;
26 using namespace std;
27 
28 void FrequentistCalculator::PreHook() const {
29  if (fFitInfo != NULL) {
30  delete fFitInfo;
31  fFitInfo = NULL;
32  }
33  if (fStoreFitInfo) {
34  fFitInfo = new RooArgSet();
35  }
36 }
37 
38 void FrequentistCalculator::PostHook() const {
39 }
40 
41 int FrequentistCalculator::PreNullHook(RooArgSet *parameterPoint, double obsTestStat) const {
42 
43  // ****** any TestStatSampler ********
44 
45  // create profile keeping everything but nuisance parameters fixed
46  RooArgSet * allParams = fNullModel->GetPdf()->getParameters(*fData);
47  RemoveConstantParameters(allParams);
48 
49  // note: making nll or profile class variables can only be done in the constructor
50  // as all other hooks are const (which has to be because GetHypoTest is const). However,
51  // when setting it only in constructor, they would have to be changed every time SetNullModel
52  // or SetAltModel is called. Simply put, converting them into class variables breaks
53  // encapsulation.
54 
55  bool doProfile = true;
56  RooArgSet allButNuisance(*allParams);
57  if( fNullModel->GetNuisanceParameters() ) {
58  allButNuisance.remove(*fNullModel->GetNuisanceParameters());
59  if( fConditionalMLEsNull ) {
60  oocoutI((TObject*)0,InputArguments) << "Using given conditional MLEs for Null." << endl;
61  *allParams = *fConditionalMLEsNull;
62  // LM: fConditionalMLEsNull must be nuisance parameters otherwise an error message will be printed
63  allButNuisance.add( *fConditionalMLEsNull );
64  if (fNullModel->GetNuisanceParameters()) {
65  RooArgSet remain(*fNullModel->GetNuisanceParameters());
66  remain.remove(*fConditionalMLEsNull,true,true);
67  if( remain.getSize() == 0 ) doProfile = false;
68  }
69  }
70  }else{
71  doProfile = false;
72  }
73  if (doProfile) {
74  oocoutI((TObject*)0,InputArguments) << "Profiling conditional MLEs for Null." << endl;
77 
78  RooArgSet conditionalObs;
79  if (fNullModel->GetConditionalObservables()) conditionalObs.add(*fNullModel->GetConditionalObservables());
80 
81  RooAbsReal* nll = fNullModel->GetPdf()->createNLL(*const_cast<RooAbsData*>(fData), RooFit::CloneData(kFALSE), RooFit::Constrain(*allParams),
83  RooProfileLL* profile = dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance));
84  profile->getVal(); // this will do fit and set nuisance parameters to profiled values
85 
86  // Hack to extract a RooFitResult
87  if (fStoreFitInfo) {
88  RooFitResult *result = profile->minimizer()->save();
89  RooArgSet * detOutput = DetailedOutputAggregator::GetAsArgSet(result, "fitNull_");
90  fFitInfo->addOwned(*detOutput);
91  delete detOutput;
92  delete result;
93  }
94 
95  delete profile;
96  delete nll;
98  }
99 
100  // add nuisance parameters to parameter point
101  if(fNullModel->GetNuisanceParameters())
102  parameterPoint->add(*fNullModel->GetNuisanceParameters());
103 
104  delete allParams;
105 
106 
107 
108  // ***** ToyMCSampler specific *******
109 
110  // check whether TestStatSampler is a ToyMCSampler
111  ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
112  if(toymcs) {
113  oocoutI((TObject*)0,InputArguments) << "Using a ToyMCSampler. Now configuring for Null." << endl;
114 
115  // variable number of toys
116  if(fNToysNull >= 0) toymcs->SetNToys(fNToysNull);
117 
118  // set the global observables to be generated by the ToyMCSampler
119  toymcs->SetGlobalObservables(*fNullModel->GetGlobalObservables());
120 
121  // adaptive sampling
122  if(fNToysNullTail) {
123  oocoutI((TObject*)0,InputArguments) << "Adaptive Sampling" << endl;
124  if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
125  toymcs->SetToysRightTail(fNToysNullTail, obsTestStat);
126  }else{
127  toymcs->SetToysLeftTail(fNToysNullTail, obsTestStat);
128  }
129  }else{
130  toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
131  }
132 
133  GetNullModel()->LoadSnapshot();
134  }
135 
136  return 0;
137 }
138 
139 
140 int FrequentistCalculator::PreAltHook(RooArgSet *parameterPoint, double obsTestStat) const {
141 
142  // ****** any TestStatSampler ********
143 
144  // create profile keeping everything but nuisance parameters fixed
145  RooArgSet * allParams = fAltModel->GetPdf()->getParameters(*fData);
146  RemoveConstantParameters(allParams);
147 
148  bool doProfile = true;
149  RooArgSet allButNuisance(*allParams);
150  if( fAltModel->GetNuisanceParameters() ) {
151  allButNuisance.remove(*fAltModel->GetNuisanceParameters());
152  if( fConditionalMLEsAlt ) {
153  oocoutI((TObject*)0,InputArguments) << "Using given conditional MLEs for Alt." << endl;
154  *allParams = *fConditionalMLEsAlt;
155  // LM: fConditionalMLEsAlt must be nuisance parameters otherwise an error message will be printed
156  allButNuisance.add( *fConditionalMLEsAlt );
157  if (fAltModel->GetNuisanceParameters()) {
158  RooArgSet remain(*fAltModel->GetNuisanceParameters());
159  remain.remove(*fConditionalMLEsAlt,true,true);
160  if( remain.getSize() == 0 ) doProfile = false;
161  }
162  }
163  }else{
164  doProfile = false;
165  }
166  if (doProfile) {
167  oocoutI((TObject*)0,InputArguments) << "Profiling conditional MLEs for Alt." << endl;
170 
171  RooArgSet conditionalObs;
172  if (fAltModel->GetConditionalObservables()) conditionalObs.add(*fAltModel->GetConditionalObservables());
173 
174  RooAbsReal* nll = fAltModel->GetPdf()->createNLL(*const_cast<RooAbsData*>(fData), RooFit::CloneData(kFALSE), RooFit::Constrain(*allParams),
176 
177  RooProfileLL* profile = dynamic_cast<RooProfileLL*>(nll->createProfile(allButNuisance));
178  profile->getVal(); // this will do fit and set nuisance parameters to profiled values
179 
180  // Hack to extract a RooFitResult
181  if (fStoreFitInfo) {
182  RooFitResult *result = profile->minimizer()->save();
183  RooArgSet * detOutput = DetailedOutputAggregator::GetAsArgSet(result, "fitAlt_");
184  fFitInfo->addOwned(*detOutput);
185  delete detOutput;
186  delete result;
187  }
188 
189  delete profile;
190  delete nll;
192  }
193 
194  // add nuisance parameters to parameter point
195  if(fAltModel->GetNuisanceParameters())
196  parameterPoint->add(*fAltModel->GetNuisanceParameters());
197 
198  delete allParams;
199 
200 
201 
202 
203  // ***** ToyMCSampler specific *******
204 
205  // check whether TestStatSampler is a ToyMCSampler
206  ToyMCSampler *toymcs = dynamic_cast<ToyMCSampler*>(GetTestStatSampler());
207  if(toymcs) {
208  oocoutI((TObject*)0,InputArguments) << "Using a ToyMCSampler. Now configuring for Alt." << endl;
209 
210  // variable number of toys
211  if(fNToysAlt >= 0) toymcs->SetNToys(fNToysAlt);
212 
213  // set the global observables to be generated by the ToyMCSampler
214  toymcs->SetGlobalObservables(*fAltModel->GetGlobalObservables());
215 
216  // adaptive sampling
217  if(fNToysAltTail) {
218  oocoutI((TObject*)0,InputArguments) << "Adaptive Sampling" << endl;
219  if(GetTestStatSampler()->GetTestStatistic()->PValueIsRightTail()) {
220  toymcs->SetToysLeftTail(fNToysAltTail, obsTestStat);
221  }else{
222  toymcs->SetToysRightTail(fNToysAltTail, obsTestStat);
223  }
224  }else{
225  toymcs->SetToysBothTails(0, 0, obsTestStat); // disable adaptive sampling
226  }
227 
228  }
229 
230  return 0;
231 }
232 
233 
234 
235 
RooCmdArg Offset(Bool_t flag=kTRUE)
void SetToysLeftTail(Double_t toys, Double_t threshold)
Definition: ToyMCSampler.h:246
virtual void SetGlobalObservables(const RooArgSet &o)
Definition: ToyMCSampler.h:205
RooCmdArg CloneData(Bool_t flag)
#define oocoutI(o, a)
Definition: RooMsgService.h:45
MINIMIZER * minimizer()
Definition: RooProfileLL.h:38
const Bool_t kFALSE
Definition: Rtypes.h:92
static RooMsgService & instance()
Return reference to singleton instance.
STL namespace.
void SetToysRightTail(Double_t toys, Double_t threshold)
Definition: ToyMCSampler.h:251
void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold)
Definition: ToyMCSampler.h:256
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
RooFit::MsgLevel globalKillBelow() const
virtual void SetNToys(const Int_t ntoy)
Definition: ToyMCSampler.h:176
ClassImp(RooStats::FrequentistCalculator) using namespace RooStats
Does a frequentist hypothesis test.
void setGlobalKillBelow(RooFit::MsgLevel level)
ToyMCSampler is an implementation of the TestStatSampler interface.
Definition: ToyMCSampler.h:99
Namespace for the RooStats classes.
Definition: Asimov.h:20
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
bool IsNLLOffset()
Mother of all ROOT objects.
Definition: TObject.h:58
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
void RemoveConstantParameters(RooArgSet *set)
Definition: RooStatsUtils.h:73
virtual RooAbsReal * createProfile(const RooArgSet &paramsOfInterest)
Create a RooProfileLL object that eliminates all nuisance parameters in the present function...
Definition: RooAbsReal.cxx:463
Hypothesis Test Calculator using a full frequentist procedure for sampling the test statistic distrib...
#define NULL
Definition: Rtypes.h:82
double result[121]
RooCmdArg ConditionalObservables(const RooArgSet &set)
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add element to non-owning set.
Definition: RooArgSet.cxx:448
RooCmdArg Constrain(const RooArgSet &params)