9 #include <BAT/BCParameter.h>
10 #include <BAT/BCMath.h>
24 const std::vector<ModelParameter>& ModPars_i,
25 boost::ptr_vector<Observable>& Obs_i,
26 std::vector<Observable2D>& Obs2D_i,
27 std::vector<CorrelatedGaussianObservables>& CGO_i,
28 std::vector<CorrelatedGaussianParameters>& CGP_i)
29 : BCModel(
""), ModPars(ModPars_i), CGP(CGP_i), Obs_ALL(Obs_i), Obs2D_ALL(Obs2D_i),
30 CGO(CGO_i), NumOfUsedEvents(0), NumOfDiscardedEvents(0) {
34 TH1::StatOverflows(kTRUE);
36 rank = MPI::COMM_WORLD.Get_rank();
43 TH1D * lhisto =
new TH1D(
"LogLikelihood",
"LogLikelihood",
45 lhisto->SetDefaultBufferSize(100000);
46 lhisto->GetXaxis()->SetTitle(
"LogLikelihood");
47 BCH1D * bclhisto =
new BCH1D(lhisto);
48 Histo1D[
"LogLikelihood"] = bclhisto;
50 int k = 0, kweight = 0;
51 for (boost::ptr_vector<Observable>::iterator it =
Obs_ALL.begin();
55 if (it->getDistr().compare(
"noweight") != 0)
58 std::string HistName = it->getName();
60 TH1D * histo =
new TH1D(HistName.c_str(), it->getLabel().c_str(),
61 NBINS1D, it->getMin(), it->getMax());
62 histo->GetXaxis()->SetTitle(it->getLabel().c_str());
63 BCH1D * bchisto =
new BCH1D(histo);
65 thMin[it->getName()] = std::numeric_limits<double>::max();
66 thMax[it->getName()] = -std::numeric_limits<double>::max();
69 for (std::vector<Observable2D>::iterator it =
Obs2D_ALL.begin();
71 if ((it->getDistr()).compare(
"file") == 0) {
73 throw std::runtime_error(
"ERROR: cannot handle noMCMC for Observable2D file yet!");
74 }
else if (it->getDistr().compare(
"weight") == 0)
75 throw std::runtime_error(
"ERROR: do not use Observable2D for analytic 2D weights!");
76 std::string HistName = it->getName();
78 TH2D * histo2 =
new TH2D(HistName.c_str(),
79 (it->getLabel() +
" vs " + it->getLabel2()).c_str(),
80 NBINS2D, it->getMin(), it->getMax(),
81 NBINS2D, it->getMin2(), it->getMax2());
82 histo2->GetXaxis()->SetTitle(it->getLabel().c_str());
83 histo2->GetYaxis()->SetTitle(it->getLabel2().c_str());
84 BCH2D * bchisto2 =
new BCH2D(histo2);
88 for (std::vector<CorrelatedGaussianObservables>::iterator it1 =
CGO.begin();
89 it1 !=
CGO.end(); ++it1) {
90 std::vector<Observable> pino(it1->getObs());
91 for (std::vector<Observable>::iterator it = pino.begin();
92 it != pino.end(); ++it) {
95 if ((it->getDistr()).compare(
"file") == 0)
96 throw std::runtime_error(
"Cannot use file in CorrelatedGaussianObservables!");
97 if (!(it->isTMCMC())) {
99 if (it->getDistr().compare(
"noweight") != 0)
100 throw std::runtime_error(
"Cannot use weight in CorrelatedGaussianObservables!");
102 std::string HistName = it->getName();
104 TH1D * histo =
new TH1D(HistName.c_str(), it->getLabel().c_str(),
105 NBINS1D, it->getMin(), it->getMax());
106 histo->GetXaxis()->SetTitle(it->getLabel().c_str());
107 BCH1D * bchisto =
new BCH1D(histo);
109 thMin[HistName] = std::numeric_limits<double>::max();
110 thMax[HistName] = -std::numeric_limits<double>::max();
113 if (it1->isPrediction()){
114 CorrelationMap[it1->getName()] =
new TPrincipal(it1->getObs().size());
146 for (std::map<std::string, TPrincipal *>::iterator it =
CorrelationMap.begin();
159 if (
rank == 0) std::cout <<
"\nParameters varied in Monte Carlo:" << std::endl;
161 for (std::vector<ModelParameter>::const_iterator it =
ModPars.begin();
163 if (it->geterrf() == 0. && it->geterrg() == 0.)
165 AddParameter(it->getname().c_str(), it->getmin(), it->getmax());
166 if (
rank == 0) std::cout << k <<
": " << it->getname() <<
", ";
167 if (it->IsCorrelated()) {
168 for (
unsigned int i = 0; i <
CGP.size(); i++) {
169 if (
CGP[i].getName().compare(it->getCgp_name()) == 0) {
170 std::string index = it->getname().substr(
CGP[i].getName().size());
171 long int lindex = strtol(index.c_str(),NULL,10);
173 DPars[
CGP[i].getPar(lindex - 1).getname()] = 0.;
175 std::stringstream out;
176 out << it->getname();
177 throw std::runtime_error(
"MonteCarloEngine::DefineParameters(): " + out.str() +
"seems to be part of a CorrelatedGaussianParameters object, but I couldn't find the corresponding object");
182 DPars[it->getname()] = 0.;
183 if (it->geterrf() == 0.) SetPriorGauss(k, it->getave(), it->geterrg());
184 else if (it->geterrg() == 0.) SetPriorConstant(k);
186 TF1 * combined =
new TF1(it->getname().c_str(),
187 "(TMath::Erf((x-[0]+[2])/sqrt(2.)/[1])-TMath::Erf((x-[0]-[2])/sqrt(2.)/[1]))/4./[2]",
188 it->getmin(), it->getmax());
189 combined->SetParameter(0, it->getave());
190 combined->SetParameter(1, it->geterrg());
191 combined->SetParameter(2, it->geterrf());
192 SetPrior(k, combined);
200 std::map<std::string,double>& DPars_i)
202 std::map<std::string, std::vector<double> > cgpmap;
205 for (std::vector<ModelParameter>::const_iterator it =
ModPars.begin(); it <
ModPars.end(); it++){
208 if(it->getname().compare(GetParameter(k)->GetName()) != 0)
210 std::stringstream out;
211 out << it->getname();
212 throw std::runtime_error(
"MonteCarloEngine::setDParsFromParameters(): " + out.str() +
"is sitting at the wrong position in the BAT parameters vector");
214 if (it->IsCorrelated()) {
215 std::string index = it->getname().substr(it->getCgp_name().size());
216 unsigned long int lindex = strtol(index.c_str(),NULL,10);
217 if (lindex - 1 == cgpmap[it->getCgp_name()].size())
218 cgpmap[it->getCgp_name()].push_back(parameters[k]);
220 std::stringstream out;
221 out << it->getname() <<
" " << lindex;
222 throw std::runtime_error(
"MonteCarloEngine::setDParsFromParameters(): " + out.str() +
"seems to be a CorrelatedGaussianParameters object but the corresponding parameters are missing or not in the right order");
226 DPars_i[it->getname()] = parameters[k];
230 for (
unsigned int j = 0; j <
CGP.size(); j++) {
231 std::vector<double> current = cgpmap.at(
CGP[j].getName());
232 if (current.size() !=
CGP[j].getPars().size()) {
233 std::stringstream out;
234 out <<
CGP[j].getName();
235 throw std::runtime_error(
"MonteCarloEngine::setDParsFromParameters(): " + out.str() +
" appears to be represented in cgpmap with a wrong size");
238 std::vector<double> porig =
CGP[j].getOrigParsValue(current);
240 for(
unsigned int l = 0; l < porig.size(); l++) {
241 DPars_i[
CGP[j].getPar(l).getname()] = porig[l];
259 std::cout <<
"event discarded" << std::endl;
273 for (boost::ptr_vector<Observable>::iterator it =
Obs_ALL.begin(); it !=
Obs_ALL.end(); it++) {
274 if (it->isTMCMC()) logprob += it->computeWeight();
277 for (std::vector<Observable2D>::iterator it =
Obs2D_ALL.begin(); it !=
Obs2D_ALL.end(); it++) {
278 if (it->isTMCMC()) logprob += it->computeWeight();
281 for (std::vector<CorrelatedGaussianObservables>::iterator it =
CGO.begin(); it <
CGO.end(); it++) {
282 if(!(it->isPrediction())) logprob += it->computeWeight();
284 if (isnan(logprob)) {
286 std::cout <<
"Event discarded since logprob evaluated to NAN.\n";
295 unsigned mychain = 0;
297 unsigned npars = GetNParameters();
298 int buffsize = npars + 1;
299 int index_chain[procnum];
300 double *recvbuff =
new double[buffsize];
301 std::vector<std::vector<double> > fMCMCxvect;
302 std::vector<double> pars;
306 buff =
new double*[procnum];
308 for (std::vector<CorrelatedGaussianObservables>::iterator it1 =
CGO.begin(); it1 <
CGO.end(); it1++)
309 obsbuffsize += it1->getObs().size();
310 buff[0] =
new double[procnum * obsbuffsize];
311 for (
int i = 1; i < procnum; i++) {
312 buff[i] = buff[i - 1] + obsbuffsize;
316 double ** sendbuff =
new double *[procnum];
317 sendbuff[0] =
new double[procnum * buffsize];
318 for (
int il = 1; il < procnum; il++)
319 sendbuff[il] = sendbuff[il - 1] + buffsize;
321 while (mychain < fMCMCNChains) {
323 for (
unsigned int k = 0; k < npars; k++)
324 pars.push_back(fMCMCx.at(mychain * npars + k));
326 fMCMCxvect.push_back(pars);
327 index_chain[iproc] = mychain;
330 if (iproc < procnum && mychain < fMCMCNChains)
333 for (
int il = 0; il < iproc; il++) {
336 sendbuff[il][0] = 2.;
337 for (
int im = 1; im < buffsize; im++)
338 sendbuff[il][im] = fMCMCxvect[il][im - 1];
340 for (
int il = iproc; il < procnum; il++) {
341 sendbuff[il][0] = 3.;
342 index_chain[il] = -1;
345 MPI::COMM_WORLD.Scatter(sendbuff[0], buffsize, MPI::DOUBLE,
346 recvbuff, buffsize, MPI::DOUBLE,
349 if (recvbuff[0] == 2.) {
350 double sbuff[obsbuffsize];
351 std::map<std::string, double>
DPars;
352 pars.assign(recvbuff + 1, recvbuff + buffsize);
357 for (boost::ptr_vector<Observable>::iterator it =
Obs_ALL.begin(); it <
Obs_ALL.end(); it++) {
358 sbuff[k++] = it->computeTheoryValue();
360 for (std::vector<Observable2D>::iterator it =
Obs2D_ALL.begin(); it <
Obs2D_ALL.end(); it++) {
361 sbuff[k++] = it->computeTheoryValue();
362 sbuff[k++] = it->computeTheoryValue2();
365 for (std::vector<CorrelatedGaussianObservables>::iterator it1 =
CGO.begin(); it1 <
CGO.end(); it1++) {
366 std::vector<Observable> pino(it1->getObs());
367 for (std::vector<Observable>::iterator it = pino.begin(); it != pino.end(); ++it)
368 sbuff[k++] = it->computeTheoryValue();
370 MPI::COMM_WORLD.Gather(sbuff, obsbuffsize, MPI::DOUBLE, buff[0], obsbuffsize, MPI::DOUBLE, 0);
371 }
else if (recvbuff[0] == 3.) {
372 double sbuff[obsbuffsize];
373 MPI::COMM_WORLD.Gather(sbuff, obsbuffsize, MPI::DOUBLE, buff[0], obsbuffsize, MPI::DOUBLE, 0);
376 for (
int il = 0; il < procnum; il++) {
377 if (index_chain[il] >= 0) {
380 int ko = 0, kweight = 0;
381 for (boost::ptr_vector<Observable>::iterator it =
Obs_ALL.begin();
383 double th = buff[il][k++];
385 if (th <
thMin[it->getName()])
thMin[it->getName()] = th;
386 if (th >
thMax[it->getName()])
thMax[it->getName()] = th;
387 Histo1D[it->getName()]->GetHistogram()->Fill(th);
388 if (!it->isTMCMC()) {
389 obval[index_chain[il] *
kmax + ko++] = th;
390 if (it->getDistr().compare(
"noweight") != 0) {
391 obweight[index_chain[il] *
kwmax + kweight++] = it->computeWeight(th);
397 for (std::vector<Observable2D>::iterator it =
Obs2D_ALL.begin();
399 double th1 = buff[il][k++];
400 double th2 = buff[il][k++];
401 Histo2D[it->getName()]->GetHistogram()->Fill(th1, th2);
405 for (std::vector<CorrelatedGaussianObservables>::iterator it1 =
CGO.begin();
406 it1 <
CGO.end(); it1++) {
407 std::vector<Observable> pino(it1->getObs());
408 Double_t * COdata =
new Double_t[pino.size()];
410 for (std::vector<Observable>::iterator it = pino.begin();
411 it != pino.end(); ++it) {
412 double th = buff[il][k++];
414 if (th <
thMin[it->getName()])
thMin[it->getName()] = th;
415 if (th >
thMax[it->getName()])
thMax[it->getName()] = th;
416 Histo1D[it->getName()]->GetHistogram()->Fill(th);
417 if (it1->isPrediction()) COdata[nObs++] = th;
419 if (it1->isPrediction())
CorrelationMap[it1->getName()]->AddRow(COdata);
433 for (
unsigned int i = 0; i < fMCMCNChains; ++i) {
434 std::vector<double>::const_iterator first = fMCMCx.begin() + i * GetNParameters();
435 std::vector<double>::const_iterator last = first + GetNParameters();
436 std::vector<double> currvec(first, last);
442 int k = 0, kweight = 0;
443 for (boost::ptr_vector<Observable>::iterator it =
Obs_ALL.begin();
445 double th = it->computeTheoryValue();
447 if (th <
thMin[it->getName()])
thMin[it->getName()] = th;
448 if (th >
thMax[it->getName()])
thMax[it->getName()] = th;
449 Histo1D[it->getName()]->GetHistogram()->Fill(th);
450 if (!it->isTMCMC()) {
453 if (it->getDistr().compare(
"noweight") != 0) {
461 for (std::vector<Observable2D>::iterator it =
Obs2D_ALL.begin();
463 double th1 = it->computeTheoryValue();
464 double th2 = it->computeTheoryValue2();
465 Histo2D[it->getName()]->GetHistogram()->Fill(th1, th2);
469 for (std::vector<CorrelatedGaussianObservables>::iterator it1 =
CGO.begin();
470 it1 <
CGO.end(); it1++) {
471 std::vector<Observable> pino(it1->getObs());
472 Double_t * COdata =
new Double_t[pino.size()];
474 for (std::vector<Observable>::iterator it = pino.begin();
475 it != pino.end(); ++it) {
476 double th = it->computeTheoryValue();
478 if (th <
thMin[it->getName()])
thMin[it->getName()] = th;
479 if (th >
thMax[it->getName()])
thMax[it->getName()] = th;
480 Histo1D[it->getName()]->GetHistogram()->Fill(th);
481 if (it1->isPrediction()) COdata[nObs++] = th;
483 if (it1->isPrediction())
CorrelationMap[it1->getName()]->AddRow(COdata);
488 for (
unsigned int i = 0; i < fMCMCNChains; i++)
490 Histo1D[
"LogLikelihood"]->GetHistogram()->Fill(MCMCGetLogProbx(i)-LogAPrioriProbability(MCMCGetx(i)));
496 double UnderFlowContent = hist.GetBinContent(0);
497 double OverFlowContent = hist.GetBinContent(
NBINS1D + 1);
498 double Integral = hist.Integral();
499 double TotalContent = 0.0;
500 for (
int n = 0; n <=
NBINS1D + 1; n++)
501 TotalContent += hist.GetBinContent(n);
503 << Integral / TotalContent * 100. <<
"% within the range, "
504 << UnderFlowContent / TotalContent * 100. <<
"% underflow, "
505 << OverFlowContent / TotalContent * 100. <<
"% overflow"
510 double Integral = hist.Integral();
511 double TotalContent = 0.0;
512 for (
int m = 0; m <=
NBINS2D + 1; m++)
513 for (
int n = 0; n <=
NBINS2D + 1; n++)
514 TotalContent += hist.GetBinContent(m, n);
516 << Integral / TotalContent * 100. <<
"% within the ranges"
521 const std::string OutputDir) {
522 std::string HistName = it.
getName();
526 if (
Histo1D[HistName]->GetHistogram()->Integral() > 0.0) {
527 std::string fname = OutputDir +
"/" + HistName +
".pdf";
532 Histo1D[HistName]->Print(fname.c_str());
533 std::cout << fname <<
" has been created." << std::endl;
534 out.Write(
Histo1D[HistName]->GetHistogram());
537 HistoLog <<
"WARNING: The histogram of "
538 << it.
getName() <<
" is empty!" << std::endl;
541 HistoLog <<
" [min, max]=[" << min <<
", " << max <<
"]" << std::endl;
546 std::vector<double> mode(GetBestFitParameters());
547 if (mode.size() == 0) {
548 if (
rank == 0)
throw std::runtime_error(
"\n ERROR: Global Mode could not be determined possibly because of infinite loglikelihood. Observables histogram cannot be generated.\n");
556 for (boost::ptr_vector<Observable>::iterator it =
Obs_ALL.begin(); it <
Obs_ALL.end();
560 for (std::vector<CorrelatedGaussianObservables>::iterator it1 =
CGO.begin();
561 it1 <
CGO.end(); it1++) {
562 std::vector<Observable> pino(it1->getObs());
563 for (std::vector<Observable>::iterator it = pino.begin();
564 it != pino.end(); ++it)
567 for (std::vector<Observable2D>::iterator it =
Obs2D_ALL.begin();
569 std::string HistName = it->getName();
570 if (
Histo2D[HistName]->GetHistogram()->Integral() > 0.0) {
571 std::string fname = OutputDir +
"/" + HistName +
".pdf";
573 th[0] = it->computeTheoryValue();
574 th[1] = it->computeTheoryValue2();
575 Histo2D[HistName]->SetGlobalMode(th);
576 Histo2D[HistName]->Print(fname.c_str());
577 std::cout << fname <<
" has been created." << std::endl;
578 out.Write(
Histo2D[HistName]->GetHistogram());
581 HistoLog <<
"WARNING: The histogram of "
582 << HistName <<
" is empty!" << std::endl;
584 std::string fname = OutputDir +
"/LogLikelihood.pdf";
585 Histo1D[
"LogLikelihood"]->Print(fname.c_str());
586 std::cout << fname <<
" has been created." << std::endl;
587 out.Write(
Histo1D[
"LogLikelihood"]->GetHistogram());
591 fMCMCFlagWritePreRunToFile =
false;
592 int k = 0, kweight = 0;
593 for (boost::ptr_vector<Observable>::iterator it =
Obs_ALL.begin();
595 if (!it->isTMCMC()) {
596 for (
unsigned int i = 0; i < fMCMCNChains; ++i)
597 fMCMCTrees[i]->Branch(it->getName().c_str(), &
obval[i *
kmax + k],
598 (it->getName() +
"/D").c_str());
600 if (it->getDistr().compare(
"noweight") != 0) {
601 for (
unsigned int i = 0; i < fMCMCNChains; ++i)
602 fMCMCTrees[i]->Branch((it->getName() +
"_weight").c_str(),
604 (it->getName() +
"_weight/D").c_str());
613 out.open(filename.c_str(), std::ios::out);
615 int npar = GetNParameters();
617 for (
int i = 0; i < npar; ++i)
618 out <<
" & " << GetParameter(i)->GetName();
619 out <<
" \\\\" << std::endl;
621 for (
int i = 0; i < npar; ++i) {
622 out << GetParameter(i)->GetName() <<
" & $";
623 for (
int j = 0; j < npar; ++j) {
625 BCH2D* bch2d_temp = GetMarginalized(GetParameter(i), GetParameter(j));
626 if (bch2d_temp != NULL)
627 out << bch2d_temp->GetHistogram()->GetCorrelationFactor();
632 if (j == npar - 1) out <<
"$ \\\\" << std::endl;
641 std::vector<double> mode(GetBestFitParameters());
642 if (mode.size() == 0) {
643 if(
rank == 0)
throw std::runtime_error(
"\n ERROR: Global Mode could not be determined possibly because of infinite loglikelihood. Observables statistics cannot be generated.\n");
646 std::ostringstream StatsLog;
648 StatsLog <<
"Statistics file for Observables, Binned Observables and Corellated Gaussian Observables.\n" << std::endl;
649 if (
Obs_ALL.size() > 0) StatsLog <<
"Observables:\n" << std::endl;
650 for (boost::ptr_vector<Observable>::iterator it =
Obs_ALL.begin(); it <
Obs_ALL.end(); it++) {
652 if (it->getObsType().compare(
"BinnedObservable") == 0) {
653 StatsLog <<
" (" << ++i <<
") Binned Observable \"";
654 StatsLog << it->getName() <<
"[" << it->getTho()->getBinMin() <<
", " << it->getTho()->getBinMax() <<
"]" <<
"\":";
655 }
else if (it->getObsType().compare(
"FunctionObservable") == 0) {
656 StatsLog <<
" (" << ++i <<
") Function Observable \"";
657 StatsLog << it->getName() <<
"[" << it->getTho()->getBinMin() <<
"]" <<
"\":";
658 }
else if (it->getObsType().compare(
"HiggsObservable") == 0) {
659 StatsLog <<
" (" << ++i <<
") Higgs Observable \"";
660 StatsLog << it->getName() <<
"\":";
662 StatsLog <<
" (" << ++i <<
") " << it->getObsType() <<
" \"";
663 StatsLog << it->getName() <<
"\":";
666 StatsLog << std::endl;
668 BCH1D * bch1d =
Histo1D[it->getName()];
669 if (bch1d->GetHistogram()->Integral() > 0.0) {
670 StatsLog <<
" Mean +- sqrt(V): " << std::setprecision(4)
671 << bch1d->GetMean() <<
" +- " << std::setprecision(4)
672 << bch1d->GetRMS() << std::endl
674 <<
" (Marginalized) mode: " << bch1d->GetMode() << std::endl;
676 std::vector<double> v1;
677 std::vector<double> v2;
678 std::vector<double> v3;
679 v1 = bch1d->GetSmallestIntervals(0.6827);
680 v2 = bch1d->GetSmallestIntervals(0.9545);
681 v3 = bch1d->GetSmallestIntervals(0.9973);
682 StatsLog <<
" Smallest interval(s) containing at least 68.27% and local mode(s):"
684 for (
unsigned j = 0; j < v1.size(); j += 5)
685 StatsLog <<
" (" << v1[j] <<
", " << v1[j + 1]
686 <<
") (local mode at " << v1[j + 3] <<
" with rel. height "
687 << v1[j + 2] <<
"; rel. area " << v1[j + 4] <<
")"
689 StatsLog << std::endl;
691 StatsLog <<
" Smallest interval(s) containing at least 95.45% and local mode(s):"
693 for (
unsigned j = 0; j < v2.size(); j += 5)
694 StatsLog <<
" (" << v2[j] <<
", " << v2[j + 1]
695 <<
") (local mode at " << v2[j + 3] <<
" with rel. height "
696 << v2[j + 2] <<
"; rel. area " << v2[j + 4] <<
")"
698 StatsLog << std::endl;
700 StatsLog <<
" Smallest interval(s) containing at least 99.73% and local mode(s):"
702 for (
unsigned j = 0; j < v3.size(); j += 5)
703 StatsLog <<
" (" << v3[j] <<
", " << v3[j + 1]
704 <<
") (local mode at " << v3[j + 3] <<
" with rel. height "
705 << v3[j + 2] <<
"; rel. area " << v3[j + 4] <<
")"
707 StatsLog << std::endl;
709 StatsLog <<
"\nWARNING: The histogram of " << it->getName() <<
" is empty! Statistics cannot be generated\n" << std::endl;
713 if (
CGO.size() > 0) StatsLog <<
"\nCorellated (Gaussian) Observables:\n" << std::endl;
714 for (std::vector<CorrelatedGaussianObservables>::iterator it1 =
CGO.begin();
715 it1 <
CGO.end(); it1++) {
716 StatsLog <<
"\n" << it1->getName() <<
":\n" << std::endl;
718 std::vector<Observable> CGObs(it1->getObs());
719 for (std::vector<Observable>::iterator it2 = CGObs.begin(); it2 < CGObs.end(); it2++) {
721 if (it2->getObsType().compare(
"BinnedObservable") == 0) {
722 StatsLog <<
" (" << ++i <<
") Binned Observable \"";
723 StatsLog << it2->getName() <<
"[" << it2->getTho()->getBinMin() <<
", " << it2->getTho()->getBinMax() <<
"]" <<
"\":";
724 }
else if (it2->getObsType().compare(
"FunctionObservable") == 0) {
725 StatsLog <<
" (" << ++i <<
") Function Observable \"";
726 StatsLog << it2->getName() <<
"[" << it2->getTho()->getBinMin() <<
"]" <<
"\":";
727 }
else if (it2->getObsType().compare(
"HiggsObservable") == 0) {
728 StatsLog <<
" (" << ++i <<
") Higgs Observable \"";
729 StatsLog << it2->getName() <<
"\":";
731 StatsLog <<
" (" << ++i <<
") " << it2->getObsType() <<
" \"";
732 StatsLog << it2->getName() <<
"\":";
735 StatsLog << std::endl;
736 BCH1D * bch1d =
Histo1D[it2->getName()];
737 if (bch1d->GetHistogram()->Integral() > 0.0) {
738 StatsLog <<
" Mean +- sqrt(V): " << std::setprecision(4)
739 << bch1d->GetMean() <<
" +- " << std::setprecision(4)
740 << bch1d->GetRMS() << std::endl
742 <<
" (Marginalized) mode: " << bch1d->GetMode() << std::endl;
744 std::vector<double> v1;
745 std::vector<double> v2;
746 std::vector<double> v3;
747 v1 = bch1d->GetSmallestIntervals(0.6827);
748 v2 = bch1d->GetSmallestIntervals(0.9545);
749 v3 = bch1d->GetSmallestIntervals(0.9973);
750 StatsLog <<
" Smallest interval(s) containing at least 68.27% and local mode(s):"
752 for (
unsigned j = 0; j < v1.size(); j += 5)
753 StatsLog <<
" (" << v1[j] <<
", " << v1[j + 1]
754 <<
") (local mode at " << v1[j + 3] <<
" with rel. height "
755 << v1[j + 2] <<
"; rel. area " << v1[j + 4] <<
")"
757 StatsLog << std::endl;
759 StatsLog <<
" Smallest interval(s) containing at least 95.45% and local mode(s):"
761 for (
unsigned j = 0; j < v2.size(); j += 5)
762 StatsLog <<
" (" << v2[j] <<
", " << v2[j + 1]
763 <<
") (local mode at " << v2[j + 3] <<
" with rel. height "
764 << v2[j + 2] <<
"; rel. area " << v2[j + 4] <<
")"
766 StatsLog << std::endl;
768 StatsLog <<
" Smallest interval(s) containing at least 99.73% and local mode(s):"
770 for (
unsigned j = 0; j < v3.size(); j += 5)
771 StatsLog <<
" (" << v3[j] <<
", " << v3[j + 1]
772 <<
") (local mode at " << v3[j + 3] <<
" with rel. height "
773 << v3[j + 2] <<
"; rel. area " << v3[j + 4] <<
")"
775 StatsLog << std::endl;
777 StatsLog <<
"\nWARNING: The histogram of " << it2->getName() <<
" is empty! Statistics cannot be generated\n" << std::endl;
780 if (it1->isPrediction()) {
781 int size = it1->getObs().size();
784 TMatrixD * corr =
const_cast<TMatrixD*
>(
CorrelationMap[it1->getName()]->GetCovarianceMatrix());
785 *corr *= (double)size;
786 StatsLog <<
"\nThe correlation matrix for " << it1->getName() <<
" is given by the " << size <<
"x"<< size <<
" matrix:\n" << std::endl;
788 for (
int i = 0; i < size + 1; i++) {
789 if (i == 0) StatsLog << std::setw(4) <<
"" <<
" | ";
790 else StatsLog << std::setw(5) << i << std::setw(5) <<
" |";
792 StatsLog << std::endl;
793 for (
int i = 0; i < size + 1; i++) {
794 if (i == 0) StatsLog << std::setw(8) <<
"--------";
795 else StatsLog << std::setw(10) <<
"----------";
797 StatsLog << std::endl;
798 for (
int i = 0; i < size; i++) {
799 for (
int j = 0; j < size + 1; j++) {
800 if (j == 0) StatsLog << std::setw(4) << i+1 <<
" |";
801 else StatsLog << std::setprecision(5) << std::setw(10) << (*corr)(i, j - 1);
803 StatsLog << std::endl;
808 std::vector<double> parsa;
809 for (
unsigned int i = 0; i < GetNParameters(); i++)
810 parsa.push_back(GetMarginalized(fParameters[i])->GetMean());
812 StatsLog <<
"LogLikelihood on the mean values of parameters: " << llik << std::endl;
813 double llika =
Histo1D[
"LogLikelihood"]->GetMean();
814 StatsLog <<
"LogLikelihood mean value: " << llika << std::endl;
815 double dbar = -2.*llika;
816 double dothetabar = -2.*llik;
817 StatsLog <<
"IC value (don't ask me what it means...): " << 3.*dbar - 2.*dothetabar << std::endl;
818 StatsLog <<
"DIC value (same as above...): " << 2.*dbar - dothetabar << std::endl;
819 return StatsLog.str().c_str();
824 std::vector<double> mode(GetBestFitParameters());
825 std::vector<double> scales(MCMCGetTrialFunctionScaleFactor(0));
826 std::ostringstream StatsLog;
827 for (
unsigned int i = 0; i < mode.size(); i++)
828 StatsLog << GetParameter(i)->GetName() <<
" " << mode.at(i) <<
" " << scales.at(i) << std::endl;
829 return StatsLog.str().c_str();
834 unsigned int Npars = GetNParameters();
835 std::vector<double> mode(GetBestFitParameters());
838 for (
unsigned int i = 0; i < Npars; i++)
839 for (
unsigned int j = 0; j < Npars; j++) {
846 return exp(Npars / 2. *
log(2. * M_PI) + 0.5 *
log(1. / det_Hessian) +
LogLikelihood(mode) + LogAPrioriProbability(mode));
851 if (point.size() != GetNParameters()) {
852 throw std::runtime_error(
"MCMCENgine::SecondDerivative : Invalid number of entries in the vector.");
856 const double dy1 = par2->GetRangeWidth() /
NSTEPS;
857 const double dy2 = dy1 * 2.;
858 const double dy3 = dy1 * 3.;
861 std::vector<double> y1p = point;
862 std::vector<double> y1m = point;
863 std::vector<double> y2p = point;
864 std::vector<double> y2m = point;
865 std::vector<double> y3p = point;
866 std::vector<double> y3m = point;
868 unsigned idy = GetParameters().Index(par2->GetName());
881 return 3. / 2. * m1 - 3. / 5. * m2 + 1. / 10. * m3;
886 if (point.size() != GetNParameters()) {
887 throw std::runtime_error(
"MCMCENgine::FirstDerivative : Invalid number of entries in the vector.");
891 const double dx1 = par->GetRangeWidth() /
NSTEPS;
892 const double dx2 = dx1 * 2.;
893 const double dx3 = dx1 * 3.;
896 std::vector<double> x1p = point;
897 std::vector<double> x1m = point;
898 std::vector<double> x2p = point;
899 std::vector<double> x2m = point;
900 std::vector<double> x3p = point;
901 std::vector<double> x3m = point;
903 unsigned idx = GetParameters().Index(par->GetName());
916 return 3. / 2. * m1 - 3. / 5. * m2 + 1. / 10. * m3;
920 if (point.size() != GetNParameters()) {
921 throw std::runtime_error(
"MCMCENgine::Function_h : Invalid number of entries in the vector.");
int NumOfDiscardedEvents
The number of events for which the update of the model fails and these events are not used for the MC...
const std::vector< ModelParameter > & ModPars
A vector of model parameters.
virtual bool Update(const std::map< std::string, double > &DPars)=0
The update method for the model.
std::map< std::string, double > thMax
A map between the name of a theory observable and its maximum value.
std::vector< Observable2D > & Obs2D_ALL
A vector of all pairs of observable for Observable2D.
A class for the template of models.
double FirstDerivative(const BCParameter *par, std::vector< double > point)
A class for constructing and defining operations on real matrices.
boost::ptr_vector< Observable > & Obs_ALL
A vector of all observables.
unsigned int kwmax
The number of observables whose weights are used for the MCMC.
const std::vector< CorrelatedGaussianParameters > & CGP
A vector of correlated Gaussian parameters.
double SecondDerivative(const BCParameter *par1, const BCParameter *par2, std::vector< double > point)
double LogLikelihood(const std::vector< double > ¶meters)
This member calculates the loglikelihood for the observables included in the MCMC run...
std::string computeStatistics()
A get method to compute the mean and rms of the computed observables.
std::map< std::string, double > thMin
A map between the name of a theory observable and its minimum value.
double * obval
A pointer to the vector of observable values.
void CheckHistogram(const TH1D &hist, const std::string name)
This member checks if there is overflow of the 1D histogram.
std::map< std::string, double > DPars
A map between parameter names and their values.
void assign(const size_t &i, const size_t &j, const double &a)
void PrintHistogram(BCModelOutput &out, Observable &it, const std::string OutputDir)
Overloaded from PrintHistogram(BCModelOutput&, const std::string) to print histogram for observables...
MonteCarloEngine(const std::vector< ModelParameter > &ModPars_i, boost::ptr_vector< Observable > &Obs_i, std::vector< Observable2D > &Obs2D_i, std::vector< CorrelatedGaussianObservables > &CGO_i, std::vector< CorrelatedGaussianParameters > &CGP_i)
Constructor.
double computeNormalization()
std::string getName() const
A get method to access the name of the observable.
void AddChains()
A method to add the observable values and weights to the chain information.
void setDParsFromParameters(const std::vector< double > ¶meters, std::map< std::string, double > &DPars_i)
void setNChains(unsigned int i)
A set method to fix the number of chains.
std::string writePreRunData()
A method to write in a text file the best fit parameters and the prerun scale factors.
int NumOfUsedEvents
The number of events for which the model is successfully updated and hence used for the MCMC run...
~MonteCarloEngine()
The default destructor. Some pointers defined in this class are explicitly freed. ...
int rank
Rank of the process for a MPI run. Value is 0 for a serial run.
void DefineParameters()
A member to classify the prior of the model parameters varied in the Monte Carlo. ...
std::map< std::string, TPrincipal * > CorrelationMap
A map between the name of a theory observable and its maximum value.
std::map< std::string, BCH2D * > Histo2D
A map between pointers to objects of type BCH2D (BAT) and their given names.
void PrintCorrelationMatrix(const std::string filename)
This member generates the correlation matrix using BCH2D from the BAT libraries.
double Function_h(std::vector< double > point)
complex log(const complex &z)
double * obweight
A pointer to the vector of observable weights.
complex exp(const complex &z)
double computeTheoryValue()
A method to access the computed theory value of the observable.
unsigned int kmax
The number of observables.
void Initialize(Model *Mod_i)
Initialization of the Monte Carlo Engine.
void MCMCIterationInterface()
Overloaded from BCEngineMCMC in BAT
std::map< std::string, BCH1D * > Histo1D
A map between pointers to objects of type BCH1D (BAT) and their given names.
std::ostringstream HistoLog
A stream to store the output messages from printing and checking histograms.
Model * Mod
A pointer to an abject of type Model.
std::vector< CorrelatedGaussianObservables > & CGO
A vector of correlated Gaussian observables.