00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "RooStats/ConfInterval.h"
00023 #include "RooStats/PointSetInterval.h"
00024 #include "RooStats/ConfidenceBelt.h"
00025 #include "RooStats/FeldmanCousins.h"
00026 #include "RooStats/ProfileLikelihoodCalculator.h"
00027 #include "RooStats/MCMCCalculator.h"
00028 #include "RooStats/BayesianCalculator.h"
00029 #include "RooStats/MCMCIntervalPlot.h"
00030 #include "RooStats/LikelihoodIntervalPlot.h"
00031
00032 #include "RooStats/ProofConfig.h"
00033 #include "RooStats/ToyMCSampler.h"
00034
00035 #include "RooRandom.h"
00036 #include "RooDataSet.h"
00037 #include "RooRealVar.h"
00038 #include "RooConstVar.h"
00039 #include "RooAddition.h"
00040 #include "RooDataHist.h"
00041 #include "RooPoisson.h"
00042 #include "RooPlot.h"
00043
00044 #include "TCanvas.h"
00045 #include "TTree.h"
00046 #include "TStyle.h"
00047 #include "TMath.h"
00048 #include"Math/DistFunc.h"
00049 #include "TH1F.h"
00050 #include "TMarker.h"
00051 #include "TStopwatch.h"
00052
00053 #include <iostream>
00054
00055
00056 using namespace RooFit ;
00057 using namespace RooStats ;
00058
00059
00060 void IntervalExamples()
00061 {
00062
00063
00064 TStopwatch t;
00065 t.Start();
00066
00067
00068
00069 RooRandom::randomGenerator()->SetSeed(3001);
00070
00071
00072 RooWorkspace* wspace = new RooWorkspace();
00073 wspace->factory("Gaussian::normal(x[-10,10],mu[-1,1],sigma[1])");
00074 wspace->defineSet("poi","mu");
00075 wspace->defineSet("obs","x");
00076
00077
00078 ModelConfig* modelConfig = new ModelConfig("Example G(x|mu,1)");
00079 modelConfig->SetWorkspace(*wspace);
00080 modelConfig->SetPdf( *wspace->pdf("normal") );
00081 modelConfig->SetParametersOfInterest( *wspace->set("poi") );
00082 modelConfig->SetObservables( *wspace->set("obs") );
00083
00084
00085 RooDataSet* data = wspace->pdf("normal")->generate(*wspace->set("obs"),100);
00086 data->Print();
00087
00088
00089 RooRealVar* x = wspace->var("x");
00090 RooRealVar* mu = wspace->var("mu");
00091
00092
00093 double confidenceLevel = 0.95;
00094
00095
00096 ProfileLikelihoodCalculator plc(*data, *modelConfig);
00097 plc.SetConfidenceLevel( confidenceLevel);
00098 LikelihoodInterval* plInt = plc.GetInterval();
00099
00100
00101 FeldmanCousins fc(*data, *modelConfig);
00102 fc.SetConfidenceLevel( confidenceLevel);
00103 fc.SetNBins(100);
00104 fc.UseAdaptiveSampling(true);
00105
00106
00107
00108 fc.FluctuateNumDataEntries(false);
00109
00110
00111
00112
00113
00114
00115
00116 PointSetInterval* interval = (PointSetInterval*) fc.GetInterval();
00117
00118
00119
00120
00121 wspace->factory("Uniform::prior(mu)");
00122 modelConfig->SetPriorPdf(*wspace->pdf("prior"));
00123
00124
00125 BayesianCalculator bc(*data, *modelConfig);
00126 bc.SetConfidenceLevel( confidenceLevel);
00127 SimpleInterval* bcInt = bc.GetInterval();
00128
00129
00130 MCMCCalculator mc(*data, *modelConfig);
00131 mc.SetConfidenceLevel( confidenceLevel);
00132
00133 mc.SetNumBins(200);
00134 mc.SetNumBurnInSteps(500);
00135 mc.SetNumIters(100000);
00136 mc.SetLeftSideTailFraction(0.5);
00137 MCMCInterval* mcInt = mc.GetInterval();
00138
00139
00140 double expectedLL = data->mean(*x)
00141 + ROOT::Math::normal_quantile( (1-confidenceLevel)/2,1)
00142 / sqrt(data->numEntries());
00143 double expectedUL = data->mean(*x)
00144 + ROOT::Math::normal_quantile_c((1-confidenceLevel)/2,1)
00145 / sqrt(data->numEntries()) ;
00146
00147
00148 std::cout << "expected interval is [" <<
00149 expectedLL << ", " <<
00150 expectedUL << "]" << endl;
00151
00152 cout << "plc interval is [" <<
00153 plInt->LowerLimit(*mu) << ", " <<
00154 plInt->UpperLimit(*mu) << "]" << endl;
00155
00156 std::cout << "fc interval is ["<<
00157 interval->LowerLimit(*mu) << " , " <<
00158 interval->UpperLimit(*mu) << "]" << endl;
00159
00160 cout << "bc interval is [" <<
00161 bcInt->LowerLimit() << ", " <<
00162 bcInt->UpperLimit() << "]" << endl;
00163
00164 cout << "mc interval is [" <<
00165 mcInt->LowerLimit(*mu) << ", " <<
00166 mcInt->UpperLimit(*mu) << "]" << endl;
00167
00168 mu->setVal(0);
00169 cout << "is mu=0 in the interval? " <<
00170 plInt->IsInInterval(RooArgSet(*mu)) << endl;
00171
00172
00173
00174 gStyle->SetCanvasColor(0);
00175 gStyle->SetCanvasBorderMode(0);
00176 gStyle->SetPadBorderMode(0);
00177 gStyle->SetPadColor(0);
00178 gStyle->SetCanvasColor(0);
00179 gStyle->SetTitleFillColor(0);
00180 gStyle->SetFillColor(0);
00181 gStyle->SetFrameFillColor(0);
00182 gStyle->SetStatColor(0);
00183
00184
00185
00186 TCanvas* canvas = new TCanvas("canvas");
00187 canvas->Divide(2,2);
00188
00189
00190 canvas->cd(1);
00191 RooPlot* frame = x->frame();
00192 data->plotOn(frame);
00193 data->statOn(frame);
00194 frame->Draw();
00195
00196
00197 canvas->cd(2);
00198 LikelihoodIntervalPlot plot(plInt);
00199 plot.Draw();
00200
00201
00202 canvas->cd(3);
00203 MCMCIntervalPlot* mcPlot = new MCMCIntervalPlot(*mcInt);
00204 mcPlot->SetLineColor(kGreen);
00205 mcPlot->SetLineWidth(2);
00206 mcPlot->Draw();
00207
00208 canvas->cd(4);
00209 RooPlot * bcPlot = bc.GetPosteriorPlot();
00210 bcPlot->Draw();
00211
00212 canvas->Update();
00213
00214 t.Stop();
00215 t.Print();
00216
00217 }