Logo ROOT  
Reference Guide
StandardBayesianMCMCDemo.C File Reference

Detailed Description

View in nbviewer Open in SWAN Standard demo of the Bayesian MCMC calculator

This is a standard demo that can be used with any ROOT file prepared in the standard way. You specify:

  • name for input ROOT file
  • name of workspace inside ROOT file that holds model and data
  • name of ModelConfig that specifies details for calculator tools
  • name of dataset

With default parameters the macro will attempt to run the standard hist2workspace example and read the ROOT file that it produces.

The actual heart of the demo is only about 10 lines long.

The MCMCCalculator is a Bayesian tool that uses the Metropolis-Hastings algorithm to efficiently integrate in many dimensions. It is not as accurate as the BayesianCalculator for simple problems, but it scales to much more complicated cases.

RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University
All rights reserved, please read http://roofit.sourceforge.net/license.txt
[#1] INFO:Minization -- p.d.f. provides expected number of events, including extended term in likelihood.
[#1] INFO:Minization -- createNLL: caching constraint set under name CONSTR_OF_PDF_simPdf_FOR_OBS_channelCat:obs_x_channel1 with 6 entries
[#1] INFO:Minization -- Including the following constraint terms in minimization: (lumiConstraint,alpha_syst1Constraint,alpha_syst2Constraint,alpha_syst3Constraint,gamma_stat_channel1_bin_0_constraint,gamma_stat_channel1_bin_1_constraint)
[#1] INFO:Minization -- The following global observables have been defined: (nom_alpha_syst2,nom_alpha_syst3,nom_gamma_stat_channel1_bin_0,nom_gamma_stat_channel1_bin_1)
Metropolis-Hastings progress: ....................................................................................................
[#1] INFO:Eval -- Proposal acceptance rate: 27.377%
[#1] INFO:Eval -- Number of steps in chain: 27377
>>>> RESULT : 95% interval on SigXsecOverSM is : [0, 2.17276]
#include "TFile.h"
#include "TROOT.h"
#include "TCanvas.h"
#include "TMath.h"
#include "TSystem.h"
#include "RooWorkspace.h"
#include "RooAbsData.h"
#include "RooFitResult.h"
using namespace RooFit;
using namespace RooStats;
struct BayesianMCMCOptions {
double confLevel = 0.95;
int intervalType = 2; // type of interval (0 is shortest, 1 central, 2 upper limit)
double maxPOI = -999; // force different values of POI for doing the scan (default is given value)
double minPOI = -999;
int numIters = 100000; // number of iterations
int numBurnInSteps = 100; // number of burn in steps to be ignored
};
BayesianMCMCOptions optMCMC;
void StandardBayesianMCMCDemo(const char *infile = "", const char *workspaceName = "combined",
const char *modelConfigName = "ModelConfig", const char *dataName = "obsData")
{
// -------------------------------------------------------
// First part is just to access a user-defined file
// or create the standard example file if it doesn't exist
const char *filename = "";
if (!strcmp(infile, "")) {
filename = "results/example_combined_GaussExample_model.root";
bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
// if file does not exists generate with histfactory
if (!fileExist) {
#ifdef _WIN32
cout << "HistFactory file cannot be generated on Windows - exit" << endl;
return;
#endif
// Normally this would be run on the command line
cout << "will run standard hist2workspace example" << endl;
gROOT->ProcessLine(".! prepareHistFactory .");
gROOT->ProcessLine(".! hist2workspace config/example.xml");
cout << "\n\n---------------------" << endl;
cout << "Done creating example input" << endl;
cout << "---------------------\n\n" << endl;
}
} else
filename = infile;
// Try to open the file
TFile *file = TFile::Open(filename);
// if input file was specified byt not found, quit
if (!file) {
cout << "StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
return;
}
// -------------------------------------------------------
// Tutorial starts here
// -------------------------------------------------------
// get the workspace out of the file
RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);
if (!w) {
cout << "workspace not found" << endl;
return;
}
// get the modelConfig out of the file
ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);
// get the modelConfig out of the file
RooAbsData *data = w->data(dataName);
// make sure ingredients are found
if (!data || !mc) {
w->Print();
cout << "data or ModelConfig was not found" << endl;
return;
}
// Want an efficient proposal function
// default is uniform.
/*
// this one is based on the covariance matrix of fit
RooFitResult* fit = mc->GetPdf()->fitTo(*data,Save());
ProposalHelper ph;
ph.SetVariables((RooArgSet&)fit->floatParsFinal());
ph.SetCovMatrix(fit->covarianceMatrix());
ph.SetUpdateProposalParameters(kTRUE); // auto-create mean vars and add mappings
ph.SetCacheSize(100);
ProposalFunction* pf = ph.GetProposalFunction();
*/
// this proposal function seems fairly robust
// -------------------------------------------------------
// create and use the MCMCCalculator
// to find and plot the 95% credible interval
// on the parameter of interest as specified
// in the model config
MCMCCalculator mcmc(*data, *mc);
mcmc.SetConfidenceLevel(optMCMC.confLevel); // 95% interval
// mcmc.SetProposalFunction(*pf);
mcmc.SetProposalFunction(sp);
mcmc.SetNumIters(optMCMC.numIters); // Metropolis-Hastings algorithm iterations
mcmc.SetNumBurnInSteps(optMCMC.numBurnInSteps); // first N steps to be ignored as burn-in
// default is the shortest interval.
if (optMCMC.intervalType == 0)
mcmc.SetIntervalType(MCMCInterval::kShortest); // for shortest interval (not really needed)
if (optMCMC.intervalType == 1)
mcmc.SetLeftSideTailFraction(0.5); // for central interval
if (optMCMC.intervalType == 2)
mcmc.SetLeftSideTailFraction(0.); // for upper limit
if (optMCMC.minPOI != -999)
firstPOI->setMin(optMCMC.minPOI);
if (optMCMC.maxPOI != -999)
firstPOI->setMax(optMCMC.maxPOI);
MCMCInterval *interval = mcmc.GetInterval();
// make a plot
// TCanvas* c1 =
auto c1 = new TCanvas("IntervalPlot");
MCMCIntervalPlot plot(*interval);
plot.Draw();
TCanvas *c2 = new TCanvas("extraPlots");
const RooArgSet *list = mc->GetNuisanceParameters();
if (list->getSize() > 1) {
double n = list->getSize();
int ny = TMath::CeilNint(sqrt(n));
int nx = TMath::CeilNint(double(n) / ny);
c2->Divide(nx, ny);
}
// draw a scatter plot of chain results for poi vs each nuisance parameters
RooRealVar *nuis = NULL;
int iPad = 1; // iPad, that's funny
while ((nuis = (RooRealVar *)it.Next())) {
c2->cd(iPad++);
plot.DrawChainScatter(*firstPOI, *nuis);
}
// print out the interval on the first Parameter of Interest
cout << "\n>>>> RESULT : " << optMCMC.confLevel * 100 << "% interval on " << firstPOI->GetName() << " is : ["
<< interval->LowerLimit(*firstPOI) << ", " << interval->UpperLimit(*firstPOI) << "] " << endl;
gPad = c1;
}
Author
Kyle Cranmer

Definition in file StandardBayesianMCMCDemo.C.

RooWorkspace::data
RooAbsData * data(const char *name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found.
Definition: RooWorkspace.cxx:1368
n
const Int_t n
Definition: legend1.C:16
RooWorkspace.h
RooStats::MCMCInterval
MCMCInterval is a concrete implementation of the RooStats::ConfInterval interface.
Definition: MCMCInterval.h:33
RooAbsCollection::first
RooAbsArg * first() const
Definition: RooAbsCollection.h:187
RooRealVar::setMin
void setMin(const char *name, Double_t value)
Set minimum of name range to given value.
Definition: RooRealVar.cxx:461
RooAbsData
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:49
TMath::CeilNint
Int_t CeilNint(Double_t x)
Definition: TMath.h:699
TFile::Open
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3997
RooStats::MCMCInterval::LowerLimit
virtual Double_t LowerLimit(RooRealVar &param)
get the lowest value of param that is within the confidence interval
Definition: MCMCInterval.cxx:1006
TCanvas.h
TSystem::AccessPathName
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1294
RooStats::MCMCIntervalPlot
This class provides simple and straightforward utilities to plot a MCMCInterval object.
Definition: MCMCIntervalPlot.h:28
TFile.h
TROOT.h
TSystem.h
RooWorkspace::Print
void Print(Option_t *opts=0) const
Print contents of the workspace.
Definition: RooWorkspace.cxx:2194
MCMCIntervalPlot.h
ModelConfig.h
RooFit
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: RooCFunction1Binding.h:29
RooStats::MCMCInterval::UpperLimit
virtual Double_t UpperLimit(RooRealVar &param)
get the highest value of param that is within the confidence interval
Definition: MCMCInterval.cxx:1022
MCMCInterval.h
RooStats::SequentialProposal
Class implementing a proposal function that samples the parameter space by moving only in one coordin...
Definition: SequentialProposal.h:20
RooAbsCollection::createIterator
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
Definition: RooAbsCollection.h:129
RooWorkspace::obj
TObject * obj(const char *name) const
Return any type of object (RooAbsArg, RooAbsData or generic object) with given name)
Definition: RooWorkspace.cxx:2106
RooStats::MCMCCalculator
Bayesian Calculator estimating an interval or a credible region using the Markov-Chain Monte Carlo me...
Definition: MCMCCalculator.h:31
sqrt
double sqrt(double)
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
RooFitResult.h
MCMCCalculator.h
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
RooStats::ModelConfig::GetParametersOfInterest
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return NULL if not existing)
Definition: ModelConfig.h:237
TIter::Next
TObject * Next()
Definition: TCollection.h:249
RooWorkspace
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:43
RooRealVar::setMax
void setMax(const char *name, Double_t value)
Set maximum of name range to given value.
Definition: RooRealVar.cxx:491
RooAbsData.h
TCanvas
The Canvas class.
Definition: TCanvas.h:23
RooStats
Namespace for the RooStats classes.
Definition: Asimov.h:19
file
Definition: file.py:1
c2
return c2
Definition: legend2.C:14
gPad
#define gPad
Definition: TVirtualPad.h:287
TIter
Definition: TCollection.h:233
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
RooStats::ModelConfig::GetNuisanceParameters
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return NULL if not existing)
Definition: ModelConfig.h:240
RooRealVar
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:39
RooStats::ModelConfig
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:30
SequentialProposal.h
RooAbsCollection::getSize
Int_t getSize() const
Definition: RooAbsCollection.h:182
TMath.h
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29
gROOT
#define gROOT
Definition: TROOT.h:406
ProposalHelper.h
c1
return c1
Definition: legend1.C:41