Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
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"
48#include "RooProdPdf.h"
49
50
51using namespace RooFit;
52using namespace RooStats;
53using std::endl;
54
55////////////////////////////////////////////////////////////////////////////////
56/// default constructor
57
59 fPropFunc(nullptr),
60 fPdf(nullptr),
61 fPriorPdf(nullptr),
62 fData(nullptr),
63 fAxes(nullptr)
64{
65}
66
67////////////////////////////////////////////////////////////////////////////////
68/// constructor from a Model Config with a basic settings package configured
69/// by SetupBasicUsage()
70
72 fPropFunc(nullptr),
73 fData(&data),
74 fAxes(nullptr)
75{
76 SetModel(model);
78}
79
81 // set the model
82 fPdf = model.GetPdf();
83 fPriorPdf = model.GetPriorPdf();
88 if (model.GetParametersOfInterest())
90 if (model.GetNuisanceParameters())
92 if (model.GetConditionalObservables())
94 if (model.GetGlobalObservables())
95 fGlobalObs.add( *(model.GetGlobalObservables() ) );
96
97}
98
99////////////////////////////////////////////////////////////////////////////////
100/// Constructor for automatic configuration with basic settings. Uses a
101/// UniformProposal, 10,000 iterations, 40 burn in steps, 50 bins for each
102/// RooRealVar, determines interval by histogram. Finds a 95% confidence
103/// interval.
104
106{
107 fPropFunc = nullptr;
108 fNumIters = 10000;
109 fNumBurnInSteps = 40;
110 fNumBins = 50;
111 fUseKeys = false;
112 fUseSparseHist = false;
113 SetTestSize(0.05);
115 fLeftSideTF = -1;
116 fEpsilon = -1;
117 fDelta = -1;
118}
119
120////////////////////////////////////////////////////////////////////////////////
121
123{
124 if (a < 0 || a > 1) {
125 coutE(InputArguments) << "MCMCCalculator::SetLeftSideTailFraction: "
126 << "Fraction must be in the range [0, 1]. "
127 << a << "is not allowed." << std::endl;
128 return;
129 }
130
131 fLeftSideTF = a;
133}
134
135////////////////////////////////////////////////////////////////////////////////
136/// Main interface to get a RooStats::ConfInterval.
137
139{
140
141 if (!fData || !fPdf ) return nullptr;
142 if (fPOI.empty()) return nullptr;
143
144 if (fSize < 0) {
145 coutE(InputArguments) << "MCMCCalculator::GetInterval: "
146 << "Test size/Confidence level not set. Returning nullptr." << std::endl;
147 return nullptr;
148 }
149
150 // if a proposal function has not been specified create a default one
151 bool useDefaultPropFunc = (fPropFunc == nullptr);
152 bool usePriorPdf = (fPriorPdf != nullptr);
154
155 // if prior is given create product
157 if (usePriorPdf) {
158 TString prodName = TString("product_") + TString(fPdf->GetName()) + TString("_") + TString(fPriorPdf->GetName() );
160 }
161
162 std::unique_ptr<RooArgSet> constrainedParams{prodPdf->getParameters(*fData)};
164
165 std::unique_ptr<RooArgSet> params{nll->getParameters(*fData)};
166 RemoveConstantParameters(&*params);
167 if (fNumBins > 0) {
168 SetBins(*params, fNumBins);
170 if (dynamic_cast<PdfProposal*>(fPropFunc)) {
171 std::unique_ptr<RooArgSet> proposalVars{(static_cast<PdfProposal*>(fPropFunc))->GetPdf()->
172 getParameters((RooAbsData*)nullptr)};
174 }
175 }
176
178 mh.SetFunction(*nll);
181 mh.SetParameters(*params);
182 if (!fChainParams.empty()) mh.SetChainParameters(fChainParams);
183 mh.SetProposalFunction(*fPropFunc);
184 mh.SetNumIters(fNumIters);
185
186 MarkovChain* chain = mh.ConstructChain();
187
188 TString name = TString("MCMCInterval_") + TString(GetName() );
190 if (fAxes != nullptr)
191 interval->SetAxes(*fAxes);
192 if (fNumBurnInSteps > 0)
193 interval->SetNumBurnInSteps(fNumBurnInSteps);
194 interval->SetUseKeys(fUseKeys);
195 interval->SetUseSparseHist(fUseSparseHist);
196 interval->SetIntervalType(fIntervalType);
198 interval->SetLeftSideTailFraction(fLeftSideTF);
199 if (fEpsilon >= 0)
200 interval->SetEpsilon(fEpsilon);
201 if (fDelta >= 0)
202 interval->SetDelta(fDelta);
203 interval->SetConfidenceLevel(1.0 - fSize);
204
205 if (useDefaultPropFunc) delete fPropFunc;
206 if (usePriorPdf) delete prodPdf;
207
208 return interval;
209}
#define a(i)
Definition RSha256.hxx:99
#define coutE(a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
char name[80]
Definition TGX11.cxx:110
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
Efficient implementation of a product of PDFs of the form.
Definition RooProdPdf.h:36
RooArgSet fChainParams
parameters to store in the chain (if not specified they are all of them )
enum MCMCInterval::IntervalType fIntervalType
RooAbsPdf * fPdf
pointer to common PDF (owned by the workspace)
RooAbsData * fData
pointer to the data (owned by the workspace)
bool fUseKeys
whether to use kernel estimation to determine interval
bool fUseSparseHist
whether to use sparse histogram (if using hist at all)
void SetBins(const RooAbsCollection &coll, Int_t numBins) const
void SetupBasicUsage()
Constructor for automatic configuration with basic settings.
double fDelta
acceptable error for Keys cutoffs being equal topCutoff (a) considered == bottomCutoff (b) iff (std::...
MCMCCalculator()
default constructor
void SetModel(const ModelConfig &model) override
Set the Model.
virtual void SetLeftSideTailFraction(double a)
Set the left side tail fraction.
RooArgSet fNuisParams
nuisance parameters for interval (not really used)
Int_t fNumIters
number of iterations to run metropolis algorithm
Int_t fNumBins
set the number of bins to create for each axis when constructing the interval
RooArgSet fConditionalObs
conditional observables
double fEpsilon
acceptable error for Keys interval determination
void SetTestSize(double size) override
set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
double fLeftSideTF
left side tail-fraction for interval
double fSize
size of the test (eg. specified rate of Type I error)
Int_t fNumBurnInSteps
number of iterations to discard as burn-in, starting from the first
RooArgSet fPOI
parameters of interest for interval
RooArgList * fAxes
which variables to put on each axis
RooArgSet fGlobalObs
global observables
MCMCInterval * GetInterval() const override
Main interface to get a ConfInterval.
RooAbsPdf * fPriorPdf
pointer to prior PDF (owned by the workspace)
ProposalFunction * fPropFunc
Proposal function for MCMC integration.
MCMCInterval is a concrete implementation of the RooStats::ConfInterval interface.
Stores the steps in a Markov Chain of points.
Definition MarkovChain.h:26
This class uses the Metropolis-Hastings algorithm to construct a Markov Chain of data points using Mo...
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition ModelConfig.h:35
const RooArgSet * GetConditionalObservables() const
get RooArgSet for conditional observables (return nullptr if not existing)
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return nullptr if not existing)
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return nullptr if not existing)
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return nullptr if not existing)
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
RooAbsPdf * GetPriorPdf() const
get parameters prior pdf (return nullptr if not existing)
PdfProposal is a concrete implementation of the ProposalFunction interface.
Definition PdfProposal.h:30
UniformProposal is a concrete implementation of the ProposalFunction interface for use with a Markov ...
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Basic string class.
Definition TString.h:139
RooCmdArg Constrain(const RooArgSet &params)
RooCmdArg GlobalObservables(Args_t &&... argsOrArgSet)
RooCmdArg ConditionalObservables(Args_t &&... argsOrArgSet)
Create a RooCmdArg to declare conditional observables.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:64
@ InputArguments
Namespace for the RooStats classes.
Definition CodegenImpl.h:58
void RemoveConstantParameters(RooArgSet *set)