00001 // @(#)root/roostats:$Id: MCMCCalculator.h 34109 2010-06-24 15:00:16Z moneta $ 00002 // Authors: Kevin Belasco 17/06/2009 00003 // Authors: Kyle Cranmer 17/06/2009 00004 /************************************************************************* 00005 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 #ifndef ROOSTATS_MCMCCalculator 00013 #define ROOSTATS_MCMCCalculator 00014 00015 #ifndef ROOT_Rtypes 00016 #include "Rtypes.h" 00017 #endif 00018 00019 #ifndef ROOT_TObject 00020 #include "TObject.h" 00021 #endif 00022 #ifndef ROO_ABS_PDF 00023 #include "RooAbsPdf.h" 00024 #endif 00025 #ifndef ROO_ABS_DATA 00026 #include "RooAbsData.h" 00027 #endif 00028 #ifndef ROO_ARG_SET 00029 #include "RooArgSet.h" 00030 #endif 00031 #ifndef ROO_ARG_LIST 00032 #include "RooArgList.h" 00033 #endif 00034 #ifndef ROOSTATS_ProposalFunction 00035 #include "RooStats/ProposalFunction.h" 00036 #endif 00037 #ifndef ROOSTATS_IntervalCalculator 00038 #include "RooStats/IntervalCalculator.h" 00039 #endif 00040 #ifndef RooStats_MCMCInterval 00041 #include "RooStats/MCMCInterval.h" 00042 #endif 00043 00044 namespace RooStats { 00045 00046 class ModelConfig; 00047 00048 class MCMCCalculator : public IntervalCalculator, public TNamed { 00049 00050 public: 00051 // default constructor 00052 MCMCCalculator(); 00053 00054 // Constructor for automatic configuration with basic settings and a 00055 // ModelConfig. Uses a UniformProposal, 10,000 iterations, 40 burn in 00056 // steps, 50 bins for each RooRealVar, determines interval by histogram, 00057 // and finds a 95% confidence interval. Any of these basic settings can 00058 // be overridden by calling one of the Set...() methods. 00059 MCMCCalculator(RooAbsData& data, const ModelConfig& model); 00060 00061 virtual ~MCMCCalculator() {} 00062 00063 // Main interface to get a ConfInterval 00064 virtual MCMCInterval* GetInterval() const; 00065 00066 // Get the size of the test (eg. rate of Type I error) 00067 virtual Double_t Size() const {return fSize;} 00068 // Get the Confidence level for the test 00069 virtual Double_t ConfidenceLevel() const {return 1.-fSize;} 00070 00071 virtual void SetModel(const ModelConfig & model); 00072 00073 // Set the DataSet if not already there 00074 virtual void SetData(RooAbsData& data) { fData = &data; } 00075 00076 // Set the Pdf if not already there 00077 virtual void SetPdf(RooAbsPdf& pdf) { fPdf = &pdf; } 00078 00079 // Set the Prior Pdf if not already there 00080 virtual void SetPriorPdf(RooAbsPdf& pdf) { fPriorPdf = &pdf; } 00081 00082 // specify the parameters of interest in the interval 00083 virtual void SetParameters(const RooArgSet& set) { fPOI.removeAll(); fPOI.add(set); } 00084 00085 // specify the nuisance parameters (eg. the rest of the parameters) 00086 virtual void SetNuisanceParameters(const RooArgSet& set) {fNuisParams.removeAll(); fNuisParams.add(set);} 00087 00088 // set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval) 00089 virtual void SetTestSize(Double_t size) {fSize = size;} 00090 00091 // set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval) 00092 virtual void SetConfidenceLevel(Double_t cl) {fSize = 1.-cl;} 00093 00094 // set the proposal function for suggesting new points for the MCMC 00095 virtual void SetProposalFunction(ProposalFunction& proposalFunction) 00096 { fPropFunc = &proposalFunction; } 00097 00098 // set the number of iterations to run the metropolis algorithm 00099 virtual void SetNumIters(Int_t numIters) 00100 { fNumIters = numIters; } 00101 00102 // set the number of steps in the chain to discard as burn-in, 00103 // starting from the first 00104 virtual void SetNumBurnInSteps(Int_t numBurnInSteps) 00105 { fNumBurnInSteps = numBurnInSteps; } 00106 00107 // set the number of bins to create for each axis when constructing the interval 00108 virtual void SetNumBins(Int_t numBins) { fNumBins = numBins; } 00109 // set which variables to put on each axis 00110 virtual void SetAxes(RooArgList& axes) 00111 { fAxes = &axes; } 00112 // set whether to use kernel estimation to determine the interval 00113 virtual void SetUseKeys(Bool_t useKeys) { fUseKeys = useKeys; } 00114 // set whether to use sparse histogram (if using histogram at all) 00115 virtual void SetUseSparseHist(Bool_t useSparseHist) 00116 { fUseSparseHist = useSparseHist; } 00117 00118 // set what type of interval to have the MCMCInterval represent 00119 virtual void SetIntervalType(enum MCMCInterval::IntervalType intervalType) 00120 { fIntervalType = intervalType; } 00121 00122 // Set the left side tail fraction. This will automatically configure the 00123 // MCMCInterval to find a tail-fraction interval. 00124 // Note: that `a' must be in the range 0 <= a <= 1 00125 // or the user will be notified of the error 00126 virtual void SetLeftSideTailFraction(Double_t a); 00127 00128 // Set the desired level of confidence-level accuracy for Keys interval 00129 // determination. 00130 // 00131 // When determining the cutoff PDF height that gives the 00132 // desired confidence level (C_d), the algorithm will consider acceptable 00133 // any found confidence level c such that Abs(c - C_d) < epsilon. 00134 // 00135 // Any value of this "epsilon" > 0 is considered acceptable, though it is 00136 // advisable to not use a value too small, because the integration of the 00137 // Keys PDF sometimes does not have extremely high accuracy. 00138 virtual void SetKeysConfidenceAccuracy(Double_t epsilon) 00139 { 00140 if (epsilon < 0) 00141 coutE(InputArguments) << "MCMCInterval::SetEpsilon will not allow " 00142 << "negative epsilon value" << endl; 00143 else 00144 fEpsilon = epsilon; 00145 } 00146 00147 // When the shortest interval using Keys PDF could not be found to have 00148 // the desired confidence level +/- the accuracy (see 00149 // SetKeysConfidenceAccuracy()), the interval determination algorithm 00150 // will have to terminate with an unsatisfactory confidence level when 00151 // the bottom and top of the cutoff search range are very close to being 00152 // equal. This scenario comes into play when there seems to be an error 00153 // in the accuracy of the Keys PDF integration, so the search range 00154 // continues to shrink without converging to a cutoff value that will 00155 // give an acceptable confidence level. To choose how small to allow the 00156 // search range to be before terminating, set the fraction delta such 00157 // that the search will terminate when topCutoff (a) and bottomCutoff (b) 00158 // satisfy this condition: 00159 // 00160 // TMath::Abs(a - b) < TMath::Abs(delta * (a + b)/2) 00161 virtual void SetKeysTerminationThreshold(Double_t delta) 00162 { 00163 if (delta < 0.) 00164 coutE(InputArguments) << "MCMCInterval::SetDelta will not allow " 00165 << "negative delta value" << endl; 00166 else 00167 fDelta = delta; 00168 } 00169 00170 protected: 00171 00172 Double_t fSize; // size of the test (eg. specified rate of Type I error) 00173 RooArgSet fPOI; // parameters of interest for interval 00174 RooArgSet fNuisParams; // nuisance parameters for interval (not really used) 00175 mutable ProposalFunction* fPropFunc; // Proposal function for MCMC integration 00176 RooAbsPdf * fPdf; // pointer to common PDF (owned by the workspace) 00177 RooAbsPdf * fPriorPdf; // pointer to prior PDF (owned by the workspace) 00178 RooAbsData * fData; // pointer to the data (owned by the workspace) 00179 Int_t fNumIters; // number of iterations to run metropolis algorithm 00180 Int_t fNumBurnInSteps; // number of iterations to discard as burn-in, starting from the first 00181 Int_t fNumBins; // set the number of bins to create for each 00182 // axis when constructing the interval 00183 RooArgList * fAxes; // which variables to put on each axis 00184 Bool_t fUseKeys; // whether to use kernel estimation to determine interval 00185 Bool_t fUseSparseHist; // whether to use sparse histogram (if using hist at all) 00186 Double_t fLeftSideTF; // left side tail-fraction for interval 00187 Double_t fEpsilon; // acceptable error for Keys interval determination 00188 00189 Double_t fDelta; // acceptable error for Keys cutoffs being equal 00190 // topCutoff (a) considered == bottomCutoff (b) iff 00191 // (TMath::Abs(a - b) < TMath::Abs(fDelta * (a + b)/2)); 00192 // Theoretically, the Abs is not needed here, but 00193 // floating-point arithmetic does not always work 00194 // perfectly, and the Abs doesn't hurt 00195 enum MCMCInterval::IntervalType fIntervalType; // type of interval to find 00196 00197 void SetupBasicUsage(); 00198 void SetBins(const RooAbsCollection& coll, Int_t numBins) const 00199 { 00200 TIterator* it = coll.createIterator(); 00201 RooAbsArg* r; 00202 while ((r = (RooAbsArg*)it->Next()) != NULL) 00203 if (dynamic_cast<RooRealVar*>(r)) 00204 ((RooRealVar*)r)->setBins(numBins); 00205 delete it; 00206 } 00207 00208 ClassDef(MCMCCalculator,1) // Markov Chain Monte Carlo calculator for Bayesian credible intervals 00209 }; 00210 } 00211 00212 00213 #endif