ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MCMCCalculator.cxx
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Authors: Kevin Belasco 17/06/2009
3 // Authors: Kyle Cranmer 17/06/2009
4 /*************************************************************************
5  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 
13 
14 #ifndef ROOT_Rtypes
15 #include "Rtypes.h"
16 #endif
17 #ifndef ROO_GLOBAL_FUNC
18 #include "RooGlobalFunc.h"
19 #endif
20 #ifndef ROO_ABS_REAL
21 #include "RooAbsReal.h"
22 #endif
23 #ifndef ROO_ARG_SET
24 #include "RooArgSet.h"
25 #endif
26 #ifndef ROO_ARG_LIST
27 #include "RooArgList.h"
28 #endif
29 #ifndef ROOSTATS_ModelConfig
30 #include "RooStats/ModelConfig.h"
31 #endif
32 #ifndef RooStats_RooStatsUtils
33 #include "RooStats/RooStatsUtils.h"
34 #endif
35 #ifndef ROOSTATS_MCMCCalculator
37 #endif
38 #ifndef ROOSTATS_MetropolisHastings
40 #endif
41 #ifndef ROOSTATS_MarkovChain
42 #include "RooStats/MarkovChain.h"
43 #endif
44 #ifndef RooStats_MCMCInterval
45 #include "RooStats/MCMCInterval.h"
46 #endif
47 #ifndef ROOT_TIterator
48 #include "TIterator.h"
49 #endif
50 #ifndef ROOSTATS_UniformProposal
52 #endif
53 #ifndef ROOSTATS_PdfProposal
54 #include "RooStats/PdfProposal.h"
55 #endif
56 #ifndef ROO_PROD_PDF
57 #include "RooProdPdf.h"
58 #endif
59 
61 
62 using namespace RooFit;
63 using namespace RooStats;
64 using namespace std;
65 
66 // default constructor
67 MCMCCalculator::MCMCCalculator() :
68  fPropFunc(0),
69  fPdf(0),
70  fPriorPdf(0),
71  fData(0),
72  fAxes(0)
73 {
74  fNumIters = 0;
75  fNumBurnInSteps = 0;
76  fNumBins = 0;
77  fUseKeys = kFALSE;
79  fSize = -1;
80  fIntervalType = MCMCInterval::kShortest;
81  fLeftSideTF = -1;
82  fEpsilon = -1;
83  fDelta = -1;
84 }
85 
86 // constructor from a Model Config with a basic settings package configured
87 // by SetupBasicUsage()
89  fPropFunc(0),
90  fData(&data),
91  fAxes(0)
92 {
93  SetModel(model);
95 }
96 
97 void MCMCCalculator::SetModel(const ModelConfig & model) {
98  // set the model
99  fPdf = model.GetPdf();
100  fPriorPdf = model.GetPriorPdf();
101  fPOI.removeAll();
104  if (model.GetParametersOfInterest())
105  fPOI.add(*model.GetParametersOfInterest());
106  if (model.GetNuisanceParameters())
108  if (model.GetConditionalObservables())
110 
111 }
112 
113 // Constructor for automatic configuration with basic settings. Uses a
114 // UniformProposal, 10,000 iterations, 40 burn in steps, 50 bins for each
115 // RooRealVar, determines interval by histogram. Finds a 95% confidence
116 // interval.
118 {
119  fPropFunc = 0;
120  fNumIters = 10000;
121  fNumBurnInSteps = 40;
122  fNumBins = 50;
123  fUseKeys = kFALSE;
125  SetTestSize(0.05);
126  fIntervalType = MCMCInterval::kShortest;
127  fLeftSideTF = -1;
128  fEpsilon = -1;
129  fDelta = -1;
130 }
131 
133 {
134  if (a < 0 || a > 1) {
135  coutE(InputArguments) << "MCMCCalculator::SetLeftSideTailFraction: "
136  << "Fraction must be in the range [0, 1]. "
137  << a << "is not allowed." << endl;
138  return;
139  }
140 
141  fLeftSideTF = a;
142  fIntervalType = MCMCInterval::kTailFraction;
143 }
144 
145 MCMCInterval* MCMCCalculator::GetInterval() const
146 {
147  // Main interface to get a RooStats::ConfInterval.
148 
149  if (!fData || !fPdf ) return 0;
150  if (fPOI.getSize() == 0) return 0;
151 
152  if (fSize < 0) {
153  coutE(InputArguments) << "MCMCCalculator::GetInterval: "
154  << "Test size/Confidence level not set. Returning NULL." << endl;
155  return NULL;
156  }
157 
158  // if a proposal funciton has not been specified create a default one
159  bool useDefaultPropFunc = (fPropFunc == 0);
160  bool usePriorPdf = (fPriorPdf != 0);
161  if (useDefaultPropFunc) fPropFunc = new UniformProposal();
162 
163  // if prior is given create product
164  RooAbsPdf * prodPdf = fPdf;
165  if (usePriorPdf) {
166  TString prodName = TString("product_") + TString(fPdf->GetName()) + TString("_") + TString(fPriorPdf->GetName() );
167  prodPdf = new RooProdPdf(prodName,prodName,RooArgList(*fPdf,*fPriorPdf) );
168  }
169 
170  RooArgSet* constrainedParams = prodPdf->getParameters(*fData);
171  RooAbsReal* nll = prodPdf->createNLL(*fData, Constrain(*constrainedParams),ConditionalObservables(fConditionalObs));
172  delete constrainedParams;
173 
174  RooArgSet* params = nll->getParameters(*fData);
175  RemoveConstantParameters(params);
176  if (fNumBins > 0) {
177  SetBins(*params, fNumBins);
179  if (dynamic_cast<PdfProposal*>(fPropFunc)) {
180  RooArgSet* proposalVars = ((PdfProposal*)fPropFunc)->GetPdf()->
181  getParameters((RooAbsData*)NULL);
182  SetBins(*proposalVars, fNumBins);
183  }
184  }
185 
187  mh.SetFunction(*nll);
190  mh.SetParameters(*params);
194 
196 
197  TString name = TString("MCMCInterval_") + TString(GetName() );
198  MCMCInterval* interval = new MCMCInterval(name, fPOI, *chain);
199  if (fAxes != NULL)
200  interval->SetAxes(*fAxes);
201  if (fNumBurnInSteps > 0)
202  interval->SetNumBurnInSteps(fNumBurnInSteps);
203  interval->SetUseKeys(fUseKeys);
204  interval->SetUseSparseHist(fUseSparseHist);
205  interval->SetIntervalType(fIntervalType);
206  if (fIntervalType == MCMCInterval::kTailFraction)
207  interval->SetLeftSideTailFraction(fLeftSideTF);
208  if (fEpsilon >= 0)
209  interval->SetEpsilon(fEpsilon);
210  if (fDelta >= 0)
211  interval->SetDelta(fDelta);
212  interval->SetConfidenceLevel(1.0 - fSize);
213 
214  if (useDefaultPropFunc) delete fPropFunc;
215  if (usePriorPdf) delete prodPdf;
216  delete nll;
217  delete params;
218 
219  return interval;
220 }
virtual RooAbsReal * createNLL(RooAbsData &data, const RooLinkedList &cmdList)
Construct representation of -log(L) of PDFwith given dataset.
Definition: RooAbsPdf.cxx:777
virtual void SetSign(enum FunctionSign sign)
MCMCCalculator()
default constructor
#define coutE(a)
Definition: RooMsgService.h:35
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:52
RooAbsPdf * GetPdf() const
get model PDF (return NULL if pdf has not been specified or does not exist)
Definition: ModelConfig.h:244
const RooArgSet * GetConditionalObservables() const
get RooArgSet for conditional observables (return NULL if not existing)
Definition: ModelConfig.h:262
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:31
UniformProposal is a concrete implementation of the ProposalFunction interface for use with a Markov ...
virtual void SetType(enum FunctionType type)
virtual MarkovChain * ConstructChain()
Basic string class.
Definition: TString.h:137
TArc * a
Definition: textangle.C:12
const Bool_t kFALSE
Definition: Rtypes.h:92
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return NULL if not existing)
Definition: ModelConfig.h:250
This class uses the Metropolis-Hastings algorithm to construct a Markov Chain of data points using Mo...
TChain chain("h42")
virtual void SetParameters(const RooArgSet &set)
virtual void SetNumIters(Int_t numIters)
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
ClassImp(RooStats::MCMCCalculator)
RooArgSet * getParameters(const RooAbsData *data, Bool_t stripDisconnected=kTRUE) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
Definition: RooAbsArg.cxx:560
virtual void SetFunction(RooAbsReal &function)
virtual void SetProposalFunction(ProposalFunction &proposalFunction)
virtual MCMCInterval * GetInterval() const
Main interface to get a ConfInterval.
virtual void SetTestSize(Double_t size)
set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval) ...
virtual void SetChainParameters(const RooArgSet &set)
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
Stores the steps in a Markov Chain of points.
Definition: MarkovChain.h:53
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
ProposalFunction * fPropFunc
enum MCMCInterval::IntervalType fIntervalType
PdfProposal is a concrete implementation of the ProposalFunction interface.
Definition: PdfProposal.h:85
#define name(a, b)
Definition: linkTestLib0.cpp:5
void RemoveConstantParameters(RooArgSet *set)
Definition: RooStatsUtils.h:73
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
#define NULL
Definition: Rtypes.h:82
virtual void SetLeftSideTailFraction(Double_t a)
Set the left side tail fraction.
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return NULL if not existing)
Definition: ModelConfig.h:247
void SetBins(const RooAbsCollection &coll, Int_t numBins) const
Int_t getSize() const
RooAbsPdf * GetPriorPdf() const
get parameters prior pdf (return NULL if not existing)
Definition: ModelConfig.h:256
RooCmdArg ConditionalObservables(const RooArgSet &set)
Bayesian Calculator estimating an interval or a credible region using the Markov-Chain Monte Carlo me...
virtual void SetModel(const ModelConfig &model)
Set the Model.
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)