Logo ROOT   6.14/05
Reference Guide
IntervalExamples.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roostats
3 /// \notebook
4 /// Example showing confidence intervals with four techniques.
5 ///
6 /// An example that shows confidence intervals with four techniques.
7 /// The model is a Normal Gaussian G(x|mu,sigma) with 100 samples of x.
8 /// The answer is known analytically, so this is a good example to validate
9 /// the RooStats tools.
10 ///
11 /// - expected interval is [-0.162917, 0.229075]
12 /// - plc interval is [-0.162917, 0.229075]
13 /// - fc interval is [-0.17 , 0.23] // stepsize is 0.01
14 /// - bc interval is [-0.162918, 0.229076]
15 /// - mcmc interval is [-0.166999, 0.230224]
16 ///
17 /// \macro_image
18 /// \macro_output
19 /// \macro_code
20 ///
21 /// \author Kyle Cranmer
22 
23 #include "RooStats/ConfInterval.h"
32 
33 #include "RooStats/ProofConfig.h"
34 #include "RooStats/ToyMCSampler.h"
35 
36 #include "RooRandom.h"
37 #include "RooDataSet.h"
38 #include "RooRealVar.h"
39 #include "RooConstVar.h"
40 #include "RooAddition.h"
41 #include "RooDataHist.h"
42 #include "RooPoisson.h"
43 #include "RooPlot.h"
44 
45 #include "TCanvas.h"
46 #include "TTree.h"
47 #include "TStyle.h"
48 #include "TMath.h"
49 #include"Math/DistFunc.h"
50 #include "TH1F.h"
51 #include "TMarker.h"
52 #include "TStopwatch.h"
53 
54 #include <iostream>
55 
56 // use this order for safety on library loading
57 using namespace RooFit;
58 using namespace RooStats;
59 
60 
61 void IntervalExamples()
62 {
63 
64  // Time this macro
65  TStopwatch t;
66  t.Start();
67 
68 
69  // set RooFit random seed for reproducible results
71 
72  // make a simple model via the workspace factory
73  RooWorkspace* wspace = new RooWorkspace();
74  wspace->factory("Gaussian::normal(x[-10,10],mu[-1,1],sigma[1])");
75  wspace->defineSet("poi","mu");
76  wspace->defineSet("obs","x");
77 
78  // specify components of model for statistical tools
79  ModelConfig* modelConfig = new ModelConfig("Example G(x|mu,1)");
80  modelConfig->SetWorkspace(*wspace);
81  modelConfig->SetPdf( *wspace->pdf("normal") );
82  modelConfig->SetParametersOfInterest( *wspace->set("poi") );
83  modelConfig->SetObservables( *wspace->set("obs") );
84 
85  // create a toy dataset
86  RooDataSet* data = wspace->pdf("normal")->generate(*wspace->set("obs"),100);
87  data->Print();
88 
89  // for convenience later on
90  RooRealVar* x = wspace->var("x");
91  RooRealVar* mu = wspace->var("mu");
92 
93  // set confidence level
94  double confidenceLevel = 0.95;
95 
96  // example use profile likelihood calculator
97  ProfileLikelihoodCalculator plc(*data, *modelConfig);
98  plc.SetConfidenceLevel( confidenceLevel);
99  LikelihoodInterval* plInt = plc.GetInterval();
100 
101  // example use of Feldman-Cousins
102  FeldmanCousins fc(*data, *modelConfig);
103  fc.SetConfidenceLevel( confidenceLevel);
104  fc.SetNBins(100); // number of points to test per parameter
105  fc.UseAdaptiveSampling(true); // make it go faster
106 
107  // Here, we consider only ensembles with 100 events
108  // The PDF could be extended and this could be removed
109  fc.FluctuateNumDataEntries(false);
110 
111  // Proof
112  // ProofConfig pc(*wspace, 4, "workers=4", kFALSE); // proof-lite
113  //ProofConfig pc(w, 8, "localhost"); // proof cluster at "localhost"
114  // ToyMCSampler* toymcsampler = (ToyMCSampler*) fc.GetTestStatSampler();
115  // toymcsampler->SetProofConfig(&pc); // enable proof
116 
117  PointSetInterval* interval = (PointSetInterval*) fc.GetInterval();
118 
119 
120  // example use of BayesianCalculator
121  // now we also need to specify a prior in the ModelConfig
122  wspace->factory("Uniform::prior(mu)");
123  modelConfig->SetPriorPdf(*wspace->pdf("prior"));
124 
125  // example usage of BayesianCalculator
126  BayesianCalculator bc(*data, *modelConfig);
127  bc.SetConfidenceLevel( confidenceLevel);
128  SimpleInterval* bcInt = bc.GetInterval();
129 
130  // example use of MCMCInterval
131  MCMCCalculator mc(*data, *modelConfig);
132  mc.SetConfidenceLevel( confidenceLevel);
133  // special options
134  mc.SetNumBins(200); // bins used internally for representing posterior
135  mc.SetNumBurnInSteps(500); // first N steps to be ignored as burn-in
136  mc.SetNumIters(100000); // how long to run chain
137  mc.SetLeftSideTailFraction(0.5); // for central interval
138  MCMCInterval* mcInt = mc.GetInterval();
139 
140  // for this example we know the expected intervals
141  double expectedLL = data->mean(*x)
142  + ROOT::Math::normal_quantile( (1-confidenceLevel)/2,1)
143  / sqrt(data->numEntries());
144  double expectedUL = data->mean(*x)
145  + ROOT::Math::normal_quantile_c((1-confidenceLevel)/2,1)
146  / sqrt(data->numEntries()) ;
147 
148  // Use the intervals
149  std::cout << "expected interval is [" <<
150  expectedLL << ", " <<
151  expectedUL << "]" << endl;
152 
153  cout << "plc interval is [" <<
154  plInt->LowerLimit(*mu) << ", " <<
155  plInt->UpperLimit(*mu) << "]" << endl;
156 
157  std::cout << "fc interval is ["<<
158  interval->LowerLimit(*mu) << " , " <<
159  interval->UpperLimit(*mu) << "]" << endl;
160 
161  cout << "bc interval is [" <<
162  bcInt->LowerLimit() << ", " <<
163  bcInt->UpperLimit() << "]" << endl;
164 
165  cout << "mc interval is [" <<
166  mcInt->LowerLimit(*mu) << ", " <<
167  mcInt->UpperLimit(*mu) << "]" << endl;
168 
169  mu->setVal(0);
170  cout << "is mu=0 in the interval? " <<
171  plInt->IsInInterval(RooArgSet(*mu)) << endl;
172 
173 
174  // make a reasonable style
178  gStyle->SetPadColor(0);
181  gStyle->SetFillColor(0);
183  gStyle->SetStatColor(0);
184 
185 
186  // some plots
187  TCanvas* canvas = new TCanvas("canvas");
188  canvas->Divide(2,2);
189 
190  // plot the data
191  canvas->cd(1);
192  RooPlot* frame = x->frame();
193  data->plotOn(frame);
194  data->statOn(frame);
195  frame->Draw();
196 
197  // plot the profile likelihood
198  canvas->cd(2);
199  LikelihoodIntervalPlot plot(plInt);
200  plot.Draw();
201 
202  // plot the MCMC interval
203  canvas->cd(3);
204  MCMCIntervalPlot* mcPlot = new MCMCIntervalPlot(*mcInt);
205  mcPlot->SetLineColor(kGreen);
206  mcPlot->SetLineWidth(2);
207  mcPlot->Draw();
208 
209  canvas->cd(4);
210  RooPlot * bcPlot = bc.GetPosteriorPlot();
211  bcPlot->Draw();
212 
213  canvas->Update();
214 
215  t.Stop();
216  t.Print();
217 
218 }
double normal_quantile(double z, double sigma)
Inverse ( ) of the cumulative distribution function of the lower tail of the normal (Gaussian) distri...
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:30
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:568
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
This class provides simple and straightforward utilities to plot a MCMCInterval object.
void SetStatColor(Color_t color=19)
Definition: TStyle.h:367
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:219
LikelihoodInterval is a concrete implementation of the RooStats::ConfInterval interface.
virtual void SetWorkspace(RooWorkspace &ws)
Definition: ModelConfig.h:66
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:406
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
This class provides simple and straightforward utilities to plot a LikelihoodInterval object...
ProfileLikelihoodCalculator is a concrete implementation of CombinedCalculator (the interface class f...
void Draw(const Option_t *options=NULL)
Definition: Rtypes.h:59
virtual void SetObservables(const RooArgSet &set)
specify the observables
Definition: ModelConfig.h:134
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:321
double sqrt(double)
static struct mg_connection * fc(struct mg_context *ctx)
Definition: civetweb.c:3352
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
Double_t x[n]
Definition: legend1.C:17
void SetFrameFillColor(Color_t color=1)
Definition: TStyle.h:349
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:589
virtual void SetPdf(const RooAbsPdf &pdf)
Set the Pdf, add to the the workspace if not already there.
Definition: ModelConfig.h:75
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition: RooRandom.cxx:54
virtual Double_t LowerLimit()
virtual Bool_t IsInInterval(const RooArgSet &) const
check if given point is in the interval
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
virtual void setVal(Double_t value)
Set value of variable to &#39;value&#39;.
Definition: RooRealVar.cxx:204
Double_t LowerLimit(RooRealVar &param)
return lower limit on a given parameter
Double_t UpperLimit(const RooRealVar &param)
return the upper bound of the interval on a given parameter
void SetPadBorderMode(Int_t mode=1)
Definition: TStyle.h:334
void SetCanvasBorderMode(Int_t mode=1)
Definition: TStyle.h:323
void SetPadColor(Color_t color=19)
Definition: TStyle.h:332
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
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.
Double_t mean(RooRealVar &var, const char *cutSpec=0, const char *cutRange=0) const
Definition: RooAbsData.h:177
void SetLineColor(Color_t color)
void SetLineWidth(Int_t width)
virtual Double_t UpperLimit(RooRealVar &param)
get the highest value of param that is within the confidence interval
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
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
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...
The Canvas class.
Definition: TCanvas.h:31
Namespace for the RooStats classes.
Definition: Asimov.h:20
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:159
PointSetInterval is a concrete implementation of the ConfInterval interface.
RooAbsPdf * pdf(const char *name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
virtual Double_t LowerLimit(RooRealVar &param)
get the lowest value of param that is within the confidence interval
virtual void SetPriorPdf(const RooAbsPdf &pdf)
Set the Prior Pdf, add to the the workspace if not already there.
Definition: ModelConfig.h:81
RooRealVar * var(const char *name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found...
RooFactoryWSTool & factory()
Return instance to factory tool.
virtual Double_t UpperLimit()
SimpleInterval is a concrete implementation of the ConfInterval interface.
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1162
void SetTitleFillColor(Color_t color=1)
Definition: TStyle.h:381
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:1725
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 SetParametersOfInterest(const RooArgSet &set)
Definition: ModelConfig.h:93
MCMCInterval is a concrete implementation of the RooStats::ConfInterval interface.
Definition: MCMCInterval.h:30
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2248
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:43
virtual Int_t numEntries() const
Definition: RooAbsData.cxx:285
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:558
Stopwatch class.
Definition: TStopwatch.h:28