Logo ROOT   6.10/09
Reference Guide
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 /** \class RooStats::MCMCCalculator
14  \ingroup Roostats
15 
16  Bayesian Calculator estimating an interval or a credible region using the
17  Markov-Chain Monte Carlo method to integrate the likelihood function with the
18  prior to obtain the posterior function.
19 
20  By using the Markov-Chain Monte Carlo methods this calculator can work with
21  model which require the integration of a large number of parameters.
22 
23  MCMCCalculator is a concrete implementation of IntervalCalculator. It uses a
24  MetropolisHastings object to construct a Markov Chain of data points in the
25  parameter space. From this Markov Chain, this class can generate a
26  MCMCInterval as per user specification.
27 
28  The interface allows one to pass the model, data, and parameters via a
29  workspace and then specify them with names.
30 
31  After configuring the calculator, one only needs to ask GetInterval(), which
32  will return an ConfInterval (MCMCInterval in this case).
33  */
34 
35 #include "Rtypes.h"
36 #include "RooGlobalFunc.h"
37 #include "RooAbsReal.h"
38 #include "RooArgSet.h"
39 #include "RooArgList.h"
40 #include "RooStats/ModelConfig.h"
41 #include "RooStats/RooStatsUtils.h"
44 #include "RooStats/MarkovChain.h"
45 #include "RooStats/MCMCInterval.h"
46 #include "TIterator.h"
48 #include "RooStats/PdfProposal.h"
49 #include "RooProdPdf.h"
50 
52 
53 using namespace RooFit;
54 using namespace RooStats;
55 using namespace std;
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// default constructor
59 
60 MCMCCalculator::MCMCCalculator() :
61  fPropFunc(0),
62  fPdf(0),
63  fPriorPdf(0),
64  fData(0),
65  fAxes(0)
66 {
67  fNumIters = 0;
68  fNumBurnInSteps = 0;
69  fNumBins = 0;
70  fUseKeys = kFALSE;
72  fSize = -1;
74  fLeftSideTF = -1;
75  fEpsilon = -1;
76  fDelta = -1;
77 }
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 /// constructor from a Model Config with a basic settings package configured
81 /// by SetupBasicUsage()
82 
84  fPropFunc(0),
85  fData(&data),
86  fAxes(0)
87 {
88  SetModel(model);
90 }
91 
93  // set the model
94  fPdf = model.GetPdf();
95  fPriorPdf = model.GetPriorPdf();
96  fPOI.removeAll();
99  if (model.GetParametersOfInterest())
100  fPOI.add(*model.GetParametersOfInterest());
101  if (model.GetNuisanceParameters())
103  if (model.GetConditionalObservables())
105 
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Constructor for automatic configuration with basic settings. Uses a
110 /// UniformProposal, 10,000 iterations, 40 burn in steps, 50 bins for each
111 /// RooRealVar, determines interval by histogram. Finds a 95% confidence
112 /// interval.
113 
115 {
116  fPropFunc = 0;
117  fNumIters = 10000;
118  fNumBurnInSteps = 40;
119  fNumBins = 50;
120  fUseKeys = kFALSE;
122  SetTestSize(0.05);
124  fLeftSideTF = -1;
125  fEpsilon = -1;
126  fDelta = -1;
127 }
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 
132 {
133  if (a < 0 || a > 1) {
134  coutE(InputArguments) << "MCMCCalculator::SetLeftSideTailFraction: "
135  << "Fraction must be in the range [0, 1]. "
136  << a << "is not allowed." << endl;
137  return;
138  }
139 
140  fLeftSideTF = a;
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// Main interface to get a RooStats::ConfInterval.
146 
148 {
149 
150  if (!fData || !fPdf ) return 0;
151  if (fPOI.getSize() == 0) return 0;
152 
153  if (fSize < 0) {
154  coutE(InputArguments) << "MCMCCalculator::GetInterval: "
155  << "Test size/Confidence level not set. Returning NULL." << endl;
156  return NULL;
157  }
158 
159  // if a proposal function has not been specified create a default one
160  bool useDefaultPropFunc = (fPropFunc == 0);
161  bool usePriorPdf = (fPriorPdf != 0);
162  if (useDefaultPropFunc) fPropFunc = new UniformProposal();
163 
164  // if prior is given create product
165  RooAbsPdf * prodPdf = fPdf;
166  if (usePriorPdf) {
167  TString prodName = TString("product_") + TString(fPdf->GetName()) + TString("_") + TString(fPriorPdf->GetName() );
168  prodPdf = new RooProdPdf(prodName,prodName,RooArgList(*fPdf,*fPriorPdf) );
169  }
170 
171  RooArgSet* constrainedParams = prodPdf->getParameters(*fData);
172  RooAbsReal* nll = prodPdf->createNLL(*fData, Constrain(*constrainedParams),ConditionalObservables(fConditionalObs));
173  delete constrainedParams;
174 
175  RooArgSet* params = nll->getParameters(*fData);
176  RemoveConstantParameters(params);
177  if (fNumBins > 0) {
178  SetBins(*params, fNumBins);
180  if (dynamic_cast<PdfProposal*>(fPropFunc)) {
181  RooArgSet* proposalVars = ((PdfProposal*)fPropFunc)->GetPdf()->
182  getParameters((RooAbsData*)NULL);
183  SetBins(*proposalVars, fNumBins);
184  }
185  }
186 
188  mh.SetFunction(*nll);
191  mh.SetParameters(*params);
195 
196  MarkovChain* chain = mh.ConstructChain();
197 
198  TString name = TString("MCMCInterval_") + TString(GetName() );
199  MCMCInterval* interval = new MCMCInterval(name, fPOI, *chain);
200  if (fAxes != NULL)
201  interval->SetAxes(*fAxes);
202  if (fNumBurnInSteps > 0)
204  interval->SetUseKeys(fUseKeys);
205  interval->SetUseSparseHist(fUseSparseHist);
206  interval->SetIntervalType(fIntervalType);
209  if (fEpsilon >= 0)
210  interval->SetEpsilon(fEpsilon);
211  if (fDelta >= 0)
212  interval->SetDelta(fDelta);
213  interval->SetConfidenceLevel(1.0 - fSize);
214 
215  if (useDefaultPropFunc) delete fPropFunc;
216  if (usePriorPdf) delete prodPdf;
217  delete nll;
218  delete params;
219 
220  return interval;
221 }
virtual RooAbsReal * createNLL(RooAbsData &data, const RooLinkedList &cmdList)
Construct representation of -log(L) of PDFwith given dataset.
Definition: RooAbsPdf.cxx:777
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void SetSign(enum FunctionSign sign)
MCMCCalculator()
default constructor
#define coutE(a)
Definition: RooMsgService.h:34
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:30
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:86
virtual void SetNumBurnInSteps(Int_t numBurnInSteps)
set the number of steps in the chain to discard as burn-in, starting from the first ...
Definition: MCMCInterval.h:158
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 SetAxes(RooArgList &axes)
Set which parameters go on which axis.
virtual void SetType(enum FunctionType type)
virtual MarkovChain * ConstructChain()
void SetupBasicUsage()
Constructor for automatic configuration with basic settings.
TArc * a
Definition: textangle.C:12
STL namespace.
#define NULL
Definition: RtypesCore.h:88
This class uses the Metropolis-Hastings algorithm to construct a Markov Chain of data points using Mo...
virtual void SetParameters(const RooArgSet &set)
virtual void SetNumIters(Int_t numIters)
virtual void SetEpsilon(Double_t epsilon)
set the acceptable level or error for Keys interval determination
Definition: MCMCInterval.h:226
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
virtual void SetFunction(RooAbsReal &function)
RooAbsPdf * GetPriorPdf() const
get parameters prior pdf (return NULL if not existing)
Definition: ModelConfig.h:234
const RooArgSet * GetConditionalObservables() const
get RooArgSet for conditional observables (return NULL if not existing)
Definition: ModelConfig.h:240
Int_t getSize() const
virtual void SetProposalFunction(ProposalFunction &proposalFunction)
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 SetIntervalType(enum IntervalType intervalType)
Set the type of interval to find.
Definition: MCMCInterval.h:239
virtual void SetChainParameters(const RooArgSet &set)
virtual void SetLeftSideTailFraction(Double_t a)
set the left-side tail fraction for a tail-fraction interval
Definition: MCMCInterval.h:247
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
virtual void SetConfidenceLevel(Double_t cl)
set the desired confidence level (see GetActualConfidenceLevel()) Note: calling this function trigger...
const Bool_t kFALSE
Definition: RtypesCore.h:92
void SetBins(const RooAbsCollection &coll, Int_t numBins) const
virtual void SetUseKeys(Bool_t useKeys)
set whether to use kernel estimation to determine the interval
Definition: MCMCInterval.h:162
Stores the steps in a Markov Chain of points.
Definition: MarkovChain.h:30
Namespace for the RooStats classes.
Definition: Asimov.h:20
RooAbsPdf * GetPdf() const
get model PDF (return NULL if pdf has not been specified or does not exist)
Definition: ModelConfig.h:222
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return NULL if not existing)
Definition: ModelConfig.h:225
#define ClassImp(name)
Definition: Rtypes.h:336
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
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&#39;t match any of...
Definition: RooAbsArg.cxx:560
ProposalFunction * fPropFunc
enum MCMCInterval::IntervalType fIntervalType
PdfProposal is a concrete implementation of the ProposalFunction interface.
Definition: PdfProposal.h:30
void RemoveConstantParameters(RooArgSet *set)
Definition: RooStatsUtils.h:62
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
virtual MCMCInterval * GetInterval() const
Main interface to get a ConfInterval.
virtual void SetDelta(Double_t delta)
kbelasco: The inner-workings of the class really should not be exposed like this in a comment...
Definition: MCMCInterval.h:260
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return NULL if not existing)
Definition: ModelConfig.h:228
virtual void SetLeftSideTailFraction(Double_t a)
Set the left side tail fraction.
MCMCInterval is a concrete implementation of the RooStats::ConfInterval interface.
Definition: MCMCInterval.h:30
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.
RooCmdArg Constrain(const RooArgSet &params)
virtual void SetUseSparseHist(Bool_t useSparseHist)
set whether to use a sparse histogram.
Definition: MCMCInterval.h:166