ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
IntervalExamples.C
Go to the documentation of this file.
1 // Example showing confidence intervals with four techniques.
2 /*
3 IntervalExamples
4 
5 Author Kyle Cranmer
6 date Sep. 2010
7 
8 An example that shows confidence intervals with four techniques.
9 The model is a Normal Gaussian G(x|mu,sigma) with 100 samples of x.
10 The answer is known analytically, so this is a good example to validate
11 the RooStats tools.
12 
13 expected interval is [-0.162917, 0.229075]
14 plc interval is [-0.162917, 0.229075]
15 fc interval is [-0.17 , 0.23] // stepsize is 0.01
16 bc interval is [-0.162918, 0.229076]
17 mcmc interval is [-0.166999, 0.230224]
18 
19 
20 */
21 
22 #include "RooStats/ConfInterval.h"
31 
32 #include "RooStats/ProofConfig.h"
33 #include "RooStats/ToyMCSampler.h"
34 
35 #include "RooRandom.h"
36 #include "RooDataSet.h"
37 #include "RooRealVar.h"
38 #include "RooConstVar.h"
39 #include "RooAddition.h"
40 #include "RooDataHist.h"
41 #include "RooPoisson.h"
42 #include "RooPlot.h"
43 
44 #include "TCanvas.h"
45 #include "TTree.h"
46 #include "TStyle.h"
47 #include "TMath.h"
48 #include"Math/DistFunc.h"
49 #include "TH1F.h"
50 #include "TMarker.h"
51 #include "TStopwatch.h"
52 
53 #include <iostream>
54 
55 // use this order for safety on library loading
56 using namespace RooFit ;
57 using namespace RooStats ;
58 
59 
61 {
62 
63  // Time this macro
64  TStopwatch t;
65  t.Start();
66 
67 
68  // set RooFit random seed for reproducible results
70 
71  // make a simple model via the workspace factory
72  RooWorkspace* wspace = new RooWorkspace();
73  wspace->factory("Gaussian::normal(x[-10,10],mu[-1,1],sigma[1])");
74  wspace->defineSet("poi","mu");
75  wspace->defineSet("obs","x");
76 
77  // specify components of model for statistical tools
78  ModelConfig* modelConfig = new ModelConfig("Example G(x|mu,1)");
79  modelConfig->SetWorkspace(*wspace);
80  modelConfig->SetPdf( *wspace->pdf("normal") );
81  modelConfig->SetParametersOfInterest( *wspace->set("poi") );
82  modelConfig->SetObservables( *wspace->set("obs") );
83 
84  // create a toy dataset
85  RooDataSet* data = wspace->pdf("normal")->generate(*wspace->set("obs"),100);
86  data->Print();
87 
88  // for convenience later on
89  RooRealVar* x = wspace->var("x");
90  RooRealVar* mu = wspace->var("mu");
91 
92  // set confidence level
93  double confidenceLevel = 0.95;
94 
95  // example use profile likelihood calculator
96  ProfileLikelihoodCalculator plc(*data, *modelConfig);
97  plc.SetConfidenceLevel( confidenceLevel);
98  LikelihoodInterval* plInt = plc.GetInterval();
99 
100  // example use of Feldman-Cousins
101  FeldmanCousins fc(*data, *modelConfig);
102  fc.SetConfidenceLevel( confidenceLevel);
103  fc.SetNBins(100); // number of points to test per parameter
104  fc.UseAdaptiveSampling(true); // make it go faster
105 
106  // Here, we consider only ensembles with 100 events
107  // The PDF could be extended and this could be removed
108  fc.FluctuateNumDataEntries(false);
109 
110  // Proof
111  // ProofConfig pc(*wspace, 4, "workers=4", kFALSE); // proof-lite
112  //ProofConfig pc(w, 8, "localhost"); // proof cluster at "localhost"
113  // ToyMCSampler* toymcsampler = (ToyMCSampler*) fc.GetTestStatSampler();
114  // toymcsampler->SetProofConfig(&pc); // enable proof
115 
116  PointSetInterval* interval = (PointSetInterval*) fc.GetInterval();
117 
118 
119  // example use of BayesianCalculator
120  // now we also need to specify a prior in the ModelConfig
121  wspace->factory("Uniform::prior(mu)");
122  modelConfig->SetPriorPdf(*wspace->pdf("prior"));
123 
124  // example usage of BayesianCalculator
125  BayesianCalculator bc(*data, *modelConfig);
126  bc.SetConfidenceLevel( confidenceLevel);
127  SimpleInterval* bcInt = bc.GetInterval();
128 
129  // example use of MCMCInterval
130  MCMCCalculator mc(*data, *modelConfig);
131  mc.SetConfidenceLevel( confidenceLevel);
132  // special options
133  mc.SetNumBins(200); // bins used internally for representing posterior
134  mc.SetNumBurnInSteps(500); // first N steps to be ignored as burn-in
135  mc.SetNumIters(100000); // how long to run chain
136  mc.SetLeftSideTailFraction(0.5); // for central interval
137  MCMCInterval* mcInt = mc.GetInterval();
138 
139  // for this example we know the expected intervals
140  double expectedLL = data->mean(*x)
141  + ROOT::Math::normal_quantile( (1-confidenceLevel)/2,1)
142  / sqrt(data->numEntries());
143  double expectedUL = data->mean(*x)
144  + ROOT::Math::normal_quantile_c((1-confidenceLevel)/2,1)
145  / sqrt(data->numEntries()) ;
146 
147  // Use the intervals
148  std::cout << "expected interval is [" <<
149  expectedLL << ", " <<
150  expectedUL << "]" << endl;
151 
152  cout << "plc interval is [" <<
153  plInt->LowerLimit(*mu) << ", " <<
154  plInt->UpperLimit(*mu) << "]" << endl;
155 
156  std::cout << "fc interval is ["<<
157  interval->LowerLimit(*mu) << " , " <<
158  interval->UpperLimit(*mu) << "]" << endl;
159 
160  cout << "bc interval is [" <<
161  bcInt->LowerLimit() << ", " <<
162  bcInt->UpperLimit() << "]" << endl;
163 
164  cout << "mc interval is [" <<
165  mcInt->LowerLimit(*mu) << ", " <<
166  mcInt->UpperLimit(*mu) << "]" << endl;
167 
168  mu->setVal(0);
169  cout << "is mu=0 in the interval? " <<
170  plInt->IsInInterval(RooArgSet(*mu)) << endl;
171 
172 
173  // make a reasonable style
177  gStyle->SetPadColor(0);
180  gStyle->SetFillColor(0);
182  gStyle->SetStatColor(0);
183 
184 
185  // some plots
186  TCanvas* canvas = new TCanvas("canvas");
187  canvas->Divide(2,2);
188 
189  // plot the data
190  canvas->cd(1);
191  RooPlot* frame = x->frame();
192  data->plotOn(frame);
193  data->statOn(frame);
194  frame->Draw();
195 
196  // plot the profile likeihood
197  canvas->cd(2);
199  plot.Draw();
200 
201  // plot the MCMC interval
202  canvas->cd(3);
203  MCMCIntervalPlot* mcPlot = new MCMCIntervalPlot(*mcInt);
204  mcPlot->SetLineColor(kGreen);
205  mcPlot->SetLineWidth(2);
206  mcPlot->Draw();
207 
208  canvas->cd(4);
209  RooPlot * bcPlot = bc.GetPosteriorPlot();
210  bcPlot->Draw();
211 
212  canvas->Update();
213 
214  t.Stop();
215  t.Print();
216 
217 }
double normal_quantile(double z, double sigma)
Inverse ( ) of the cumulative distribution function of the lower tail of the normal (Gaussian) distri...
RooPlot * GetPosteriorPlot(bool norm=false, double precision=0.01) const
get the plot with option to get it normalized
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:217
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:52
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
void Draw(const Option_t *options=0)
draw the likelihood interval or contour for the 1D case a RooPlot is drawn by default of the profiled...
This class provides simple and straightforward utilities to plot a MCMCInterval object.
void SetStatColor(Color_t color=19)
Definition: TStyle.h:384
LikelihoodInterval is a concrete implementation of the RooStats::ConfInterval interface.
virtual void SetWorkspace(RooWorkspace &ws)
Definition: ModelConfig.h:88
double normal_quantile_c(double z, double sigma)
Inverse ( ) of the cumulative distribution function of the upper tail of the normal (Gaussian) distri...
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
void IntervalExamples()
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:157
This class provides simple and straightforward utilities to plot a LikelihoodInterval object...
double confidenceLevel
ProfileLikelihoodCalculator is a concrete implementation of CombinedCalculator (the interface class f...
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 SetNumBins(Int_t numBins)
set the number of bins to create for each axis when constructing the interval
void Draw(const Option_t *options=NULL)
Definition: Rtypes.h:61
virtual void SetObservables(const RooArgSet &set)
specify the observables
Definition: ModelConfig.h:156
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
Double_t LowerLimit(const RooRealVar &param)
return the lower bound of the interval on a given parameter
void SetCanvasColor(Color_t color=19)
Definition: TStyle.h:339
virtual LikelihoodInterval * GetInterval() const
Return a likelihood interval.
virtual SimpleInterval * GetInterval() const
compute the interval.
void plot(TString fname="data.root", TString var0="var0", TString var1="var1")
Definition: createData.C:17
double sqrt(double)
static struct mg_connection * fc(struct mg_context *ctx)
Definition: civetweb.c:839
virtual void SetConfidenceLevel(Double_t cl)
set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval) ...
virtual void SetConfidenceLevel(Double_t cl)
set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval) ...
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:75
Double_t x[n]
Definition: legend1.C:17
void SetFrameFillColor(Color_t color=1)
Definition: TStyle.h:367
RooAbsPdf * pdf(const char *name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
virtual void SetPdf(const RooAbsPdf &pdf)
Set the Pdf, add to the the workspace if not already there.
Definition: ModelConfig.h:97
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition: RooRandom.cxx:54
virtual Double_t LowerLimit()
virtual RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Plot dataset on specified frame.
Definition: RooAbsData.cxx:626
virtual void SetConfidenceLevel(Double_t cl)
set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval) ...
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
virtual void setVal(Double_t value)
Set value of variable to 'value'.
Definition: RooRealVar.cxx:203
Double_t LowerLimit(RooRealVar &param)
return lower limit on a given parameter
TThread * t[5]
Definition: threadsh1.C:13
Double_t UpperLimit(const RooRealVar &param)
return the upper bound of the interval on a given parameter
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.
void SetPadBorderMode(Int_t mode=1)
Definition: TStyle.h:352
void SetCanvasBorderMode(Int_t mode=1)
Definition: TStyle.h:341
Double_t mean(RooRealVar &var, const char *cutSpec=0, const char *cutRange=0) const
Definition: RooAbsData.h:175
void SetPadColor(Color_t color=19)
Definition: TStyle.h:350
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
void SetNBins(Int_t bins)
virtual RooPlot * statOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Add a box with statistics information to the specified frame.
Bool_t defineSet(const char *name, const RooArgSet &aset, Bool_t importMissing=kFALSE)
Define a named RooArgSet with given constituents.
virtual Int_t numEntries() const
Definition: RooAbsData.cxx:293
void FluctuateNumDataEntries(bool flag=true)
void SetLineColor(Color_t color)
void SetLineWidth(Int_t width)
RooPlot * frame(const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
RooRealVar * var(const char *name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found...
Double_t UpperLimit(RooRealVar &param)
return upper limit on a given parameter
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
The FeldmanCousins class (like the Feldman-Cousins technique) is essentially a specific configuration...
PointSetInterval is a concrete implementation of the ConfInterval interface.
virtual Bool_t IsInInterval(const RooArgSet &) const
check if given point is in the interval
virtual void SetPriorPdf(const RooAbsPdf &pdf)
Set the Prior Pdf, add to the the workspace if not already there.
Definition: ModelConfig.h:103
virtual PointSetInterval * GetInterval() const
Main interface to get a ConfInterval (will be a PointSetInterval)
virtual void SetConfidenceLevel(Double_t cl)
set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval) ...
RooFactoryWSTool & factory()
Return instance to factory tool.
virtual Double_t UpperLimit()
void SetTitleFillColor(Color_t color=1)
Definition: TStyle.h:398
RooDataSet * generate(const RooArgSet &whatVars, Int_t nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none())
Generate a new dataset containing the specified variables with events sampled from our distribution...
Definition: RooAbsPdf.cxx:1702
const RooArgSet * set(const char *name)
Return pointer to previously defined named set with given nmame If no such set is found a null pointe...
virtual void SetLeftSideTailFraction(Double_t a)
Set the left side tail fraction.
virtual void SetParametersOfInterest(const RooArgSet &set)
Definition: ModelConfig.h:115
Bayesian Calculator estimating an interval or a credible region using the Markov-Chain Monte Carlo me...
BayesianCalculator is a concrete implementation of IntervalCalculator, providing the computation of a...
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42
void UseAdaptiveSampling(bool flag=true)
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559
Stopwatch class.
Definition: TStopwatch.h:30