42 fDataPointLowerBoundaries(0),
43 fDataPointUpperBoundaries(0),
48 fGoFNIterationsMax(100000),
49 fGoFNIterationsRun(2000),
51 fPriorConstantAll(false)
198 BCLog::OutError(
"BCModel::SetDataBoundaries : Need to define data set first.");
259 BCLog::OutError(
"BCModel::LogProbability. Normalization not available or zero.");
297 logprob += log(f->Eval(parameters[i]));
300 logprob += log(h->Interpolate(parameters[i]));
302 logprob += log(h->GetBinContent(h->FindBin(parameters[i])));
306 "BCModel::LogAPrioriProbability : Prior for parameter %s "
307 "is defined but not recognized.",
313 "BCModel::LogAPrioriProbability: Prior for parameter %s "
314 "is undefined. Using constant prior to proceed.",
332 return exp(
LogEval(parameters));
344 double probability = 1;
346 probability *= 1. /
fParameters[i]->GetRangeWidth();
356 double sum_sigma = 0;
357 for (
int i = 0; i < n; i++)
358 sum_sigma += log(
GetDataPoint(i)->GetValue(sigma_index));
360 double chi2 = -2. * (ll + (double) n / 2. * log(2. * M_PI) + sum_sigma);
362 fPValue = TMath::Prob(chi2, n);
370 std::vector<double> x;
381 double sum_sigma = 0;
382 for (
int i = 0; i < n; i++)
383 sum_sigma += log(
GetDataPoint(i)->GetValue(sigma_index));
385 double chi2 = -2. * (ll + (double) n / 2. * log(2. * M_PI) + sum_sigma);
398 "test defined only for continuous distributions."));
409 std::set<double> uniqueObservations;
410 for (
int i = 0; i < N; i++)
411 uniqueObservations.insert(
CDF(par, i,
false));
413 int nUnique = uniqueObservations.size();
414 if (nUnique != ECDF->GetNbinsX() + 1) {
416 "Number of unique data points doesn't match (%d vs %d)", nUnique,
417 ECDF->GetNbinsX() + 1));
427 std::set<double>::const_iterator iter = uniqueObservations.begin();
428 for (
int iBin = 0; iBin < nUnique; ++iBin) {
430 dist = TMath::Abs(*iter - ECDF->GetBinContent(iBin + 1));
432 distMax = TMath::Max(dist, distMax);
440 double z = distMax * sqrt(N);
442 fPValue = TMath::KolmogorovProb(z);
482 if (flag_histogram) {
505 BCLog::OutError(
"BCModel::HessianMatrixElement : Invalid number of entries in the vector.");
515 std::vector<double> xpp = point;
516 std::vector<double> xpm = point;
517 std::vector<double> xmp = point;
518 std::vector<double> xmm = point;
542 return (ppp + pmm - ppm - pmp) / (4. * dx1 * dx2);
620 "1./sqrt(2.*TMath::Pi())/[1] * exp(- (x-[0])*(x-[0])/2./[1]/[1])",
623 f->SetParameter(0, mean);
624 f->SetParameter(1, sigma);
658 f->SetParameter(0, mean);
659 f->SetParameter(1, sigmadown);
660 f->SetParameter(2, sigmaup);
694 if (h->GetDimension() != 1) {
695 BCLog::OutError(Form(
"BCModel::SetPrior : Histogram given for parameter %d is not 1D.",index));
700 h->Scale(1./h->Integral(
"width"));
728 return SetPrior(index, h, interpolate);
821 std::ofstream ofi(file);
824 if (!ofi.is_open()) {
825 std::cerr <<
"Couldn't open file " << file << std::endl;
837 <<
" -----------------------------------------------------" << std::endl
838 <<
" Summary" << std::endl
839 <<
" -----------------------------------------------------" << std::endl
842 ofi <<
" Model summary" << std::endl <<
" =============" << std::endl
843 <<
" Model: " <<
fName.data() << std::endl
844 <<
" Number of parameters: " << npar << std::endl
845 <<
" List of Parameters and ranges:" << std::endl;
846 for (
unsigned i = 0; i < npar; ++i) {
847 ofi <<
" (" << i <<
") Parameter \""
858 ofi <<
" Results of the optimization" << std::endl
859 <<
" ===========================" << std::endl
860 <<
" Optimization algorithm used: "
864 ofi <<
" Log of the maximum posterior: " <<
GetLogMaximum() << std::endl;
865 ofi <<
" List of parameters and global mode:" << std::endl;
866 for (
unsigned i = 0; i < npar; ++i) {
867 ofi <<
" (" << i <<
") Parameter \""
877 ofi <<
" (no error estimate available) ";
880 ofi <<
" (no error estimate available) ";
887 ofi <<
" No best fit information available." << std::endl;
892 ofi <<
" Results of the model test" << std::endl
893 <<
" =========================" << std::endl
894 <<
" p-value: " <<
fPValue << std::endl;
896 ofi <<
" p-value corrected for degrees of freedom: " <<
fPValueNDoF << std::endl;
902 ofi <<
" Results of the integration" << std::endl
903 <<
" ============================" << std::endl
904 <<
" Integration method used: "
908 ofi <<
" +- " <<
GetError() << std::endl;
910 ofi <<
" (no error estimate available) " << std::endl;
916 ofi <<
" WARNING: the Markov Chain did not converge!" << std::endl
917 <<
" Be cautious using the following results!" << std::endl
922 ofi <<
" Results of the marginalization" << std::endl
923 <<
" ==============================" << std::endl
924 <<
" Marginalization algorithm used: "
926 <<
" List of parameters and properties of the marginalized"
927 << std::endl <<
" distributions:" << std::endl;
928 for (
unsigned i = 0; i < npar; ++i) {
935 ofi <<
" (" << i <<
") Parameter \""
939 ofi <<
" fixed (or histogram does not exist) " << std::endl;
945 ofi <<
" Mean +- sqrt(V): " << std::setprecision(4)
946 << bch1d->
GetMean() <<
" +- " << std::setprecision(4)
947 << bch1d->
GetRMS() << std::endl
949 <<
" Median +- central 68% interval: "
950 << std::setprecision(4) << bch1d->
GetMedian() <<
" + "
952 <<
" - " << std::setprecision(4)
955 <<
" (Marginalized) mode: " << bch1d->
GetMode() << std::endl;
957 ofi <<
" 5% quantile: " << std::setprecision(4)
959 <<
" 10% quantile: " << std::setprecision(4)
961 <<
" 16% quantile: " << std::setprecision(4)
963 <<
" 84% quantile: " << std::setprecision(4)
965 <<
" 90% quantile: " << std::setprecision(4)
967 <<
" 95% quantile: " << std::setprecision(4)
970 std::vector<double> v;
972 ofi <<
" Smallest interval(s) containing at least 68% and local mode(s):"
974 for (
unsigned j = 0; j < v.size(); j += 5)
975 ofi <<
" (" << v[j] <<
", " << v[j + 1]
976 <<
") (local mode at " << v[j + 3] <<
" with rel. height "
977 << v[j + 2] <<
"; rel. area " << v[j + 4] <<
")"
983 ofi <<
" Status of the MCMC" << std::endl <<
" ==================" << std::endl
984 <<
" Convergence reached: " << (flag_conv ?
"yes" :
"no")
988 ofi <<
" Number of iterations until convergence: "
991 ofi <<
" WARNING: the Markov Chain did not converge! Be\n"
992 <<
" cautious using the following results!" << std::endl
996 <<
" Average efficiencies:" << std::endl;
998 std::vector<double> efficiencies;
999 efficiencies.assign(npar, 0.);
1001 for (
unsigned ipar = 0; ipar < npar; ++ipar)
1002 for (
unsigned ichain = 0; ichain < nchains; ++ichain) {
1003 unsigned index = ichain * npar + ipar;
1004 efficiencies[ipar] +=
1008 for (
unsigned ipar = 0; ipar < npar; ++ipar)
1009 ofi <<
" (" << ipar <<
") Parameter \""
1011 << efficiencies.at(ipar) <<
"%" << std::endl;
1015 ofi <<
" -----------------------------------------------------" << std::endl
1016 <<
" Notation:" << std::endl
1017 <<
" Mean : mean value of the marg. pdf" << std::endl
1018 <<
" Median : median of the marg. pdf" << std::endl
1019 <<
" Marg. mode : most probable value of the marg. pdf" << std::endl
1020 <<
" V : Variance of the marg. pdf" << std::endl
1021 <<
" Quantiles : most commonly used quantiles" <<std::endl
1022 <<
" -----------------------------------------------------" << std::endl
1061 BCLog::OutError(
"BCModel::PrintHessianMatrix : Invalid number of entries in the vector");
1069 for (
int i = 0; i < int(parameters.size()); i++)
1075 for (
unsigned int j = 0; j < i; j++) {
1098 if (strlen(string1) != strlen(string2))
1101 for (
int i = 0; i < int(strlen(string1)); i++)
1102 if (string1[i] != string2[i])