IntervalExamples.C

Go to the documentation of this file.
00001 // Example showing confidence intervals with four techniques.
00002 /*
00003 IntervalExamples
00004 
00005 Author Kyle Cranmer 
00006 date   Sep. 2010
00007 
00008 An example that shows confidence intervals with four techniques.
00009 The model is a Normal Gaussian G(x|mu,sigma) with 100 samples of x.
00010 The answer is known analytically, so this is a good example to validate
00011 the RooStats tools.
00012 
00013 expected interval is [-0.162917, 0.229075]
00014 plc  interval is     [-0.162917, 0.229075]
00015 fc   interval is     [-0.17    , 0.23]        // stepsize is 0.01
00016 bc   interval is     [-0.162918, 0.229076]
00017 mcmc interval is     [-0.166999, 0.230224]
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 // use this order for safety on library loading
00056 using namespace RooFit ;
00057 using namespace RooStats ;
00058 
00059 
00060 void IntervalExamples()
00061 {
00062   
00063   // Time this macro
00064   TStopwatch t;
00065   t.Start();
00066 
00067 
00068   // set RooFit random seed for reproducible results
00069   RooRandom::randomGenerator()->SetSeed(3001);
00070 
00071   // make a simple model via the workspace factory
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   // specify components of model for statistical tools
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   // create a toy dataset
00085   RooDataSet* data = wspace->pdf("normal")->generate(*wspace->set("obs"),100);
00086   data->Print();
00087 
00088   // for convenience later on 
00089   RooRealVar* x = wspace->var("x");
00090   RooRealVar* mu = wspace->var("mu");
00091 
00092   // set confidence level
00093   double confidenceLevel = 0.95;
00094 
00095   // example use profile likelihood calculator
00096   ProfileLikelihoodCalculator plc(*data, *modelConfig);
00097   plc.SetConfidenceLevel( confidenceLevel);
00098   LikelihoodInterval* plInt = plc.GetInterval();
00099 
00100   // example use of Feldman-Cousins
00101   FeldmanCousins fc(*data, *modelConfig); 
00102   fc.SetConfidenceLevel( confidenceLevel);
00103   fc.SetNBins(100); // number of points to test per parameter
00104   fc.UseAdaptiveSampling(true); // make it go faster
00105 
00106   // Here, we consider only ensembles with 100 events
00107   // The PDF could be extended and this could be removed
00108   fc.FluctuateNumDataEntries(false); 
00109 
00110   // Proof
00111   //  ProofConfig pc(*wspace, 4, "workers=4", kFALSE);    // proof-lite
00112   //ProofConfig pc(w, 8, "localhost");    // proof cluster at "localhost"
00113   //  ToyMCSampler* toymcsampler = (ToyMCSampler*) fc.GetTestStatSampler();
00114   //  toymcsampler->SetProofConfig(&pc);     // enable proof
00115 
00116   PointSetInterval* interval = (PointSetInterval*) fc.GetInterval();
00117 
00118 
00119   // example use of BayesianCalculator
00120   // now we also need to specify a prior in the ModelConfig
00121   wspace->factory("Uniform::prior(mu)");
00122   modelConfig->SetPriorPdf(*wspace->pdf("prior"));
00123 
00124   // example usage of BayesianCalculator
00125   BayesianCalculator bc(*data, *modelConfig);
00126   bc.SetConfidenceLevel( confidenceLevel);
00127   SimpleInterval* bcInt = bc.GetInterval();
00128 
00129   // example use of MCMCInterval
00130   MCMCCalculator mc(*data, *modelConfig);
00131   mc.SetConfidenceLevel( confidenceLevel);
00132   // special options
00133   mc.SetNumBins(200);     // bins used internally for representing posterior
00134   mc.SetNumBurnInSteps(500); // first N steps to be ignored as burn-in
00135   mc.SetNumIters(100000);    // how long to run chain
00136   mc.SetLeftSideTailFraction(0.5); // for central interval
00137   MCMCInterval* mcInt = mc.GetInterval();
00138   
00139   // for this example we know the expected intervals
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   // Use the intervals
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   // make a reasonable style 
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   // some plots
00186   TCanvas* canvas = new TCanvas("canvas");
00187   canvas->Divide(2,2);
00188 
00189   // plot the data
00190   canvas->cd(1);
00191   RooPlot* frame = x->frame();
00192   data->plotOn(frame);
00193   data->statOn(frame);
00194   frame->Draw();
00195 
00196   // plot the profile likeihood
00197   canvas->cd(2);
00198   LikelihoodIntervalPlot plot(plInt);
00199   plot.Draw();
00200 
00201   // plot the MCMC interval
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 }

Generated on Tue Jul 5 15:45:09 2011 for ROOT_528-00b_version by  doxygen 1.5.1