Logo ROOT   6.08/07
Reference Guide
MCMCCalculator.h
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 #ifndef ROOSTATS_MCMCCalculator
13 #define ROOSTATS_MCMCCalculator
14 
15 #ifndef ROOT_Rtypes
16 #include "Rtypes.h"
17 #endif
18 
19 #ifndef ROOT_TObject
20 #include "TObject.h"
21 #endif
22 #ifndef ROO_ABS_PDF
23 #include "RooAbsPdf.h"
24 #endif
25 #ifndef ROO_ABS_DATA
26 #include "RooAbsData.h"
27 #endif
28 #ifndef ROO_ARG_SET
29 #include "RooArgSet.h"
30 #endif
31 #ifndef ROO_ARG_LIST
32 #include "RooArgList.h"
33 #endif
34 #ifndef ROOSTATS_ProposalFunction
36 #endif
37 #ifndef ROOSTATS_IntervalCalculator
39 #endif
40 #ifndef RooStats_MCMCInterval
41 #include "RooStats/MCMCInterval.h"
42 #endif
43 
44 
45 namespace RooStats {
46 
47  class ModelConfig;
48 
49 
50 /**
51  Bayesian Calculator estimating an interval or a credible region using the Markov-Chain Monte Carlo method to
52  integrate the likelihood function with the prior to obtain the posterior function.
53 
54  By using the Markov-Chain Monte Carlo methods this calculator can work with model which require the integration of a large number of parameters.
55 
56  MCMCCalculator is a concrete implementation of IntervalCalculator. It uses a
57  MetropolisHastings object to construct a Markov Chain of data points in the
58  parameter space. From this Markov Chain, this class can generate a
59  MCMCInterval as per user specification.
60 
61  The interface allows one to pass the model, data, and parameters via a
62  workspace and then specify them with names.
63 
64  After configuring the calculator, one only needs to ask GetInterval(), which
65  will return an ConfInterval (MCMCInterval in this case).
66 
67  \ingroup Roostats
68  */
69 
70 
71 
72  class MCMCCalculator : public IntervalCalculator, public TNamed {
73 
74  public:
75  /// default constructor
77 
78  /// Constructor for automatic configuration with basic settings and a
79  /// ModelConfig. Uses a UniformProposal, 10,000 iterations, 40 burn in
80  /// steps, 50 bins for each RooRealVar, determines interval by histogram,
81  /// and finds a 95% confidence interval. Any of these basic settings can
82  /// be overridden by calling one of the Set...() methods.
84 
85  virtual ~MCMCCalculator() {}
86 
87  /// Main interface to get a ConfInterval
88  virtual MCMCInterval* GetInterval() const;
89 
90  /// Get the size of the test (eg. rate of Type I error)
91  virtual Double_t Size() const {return fSize;}
92  /// Get the Confidence level for the test
93  virtual Double_t ConfidenceLevel() const {return 1.-fSize;}
94 
95  virtual void SetModel(const ModelConfig & model);
96 
97  /// Set the DataSet if not already there
98  virtual void SetData(RooAbsData& data) { fData = &data; }
99 
100  /// Set the Pdf if not already there
101  virtual void SetPdf(RooAbsPdf& pdf) { fPdf = &pdf; }
102 
103  /// Set the Prior Pdf if not already there
104  virtual void SetPriorPdf(RooAbsPdf& pdf) { fPriorPdf = &pdf; }
105 
106  /// specify the parameters of interest in the interval
107  virtual void SetParameters(const RooArgSet& set) { fPOI.removeAll(); fPOI.add(set); }
108 
109  /// specify the parameters to store in the Markov chain
110  /// By default all the parameters are stored
111  virtual void SetChainParameters(const RooArgSet & set) { fChainParams.removeAll(); fChainParams.add(set); }
112 
113  /// specify the nuisance parameters (eg. the rest of the parameters)
115 
116  /// set the conditional observables which will be used when creating the NLL
117  /// so the pdf's will not be normalized on the conditional observables when computing the NLL
119 
120  /// set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
121  virtual void SetTestSize(Double_t size) {fSize = size;}
122 
123  /// set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
124  virtual void SetConfidenceLevel(Double_t cl) {fSize = 1.-cl;}
125 
126  /// set the proposal function for suggesting new points for the MCMC
127  virtual void SetProposalFunction(ProposalFunction& proposalFunction)
128  { fPropFunc = &proposalFunction; }
129 
130  /// set the number of iterations to run the metropolis algorithm
131  virtual void SetNumIters(Int_t numIters)
132  { fNumIters = numIters; }
133 
134  /// set the number of steps in the chain to discard as burn-in,
135  /// starting from the first
136  virtual void SetNumBurnInSteps(Int_t numBurnInSteps)
137  { fNumBurnInSteps = numBurnInSteps; }
138 
139  /// set the number of bins to create for each axis when constructing the interval
140  virtual void SetNumBins(Int_t numBins) { fNumBins = numBins; }
141  /// set which variables to put on each axis
142  virtual void SetAxes(RooArgList& axes)
143  { fAxes = &axes; }
144  /// set whether to use kernel estimation to determine the interval
145  virtual void SetUseKeys(Bool_t useKeys) { fUseKeys = useKeys; }
146  /// set whether to use sparse histogram (if using histogram at all)
147  virtual void SetUseSparseHist(Bool_t useSparseHist)
148  { fUseSparseHist = useSparseHist; }
149 
150  /// set what type of interval to have the MCMCInterval represent
151  virtual void SetIntervalType(enum MCMCInterval::IntervalType intervalType)
152  { fIntervalType = intervalType; }
153 
154  /// Set the left side tail fraction. This will automatically configure the
155  /// MCMCInterval to find a tail-fraction interval.
156  /// Note: that `a' must be in the range 0 <= a <= 1
157  /// or the user will be notified of the error
158  virtual void SetLeftSideTailFraction(Double_t a);
159 
160  /// Set the desired level of confidence-level accuracy for Keys interval
161  /// determination.
162  //
163  /// When determining the cutoff PDF height that gives the
164  /// desired confidence level (C_d), the algorithm will consider acceptable
165  /// any found confidence level c such that Abs(c - C_d) < epsilon.
166  ///
167  /// Any value of this "epsilon" > 0 is considered acceptable, though it is
168  /// advisable to not use a value too small, because the integration of the
169  /// Keys PDF sometimes does not have extremely high accuracy.
171  {
172  if (epsilon < 0)
173  coutE(InputArguments) << "MCMCInterval::SetEpsilon will not allow "
174  << "negative epsilon value" << std::endl;
175  else
176  fEpsilon = epsilon;
177  }
178 
179  /// When the shortest interval using Keys PDF could not be found to have
180  /// the desired confidence level +/- the accuracy (see
181  /// SetKeysConfidenceAccuracy()), the interval determination algorithm
182  /// will have to terminate with an unsatisfactory confidence level when
183  /// the bottom and top of the cutoff search range are very close to being
184  /// equal. This scenario comes into play when there seems to be an error
185  /// in the accuracy of the Keys PDF integration, so the search range
186  /// continues to shrink without converging to a cutoff value that will
187  /// give an acceptable confidence level. To choose how small to allow the
188  /// search range to be before terminating, set the fraction delta such
189  /// that the search will terminate when topCutoff (a) and bottomCutoff (b)
190  /// satisfy this condition:
191  ///
192  /// TMath::Abs(a - b) < TMath::Abs(delta * (a + b)/2)
194  {
195  if (delta < 0.)
196  coutE(InputArguments) << "MCMCInterval::SetDelta will not allow "
197  << "negative delta value" << std::endl;
198  else
199  fDelta = delta;
200  }
201 
202  protected:
203 
204  Double_t fSize; // size of the test (eg. specified rate of Type I error)
205  RooArgSet fPOI; // parameters of interest for interval
206  RooArgSet fNuisParams; // nuisance parameters for interval (not really used)
207  RooArgSet fChainParams; // parameters to store in the chain (if not specified they are all of them )
208  RooArgSet fConditionalObs; // conditional observables
209  mutable ProposalFunction* fPropFunc; // Proposal function for MCMC integration
210  RooAbsPdf * fPdf; // pointer to common PDF (owned by the workspace)
211  RooAbsPdf * fPriorPdf; // pointer to prior PDF (owned by the workspace)
212  RooAbsData * fData; // pointer to the data (owned by the workspace)
213  Int_t fNumIters; // number of iterations to run metropolis algorithm
214  Int_t fNumBurnInSteps; // number of iterations to discard as burn-in, starting from the first
215  Int_t fNumBins; // set the number of bins to create for each
216  // axis when constructing the interval
217  RooArgList * fAxes; // which variables to put on each axis
218  Bool_t fUseKeys; // whether to use kernel estimation to determine interval
219  Bool_t fUseSparseHist; // whether to use sparse histogram (if using hist at all)
220  Double_t fLeftSideTF; // left side tail-fraction for interval
221  Double_t fEpsilon; // acceptable error for Keys interval determination
222 
223  Double_t fDelta; // acceptable error for Keys cutoffs being equal
224  // topCutoff (a) considered == bottomCutoff (b) iff
225  // (TMath::Abs(a - b) < TMath::Abs(fDelta * (a + b)/2));
226  // Theoretically, the Abs is not needed here, but
227  // floating-point arithmetic does not always work
228  // perfectly, and the Abs doesn't hurt
229  enum MCMCInterval::IntervalType fIntervalType; // type of interval to find
230 
231  void SetupBasicUsage();
232  void SetBins(const RooAbsCollection& coll, Int_t numBins) const
233  {
234  TIterator* it = coll.createIterator();
235  RooAbsArg* r;
236  while ((r = (RooAbsArg*)it->Next()) != NULL)
237  if (dynamic_cast<RooRealVar*>(r))
238  ((RooRealVar*)r)->setBins(numBins);
239  delete it;
240  }
241 
242  ClassDef(MCMCCalculator,3) // Markov Chain Monte Carlo calculator for Bayesian credible intervals
243  };
244 }
245 
246 
247 #endif
MCMCCalculator()
default constructor
ProposalFunction is an interface for all proposal functions that would be used with a Markov Chain Mo...
TIterator * createIterator(Bool_t dir=kIterForward) const
#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
virtual Double_t Size() const
Get the size of the test (eg. rate of Type I error)
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
IntervalCalculator is an interface class for a tools which produce RooStats ConfIntervals.
virtual void SetNumBurnInSteps(Int_t numBurnInSteps)
set the number of steps in the chain to discard as burn-in, starting from the first ...
virtual void SetParameters(const RooArgSet &set)
specify the parameters of interest in the interval
virtual void SetNumBins(Int_t numBins)
set the number of bins to create for each axis when constructing the interval
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
virtual void SetKeysTerminationThreshold(Double_t delta)
When the shortest interval using Keys PDF could not be found to have the desired confidence level +/-...
Iterator abstract base class.
Definition: TIterator.h:32
virtual void SetConfidenceLevel(Double_t cl)
set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval) ...
#define ClassDef(name, id)
Definition: Rtypes.h:254
virtual void SetIntervalType(enum MCMCInterval::IntervalType intervalType)
set what type of interval to have the MCMCInterval represent
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
virtual void SetConditionalObservables(const RooArgSet &set)
set the conditional observables which will be used when creating the NLL so the pdf&#39;s will not be nor...
virtual void SetPdf(RooAbsPdf &pdf)
Set the Pdf if not already there.
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
virtual void SetNumIters(Int_t numIters)
set the number of iterations to run the metropolis algorithm
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) ...
TRandom2 r(17)
virtual void SetAxes(RooArgList &axes)
set which variables to put on each axis
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
REAL epsilon
Definition: triangle.c:617
virtual void SetData(RooAbsData &data)
Set the DataSet if not already there.
void SetBins(const RooAbsCollection &coll, Int_t numBins) const
virtual void SetKeysConfidenceAccuracy(Double_t epsilon)
Set the desired level of confidence-level accuracy for Keys interval determination.
virtual Double_t ConfidenceLevel() const
Get the Confidence level for the test.
Namespace for the RooStats classes.
Definition: Asimov.h:20
virtual void SetUseKeys(Bool_t useKeys)
set whether to use kernel estimation to determine the interval
virtual void SetProposalFunction(ProposalFunction &proposalFunction)
set the proposal function for suggesting new points for the MCMC
double Double_t
Definition: RtypesCore.h:55
ProposalFunction * fPropFunc
enum MCMCInterval::IntervalType fIntervalType
virtual void SetNuisanceParameters(const RooArgSet &set)
specify the nuisance parameters (eg. the rest of the parameters)
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects...
virtual void SetChainParameters(const RooArgSet &set)
specify the parameters to store in the Markov chain By default all the parameters are stored ...
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 TObject * Next()=0
#define NULL
Definition: Rtypes.h:82
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:88
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
virtual void SetUseSparseHist(Bool_t useSparseHist)
set whether to use sparse histogram (if using histogram at all)
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 void SetPriorPdf(RooAbsPdf &pdf)
Set the Prior Pdf if not already there.