FeldmanCousins.cxx

Go to the documentation of this file.
00001 // @(#)root/roostats:$Id: FeldmanCousins.cxx 38105 2011-02-16 21:00:08Z cranmer $
00002 // Author: Kyle Cranmer   January 2009
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //_________________________________________________
00013 /*
00014 BEGIN_HTML
00015 <p>
00016 The FeldmanCousins class (like the Feldman-Cousins technique) is essentially a specific configuration
00017  of the more general NeymanConstruction.  It is a concrete implementation of the IntervalCalculator interface that, which uses the NeymanConstruction in a particular way.  As the name suggests, it returns a ConfidenceInterval.  In particular, it produces a RooStats::PointSetInterval, which is a concrete implementation of the ConfInterval interface.  
00018 </p>
00019 <p>
00020 The Neyman Construction is not a uniquely defined statistical technique, it requires that one specify an ordering rule 
00021 or ordering principle, which is usually incoded by choosing a specific test statistic and limits of integration 
00022 (corresponding to upper/lower/central limits).  As a result, this class must be configured with the corresponding
00023 information before it can produce an interval.  
00024 </p>
00025 <p>In the case of the Feldman-Cousins approach, the ordering principle is the likelihood ratio -- motivated
00026 by the Neyman-Pearson lemma.  When nuisance parameters are involved, the profile likelihood ratio is the natural generalization.  One may either choose to perform the construction over the full space of the nuisance parameters, or restrict the nusiance parameters to their conditional MLE (eg. profiled values). 
00027 </p>
00028 END_HTML
00029 */
00030 //
00031 
00032 #ifndef RooStats_FeldmanCousins
00033 #include "RooStats/FeldmanCousins.h"
00034 #endif
00035 
00036 #ifndef RooStats_RooStatsUtils
00037 #include "RooStats/RooStatsUtils.h"
00038 #endif
00039 
00040 #ifndef RooStats_PointSetInterval
00041 #include "RooStats/PointSetInterval.h"
00042 #endif
00043 
00044 #include "RooStats/ModelConfig.h"
00045 
00046 #include "RooStats/SamplingDistribution.h"
00047 
00048 #include "RooStats/ProfileLikelihoodTestStat.h"
00049 #include "RooStats/NeymanConstruction.h"
00050 #include "RooStats/RooStatsUtils.h"
00051 
00052 #include "RooDataSet.h"
00053 #include "RooDataHist.h"
00054 #include "RooGlobalFunc.h"
00055 #include "RooMsgService.h"
00056 #include "TFile.h"
00057 #include "TTree.h"
00058 
00059 ClassImp(RooStats::FeldmanCousins) ;
00060 
00061 using namespace RooFit;
00062 using namespace RooStats;
00063 
00064 
00065 /*
00066 //_______________________________________________________
00067 FeldmanCousins::FeldmanCousins() : 
00068   //  fModel(NULL),
00069    fData(0),
00070    fTestStatSampler(0),
00071    fPointsToTest(0),
00072    fAdaptiveSampling(false), 
00073    fNbins(10), 
00074    fFluctuateData(true),
00075    fDoProfileConstruction(true),
00076    fSaveBeltToFile(false),
00077    fCreateBelt(false)
00078 {
00079    // default constructor
00080 //   fWS = new RooWorkspace("FeldmanCousinsWS");
00081 //   fOwnsWorkspace = true;
00082 //   fDataName = "";
00083 //   fPdfName = "";
00084 }
00085 */
00086 
00087 //_______________________________________________________
00088 FeldmanCousins::FeldmanCousins(RooAbsData& data, ModelConfig& model) : 
00089   fSize(0.05), 
00090   fModel(model),
00091   fData(data),
00092   fTestStatSampler(0),
00093   fPointsToTest(0),
00094   fPOIToTest(0),
00095   fConfBelt(0),
00096   fAdaptiveSampling(false), 
00097   fAdditionalNToysFactor(1.),
00098   fNbins(10), 
00099   fFluctuateData(true),
00100   fDoProfileConstruction(true),
00101   fSaveBeltToFile(false),
00102   fCreateBelt(false)
00103 {
00104    // standard constructor
00105 }
00106 
00107 //_______________________________________________________
00108 FeldmanCousins::~FeldmanCousins() {
00109    // destructor
00110    //if(fOwnsWorkspace && fWS) delete fWS;
00111   if(fPointsToTest) delete fPointsToTest;
00112   if(fPOIToTest) delete fPOIToTest;
00113   if(fTestStatSampler) delete fTestStatSampler;
00114 }
00115 
00116 
00117 //_______________________________________________________
00118 void FeldmanCousins::SetModel(const ModelConfig & model) { 
00119    // set the model
00120   fModel = model;
00121 }
00122 
00123 //_______________________________________________________
00124 TestStatSampler*  FeldmanCousins::GetTestStatSampler() const{
00125   if(!fTestStatSampler)
00126     this->CreateTestStatSampler();
00127   return fTestStatSampler; 
00128 }
00129 
00130 //_______________________________________________________
00131 void FeldmanCousins::CreateTestStatSampler() const{
00132   // specify the Test Statistic and create a ToyMC test statistic sampler
00133 
00134   // use the profile likelihood ratio as the test statistic
00135   ProfileLikelihoodTestStat* testStatistic = new ProfileLikelihoodTestStat(*fModel.GetPdf());
00136   
00137   // create the ToyMC test statistic sampler
00138   fTestStatSampler = new ToyMCSampler(*testStatistic,int(fAdditionalNToysFactor*50./fSize)) ;
00139   fTestStatSampler->SetParametersForTestStat(*fModel.GetParametersOfInterest() );
00140   if(fModel.GetObservables())
00141     fTestStatSampler->SetObservables(*fModel.GetObservables());
00142   fTestStatSampler->SetPdf(*fModel.GetPdf());
00143   
00144   if(!fAdaptiveSampling){
00145     ooccoutP(&fModel,Generation) << "FeldmanCousins: ntoys per point = " << (int) (fAdditionalNToysFactor*50./fSize) << endl;
00146   } else{
00147     ooccoutP(&fModel,Generation) << "FeldmanCousins: ntoys per point: adaptive" << endl;
00148   }
00149   if(fFluctuateData){
00150     ooccoutP(&fModel,Generation) << "FeldmanCousins: nEvents per toy will fluctuate about  expectation" << endl;
00151   } else{
00152     ooccoutP(&fModel,Generation) << "FeldmanCousins: nEvents per toy will not fluctuate, will always be " << fData.numEntries() << endl;
00153     fTestStatSampler->SetNEventsPerToy(fData.numEntries());
00154   }
00155 }
00156 
00157 
00158 //_______________________________________________________
00159 void FeldmanCousins::CreateParameterPoints() const{
00160   // specify the parameter points to perform the construction.
00161   // allow ability to profile on some nuisance paramters
00162 
00163   // get ingredients
00164   RooAbsPdf* pdf   = fModel.GetPdf(); 
00165   if (!pdf ){
00166     ooccoutE(&fModel,Generation) << "FeldmanCousins: ModelConfig has no PDF" << endl;
00167     return;
00168   }
00169 
00170   // get list of all paramters
00171   RooArgSet* parameters = new RooArgSet(*fModel.GetParametersOfInterest());
00172   if(fModel.GetNuisanceParameters())
00173     parameters->add(*fModel.GetNuisanceParameters());
00174   
00175   
00176   if( fModel.GetNuisanceParameters() && ! fModel.GetParametersOfInterest()->equals(*parameters) && fDoProfileConstruction) {
00177     // if parameters include nuisance parameters, do profile construction
00178     ooccoutP(&fModel,Generation) << "FeldmanCousins: Model has nuisance parameters, will do profile construction" << endl;
00179     
00180     // set nbins for the POI
00181     TIter it2 = fModel.GetParametersOfInterest()->createIterator();
00182     RooRealVar *myarg2; 
00183     while ((myarg2 = dynamic_cast<RooRealVar*>(it2.Next()))) { 
00184       myarg2->setBins(fNbins);
00185     }
00186     
00187     // get dataset for POI scan
00188     //     RooDataHist* parameterScan = NULL;
00189     RooAbsData* parameterScan = NULL;
00190     if(fPOIToTest)
00191       parameterScan = fPOIToTest;
00192     else
00193       parameterScan = new RooDataHist("parameterScan", "", *fModel.GetParametersOfInterest());
00194 
00195 
00196     ooccoutP(&fModel,Generation) << "FeldmanCousins: # points to test = " << parameterScan->numEntries() << endl;
00197     // make profile construction
00198     RooFit::MsgLevel previous  = RooMsgService::instance().globalKillBelow();
00199     RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL) ;
00200     RooAbsReal* nll = pdf->createNLL(fData,RooFit::CloneData(false));
00201     RooAbsReal* profile = nll->createProfile(*fModel.GetParametersOfInterest());
00202     
00203     RooDataSet* profileConstructionPoints = new RooDataSet("profileConstruction",
00204                                                            "profileConstruction",
00205                                                            *parameters);
00206     
00207     
00208     for(Int_t i=0; i<parameterScan->numEntries(); ++i){
00209       // here's where we figure out the profiled value of nuisance parameters
00210       *parameters = *parameterScan->get(i);
00211       profile->getVal();
00212       profileConstructionPoints->add(*parameters);
00213     }   
00214     RooMsgService::instance().setGlobalKillBelow(previous) ;
00215     delete profile; 
00216     delete nll;
00217     if(!fPOIToTest) delete parameterScan;
00218 
00219     // done
00220     fPointsToTest = profileConstructionPoints;
00221 
00222   } else{
00223     // Do full construction
00224     ooccoutP(&fModel,Generation) << "FeldmanCousins: Model has no nuisance parameters" << endl;
00225 
00226     TIter it = parameters->createIterator();
00227     RooRealVar *myarg; 
00228     while ((myarg = dynamic_cast<RooRealVar*>(it.Next()))) { 
00229       myarg->setBins(fNbins);
00230     }
00231 
00232     RooDataHist* parameterScan = new RooDataHist("parameterScan", "", *parameters);
00233     ooccoutP(&fModel,Generation) << "FeldmanCousins: # points to test = " << parameterScan->numEntries() << endl;
00234     
00235     fPointsToTest = parameterScan;
00236   }
00237   
00238   delete parameters;
00239   
00240 }
00241 
00242 
00243 //_______________________________________________________
00244 PointSetInterval* FeldmanCousins::GetInterval() const {
00245   // Main interface to get a RooStats::ConfInterval.  
00246   // It constructs a RooStats::PointSetInterval.
00247 
00248   // local variables
00249   //  RooAbsData* data = fData; //fWS->data(fDataName);
00250 
00251   // fill in implied variables given data
00252   fModel.GuessObsAndNuisance(fData);
00253   
00254   // create the test statistic sampler (private data member fTestStatSampler)
00255   if(!fTestStatSampler)
00256     this->CreateTestStatSampler();
00257 
00258   fTestStatSampler->SetObservables(*fModel.GetObservables());
00259 
00260   if(!fFluctuateData)
00261     fTestStatSampler->SetNEventsPerToy(fData.numEntries());
00262 
00263   // create paramter points to perform construction (private data member fPointsToTest)
00264   this->CreateParameterPoints();
00265 
00266   // Create a Neyman Construction
00267   NeymanConstruction nc(fData,fModel);
00268   // configure it
00269   nc.SetTestStatSampler(*fTestStatSampler);
00270   nc.SetTestSize(fSize); // set size of test
00271   //  nc.SetParameters( fModel.GetParametersOfInterest);
00272   nc.SetParameterPointsToTest( *fPointsToTest );
00273   nc.SetLeftSideTailFraction(0.); // part of definition of Feldman-Cousins
00274   nc.SetData(fData);
00275   nc.UseAdaptiveSampling(fAdaptiveSampling);
00276   nc.AdditionalNToysFactor(fAdditionalNToysFactor);
00277   nc.SaveBeltToFile(fSaveBeltToFile);
00278   nc.CreateConfBelt(fCreateBelt);
00279   fConfBelt = nc.GetConfidenceBelt();
00280   // use it
00281   return nc.GetInterval();
00282 }

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