12 #include "BATCalculator.h" 13 #include "BCRooInterface.h" 15 #include "../../BAT/BCLog.h" 16 #include "../../BAT/BCAux.h" 17 #include "../../BAT/BCH1D.h" 19 #include <RooAbsFunc.h> 20 #include <RooRealVar.h> 21 #include <RooBrentRootFinder.h> 22 #include <RooFormulaVar.h> 23 #include <RooGenericPdf.h> 24 #include <RooProdPdf.h> 25 #include <RooDataHist.h> 26 #include <RooHistPdf.h> 27 #include <RooStats/MarkovChain.h> 42 bool sortbyposterior(std::pair< Int_t, Double_t > pair1, std::pair< Int_t, Double_t > pair2)
44 return pair1.second > pair2.second;
61 fIntegratedLikelihood(0),
65 fBrfPrecision(0.00005),
66 fValidInterval(false),
67 fValidMCMCInterval(false),
68 fConnectedInterval(false),
71 fLeftSideFraction(0.5)
94 fIntegratedLikelihood(0),
98 fBrfPrecision(0.00005),
99 fValidInterval(
false),
100 fValidMCMCInterval(
false),
101 fConnectedInterval(
false),
104 fLeftSideFraction(0.5)
106 _myRooInterface =
new BCRooInterface(
"BCRooInterfaceForBAT", fillChain);
112 fPdf(model.GetPdf()),
113 fPOI(*model.GetParametersOfInterest()),
114 fPrior(model.GetPriorPdf()),
115 fparams(model.GetNuisanceParameters()),
120 fIntegratedLikelihood(0),
124 fBrfPrecision(0.00005),
125 fValidInterval(
false),
126 fValidMCMCInterval(
false),
127 fConnectedInterval(
false),
130 fLeftSideFraction(0.5)
134 _myRooInterface =
new BCRooInterface(
"BCRooInterfaceForBAT", fillChain);
142 delete _myRooInterface;
146 void BATCalculator::ClearAll()
const 155 if (fIntegratedLikelihood)
156 delete fIntegratedLikelihood;
158 delete fPosteriorPdf;
163 fIntegratedLikelihood = 0;
166 fValidInterval =
false;
167 fValidMCMCInterval =
false;
174 const char* POIname = fPOI.first()->GetName();
184 std::cerr <<
"BATCalculator::GetPosteriorPdf - missing pdf model" << std::endl;
189 std::cerr <<
"BATCalculator::GetPosteriorPdf - missing prior pdf" << std::endl;
192 if (fPOI.getSize() == 0) {
193 std::cerr <<
"BATCalculator::GetPosteriorPdf - missing parameter of interest" << std::endl;
197 if (fPOI.getSize() > 1) {
198 std::cerr <<
"BATCalculator::GetPosteriorPdf - current implementation works only on 1D intervals" << std::endl;
203 _myRooInterface->Initialize(*fData, *fPdf, *fPrior, fparams, fPOI);
210 _posteriorTH1D =
static_cast<TH1D*
>(
BCAux::OwnClone(posteriorTH1D,
"_posteriorTH1D"));
211 RooDataHist* posteriorRooDataHist =
new RooDataHist(
"posteriorhist",
"", fPOI, posteriorTH1D);
212 fPosteriorPdf =
new RooHistPdf(
"posteriorPdf",
"", fPOI, *posteriorRooDataHist);
214 return fPosteriorPdf;
219 RooPlot* BATCalculator::GetPosteriorPlot1D()
const 221 if (!fPosteriorPdf) {
222 std::cout <<
"BATCalculator::GetPosteriorPlot1D:" 223 <<
"Warning : posterior not available" << std::endl;
226 if ((!fValidInterval) && (!fValidMCMCInterval)) {
227 std::cout <<
"BATCalculator::GetPosteriorPlot1D:" 228 <<
"Warning : interval not available" << std::endl;
232 RooAbsRealLValue* poi =
dynamic_cast<RooAbsRealLValue*
>( fPOI.first() );
235 RooPlot* plot = poi->frame();
237 plot->SetTitle(TString(
"Posterior probability of parameter \"") + TString(poi->GetName()) + TString(
"\""));
238 fPosteriorPdf->plotOn(plot, RooFit::Range(fLower, fUpper, kFALSE), RooFit::VLines(), RooFit::DrawOption(
"F"), RooFit::MoveToBack(), RooFit::FillColor(kGray));
239 fPosteriorPdf->plotOn(plot);
240 plot->GetYaxis()->SetTitle(
"posterior probability");
249 const char* POIname = fPOI.first()->GetName();
261 std::cout <<
"BATCalculator::GetShortestInterval1D:" 262 <<
"Warning : recomputing interval for the same CL and same model" << std::endl;
265 RooRealVar* poi =
dynamic_cast<RooRealVar*
>( fPOI.find(POIname) );
274 Double_t minpoi = poi->getMin();
275 Double_t maxpoi = poi->getMax();
278 Int_t stepnumber = _posteriorTH1D->GetNbinsX();
281 Double_t stepsize = (maxpoi - minpoi) / stepnumber;
284 std::vector< std::pair< Int_t, Double_t > > posteriorVector;
287 Double_t histoIntegral = 0;
289 posteriorVector.resize(stepnumber);
293 std::vector< std::pair< Int_t, Double_t > >::iterator vecit = posteriorVector.begin();
294 std::vector< std::pair< Int_t, Double_t > >::iterator vecit_end = posteriorVector.end();
295 for ( ; vecit != vecit_end ; ++vecit) {
296 poi->setVal(poi->getMin() + i * stepsize);
297 posteriorVector[i] = std::make_pair(i, _posteriorTH1D->GetBinContent(i + 1) );
298 histoIntegral += _posteriorTH1D->GetBinContent(i);
303 double upperProbLim = 1. - ( (1. -
ConfidenceLevel()) * (1. - fLeftSideFraction) );
307 bool lowerlimset =
false;
308 bool upperlimset =
false;
311 if (fLeftSideFraction == 0) {
316 if (fLeftSideFraction == 1) {
322 Double_t integratedposterior = 0.;
325 vecit = posteriorVector.begin();
326 for ( ; vecit != vecit_end ; ++vecit) {
327 integratedposterior += posteriorVector[i].second;
328 if ( (lowerlimset !=
true) && ((integratedposterior / histoIntegral) >= lowerProbLim) ) {
329 fLower = poi->getMin() + i * stepsize;
332 if ( (upperlimset !=
true) && ((integratedposterior / histoIntegral) >= upperProbLim) ) {
333 fUpper = poi->getMin() + i * stepsize;
340 TString interval_name = TString(
"CentralBayesianInterval_a") + TString(this->GetName());
341 SimpleInterval* interval =
new SimpleInterval(interval_name, *poi, fLower, fUpper,
ConfidenceLevel());
342 interval->SetTitle(
"SimpleInterval from BATCalculator");
351 const char* POIname = fPOI.first()->GetName();
352 bool checkConnected =
true;
367 std::cout <<
"BATCalculator::GetShortestInterval1D:" 368 <<
"Warning : recomputing interval for the same CL and same model" << std::endl;
371 RooRealVar* poi =
dynamic_cast<RooRealVar*
>( fPOI.find(POIname) );
379 Double_t minpoi = poi->getMin();
380 Double_t maxpoi = poi->getMax();
383 Int_t stepnumber = _posteriorTH1D->GetNbinsX();
386 Double_t stepsize = (maxpoi - minpoi) / stepnumber;
389 std::vector< std::pair< Int_t, Double_t > > posteriorVector;
392 Double_t histoIntegral = 0;
394 posteriorVector.resize(stepnumber);
397 Double_t tmpVal = poi->getVal();
401 std::vector< std::pair< Int_t, Double_t > >::iterator vecit = posteriorVector.begin();
402 std::vector< std::pair< Int_t, Double_t > >::iterator vecit_end = posteriorVector.end();
403 for ( ; vecit != vecit_end ; ++vecit) {
404 poi->setVal(poi->getMin() + i * stepsize);
405 posteriorVector[i] = std::make_pair(i, _posteriorTH1D->GetBinContent(i + 1) );
406 histoIntegral += _posteriorTH1D->GetBinContent(i);
411 std::sort(posteriorVector.begin(), posteriorVector.end(), ::sortbyposterior);
414 Double_t integratedposterior = 0.;
417 Double_t lowerLim = posteriorVector.size();
418 Double_t upperLim = 0;
421 std::vector<bool> inInterval;
422 inInterval.resize(posteriorVector.size());
425 for (
unsigned int k = 0; k < inInterval.size(); k++)
426 inInterval[k] =
false;
430 while (((integratedposterior / histoIntegral) < (1 - fSize)) && (j < posteriorVector.size())) {
431 integratedposterior += posteriorVector[j].second;
434 inInterval[posteriorVector[j].first] =
true;
436 if (posteriorVector[j].first < lowerLim) {
437 lowerLim = posteriorVector[j].first;
439 if (posteriorVector[j].first > upperLim) {
440 upperLim = posteriorVector[j].first;
443 fLower = lowerLim * stepsize;
444 fUpper = upperLim * stepsize;
450 bool runInside =
false;
451 for (
unsigned int l = 0; l < inInterval.size(); l++) {
452 if ( (runInside ==
false) && (inInterval[l] ==
true) ) {
453 _intervalBorders1D.push_back(static_cast<double>(l)* stepsize);
456 if ( ( runInside ==
true) && (l < (inInterval.size() - 1) ) && (inInterval[l + 1] ==
false) ) {
457 _intervalBorders1D.push_back(static_cast<double>(l)* stepsize);
460 if ( ( runInside ==
true) && (l == (inInterval.size() - 1)) ) {
461 _intervalBorders1D.push_back(static_cast<double>(l)* stepsize);
466 if (checkConnected) {
467 if (_intervalBorders1D.size() > 2) {
468 fConnectedInterval =
false;
470 fConnectedInterval =
true;
476 fValidInterval =
true;
478 TString interval_name = TString(
"ShortestBayesianInterval_a") + TString(this->GetName());
479 SimpleInterval* interval =
new SimpleInterval(interval_name, *poi, fLower, fUpper,
ConfidenceLevel());
480 interval->SetTitle(
"Shortest SimpleInterval from BATCalculator");
492 std::cerr <<
"BATCalculator::GetInterval - missing pdf model" << std::endl;
497 std::cerr <<
"BATCalculator::GetInterval - missing prior pdf" << std::endl;
500 if (fPOI.getSize() == 0) {
501 std::cerr <<
"BATCalculator::GetInterval - missing parameter of interest" << std::endl;
505 if (!fPosteriorPdf) {
507 _myRooInterface->Initialize(*fData, *fPdf, *fPrior, fparams, fPOI);
514 MCMCInterval* mcmcInterval =
new MCMCInterval(
"roostatsmcmcinterval", fPOI , *roostats_chain);
515 mcmcInterval->SetUseKeys(
false);
516 mcmcInterval->SetConfidenceLevel(1. - fSize);
517 fValidMCMCInterval =
true;
536 if ( (leftSideFraction >= 0.) && (leftSideFraction <= 1.) ) {
537 fLeftSideFraction = leftSideFraction;
539 std::cout <<
"BATCalculator::SetLeftSideTailFraction(Double_t leftSideFraction ) - value needs to be in the interval [0.,1.] to be meaningful, your value: " << leftSideFraction <<
" ,left side tail fraction has not been changed!" << std::endl;
BATCalculator()
constructor
T * OwnClone(const T *o)
Create a clone of the input but avoid registering the object with ROOT so it cannot be deleted twice...
Interface allowing to run BAT on a problem/data defined in a standard RooFit workspace format...
virtual ~BATCalculator()
destructor
int MarginalizeAll()
Marginalize all probabilities wrt.
Double_t GetOneSidedUperLim()
temporary solution ?
void SetNumBins(const char *parname, int nbins)
set the number of histogram bins for a specific parameter
std::vector< double > FindMode(std::vector< double > start=std::vector< double >())
Do the mode finding using a method set via SetOptimizationMethod.
void SetNIterationsRun(unsigned n)
Sets the number of iterations.
RooAbsPdf * GetPosteriorPdf1D() const
return posterior pdf
SimpleInterval * GetShortestInterval1D() const
return SimpleInterval object: shortest interval (not necessarily connected, one poi only) ...
Adapter to call BAT from within RooStats to compute credibility intervals.
virtual SimpleInterval * GetInterval1D() const
return SimpleInterval object: central interval (one poi only)
virtual MCMCInterval * GetInterval() const
returns MCMCInterval object
A class for handling 1D distributions.
void SetLeftSideTailFraction(Double_t leftSideFraction)
set left side tail fraction (only for 1D interval, not meaningful for shortest interval) ...
BCH1D GetMarginalized(const std::string &name) const
Obtain the individual marginalized distributions with respect to one parameter.
RooStats::MarkovChain * GetRooStatsMarkovChain()
return the RooStats Markov Chain (empty if corresponding constructor option not set) ...
virtual Double_t ConfidenceLevel() const
Get the Confidence level for the test.
void SetNumBins(const char *parname, int nbins)
set the number of histogram bins for a specific parameter