Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 void IntervalExamples()
61 {
62 
63  // Time this macro
64  TStopwatch t;
65  t.Start();
66 
67  // set RooFit random seed for reproducible results
68  RooRandom::randomGenerator()->SetSeed(3001);
69 
70  // make a simple model via the workspace factory
71  RooWorkspace *wspace = new RooWorkspace();
72  wspace->factory("Gaussian::normal(x[-10,10],mu[-1,1],sigma[1])");
73  wspace->defineSet("poi", "mu");
74  wspace->defineSet("obs", "x");
75 
76  // specify components of model for statistical tools
77  ModelConfig *modelConfig = new ModelConfig("Example G(x|mu,1)");
78  modelConfig->SetWorkspace(*wspace);
79  modelConfig->SetPdf(*wspace->pdf("normal"));
80  modelConfig->SetParametersOfInterest(*wspace->set("poi"));
81  modelConfig->SetObservables(*wspace->set("obs"));
82 
83  // create a toy dataset
84  RooDataSet *data = wspace->pdf("normal")->generate(*wspace->set("obs"), 100);
85  data->Print();
86 
87  // for convenience later on
88  RooRealVar *x = wspace->var("x");
89  RooRealVar *mu = wspace->var("mu");
90 
91  // set confidence level
92  double confidenceLevel = 0.95;
93 
94  // example use profile likelihood calculator
95  ProfileLikelihoodCalculator plc(*data, *modelConfig);
96  plc.SetConfidenceLevel(confidenceLevel);
97  LikelihoodInterval *plInt = plc.GetInterval();
98 
99  // example use of Feldman-Cousins
100  FeldmanCousins fc(*data, *modelConfig);
101  fc.SetConfidenceLevel(confidenceLevel);
102  fc.SetNBins(100); // number of points to test per parameter
103  fc.UseAdaptiveSampling(true); // make it go faster
104 
105  // Here, we consider only ensembles with 100 events
106  // The PDF could be extended and this could be removed
107  fc.FluctuateNumDataEntries(false);
108 
109  // Proof
110  // ProofConfig pc(*wspace, 4, "workers=4", kFALSE); // proof-lite
111  // ProofConfig pc(w, 8, "localhost"); // proof cluster at "localhost"
112  // ToyMCSampler* toymcsampler = (ToyMCSampler*) fc.GetTestStatSampler();
113  // toymcsampler->SetProofConfig(&pc); // enable proof
114 
115  PointSetInterval *interval = (PointSetInterval *)fc.GetInterval();
116 
117  // example use of BayesianCalculator
118  // now we also need to specify a prior in the ModelConfig
119  wspace->factory("Uniform::prior(mu)");
120  modelConfig->SetPriorPdf(*wspace->pdf("prior"));
121 
122  // example usage of BayesianCalculator
123  BayesianCalculator bc(*data, *modelConfig);
124  bc.SetConfidenceLevel(confidenceLevel);
125  SimpleInterval *bcInt = bc.GetInterval();
126 
127  // example use of MCMCInterval
128  MCMCCalculator mc(*data, *modelConfig);
129  mc.SetConfidenceLevel(confidenceLevel);
130  // special options
131  mc.SetNumBins(200); // bins used internally for representing posterior
132  mc.SetNumBurnInSteps(500); // first N steps to be ignored as burn-in
133  mc.SetNumIters(100000); // how long to run chain
134  mc.SetLeftSideTailFraction(0.5); // for central interval
135  MCMCInterval *mcInt = mc.GetInterval();
136 
137  // for this example we know the expected intervals
138  double expectedLL =
139  data->mean(*x) + ROOT::Math::normal_quantile((1 - confidenceLevel) / 2, 1) / sqrt(data->numEntries());
140  double expectedUL =
141  data->mean(*x) + ROOT::Math::normal_quantile_c((1 - confidenceLevel) / 2, 1) / sqrt(data->numEntries());
142 
143  // Use the intervals
144  std::cout << "expected interval is [" << expectedLL << ", " << expectedUL << "]" << endl;
145 
146  cout << "plc interval is [" << plInt->LowerLimit(*mu) << ", " << plInt->UpperLimit(*mu) << "]" << endl;
147 
148  std::cout << "fc interval is [" << interval->LowerLimit(*mu) << " , " << interval->UpperLimit(*mu) << "]" << endl;
149 
150  cout << "bc interval is [" << bcInt->LowerLimit() << ", " << bcInt->UpperLimit() << "]" << endl;
151 
152  cout << "mc interval is [" << mcInt->LowerLimit(*mu) << ", " << mcInt->UpperLimit(*mu) << "]" << endl;
153 
154  mu->setVal(0);
155  cout << "is mu=0 in the interval? " << plInt->IsInInterval(RooArgSet(*mu)) << endl;
156 
157  // make a reasonable style
158  gStyle->SetCanvasColor(0);
159  gStyle->SetCanvasBorderMode(0);
160  gStyle->SetPadBorderMode(0);
161  gStyle->SetPadColor(0);
162  gStyle->SetCanvasColor(0);
163  gStyle->SetTitleFillColor(0);
164  gStyle->SetFillColor(0);
165  gStyle->SetFrameFillColor(0);
166  gStyle->SetStatColor(0);
167 
168  // some plots
169  TCanvas *canvas = new TCanvas("canvas");
170  canvas->Divide(2, 2);
171 
172  // plot the data
173  canvas->cd(1);
174  RooPlot *frame = x->frame();
175  data->plotOn(frame);
176  data->statOn(frame);
177  frame->Draw();
178 
179  // plot the profile likelihood
180  canvas->cd(2);
181  LikelihoodIntervalPlot plot(plInt);
182  plot.Draw();
183 
184  // plot the MCMC interval
185  canvas->cd(3);
186  MCMCIntervalPlot *mcPlot = new MCMCIntervalPlot(*mcInt);
187  mcPlot->SetLineColor(kGreen);
188  mcPlot->SetLineWidth(2);
189  mcPlot->Draw();
190 
191  canvas->cd(4);
192  RooPlot *bcPlot = bc.GetPosteriorPlot();
193  bcPlot->Draw();
194 
195  canvas->Update();
196 
197  t.Stop();
198  t.Print();
199 }