ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
StandardBayesianMCMCDemo.C
Go to the documentation of this file.
1 // Standard demo of the Bayesian MCMC calculator
2 
3 /*
4 
5 Author: Kyle Cranmer
6 date: Dec. 2010
7 updated: July 2011 for 1-sided upper limit and SequentialProposalFunction
8 
9 This is a standard demo that can be used with any ROOT file
10 prepared in the standard way. You specify:
11  - name for input ROOT file
12  - name of workspace inside ROOT file that holds model and data
13  - name of ModelConfig that specifies details for calculator tools
14  - name of dataset
15 
16 With default parameters the macro will attempt to run the
17 standard hist2workspace example and read the ROOT file
18 that it produces.
19 
20 The actual heart of the demo is only about 10 lines long.
21 
22 The MCMCCalculator is a Bayesian tool that uses
23 the Metropolis-Hastings algorithm to efficiently integrate
24 in many dimensions. It is not as accurate as the BayesianCalculator
25 for simple problems, but it scales to much more complicated cases.
26 */
27 
28 #include "TFile.h"
29 #include "TROOT.h"
30 #include "TCanvas.h"
31 #include "TMath.h"
32 #include "TSystem.h"
33 #include "RooWorkspace.h"
34 #include "RooAbsData.h"
35 
36 #include "RooStats/ModelConfig.h"
38 #include "RooStats/MCMCInterval.h"
43 #include "RooFitResult.h"
44 
45 
46 using namespace RooFit;
47 using namespace RooStats;
48 
49 int intervalType = 2; // type of interval (0 is shortest, 1 central, 2 upper limit)
50 double maxPOI = -999; // force a different value of POI for doing the scan (default is given value)
51 
52 
53 void StandardBayesianMCMCDemo(const char* infile = "",
54  const char* workspaceName = "combined",
55  const char* modelConfigName = "ModelConfig",
56  const char* dataName = "obsData"){
57 
58  /////////////////////////////////////////////////////////////
59  // First part is just to access a user-defined file
60  // or create the standard example file if it doesn't exist
61  ////////////////////////////////////////////////////////////
62 
63 
64 
65  const char* filename = "";
66  if (!strcmp(infile,"")) {
67  filename = "results/example_combined_GaussExample_model.root";
68  bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
69  // if file does not exists generate with histfactory
70  if (!fileExist) {
71 #ifdef _WIN32
72  cout << "HistFactory file cannot be generated on Windows - exit" << endl;
73  return;
74 #endif
75  // Normally this would be run on the command line
76  cout <<"will run standard hist2workspace example"<<endl;
77  gROOT->ProcessLine(".! prepareHistFactory .");
78  gROOT->ProcessLine(".! hist2workspace config/example.xml");
79  cout <<"\n\n---------------------"<<endl;
80  cout <<"Done creating example input"<<endl;
81  cout <<"---------------------\n\n"<<endl;
82  }
83 
84  }
85  else
86  filename = infile;
87 
88  // Try to open the file
89  TFile *file = TFile::Open(filename);
90 
91  // if input file was specified byt not found, quit
92  if(!file ){
93  cout <<"StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
94  return;
95  }
96 
97 
98 
99  /////////////////////////////////////////////////////////////
100  // Tutorial starts here
101  ////////////////////////////////////////////////////////////
102 
103  // get the workspace out of the file
104  RooWorkspace* w = (RooWorkspace*) file->Get(workspaceName);
105  if(!w){
106  cout <<"workspace not found" << endl;
107  return;
108  }
109 
110  // get the modelConfig out of the file
111  ModelConfig* mc = (ModelConfig*) w->obj(modelConfigName);
112 
113  // get the modelConfig out of the file
114  RooAbsData* data = w->data(dataName);
115 
116  // make sure ingredients are found
117  if(!data || !mc){
118  w->Print();
119  cout << "data or ModelConfig was not found" <<endl;
120  return;
121  }
122 
123  // Want an efficient proposal function
124  // default is uniform.
125 
126  /*
127  // this one is based on the covariance matrix of fit
128  RooFitResult* fit = mc->GetPdf()->fitTo(*data,Save());
129  ProposalHelper ph;
130  ph.SetVariables((RooArgSet&)fit->floatParsFinal());
131  ph.SetCovMatrix(fit->covarianceMatrix());
132  ph.SetUpdateProposalParameters(kTRUE); // auto-create mean vars and add mappings
133  ph.SetCacheSize(100);
134  ProposalFunction* pf = ph.GetProposalFunction();
135  */
136 
137  // this proposal function seems fairly robust
138  SequentialProposal sp(0.1);
139  /////////////////////////////////////////////
140  // create and use the MCMCCalculator
141  // to find and plot the 95% credible interval
142  // on the parameter of interest as specified
143  // in the model config
144  MCMCCalculator mcmc(*data,*mc);
145  mcmc.SetConfidenceLevel(0.95); // 95% interval
146  // mcmc.SetProposalFunction(*pf);
147  mcmc.SetProposalFunction(sp);
148  mcmc.SetNumIters(1000000); // Metropolis-Hastings algorithm iterations
149  mcmc.SetNumBurnInSteps(50); // first N steps to be ignored as burn-in
150 
151  // default is the shortest interval.
152  if (intervalType == 0) mcmc.SetIntervalType(MCMCInterval::kShortest); // for shortest interval (not really needed)
153  if (intervalType == 1) mcmc.SetLeftSideTailFraction(0.5); // for central interval
154  if (intervalType == 2) mcmc.SetLeftSideTailFraction(0.); // for upper limit
155 
156  RooRealVar* firstPOI = (RooRealVar*) mc->GetParametersOfInterest()->first();
157  if (maxPOI != -999)
158  firstPOI->setMax(maxPOI);
159 
160  MCMCInterval* interval = mcmc.GetInterval();
161 
162  // make a plot
163  //TCanvas* c1 =
164  new TCanvas("IntervalPlot");
165  MCMCIntervalPlot plot(*interval);
166  plot.Draw();
167 
168  TCanvas* c2 = new TCanvas("extraPlots");
169  const RooArgSet* list = mc->GetNuisanceParameters();
170  if(list->getSize()>1){
171  double n = list->getSize();
172  int ny = TMath::CeilNint( sqrt(n) );
173  int nx = TMath::CeilNint(double(n)/ny);
174  c2->Divide( nx,ny);
175  }
176 
177  // draw a scatter plot of chain results for poi vs each nuisance parameters
179  RooRealVar* nuis = NULL;
180  int iPad=1; // iPad, that's funny
181  while( (nuis = (RooRealVar*) it->Next() )){
182  c2->cd(iPad++);
183  plot.DrawChainScatter(*firstPOI,*nuis);
184  }
185 
186  // print out the iterval on the first Parameter of Interest
187  cout << "\n95% interval on " <<firstPOI->GetName()<<" is : ["<<
188  interval->LowerLimit(*firstPOI) << ", "<<
189  interval->UpperLimit(*firstPOI) <<"] "<<endl;
190 
191 }
const int nx
Definition: kalman.C:16
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:1213
double maxPOI
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:52
This class provides simple and straightforward utilities to plot a MCMCInterval object.
RooAbsData * data(const char *name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found...
int intervalType
static const char * filename()
virtual void SetNumBurnInSteps(Int_t numBurnInSteps)
set the number of steps in the chain to discard as burn-in, starting from the first ...
#define gROOT
Definition: TROOT.h:344
void Draw(const Option_t *options=NULL)
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return NULL if not existing)
Definition: ModelConfig.h:250
Iterator abstract base class.
Definition: TIterator.h:32
void setMax(const char *name, Double_t value)
Set maximum of name range to given value.
Definition: RooRealVar.cxx:416
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3851
RooAbsArg * first() const
void plot(TString fname="data.root", TString var0="var0", TString var1="var1")
Definition: createData.C:17
double sqrt(double)
virtual void SetConfidenceLevel(Double_t cl)
set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval) ...
virtual void SetIntervalType(enum MCMCInterval::IntervalType intervalType)
set what type of interval to have the MCMCInterval represent
const int ny
Definition: kalman.C:17
TIterator * createIterator(Bool_t dir=kIterForward) const
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
tuple infile
Definition: mrt.py:15
virtual void SetNumIters(Int_t numIters)
set the number of iterations to run the metropolis algorithm
virtual MCMCInterval * GetInterval() const
Main interface to get a ConfInterval.
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
Class implementing a proposal function that samples the parameter space by moving only in one coordin...
tuple w
Definition: qtexample.py:51
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
return c2
Definition: legend2.C:14
virtual void SetProposalFunction(ProposalFunction &proposalFunction)
set the proposal function for suggesting new points for the MCMC
tuple file
Definition: fildir.py:20
void DrawChainScatter(RooRealVar &xVar, RooRealVar &yVar)
TObject * obj(const char *name) const
Return any type of object (RooAbsArg, RooAbsData or generic object) with given name) ...
void Print(Option_t *opts=0) const
Print contents of the workspace.
virtual TObject * Next()=0
#define NULL
Definition: Rtypes.h:82
virtual void SetLeftSideTailFraction(Double_t a)
Set the left side tail fraction.
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return NULL if not existing)
Definition: ModelConfig.h:247
void StandardBayesianMCMCDemo(const char *infile="", const char *workspaceName="combined", const char *modelConfigName="ModelConfig", const char *dataName="obsData")
Int_t getSize() const
Bayesian Calculator estimating an interval or a credible region using the Markov-Chain Monte Carlo me...
const Int_t n
Definition: legend1.C:16
Int_t CeilNint(Double_t x)
Definition: TMath.h:470
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42