00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "BCModelTest.h"
00011 #include "BCModel.h"
00012 #include "BCLog.h"
00013 #include "BCErrorCodes.h"
00014 #include "BCMath.h"
00015
00016 #include <TDirectory.h>
00017 #include <TFile.h>
00018 #include <TTree.h>
00019 #include <TMath.h>
00020
00021 #include <fstream>
00022 #include <iomanip>
00023
00024
00025
00026 BCModel::BCModel(const char * name) : BCIntegrate()
00027 {
00028
00029 fNormalization = -1.0;
00030
00031 fDataSet = 0;
00032
00033 fParameterSet = new BCParameterSet;
00034
00035 fIndex = -1;
00036 fPValue = -1;
00037
00038 fName = (char *) name;
00039
00040 flag_ConditionalProbabilityEntry = true;
00041
00042 fDataPointUpperBoundaries = 0;
00043 fDataPointLowerBoundaries = 0;
00044
00045 fErrorBandXY = 0;
00046
00047 }
00048
00049
00050
00051 BCModel::BCModel() : BCIntegrate()
00052 {
00053
00054 fNormalization = -1.0;
00055
00056 fDataSet = 0;
00057
00058 fParameterSet = new BCParameterSet();
00059
00060 fIndex = -1;
00061 fPValue = -1;
00062
00063 fName = "model";
00064
00065 fDataPointUpperBoundaries = 0;
00066 fDataPointLowerBoundaries = 0;
00067
00068 flag_ConditionalProbabilityEntry = true;
00069
00070 }
00071
00072
00073
00074 BCModel::~BCModel()
00075 {
00076
00077 delete fParameterSet;
00078
00079 if (fDataSet)
00080 delete fDataSet;
00081
00082 if (fDataPointLowerBoundaries)
00083 delete fDataPointLowerBoundaries;
00084
00085 if (fDataPointUpperBoundaries)
00086 delete fDataPointUpperBoundaries;
00087
00088 }
00089
00090
00091
00092 int BCModel::GetNDataPoints()
00093 {
00094
00095 int npoints = 0;
00096
00097 if (fDataSet)
00098 npoints = fDataSet -> GetNDataPoints();
00099 else
00100 {
00101 BCLog::Out(BCLog::warning, BCLog::warning,"BCModel::GetNDataPoints(). No data set defined.");
00102 return ERROR_NOEVENTS;
00103 }
00104
00105 return npoints;
00106
00107 }
00108
00109
00110
00111 BCDataPoint * BCModel::GetDataPoint(int index)
00112 {
00113
00114 if (fDataSet)
00115 return fDataSet -> GetDataPoint(index);
00116
00117 BCLog::Out(BCLog::warning, BCLog::warning,"BCModel::GetDataPoint. No data set defined.");
00118 return 0;
00119
00120 }
00121
00122
00123
00124 BCParameter * BCModel::GetParameter(int index)
00125 {
00126 if (!fParameterSet)
00127 return 0;
00128
00129 if (index < 0 || index >= this -> GetNParameters())
00130 {
00131 BCLog::Out(BCLog::warning, BCLog::warning,
00132 Form("BCModel::GetParameter. Parameter index %d not within range.", index));
00133 return 0;
00134 }
00135
00136 return fParameterSet -> at(index);
00137 }
00138
00139
00140
00141 BCParameter * BCModel::GetParameter(const char * name)
00142 {
00143 if (!fParameterSet)
00144 return 0;
00145
00146 int index = -1;
00147
00148 for (int i = 0; i < this->GetNParameters(); i++)
00149
00150 if (name == this -> GetParameter(i) -> GetName())
00151 index = i;
00152
00153 if (index<0)
00154 {
00155 BCLog::Out(BCLog::warning, BCLog::warning,
00156
00157 Form("BCModel::GetParameter. Model %s has no parameter named %s.", (this -> GetName()).data(), name));
00158 return 0;
00159 }
00160
00161 return this->GetParameter(index);
00162 }
00163
00164
00165
00166 std::vector <double> BCModel::GetErrorBand(double level)
00167 {
00168
00169 std::vector <double> errorband;
00170
00171 if (!fErrorBandXY)
00172 return errorband;
00173
00174 int nx = fErrorBandXY -> GetNbinsX();
00175
00176 errorband.assign(nx - 1, 0.0);
00177
00178
00179
00180 for (int ix = 1; ix < nx; ix++)
00181 {
00182
00183 TH1D * temphist = fErrorBandXY -> ProjectionY("temphist", ix, ix);
00184
00185 int nprobSum = 1;
00186 double q[1];
00187 double probSum[1];
00188
00189 probSum[0] = level;
00190
00191 temphist -> GetQuantiles(nprobSum, q, probSum);
00192
00193 errorband[ix-1] = q[0];
00194 }
00195
00196 return errorband;
00197
00198 }
00199
00200
00201
00202 TGraph * BCModel::GetErrorBandGraph(double level1, double level2)
00203 {
00204
00205 if (!fErrorBandXY)
00206 return 0;
00207
00208
00209 int nx = fErrorBandXY -> GetNbinsX() - 1;
00210
00211 TGraph * graph = new TGraph(2 * nx);
00212 graph -> SetFillStyle(1001);
00213 graph -> SetFillColor(kYellow);
00214
00215
00216 std::vector <double> ymin = this -> GetErrorBand(level1);
00217 std::vector <double> ymax = this -> GetErrorBand(level2);
00218
00219 for (int i = 0; i < nx; i++)
00220 {
00221 graph -> SetPoint(i, fErrorBandXY -> GetXaxis() -> GetBinCenter(i + 1), ymin.at(i));
00222 graph -> SetPoint(nx + i, fErrorBandXY -> GetXaxis() -> GetBinCenter(nx - i), ymax.at(nx - i - 1));
00223 }
00224
00225 return graph;
00226
00227 }
00228
00229
00230
00231 TGraph * BCModel::GetFitFunctionGraph(std::vector <double> parameters)
00232 {
00233
00234 if (!fErrorBandXY)
00235 return 0;
00236
00237
00238
00239 int nx = fErrorBandXY -> GetNbinsX();
00240
00241 TGraph * graph = new TGraph(nx);
00242
00243
00244
00245 for (int i = 0; i < nx; i++)
00246 {
00247 double x = fErrorBandXY -> GetXaxis() -> GetBinCenter(i + 1);
00248
00249 std::vector <double> xvec;
00250 xvec.push_back(x);
00251
00252 double y = this -> FitFunction(xvec, parameters);
00253
00254 xvec.clear();
00255
00256 graph -> SetPoint(i, x, y);
00257 }
00258
00259 return graph;
00260
00261 }
00262
00263
00264
00265 TGraph * BCModel::GetFitFunctionGraph(std::vector <double> parameters, double xmin, double xmax, int n)
00266 {
00267
00268
00269 TGraph * graph = new TGraph(n+1);
00270
00271 double dx = (xmax-xmin)/(double)n;
00272
00273
00274 for (int i = 0; i <= n; i++)
00275 {
00276 double x = (double)i*dx+xmin;
00277
00278 std::vector <double> xvec;
00279 xvec.push_back(x);
00280
00281 double y = this -> FitFunction(xvec, parameters);
00282
00283 xvec.clear();
00284
00285 graph -> SetPoint(i, x, y);
00286 }
00287
00288 return graph;
00289
00290 }
00291
00292
00293
00294 bool BCModel::GetFlagBoundaries()
00295 {
00296
00297 if (!fDataPointLowerBoundaries)
00298 return false;
00299
00300 if (!fDataPointUpperBoundaries)
00301 return false;
00302
00303 if (int(fDataPointLowerBoundaries -> GetNValues()) != fDataSet -> GetDataPoint(0) -> GetNValues())
00304 return false;
00305
00306 if (int(fDataPointUpperBoundaries -> GetNValues()) != fDataSet -> GetDataPoint(0) -> GetNValues())
00307 return false;
00308
00309 return true;
00310
00311 }
00312
00313
00314
00315 void BCModel::SetSingleDataPoint(BCDataPoint * datapoint)
00316 {
00317
00318
00319
00320 BCDataSet * dataset = new BCDataSet();
00321
00322
00323
00324 dataset -> AddDataPoint(datapoint);
00325
00326
00327
00328 this -> SetDataSet(dataset);
00329
00330 }
00331
00332
00333
00334 void BCModel::SetSingleDataPoint(BCDataSet * dataset, int index)
00335 {
00336
00337 if (index < 0 || index > dataset -> GetNDataPoints())
00338 return;
00339
00340 this -> SetSingleDataPoint(dataset -> GetDataPoint(index));
00341
00342 }
00343
00344
00345
00346 void BCModel::SetDataBoundaries(int index, double lowerboundary, double upperboundary, bool fixed)
00347 {
00348
00349
00350
00351 if (!fDataSet)
00352 {
00353 BCLog::Out(BCLog::warning, BCLog::warning,
00354 "BCModel::SetDataBoundaries. Need to define data set first.");
00355
00356 return;
00357 }
00358
00359
00360
00361 if (index < 0 || index > fDataSet -> GetDataPoint(0) -> GetNValues())
00362 {
00363 BCLog::Out(BCLog::warning, BCLog::warning,
00364 "BCModel::SetDataBoundaries. Index out of range.");
00365
00366 return;
00367 }
00368
00369
00370
00371 if (!fDataPointLowerBoundaries)
00372 fDataPointLowerBoundaries = new BCDataPoint(fDataSet -> GetDataPoint(0) -> GetNValues());
00373
00374 if (!fDataPointUpperBoundaries)
00375 fDataPointUpperBoundaries = new BCDataPoint(fDataSet -> GetDataPoint(0) -> GetNValues());
00376
00377 if (fDataFixedValues.size() == 0)
00378 fDataFixedValues.assign(fDataSet -> GetDataPoint(0) -> GetNValues(), false);
00379
00380
00381
00382 fDataPointLowerBoundaries -> SetValue(index, lowerboundary);
00383 fDataPointUpperBoundaries -> SetValue(index, upperboundary);
00384 fDataFixedValues[index] = fixed;
00385
00386 }
00387
00388
00389
00390 void BCModel::SetErrorBandContinuous(bool flag)
00391 {
00392
00393 fErrorBandContinuous = flag;
00394
00395 if (flag)
00396 return;
00397
00398
00399
00400 fErrorBandX.clear();
00401
00402
00403
00404 for (int i = 0; i < fDataSet -> GetNDataPoints(); ++i)
00405 {
00406 fErrorBandX.push_back(fDataSet -> GetDataPoint(i) -> GetValue(fFitFunctionIndexX));
00407 }
00408
00409 }
00410
00411
00412
00413 int BCModel::AddParameter(const char * name, double lowerlimit, double upperlimit)
00414 {
00415
00416
00417
00418 BCParameter * parameter = new BCParameter(name, lowerlimit, upperlimit);
00419
00420 int flag_ok = 0;
00421
00422 flag_ok = this -> AddParameter(parameter);
00423
00424 if (flag_ok != 0)
00425 delete parameter;
00426
00427 return flag_ok;
00428
00429 }
00430
00431
00432
00433 int BCModel::AddParameter(BCParameter * parameter)
00434 {
00435
00436
00437
00438 if (!fParameterSet)
00439 {
00440 BCLog::Out(BCLog::warning, BCLog::warning,
00441 "BCModel::AddParameter. Parameter set does not exist");
00442
00443 return ERROR_PARAMETERSETDOESNOTEXIST;
00444 }
00445
00446
00447
00448 int flag_exists = 0;
00449
00450 for (int i = 0; i < this -> GetNParameters(); i++)
00451 if (this -> CompareStrings(parameter -> GetName().data(), this -> GetParameter(i) -> GetName().data()) == 0)
00452 flag_exists = -1;
00453
00454 if (flag_exists < 0)
00455 {
00456 BCLog::Out(BCLog::warning, BCLog::warning,
00457 Form("BCModel::AddParameter. Parameter with name %s exists already. ", parameter -> GetName().data()));
00458
00459 return ERROR_PARAMETEREXISTSALREADY;
00460 }
00461
00462
00463
00464 int index = int(fParameterSet -> size());
00465
00466 parameter -> SetIndex(index);
00467
00468
00469
00470 fParameterSet -> push_back(parameter);
00471
00472
00473
00474 this -> SetParameters(fParameterSet);
00475
00476
00477
00478
00479
00480
00481
00482 this -> MCMCInitialize();
00483
00484 return 0;
00485
00486 };
00487
00488
00489
00490 double BCModel::LogProbabilityNN(std::vector <double> parameters)
00491 {
00492
00493
00494
00495 double logprob = this -> LogLikelihood(parameters);
00496
00497
00498
00499 logprob += this -> LogAPrioriProbability(parameters);
00500
00501 return logprob;
00502
00503 }
00504
00505
00506
00507 double BCModel::LogProbability(std::vector <double> parameters)
00508 {
00509
00510
00511
00512 if (fNormalization<0. || fNormalization==0.)
00513 {
00514 BCLog::Out(BCLog::warning, BCLog::warning,
00515 "BCModel::LogProbability. Normalization not available or zero.");
00516 return 0.;
00517 }
00518
00519 return this -> LogProbabilityNN(parameters) - log(fNormalization);
00520
00521 }
00522
00523
00524
00525 double BCModel::LogLikelihood(std::vector <double> parameters)
00526 {
00527
00528 int ndatapoints = fDataSet -> GetNDataPoints();
00529
00530
00531
00532 double logprob = this -> LogPoissonProbability(ndatapoints, parameters);
00533
00534
00535
00536 for (int i=0;i<ndatapoints;i++)
00537 {
00538 BCDataPoint * datapoint = this -> GetDataPoint(i);
00539 logprob += this -> LogConditionalProbabilityEntry(datapoint, parameters);
00540 }
00541
00542 return logprob;
00543
00544 }
00545
00546
00547
00548 double BCModel::LogEval(std::vector <double> parameters)
00549 {
00550
00551 return this -> LogProbabilityNN(parameters);
00552
00553 }
00554
00555
00556
00557 double BCModel::EvalSampling(std::vector <double> parameters)
00558 {
00559
00560 return this -> SamplingFunction(parameters);
00561
00562 }
00563
00564
00565
00566 void BCModel::CalculateErrorBandXY(int nx, double xmin, double xmax, int ny, double ymin, double ymax, int niter)
00567 {
00568
00569
00570
00571
00572 if (niter <= 0)
00573 niter = fNbins * fNbins * fNSamplesPer2DBin * fNvar;
00574
00575
00576
00577 if (!fErrorBandXY)
00578 delete fErrorBandXY;
00579
00580 double dx = (xmax - xmin) / double(nx);
00581 double dy = (ymax - ymin) / double(ny);
00582
00583 fErrorBandXY = new TH2D("errorbandxy", "",
00584 nx + 1,
00585 xmin - 0.5 * dx,
00586 xmax + 0.5 * dx,
00587 ny + 1,
00588 ymin - 0.5 * dy,
00589 ymax + 0.5 * dy);
00590 fErrorBandXY -> SetStats(kFALSE);
00591
00592
00593
00594 std::vector <double> randparameters;
00595 randparameters.assign(fNvar, 0.0);
00596
00597 this -> InitMetro();
00598
00599
00600
00601 for(int i = 0; i <= niter; i++)
00602 {
00603
00604
00605 GetRandomPointMetro(randparameters);
00606
00607
00608
00609 double x = 0;
00610
00611 for (int ix = 0; ix < nx; ix++)
00612 {
00613
00614
00615 x = fErrorBandXY -> GetXaxis() -> GetBinCenter(ix + 1);
00616
00617
00618
00619 std::vector <double> xvec;
00620 xvec.push_back(x);
00621
00622 double y = this -> FitFunction(xvec, randparameters);
00623
00624 xvec.clear();
00625
00626
00627
00628 fErrorBandXY -> Fill(x, y);
00629 }
00630 }
00631
00632 fErrorBandXY -> Scale(1.0/fErrorBandXY -> Integral() * fErrorBandXY -> GetNbinsX());
00633
00634 }
00635
00636
00637
00638 double BCModel::SamplingFunction(std::vector <double> parameters)
00639 {
00640
00641 double probability = 1.0;
00642
00643 for (std::vector<BCParameter*>::const_iterator it = fParameterSet -> begin(); it != fParameterSet -> end(); ++it)
00644 {
00645 probability *= 1.0 / ((*it) -> GetUpperLimit() -
00646 (*it) -> GetLowerLimit());
00647 }
00648
00649 return probability;
00650
00651 }
00652
00653
00654
00655 double BCModel::Normalize()
00656 {
00657 BCLog::Out(BCLog::summary, BCLog::summary, Form("Model \'%s\': Normalizing probability",this->GetName().data()));
00658
00659 int n = this -> GetNvar();
00660
00661
00662
00663 if (n == 0)
00664 {
00665 this->SetParameters(fParameterSet);
00666 n = this->GetNvar();
00667 }
00668
00669
00670
00671
00672 fNormalization = this -> Integrate();
00673
00674 BCLog::Out(BCLog::detail, BCLog::detail, Form(" --> Normalization : %.2lf", fNormalization));
00675
00676 return fNormalization;
00677
00678 }
00679
00680
00681
00682 int BCModel::CheckParameters(std::vector <double> parameters)
00683 {
00684
00685
00686
00687 if (!parameters.size() == fParameterSet -> size())
00688 return ERROR_INVALIDNUMBEROFPARAMETERS;
00689
00690
00691
00692 for (int i = 0; i < int(fParameterSet -> size()); i++)
00693 {
00694 BCParameter * modelparameter = fParameterSet -> at(i);
00695
00696 if (modelparameter -> GetLowerLimit() > parameters.at(i) ||
00697 modelparameter -> GetUpperLimit() < parameters.at(i))
00698 {
00699 BCLog::Out(BCLog::warning, BCLog::warning,
00700 Form("BCModel::CheckParameters. Parameter %s not within limits.", fParameterSet -> at(i) -> GetName().data()));
00701
00702 return ERROR_PARAMETERNOTWITHINRANGE;
00703 }
00704 }
00705
00706 return 0;
00707
00708 }
00709
00710
00711
00712
00713 void BCModel::FindMode()
00714 {
00715
00716 BCLog::Out(BCLog::summary, BCLog::summary,
00717 Form("Model \'%s\': Finding mode", this -> GetName().data()));
00718
00719
00720 this -> SetParameters(fParameterSet);
00721
00722 switch(this -> GetOptimizationMethod())
00723 {
00724 case BCIntegrate::kOptSimulatedAnnealing:
00725 {
00726 this -> FindModeSA();
00727 return;
00728 }
00729
00730 case BCIntegrate::kOptMinuit:
00731 {
00732 this -> FindModeMinuit();
00733 return;
00734 }
00735
00736 case BCIntegrate::kOptMetropolis:
00737 {
00738
00739
00740 this -> MarginalizeAll();
00741 return;
00742 }
00743
00744 }
00745
00746 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::FindMode. Invalid mode finding method: %d. Return.",
00747 this->GetOptimizationMethod()));
00748
00749 return;
00750
00751 }
00752
00753
00754
00755 void BCModel::WriteMode(const char * file)
00756 {
00757 ofstream ofi(file);
00758 if(!ofi.is_open())
00759 {
00760 std::cerr<<"Couldn't open file "<<file<<std::endl;
00761 return;
00762 }
00763
00764 int npar = fParameterSet -> size();
00765 for (int i=0; i<npar; i++)
00766 ofi<<fBestFitParameters.at(i)<<std::endl;
00767
00768 ofi<<std::endl;
00769 ofi<<"#######################################################################"<<std::endl;
00770 ofi<<"#"<<std::endl;
00771 ofi<<"# This file was created automatically by BCModel::WriteMode() call."<<std::endl;
00772 ofi<<"# It can be read in by call to BCModel::ReadMode()."<<std::endl;
00773 ofi<<"# Do not modify it unless you know what you're doing."<<std::endl;
00774 ofi<<"#"<<std::endl;
00775 ofi<<"#######################################################################"<<std::endl;
00776 ofi<<"#"<<std::endl;
00777 ofi<<"# Best fit parameters (mode) for model:"<<std::endl;
00778 ofi<<"# \'"<<fName.data()<<"\'"<<std::endl;
00779 ofi<<"#"<<std::endl;
00780 ofi<<"# Number of parameters: "<<npar<<std::endl;
00781 ofi<<"# Parameters ordered as above:"<<std::endl;
00782
00783 for (int i=0; i<npar; i++)
00784 {
00785 ofi<<"# "<<i<<": ";
00786 ofi<<fParameterSet->at(i)->GetName().data()<<" = ";
00787 ofi<<fBestFitParameters.at(i)<<std::endl;
00788 }
00789
00790 ofi<<"#"<<std::endl;
00791 ofi<<"########################################################################"<<std::endl;
00792 }
00793
00794
00795
00796 int BCModel::ReadMode(const char * file)
00797 {
00798 ifstream ifi(file);
00799 if(!ifi.is_open())
00800 {
00801 std::cerr<<"Couldn't open file "<<file<<std::endl;
00802 return 0;
00803 }
00804
00805 int npar = fParameterSet -> size();
00806 std::vector <double> mode;
00807
00808 int i=0;
00809 while (i<npar && !ifi.eof())
00810 {
00811 double a;
00812 ifi>>a;
00813 mode.push_back(a);
00814 i++;
00815 }
00816
00817 if(i<npar)
00818 {
00819 std::cerr<<"Couldn't read mode from file "<<file<<std::endl;
00820 std::cerr<<"Expected "<<npar<<" parameters, found "<<i<<std::endl;
00821 return 0;
00822 }
00823
00824 BCLog::Out(BCLog::summary,BCLog::summary,
00825 Form("# Read in best fit parameters (mode) for model \'%s\' from file %s:",fName.data(),file));
00826 this->SetMode(mode);
00827 for(int j=0 ; j<npar; j++)
00828 BCLog::Out(BCLog::summary,BCLog::summary,
00829 Form("# -> Parameter %d : %s = %e", j, fParameterSet->at(j)->GetName().data(), fBestFitParameters[j]));
00830
00831 BCLog::Out(BCLog::warning,BCLog::warning,
00832 "# ! Best fit values obtained before this call will be overwritten !");
00833
00834 return npar;
00835 }
00836
00837
00838
00839 BCH1D * BCModel::MarginalizeProbability(BCParameter * parameter)
00840 {
00841
00842
00843 BCLog::Out(BCLog::summary, BCLog::summary, Form("Marginalize probability with respect to %s", parameter -> GetName().data()));
00844
00845 BCH1D * hprobability = new BCH1D();
00846
00847
00848
00849 TH1D * hist = this -> Marginalize(parameter);
00850
00851
00852
00853 hist -> SetName(Form("hist_%s_%s", this -> GetName().data(), parameter -> GetName().data()));
00854 hist -> SetXTitle(parameter -> GetName().data());
00855 hist -> SetYTitle(Form("p(%s|data)", parameter -> GetName().data()));
00856 hist -> SetStats(kFALSE);
00857
00858
00859
00860 hprobability -> SetHistogram(hist);
00861
00862
00863
00864 double bestfit = hprobability -> GetMode();
00865
00866 int index = parameter -> GetIndex();
00867
00868 if (fBestFitParametersMarginalized.size() == 0)
00869 for (int i = 0; i < this -> GetNParameters(); i++)
00870 fBestFitParametersMarginalized.push_back(0.0);
00871
00872 fBestFitParametersMarginalized[index] = bestfit;
00873
00874 return hprobability;
00875
00876 }
00877
00878
00879
00880 BCH2D * BCModel::MarginalizeProbability(BCParameter * parameter1, BCParameter * parameter2)
00881 {
00882
00883
00884 BCLog::Out(BCLog::summary, BCLog::summary,
00885 Form("Marginalize probability with respect to %s and %s",
00886 parameter1 -> GetName().data(),
00887 parameter2 -> GetName().data()));
00888
00889 BCH2D * hprobability = new BCH2D();
00890
00891
00892
00893 TH2D * hist = this -> Marginalize(parameter1, parameter2);
00894 hist -> SetXTitle(Form("%s", parameter1 -> GetName().data()));
00895 hist -> SetYTitle(Form("%s", parameter2 -> GetName().data()));
00896 hist -> SetStats(kFALSE);
00897
00898
00899
00900 hprobability -> SetHistogram(hist);
00901
00902 return hprobability;
00903
00904 }
00905
00906
00907
00908 int BCModel::MarginalizeAll()
00909 {
00910
00911
00912
00913
00914
00915
00916
00917
00918 double dx = 0.0;
00919 double dy = 0.0;
00920
00921 if (fFitFunctionIndexX >= 0)
00922 {
00923 dx = (fDataPointUpperBoundaries -> GetValue(fFitFunctionIndexX) - fDataPointLowerBoundaries -> GetValue(fFitFunctionIndexX))
00924 / (double)fErrorBandNbinsX;
00925
00926 dy = (fDataPointUpperBoundaries -> GetValue(fFitFunctionIndexY) - fDataPointLowerBoundaries -> GetValue(fFitFunctionIndexY))
00927 / (double)fErrorBandNbinsY;
00928
00929 fErrorBandXY = new TH2D("errorbandxy", "",
00930 fErrorBandNbinsX + 1,
00931 fDataPointLowerBoundaries -> GetValue(fFitFunctionIndexX) - .5 * dx,
00932 fDataPointUpperBoundaries -> GetValue(fFitFunctionIndexX) + .5 * dx,
00933 fErrorBandNbinsY + 1,
00934 fDataPointLowerBoundaries -> GetValue(fFitFunctionIndexY) - .5 * dy,
00935 fDataPointUpperBoundaries -> GetValue(fFitFunctionIndexY) + .5 * dy);
00936 fErrorBandXY -> SetStats(kFALSE);
00937
00938 for (int ix = 1; ix <= fErrorBandNbinsX; ++ix)
00939 for (int iy = 1; iy <= fErrorBandNbinsX; ++iy)
00940 fErrorBandXY -> SetBinContent(ix, iy, 0.0);
00941 }
00942
00943 this -> MCMCMetropolis();
00944
00945 this -> FindModeMCMC();
00946
00947
00948
00949 return 1;
00950 }
00951
00952
00953
00954
00955 BCH1D * BCModel::GetMarginalized(BCParameter * parameter)
00956 {
00957
00958
00959
00960
00961
00962
00963
00964
00965 int index = parameter -> GetIndex();
00966
00967
00968
00969
00970 TH1D * hist = this -> MCMCGetH1Marginalized(index);
00971
00972 if(!hist)
00973 return 0;
00974
00975 BCH1D * hprob = new BCH1D();
00976
00977
00978 hist -> SetName(Form("hist_%s_%s", this -> GetName().data(), parameter -> GetName().data()));
00979 hist -> SetXTitle(parameter -> GetName().data());
00980 hist -> SetYTitle(Form("p(%s|data)", parameter -> GetName().data()));
00981 hist -> SetStats(kFALSE);
00982
00983
00984 hprob -> SetHistogram(hist);
00985
00986
00987 double bestfit = hprob -> GetMode();
00988
00989 if (fBestFitParametersMarginalized.size() == 0)
00990 for (int i = 0; i < this -> GetNParameters(); i++)
00991 fBestFitParametersMarginalized.push_back(0.0);
00992
00993 fBestFitParametersMarginalized[index] = bestfit;
00994
00995 hprob->SetGlobalMode(fBestFitParameters.at(index));
00996
00997 return hprob;
00998
00999 }
01000
01001
01002
01003 int BCModel::ReadMarginalizedFromFile(const char * file)
01004 {
01005 TFile * froot = new TFile(file);
01006 if(!froot->IsOpen())
01007 {
01008 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::ReadMarginalizedFromFile. Couldn't open file %s.",file));
01009 return 0;
01010 }
01011
01012 int k=0;
01013 int n=this->GetNParameters();
01014 for(int i=0;i<n;i++)
01015 {
01016 BCParameter * a = this->GetParameter(i);
01017 TH1D * h1 = (TH1D*)froot -> Get(Form("hist_%s_%s", this -> GetName().data(), a -> GetName().data()));
01018 if(h1)
01019 {
01020 h1->SetDirectory(0);
01021 if(this->SetMarginalized(i,h1))
01022 k++;
01023 }
01024 else
01025 BCLog::Out(BCLog::warning, BCLog::warning,
01026 Form("BCModel::ReadMarginalizedFromFile. Couldn't read histogram \"hist_%s_%s\" from file %s.",
01027 this -> GetName().data(), a -> GetName().data(), file));
01028 }
01029
01030 for(int i=0;i<n-1;i++)
01031 {
01032 for(int j=i+1;j<n;j++)
01033 {
01034 BCParameter * a = this->GetParameter(i);
01035 BCParameter * b = this->GetParameter(j);
01036 TH2D * h2 = (TH2D*)froot -> Get(Form("hist_%s_%s_%s", this -> GetName().data(), a -> GetName().data(), b -> GetName().data()));
01037 if(h2)
01038 {
01039 h2->SetDirectory(0);
01040 if(this->SetMarginalized(i,j,h2))
01041 k++;
01042 }
01043 else
01044 BCLog::Out(BCLog::warning, BCLog::warning,
01045 Form("BCModel::ReadMarginalizedFromFile. Couldn't read histogram \"hist_%s_%s_%s\" from file %s.",
01046 this -> GetName().data(), a -> GetName().data(), b -> GetName().data(), file));
01047 }
01048 }
01049
01050 froot->Close();
01051
01052 return k;
01053
01054 }
01055
01056
01057
01058 int BCModel::ReadErrorBandFromFile(const char * file)
01059 {
01060 TFile * froot = new TFile(file);
01061 if(!froot->IsOpen())
01062 {
01063 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::ReadErrorBandFromFile. Couldn't open file %s.",file));
01064 return 0;
01065 }
01066
01067 int r=0;
01068
01069 TH2D * h2 = (TH2D*)froot->Get("errorbandxy");
01070 if(h2)
01071 {
01072 h2->SetDirectory(0);
01073 this->SetErrorBandHisto(h2);
01074 r=1;
01075 }
01076 else
01077 BCLog::Out(BCLog::warning, BCLog::warning,
01078 Form("BCModel::ReadErrorBandFromFile. Couldn't read histogram \"errorbandxy\" from file %s.", file));
01079
01080 froot->Close();
01081
01082 return r;
01083
01084 }
01085
01086
01087
01088 int BCModel::PrintAllMarginalized1D(const char * filebase)
01089 {
01090 if(fMCMCH1Marginalized.size()==0)
01091 {
01092 BCLog::Out(BCLog::warning, BCLog::warning,
01093 "BCModel::PrintAllMarginalized2D. MarginalizeAll() has to be run prior to this to fill the distributions.");
01094 return 0;
01095 }
01096
01097 int n=this->GetNParameters();
01098 for(int i=0;i<n;i++)
01099 {
01100 BCParameter * a = this->GetParameter(i);
01101 this -> GetMarginalized(a) -> Print(Form("%s_1D_%s.ps",filebase,a->GetName().data()));
01102 }
01103
01104 return n;
01105 }
01106
01107
01108
01109 int BCModel::PrintAllMarginalized2D(const char * filebase)
01110 {
01111 if(fMCMCH2Marginalized.size()==0)
01112 {
01113 BCLog::Out(BCLog::warning, BCLog::warning,
01114 "BCModel::PrintAllMarginalized2D. MarginalizeAll() has to be run prior to this to fill the distributions.");
01115 return 0;
01116 }
01117
01118 int k=0;
01119 int n=this->GetNParameters();
01120 for(int i=0;i<n-1;i++)
01121 {
01122 for(int j=i+1;j<n;j++)
01123 {
01124 BCParameter * a = this->GetParameter(i);
01125 BCParameter * b = this->GetParameter(j);
01126 this -> GetMarginalized(a,b) -> Print(Form("%s_2D_%s_%s.ps",filebase,a->GetName().data(),b->GetName().data()));
01127 k++;
01128 }
01129 }
01130
01131 return k;
01132 }
01133
01134
01135
01136 int BCModel::PrintAllMarginalized(const char * file, int hdiv, int vdiv)
01137 {
01138 if (fMCMCH1Marginalized.size()==1 && fMCMCH2Marginalized.size()==0)
01139 {
01140 BCParameter * a = this->GetParameter(0);
01141
01142 this -> GetMarginalized(a) -> Print(file);
01143
01144 return 1;
01145 }
01146
01147
01148 if(fMCMCH1Marginalized.size()==0 || fMCMCH2Marginalized.size()==0)
01149 {
01150 BCLog::Out(BCLog::warning, BCLog::warning,
01151 "BCModel::PrintAllMarginalized. MarginalizeAll() has to be run prior to this to fill the distributions.");
01152 return 0;
01153 }
01154
01155 int n=0;
01156
01157
01158 TCanvas * c = new TCanvas("c","canvas");
01159
01160 int type = 112;
01161
01162 TPostScript * ps = new TPostScript(file,type);
01163
01164 ps->Range(24,18);
01165
01166
01167 ps->NewPage();
01168 c->cd();
01169 c->Clear();
01170 c->Divide(hdiv,vdiv);
01171
01172 int n1d=this->GetNParameters();
01173 for(int i=0;i<n1d;i++)
01174 {
01175
01176 if(i!=0 && i%(hdiv*vdiv)==0)
01177 {
01178 c->Update();
01179 ps->NewPage();
01180 c->cd();
01181 c->Clear();
01182 c->Divide(hdiv,vdiv);
01183 }
01184
01185
01186 c->cd(i%(hdiv*vdiv)+1);
01187
01188 BCParameter * a = this->GetParameter(i);
01189 this -> GetMarginalized(a) -> Draw();
01190 n++;
01191 }
01192
01193 c->Update();
01194
01195
01196 ps->NewPage();
01197 c->cd();
01198 c->Clear();
01199 c->Divide(hdiv,vdiv);
01200
01201 int k=0;
01202 int n2d=this->GetNParameters();
01203 for(int i=0;i<n2d-1;i++)
01204 {
01205 for(int j=i+1;j<n2d;j++)
01206 {
01207
01208 if(k!=0 && k%(hdiv*vdiv)==0)
01209 {
01210 c->Update();
01211 ps->NewPage();
01212 c->cd();
01213 c->Clear();
01214 c->Divide(hdiv,vdiv);
01215 }
01216
01217
01218 c->cd(k%(hdiv*vdiv)+1);
01219
01220 BCParameter * a = this->GetParameter(i);
01221 BCParameter * b = this->GetParameter(j);
01222 this -> GetMarginalized(a,b) -> Draw(52);
01223 k++;
01224 }
01225 }
01226
01227 c->Update();
01228 ps->Close();
01229
01230 delete c;
01231 delete ps;
01232
01233
01234 return n+k;
01235 }
01236
01237
01238
01239 BCH2D * BCModel::GetMarginalized(BCParameter * parameter1, BCParameter * parameter2)
01240 {
01241
01242
01243
01244
01245
01246
01247
01248
01249 int index1 = parameter1 -> GetIndex();
01250 int index2 = parameter2 -> GetIndex();
01251
01252 if (index1 > index2)
01253 {
01254 BCLog::Out(BCLog::warning, BCLog::warning,
01255 "BCModel::GetMarginalized. Index 1 has to be smaller than index 2.");
01256 return 0;
01257 }
01258
01259
01260
01261
01262 TH2D * hist = this -> MCMCGetH2Marginalized(index1, index2);
01263
01264 if(hist==0)
01265 return 0;
01266
01267 BCH2D * hprob = new BCH2D();
01268
01269
01270
01271 hist -> SetName(Form("hist_%s_%s_%s", this -> GetName().data(), parameter1 -> GetName().data(), parameter2 -> GetName().data()));
01272 hist -> SetXTitle(Form("%s", parameter1 -> GetName().data()));
01273 hist -> SetYTitle(Form("%s", parameter2 -> GetName().data()));
01274 hist -> SetStats(kFALSE);
01275
01276
01277 double gmode[] = { fBestFitParameters.at(index1), fBestFitParameters.at(index2) };
01278
01279 hprob->SetGlobalMode(gmode);
01280
01281
01282 hprob -> SetHistogram(hist);
01283
01284 return hprob;
01285
01286 }
01287
01288
01289
01290 void BCModel::CreateData(int ndatasets, std::vector <double> parameters)
01291 {
01292
01293
01294
01295 BCLog::Out(BCLog::summary, BCLog::summary, "CreateData");
01296
01297 std::vector <bool> grid;
01298 std::vector <double> limits;
01299
01300 for (int i = 0; i < this -> GetDataSet() -> GetDataPoint(0) -> GetNValues(); i++)
01301 grid.push_back(false);
01302
01303 return this -> CreateDataGrid(ndatasets, parameters, grid, limits);
01304
01305 }
01306
01307
01308
01309 void BCModel::CreateDataGrid(int ndatasets, std::vector <double> parameters, std::vector <bool> grid, std::vector <double> limits)
01310 {
01311
01312
01313
01314 TDirectory * dir = gDirectory;
01315
01316
01317
01318 std::fstream stream_data;
01319
01320
01321
01322 std::fstream stream_list;
01323
01324 char listname[200];
01325
01326 sprintf(listname, "./data/list_%s.txt", this -> GetName().data());
01327
01328 stream_list.open(listname, std::fstream::out);
01329
01330 if (!stream_list.is_open())
01331 {
01332 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::CreateDataGrid. Couldn't open filelist: %s", listname));
01333 return;
01334 }
01335
01336
01337
01338 int nvalues = this -> GetDataSet() -> GetDataPoint(0) -> GetNValues();
01339
01340
01341
01342 int ngridaxes = 0;
01343
01344 for (int i = 0; i < nvalues; i++)
01345 if (grid.at(i) == true)
01346 ngridaxes++;
01347
01348
01349
01350 double pmaximum = 0.0;
01351 double pmaximumsingle = 0.0;
01352
01353 int ninit = 1000;
01354
01355 for (int idataset = 0; idataset < ninit; idataset++)
01356 {
01357 std::vector <double> x;
01358
01359
01360
01361 for (int ivalue = 0; ivalue < nvalues; ivalue++)
01362 {
01363
01364
01365 if (this -> GetDataPointLowerBoundary(ivalue) != this -> GetDataPointUpperBoundary(ivalue))
01366 x.push_back(fRandom -> Uniform(this -> GetDataPointLowerBoundary(ivalue),
01367 this -> GetDataPointUpperBoundary(ivalue)));
01368 else
01369 x.push_back(this -> GetDataPointUpperBoundary(ivalue));
01370 }
01371
01372
01373
01374 this -> CorrelateDataPointValues(x);
01375
01376
01377
01378 BCDataPoint * datapoint = new BCDataPoint(x);
01379
01380
01381
01382 double p = this -> ConditionalProbabilityEntry(datapoint, parameters);
01383
01384
01385
01386 if (p > pmaximumsingle)
01387 pmaximumsingle = p;
01388
01389
01390
01391 delete datapoint;
01392
01393
01394
01395 x.clear();
01396 }
01397
01398
01399
01400 pmaximum = BCMath::Min(1.0, pmaximumsingle * this -> GetNDataPointsMaximum());
01401
01402
01403
01404 for (int idataset = 0; idataset < ndatasets; idataset++)
01405 {
01406
01407
01408 char filename[200];
01409
01410 sprintf(filename, "./data/data_%s_%d.txt", this -> GetName().data(), idataset);
01411
01412 stream_data.open(filename, std::fstream::out);
01413
01414 if (!stream_data.is_open())
01415 {
01416 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::CreateDataGrid. Couldn't open file: %s", filename));
01417 return;
01418 }
01419
01420
01421
01422 int nentries = 0;
01423
01424
01425
01426 if (ngridaxes == 0)
01427 {
01428 double pnentries = 0.0;
01429 double temp_rand = 1.0;
01430
01431 while (temp_rand > pnentries)
01432 {
01433 if (this -> GetNDataPointsMinimum() != this -> GetNDataPointsMaximum())
01434 nentries = BCMath::Nint(fRandom -> Uniform(this -> GetNDataPointsMinimum(),
01435 this -> GetNDataPointsMaximum()));
01436 else
01437 nentries = this -> GetNDataPointsMaximum();
01438
01439 pnentries = this -> PoissonProbability(nentries, parameters);
01440 temp_rand = fRandom -> Uniform(0, 1);
01441 }
01442 }
01443
01444
01445
01446 else
01447 {
01448 nentries = 1;
01449
01450 for (int i = 0; i < ngridaxes; i++)
01451 nentries *= BCMath::Nint(limits.at(2 + i * 3));
01452 }
01453
01454
01455
01456 for (int ientry = 0; ientry < nentries; ientry++)
01457 {
01458 double p = 0.0;
01459 double temp_rand = 1.0;
01460
01461
01462
01463 std::vector <double> x;
01464
01465 while (temp_rand > p)
01466 {
01467
01468
01469 x.clear();
01470
01471
01472
01473 for (int ivalue = 0; ivalue < nvalues; ivalue++)
01474 {
01475
01476
01477 if (grid.at(ivalue) == false)
01478 {
01479 if (this -> GetDataPointLowerBoundary(ivalue) != this -> GetDataPointUpperBoundary(ivalue))
01480 x.push_back(fRandom -> Uniform(this -> GetDataPointLowerBoundary(ivalue),
01481
01482 this -> GetDataPointUpperBoundary(ivalue)));
01483 else
01484 x.push_back(this -> GetDataPointUpperBoundary(ivalue));
01485 }
01486
01487
01488
01489 else
01490 {
01491 double xgrid = 0;
01492
01493 if (ngridaxes == 1)
01494 xgrid = limits.at(0) + double(ientry) * limits.at(1);
01495
01496 if (ngridaxes == 2)
01497 {
01498 int ngrid = 0;
01499
01500 for (int j = 0; j < ivalue; j++)
01501 if (grid.at(j) == true)
01502 ngrid++;
01503
01504 int ix = ientry % BCMath::Nint(limits.at(1 + ngrid * 3));
01505 xgrid = limits.at(ngrid * 3) + double(ix) * limits.at(1 + ngrid * 3);
01506 }
01507 x.push_back(xgrid);
01508 }
01509 }
01510
01511
01512
01513 this -> CorrelateDataPointValues(x);
01514
01515
01516
01517 BCDataPoint * datapoint = new BCDataPoint(x);
01518
01519
01520
01521 p = this -> ConditionalProbabilityEntry(datapoint, parameters);
01522
01523
01524
01525 if (p > pmaximum)
01526 {
01527 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::CreateDataGrid. Probability larger than expected. Set limit to 1.1 x prob.."));
01528 pmaximum = 1.1 * p;
01529 }
01530
01531
01532
01533 delete datapoint;
01534
01535
01536
01537 temp_rand = pmaximum * fRandom -> Uniform();
01538 }
01539
01540
01541
01542 for (int ivalue = 0; ivalue < nvalues; ivalue++)
01543 {
01544 stream_data << x.at(ivalue);
01545 if (ivalue < nvalues-1)
01546 stream_data << " ";
01547 }
01548
01549 if (ientry < nentries - 1)
01550 stream_data << std::endl;
01551
01552
01553
01554 x.clear();
01555 }
01556
01557
01558
01559 stream_list << filename;
01560
01561 if (idataset < ndatasets - 1)
01562 stream_list << std::endl;
01563
01564
01565
01566 stream_data.close();
01567 }
01568
01569
01570
01571 stream_list.close();
01572
01573
01574
01575 gDirectory = dir;
01576
01577 }
01578
01579
01580
01581 void BCModel::CreateDataGridROOT(int ndatasets, std::vector <double> parameters, std::vector <bool> grid, std::vector <double> limits)
01582 {
01583
01584 BCLog::Out(BCLog::detail, BCLog::detail, Form("BCModel::CreateDataGridROOT. Creating %d datasets",ndatasets));
01585
01586
01587
01588 TDirectory * dir = gDirectory;
01589
01590
01591
01592 TFile * outputfile;
01593
01594 char filename[200];
01595
01596 sprintf(filename, "./data/gof_%s.root", this -> GetName().data());
01597
01598 outputfile = new TFile(filename, "RECREATE");
01599
01600
01601
01602 int nvalues = this -> GetDataSet() -> GetDataPoint(0) -> GetNValues();
01603
01604
01605
01606 int ngridaxes = 0;
01607
01608 for (int i = 0; i < nvalues; i++)
01609 if (grid.at(i) == true)
01610 ngridaxes++;
01611
01612
01613
01614 double pmaximum = 0.0;
01615 double pmaximumsingle = 0.0;
01616
01617 int ninit = 1000;
01618
01619 for (int idataset = 0; idataset < ninit; idataset++)
01620 {
01621 std::vector <double> x;
01622
01623
01624
01625 for (int ivalue = 0; ivalue < nvalues; ivalue++)
01626 {
01627
01628
01629 if (this -> GetDataPointLowerBoundary(ivalue) != this -> GetDataPointUpperBoundary(ivalue))
01630 x.push_back(fRandom -> Uniform(this -> GetDataPointLowerBoundary(ivalue),
01631 this -> GetDataPointUpperBoundary(ivalue)));
01632 else
01633 x.push_back(this -> GetDataPointUpperBoundary(ivalue));
01634 }
01635
01636
01637
01638 this -> CorrelateDataPointValues(x);
01639
01640
01641
01642 BCDataPoint * datapoint = new BCDataPoint(x);
01643
01644
01645
01646 double p = this -> ConditionalProbabilityEntry(datapoint, parameters);
01647
01648
01649
01650 if (p > pmaximumsingle)
01651 pmaximumsingle = p;
01652
01653
01654
01655 delete datapoint;
01656
01657
01658
01659 x.clear();
01660 }
01661
01662
01663
01664 pmaximum = BCMath::Min(1.0, pmaximumsingle * this -> GetNDataPointsMaximum());
01665
01666
01667
01668 for (int idataset = 0; idataset < ndatasets; idataset++)
01669 {
01670
01671
01672
01673 TTree * tree = new TTree(Form("gof_tree_%i", idataset), Form("gof_tree_%i", idataset));
01674
01675
01676
01677 int ndatapoints = 0;
01678
01679
01680
01681 if (ngridaxes == 0)
01682 {
01683 double pndatapoints = 0.0;
01684 double temp_rand = 1.0;
01685
01686 while (temp_rand > pndatapoints)
01687 {
01688 if (this -> GetNDataPointsMinimum() != this -> GetNDataPointsMaximum())
01689 ndatapoints = BCMath::Nint(fRandom -> Uniform(this -> GetNDataPointsMinimum(),
01690 this -> GetNDataPointsMaximum()));
01691 else
01692 ndatapoints = this -> GetNDataPointsMaximum();
01693
01694 pndatapoints = this -> PoissonProbability(ndatapoints, parameters);
01695 temp_rand = fRandom -> Uniform(0, 1);
01696 }
01697 }
01698
01699
01700
01701 else
01702 {
01703 ndatapoints = 1;
01704
01705 for (int i = 0; i < ngridaxes; i++)
01706 ndatapoints *= BCMath::Nint(limits.at(2 + i * 3));
01707 }
01708
01709
01710
01711 double x[MAXNDATAPOINTVALUES];
01712
01713 for (int i = 0; i < nvalues; i++)
01714 tree -> Branch(Form("data_var_%i", i),
01715 &(x[i]),
01716 Form("data_var_%i/D", i));
01717
01718
01719
01720 for (int idatapoint = 0; idatapoint < ndatapoints; idatapoint++)
01721 {
01722
01723 double p = 0.0;
01724 double temp_rand = 1.0;
01725
01726
01727
01728 while (temp_rand > p)
01729 {
01730
01731
01732 for (int ivalue = 0; ivalue < nvalues; ivalue++)
01733 {
01734
01735
01736 if (grid.at(ivalue) == false)
01737 {
01738 if (this -> GetDataPointLowerBoundary(ivalue) != this -> GetDataPointUpperBoundary(ivalue))
01739 x[ivalue] = (fRandom -> Uniform(this -> GetDataPointLowerBoundary(ivalue),
01740 this -> GetDataPointUpperBoundary(ivalue)));
01741 else
01742 x[ivalue] = (this -> GetDataPointUpperBoundary(ivalue));
01743 }
01744
01745
01746
01747 else
01748 {
01749 double xgrid = 0;
01750
01751 if (ngridaxes == 1)
01752 xgrid = limits.at(0) + double(idatapoint) * limits.at(1);
01753
01754 if (ngridaxes == 2)
01755 {
01756 int ngrid = 0;
01757
01758 for (int j = 0; j < ivalue; j++)
01759 if (grid.at(j) == true)
01760 ngrid++;
01761
01762 int ix = idatapoint % BCMath::Nint(limits.at(1 + ngrid * 3));
01763 xgrid = limits.at(ngrid * 3) + double(ix) * limits.at(1 + ngrid * 3);
01764 }
01765
01766 x[ivalue] = (xgrid);
01767
01768 }
01769 }
01770
01771
01772
01773 std::vector <double> xvector;
01774
01775 for (int j = 0; j < nvalues; ++j)
01776 xvector.push_back(x[j]);
01777
01778 this -> CorrelateDataPointValues(xvector);
01779
01780
01781
01782 BCDataPoint * datapoint = new BCDataPoint(xvector);
01783
01784
01785
01786 p = this -> ConditionalProbabilityEntry(datapoint, parameters);
01787
01788
01789
01790 if (p > pmaximum)
01791 {
01792 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::CreateDataGrid. Probability larger than expected. Set limit to 1.1 x prob.."));
01793 pmaximum = 1.1 * p;
01794 }
01795
01796
01797
01798 delete datapoint;
01799
01800
01801
01802 temp_rand = pmaximum * fRandom -> Uniform();
01803 }
01804
01805
01806
01807 tree -> Fill();
01808 }
01809
01810
01811
01812 tree -> Write();
01813
01814 delete tree;
01815 }
01816
01817
01818
01819 outputfile -> Close();
01820
01821
01822
01823 gDirectory = dir;
01824
01825 }
01826
01827
01828
01829 BCH1D * BCModel::GoodnessOfFitTest(const char * filename, std::vector <double> parameters)
01830 {
01831
01832
01833
01834 BCH1D * probability = new BCH1D();
01835
01836
01837
01838 std::vector<double> likelihoodcontainer;
01839
01840
01841
01842 std::fstream file;
01843
01844 file.open(filename, std::fstream::in);
01845
01846
01847
01848 if (file.is_open() == false)
01849 {
01850 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::GoodnessOfFitTest. Couldn't open filelist: %s", filename));
01851 return probability;
01852 }
01853
01854
01855
01856 BCDataSet * fDataSetTemp = fDataSet;
01857
01858 double NormTemp = fNormalization;
01859
01860
01861
01862 BCLog::Out(BCLog::detail, BCLog::detail, "BCModel::GoodnessOfFitTest. Loop over files");
01863
01864 while (!file.eof())
01865 {
01866 char filename_temp[100];
01867
01868
01869
01870 file >> filename_temp;
01871
01872
01873
01874 BCDataSet * dataset = new BCDataSet();
01875
01876
01877
01878 dataset -> ReadDataFromFileTxt(filename_temp, fDataSetTemp -> GetDataPoint(0) -> GetNValues());
01879
01880
01881
01882 this -> SetDataSet(dataset);
01883
01884
01885
01886 double weight = 1.0;
01887
01888
01889
01890 weight = this -> Likelihood(parameters);
01891
01892
01893
01894 likelihoodcontainer.push_back(log10(weight));
01895 }
01896
01897
01898
01899 this -> SetDataSet(fDataSetTemp);
01900
01901 fNormalization = NormTemp;
01902
01903
01904
01905 double minimum = 0.0;
01906 double maximum = 0.0;
01907
01908 for (int i = 0; i < int(likelihoodcontainer.size()); i++)
01909 {
01910 if (likelihoodcontainer.at(i) < minimum)
01911 minimum = likelihoodcontainer.at(i);
01912
01913 if (likelihoodcontainer.at(i) > maximum || i == 0)
01914 maximum = likelihoodcontainer.at(i);
01915 }
01916
01917
01918
01919 TH1D * hist = new TH1D(Form("GOF_%s", this -> GetName().data()), "", 50, minimum - 0.1 * fabs(minimum), maximum + 0.1 * fabs(minimum));
01920 hist -> SetXTitle("log_{10}y=log_{10}p(data|#lambda^{*})");
01921 hist -> SetYTitle("1/N dN/dlog_{10}y");
01922 hist -> SetStats(kFALSE);
01923
01924
01925
01926 for (int i = 0; i < int(likelihoodcontainer.size()); i++)
01927 hist -> Fill(likelihoodcontainer.at(i));
01928
01929
01930
01931 hist -> Scale(1.0 / hist -> Integral());
01932
01933
01934
01935 probability -> SetHistogram(hist);
01936
01937
01938
01939 double likelihood = this -> Likelihood(parameters);
01940
01941
01942
01943
01944 int sumleft = 0;
01945
01946 for (int i = 0; i < int(likelihoodcontainer.size()); ++i)
01947 if (likelihoodcontainer.at(i) < log10(likelihood))
01948 sumleft++;
01949
01950 fPValue = double(sumleft) / double(likelihoodcontainer.size());
01951
01952 std::cout << std::endl;
01953 std::cout << " Goodness-of-fit : " << std::endl;
01954 std::cout << std::endl;
01955 std::cout << " Model : " << this -> GetName().data() << std::endl;
01956 std::cout << std::endl;
01957 std::cout << " Parameters : " << std::endl;
01958
01959 for (int i = 0; i < int(parameters.size()); i++)
01960 std::cout << " Parameter : "
01961 << fParameterSet -> at(i) -> GetName().data()
01962 << " = " << parameters.at(i) << std::endl;
01963 std::cout << std::endl;
01964 std::cout << " Conditional probability p(data|lambda*) = " << likelihood << std::endl;
01965 std::cout << " p-value = " << fPValue << std::endl;
01966 std::cout << std::endl;
01967
01968
01969
01970 likelihoodcontainer.clear();
01971
01972 return probability;
01973
01974 }
01975
01976
01977
01978 BCH1D * BCModel::GoodnessOfFitTestROOT(int ntrees, const char * filename, std::vector <double> parameters)
01979 {
01980
01981
01982
01983 BCH1D * probability = new BCH1D();
01984
01985
01986
01987 std::vector<double> likelihoodcontainer;
01988
01989
01990
01991 BCDataSet * fDataSetTemp = fDataSet;
01992
01993 double NormTemp = fNormalization;
01994
01995
01996
01997 BCLog::Out(BCLog::detail, BCLog::detail, "BCModel::GoodnessOfFitTest. Loop over trees");
01998
01999 int nvalues = fDataSet -> GetDataPoint(0) -> GetNValues();
02000
02001
02002
02003 char branchnames[200] = "";
02004 char treename[200] = "";
02005
02006 for (int ivalue = 0; ivalue < nvalues; ++ivalue)
02007 {
02008 sprintf(branchnames, "%sdata_var_%i", branchnames, ivalue);
02009 if (ivalue<nvalues-1)
02010 sprintf(branchnames, "%s,",branchnames);
02011 }
02012
02013 for (int itree = 0; itree < ntrees; ++itree)
02014 {
02015
02016
02017 sprintf(treename, "gof_tree_%i", itree);
02018
02019 BCDataSet * dataset = new BCDataSet();
02020
02021
02022
02023 dataset -> ReadDataFromFileTree((const char *) filename,
02024 treename,
02025 (const char *) branchnames);
02026
02027
02028
02029 this -> SetDataSet(dataset);
02030
02031
02032
02033 double weight = 1.0;
02034
02035
02036
02037 weight = this -> Likelihood(parameters);
02038
02039
02040
02041 likelihoodcontainer.push_back(log10(weight));
02042
02043 delete dataset;
02044 }
02045
02046
02047
02048 this -> SetDataSet(fDataSetTemp);
02049
02050 fNormalization = NormTemp;
02051
02052
02053
02054 double minimum = 0.0;
02055 double maximum = 0.0;
02056
02057 for (int i = 0; i < int(likelihoodcontainer.size()); i++)
02058 {
02059 if (likelihoodcontainer.at(i) < minimum)
02060 minimum = likelihoodcontainer.at(i);
02061
02062 if (likelihoodcontainer.at(i) > maximum || i == 0)
02063 maximum = likelihoodcontainer.at(i);
02064 }
02065
02066
02067
02068 TH1D * hist = new TH1D(Form("GOF_%s", this -> GetName().data()), "", 50, minimum - 0.1 * fabs(minimum), maximum + 0.1 * fabs(minimum));
02069 hist -> SetXTitle("log_{10}y=log_{10}p(data|#lambda^{*})");
02070 hist -> SetYTitle("1/N dN/dlog_{10}y");
02071 hist -> SetStats(kFALSE);
02072
02073
02074
02075 for (int i = 0; i < int(likelihoodcontainer.size()); i++)
02076 hist -> Fill(likelihoodcontainer.at(i));
02077
02078
02079
02080 hist -> Scale(1.0 / hist -> Integral());
02081
02082
02083
02084 probability -> SetHistogram(hist);
02085
02086
02087
02088 double likelihood = this -> Likelihood(parameters);
02089
02090
02091
02092
02093 int sumleft = 0;
02094
02095 for (int i = 0; i < int(likelihoodcontainer.size()); ++i)
02096 if (likelihoodcontainer.at(i) < log10(likelihood))
02097 sumleft++;
02098
02099 fPValue = double(sumleft) / double(likelihoodcontainer.size());
02100
02101 std::cout << std::endl;
02102 std::cout << " Goodness-of-fit : " << std::endl;
02103 std::cout << std::endl;
02104 std::cout << " Model : " << this -> GetName().data() << std::endl;
02105 std::cout << std::endl;
02106 std::cout << " Parameters : " << std::endl;
02107
02108 for (int i = 0; i < int(parameters.size()); i++)
02109 std::cout << " Parameter : "
02110 << fParameterSet -> at(i) -> GetName().data()
02111 << " = " << parameters.at(i) << std::endl;
02112 std::cout << std::endl;
02113 std::cout << " Conditional probability p(data|lambda*) = " << likelihood << std::endl;
02114 std::cout << " p-value = " << fPValue << std::endl;
02115 std::cout << std::endl;
02116
02117
02118
02119 likelihoodcontainer.clear();
02120
02121 return probability;
02122
02123 }
02124
02125
02126
02127 BCH1D * BCModel::DoGoodnessOfFitTest(int ndatasets, std::vector<double> parameters, std::vector <bool> grid, std::vector <double> limits)
02128 {
02129
02130
02131
02132 if (flag_ConditionalProbabilityEntry == false)
02133 {
02134 BCLog::Out(BCLog::warning, BCLog::warning, "BCModel::DoGoodnessOfFitTest. The method ConditionalProbabilityEntry has not been overloaded");
02135 return 0;
02136 }
02137
02138
02139
02140 BCLog::Out(BCLog::summary, BCLog::summary, "Do goodness-of-fit-test");
02141
02142
02143
02144 this -> CreateDataGrid(ndatasets, parameters, grid, limits);
02145
02146
02147
02148 BCH1D * gof = this -> GoodnessOfFitTest(Form("./data/list_%s.txt", this -> GetName().data()), parameters);
02149
02150 return gof;
02151
02152 }
02153
02154
02155
02156 BCH1D * BCModel::DoGoodnessOfFitTestROOT(int ndatasets, std::vector<double> parameters, std::vector <bool> grid, std::vector <double> limits)
02157 {
02158
02159
02160
02161 if (flag_ConditionalProbabilityEntry == false)
02162 {
02163 BCLog::Out(BCLog::warning, BCLog::warning, "BCModel::DoGoodnessOfFitTest. The method ConditionalProbabilityEntry has not been overloaded");
02164 return 0;
02165 }
02166
02167
02168
02169 BCLog::Out(BCLog::summary, BCLog::summary, "Do goodness-of-fit-test");
02170
02171
02172
02173 this -> CreateDataGridROOT(ndatasets, parameters, grid, limits);
02174
02175
02176
02177 BCH1D * gof = this -> GoodnessOfFitTestROOT(ndatasets, Form("./data/gof_%s.root", this -> GetName().data()), parameters);
02178
02179 return gof;
02180
02181 }
02182
02183
02184
02185 BCH1D * BCModel::DoGoodnessOfFitTest(int ndatasets, std::vector<double> parameters)
02186 {
02187
02188
02189
02190 if (flag_ConditionalProbabilityEntry == false)
02191 {
02192 BCLog::Out(BCLog::warning, BCLog::warning, "BCModel::DoGoodnessOfFitTest. The method ConditionalProbabilityEntry has not been overloaded");
02193 return 0;
02194 }
02195
02196
02197
02198 BCLog::Out(BCLog::summary, BCLog::summary, "Do goodness-of-fit-test");
02199
02200
02201
02202 this -> CreateData(ndatasets, parameters);
02203
02204
02205
02206 BCH1D * gof = this -> GoodnessOfFitTest(Form("./data/list_%s.txt", this -> GetName().data()), parameters);
02207
02208 return gof;
02209
02210 }
02211
02212
02213
02214 BCH1D * BCModel::DoGoodnessOfFitTest(int ndatasets)
02215 {
02216
02217
02218
02219 if (flag_ConditionalProbabilityEntry == false)
02220 {
02221 BCLog::Out(BCLog::warning, BCLog::warning, "BCModel::DoGoodnessOfFitTest. The method ConditionalProbabilityEntry has not been overloaded");
02222 return 0;
02223 }
02224
02225 std::vector<bool> grid;
02226 std::vector<double> limits;
02227
02228 return this -> DoGoodnessOfFitTest(ndatasets, this -> GetBestFitParameters(), grid, limits);
02229
02230 }
02231
02232
02233
02234 BCH1D * BCModel::DoGoodnessOfFitTest(const char * filename, std::vector<double> parameters)
02235 {
02236
02237
02238
02239 BCLog::Out(BCLog::summary, BCLog::summary, "Do goodness-of-fit-test");
02240
02241
02242
02243 BCH1D * gof = this -> GoodnessOfFitTest(filename, parameters);
02244
02245 return gof;
02246
02247 }
02248
02249
02250
02251 BCH1D * BCModel::DoGoodnessOfFitTest(const char * filename)
02252 {
02253
02254 return this -> DoGoodnessOfFitTest(filename, this -> GetBestFitParameters());
02255
02256 }
02257
02258
02259
02260 double BCModel::GetPvalueFromChi2(std::vector<double> par, int sigma_index)
02261 {
02262 double ll = this -> LogLikelihood(par);
02263 int n = this -> GetNDataPoints();
02264
02265 double sum_sigma=0;
02266 for (int i=0;i<n;i++)
02267 sum_sigma += log(this -> GetDataPoint(i) -> GetValue(sigma_index));
02268
02269 double chi2 = -2.*(ll + (double)n/2. * log(2.*M_PI) + sum_sigma);
02270
02271 return TMath::Prob(chi2,n);
02272 }
02273
02274
02275
02276 BCH1D * BCModel::CalculatePValue(std::vector<double> par, bool flag_histogram)
02277 {
02278
02279 BCH1D * hist = 0;
02280
02281
02282 BCLog::Out(BCLog::summary, BCLog::summary, "Do goodness-of-fit-test");
02283
02284
02285 BCModelTest * modeltest = new BCModelTest("modeltest");
02286
02287
02288 modeltest -> SetTestModel(this);
02289
02290
02291
02292 if (!modeltest -> SetTestPoint(par))
02293 return 0;
02294
02295
02296 fPValue = modeltest -> GetCalculatedPValue(flag_histogram);
02297
02298
02299 if (flag_histogram)
02300 {
02301 hist = new BCH1D();
02302 hist -> SetHistogram(modeltest -> GetHistogramLogProb());
02303 }
02304
02305
02306 delete modeltest;
02307
02308
02309 return hist;
02310
02311 }
02312
02313
02314
02315 void BCModel::CorrelateDataPointValues(std::vector<double> &x)
02316 {
02317
02318 }
02319
02320
02321
02322 double BCModel::HessianMatrixElement(BCParameter * parameter1, BCParameter * parameter2, std::vector<double> point)
02323 {
02324
02325
02326
02327 if (int(point.size()) != this -> GetNParameters())
02328 {
02329 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::HessianMatrixElement. Invalid number of entries in the vector."));
02330 return -1;
02331 }
02332
02333
02334
02335 double nsteps = 1e5;
02336
02337 double dx1 = (parameter1 -> GetUpperLimit() - parameter1 -> GetLowerLimit()) / nsteps;
02338 double dx2 = (parameter2 -> GetUpperLimit() - parameter2 -> GetLowerLimit()) / nsteps ;
02339
02340
02341
02342 std::vector<double> xpp;
02343 std::vector<double> xpm;
02344 std::vector<double> xmp;
02345 std::vector<double> xmm;
02346
02347 xpp = point;
02348 xpm = point;
02349 xmp = point;
02350 xmm = point;
02351
02352 xpp.at(parameter1 -> GetIndex()) = xpp.at(parameter1 -> GetIndex()) + dx1;
02353 xpp.at(parameter2 -> GetIndex()) = xpp.at(parameter2 -> GetIndex()) + dx2;
02354
02355 xpm.at(parameter1 -> GetIndex()) = xpm.at(parameter1 -> GetIndex()) + dx1;
02356 xpm.at(parameter2 -> GetIndex()) = xpm.at(parameter2 -> GetIndex()) - dx2;
02357
02358 xmp.at(parameter1 -> GetIndex()) = xmp.at(parameter1 -> GetIndex()) - dx1;
02359 xmp.at(parameter2 -> GetIndex()) = xmp.at(parameter2 -> GetIndex()) + dx2;
02360
02361 xmm.at(parameter1 -> GetIndex()) = xmm.at(parameter1 -> GetIndex()) - dx1;
02362 xmm.at(parameter2 -> GetIndex()) = xmm.at(parameter2 -> GetIndex()) - dx2;
02363
02364
02365
02366 double ppp = this -> Likelihood(xpp);
02367 double ppm = this -> Likelihood(xpm);
02368 double pmp = this -> Likelihood(xmp);
02369 double pmm = this -> Likelihood(xmm);
02370
02371
02372
02373 double derivative = (ppp + pmm - ppm - pmp) / (4.0 * dx1 * dx2);
02374
02375 return derivative;
02376
02377 }
02378
02379
02380
02381 void BCModel::FixDataAxis(int index, bool fixed)
02382 {
02383
02384
02385
02386 if (index < 0 || index > fDataSet -> GetDataPoint(0) -> GetNValues())
02387 {
02388 BCLog::Out(BCLog::warning, BCLog::warning,
02389 "BCModel::FixDataAxis. Index out of range.");
02390
02391 return;
02392 }
02393
02394 if (fDataFixedValues.size() == 0)
02395 fDataFixedValues.assign(fDataSet -> GetDataPoint(0) -> GetNValues(), false);
02396
02397 fDataFixedValues[index] = fixed;
02398
02399 }
02400
02401
02402
02403 bool BCModel::GetFixedDataAxis(int index)
02404 {
02405
02406
02407
02408 if (index < 0 || index > fDataSet -> GetDataPoint(0) -> GetNValues())
02409 {
02410 BCLog::Out(BCLog::warning, BCLog::warning,
02411 "BCModel::GetFixedDataAxis. Index out of range.");
02412
02413 return false;
02414 }
02415
02416 return fDataFixedValues.at(index);
02417
02418 }
02419
02420
02421
02422 void BCModel::PrintSummary()
02423 {
02424 int nparameters = this -> GetNParameters();
02425
02426
02427 std::cout
02428 <<std::endl
02429 <<" ---------------------------------"<<std::endl
02430 <<" Model : " << fName.data() <<std::endl
02431 <<" ---------------------------------"<<std::endl
02432 <<" Index : "<< fIndex <<std::endl
02433 <<" Number of parameters : "<< nparameters <<std::endl
02434 <<std::endl
02435 <<" - Parameters : " <<std::endl
02436 <<std::endl;
02437
02438
02439 for (int i=0; i<nparameters; i++)
02440 fParameterSet -> at(i) -> PrintSummary();
02441
02442
02443 if (this -> GetBestFitParameters().size() > 0)
02444 {
02445 std::cout
02446 <<std::endl
02447 <<" - Best fit parameters :"<<std::endl
02448 <<std::endl;
02449
02450 for (int i=0; i<nparameters; i++)
02451 {
02452 std::cout
02453 <<" "<< fParameterSet -> at(i) -> GetName().data()
02454 <<" = "<< this -> GetBestFitParameter(i)
02455 <<" (overall)"<<std::endl;
02456 if ((int)fBestFitParametersMarginalized.size() == nparameters)
02457 std::cout
02458 <<" "<< fParameterSet -> at(i) -> GetName().data()
02459 <<" = "<< this -> GetBestFitParameterMarginalized(i)
02460 <<" (marginalized)"<<std::endl;
02461 }
02462 }
02463
02464 std::cout<<std::endl;
02465
02466
02467 if (fPValue >= 0)
02468 {
02469 double likelihood = this -> Likelihood(this -> GetBestFitParameters());
02470
02471 std::cout
02472 <<" - Model testing:"<<std::endl
02473 <<std::endl
02474 <<" p(data|lambda*) = "<< likelihood <<std::endl
02475 <<" p-value = "<< fPValue <<std::endl
02476 <<std::endl;
02477 }
02478
02479
02480
02481 if (fNormalization > 0)
02482 std::cout <<" Normalization : " << fNormalization << std::endl;
02483
02484 }
02485
02486
02487
02488 void BCModel::PrintResults(const char * file)
02489 {
02490
02491
02492
02493
02494 ofstream ofi(file);
02495
02496
02497 if(!ofi.is_open())
02498 {
02499 std::cerr << "Couldn't open file " << file <<std::endl;
02500 return;
02501 }
02502
02503
02504 int npar = fParameterSet -> size();
02505
02506
02507 bool flag_conv = ((this -> MCMCGetNIterationsConvergenceGlobal() > 0)?1:0);
02508
02509 ofi << std::endl;
02510 ofi << " -----------------------------------------------------" << std::endl;
02511 ofi << " Summary of the Markov Chain Monte Carlo run" << std::endl;
02512 ofi << " -----------------------------------------------------" << std::endl;
02513 ofi << std::endl;
02514
02515 if (!flag_conv)
02516 {
02517 ofi << " WARNING: the Markov Chain did not converge! Be\n"
02518 << " cautious using the following results!" << std::endl;
02519 ofi << std::endl;
02520 }
02521
02522 ofi << " Model summary" << std::endl;
02523 ofi << " =============" << std::endl;
02524 ofi << " Model: " << fName.data() << std::endl;
02525 ofi << " Number of parameters: " << npar << std::endl;
02526 ofi << " List of Parameters and ranges: " << std::endl;
02527 for (int i = 0; i < npar; ++i)
02528 {
02529 ofi << " (" << i << ") Parameter \""
02530 << fParameterSet -> at(i) -> GetName().data() << "\""
02531 << ": " << fParameterSet -> at(i) -> GetLowerLimit()
02532 << " - "
02533 << fParameterSet -> at(i) -> GetUpperLimit() << std::endl;
02534 }
02535 ofi << std::endl;
02536
02537 if (flag_conv)
02538 {
02539 ofi << " Results of the marginalization" << std::endl;
02540 ofi << " ==============================" << std::endl;
02541 ofi << " List of parameters and properties of the marginalized\n"
02542 << " distributions:" << std::endl;
02543 for (int i = 0; i < npar; ++i)
02544 {
02545 BCH1D * bch1d = this -> GetMarginalized(fParameterSet -> at(i));
02546
02547 ofi << " (" << i << ") Parameter \""
02548 << fParameterSet -> at(i) -> GetName().data() << "\"" << std::endl;
02549 ofi << " Mean +- RMS: "
02550 << std::setprecision(4) << bch1d -> GetMean()
02551 << " +- "
02552 << std::setprecision(4) << bch1d -> GetRMS() << std::endl;
02553 ofi << " Median +- sigma: "
02554 << std::setprecision(4) << bch1d -> GetMedian()
02555 << " + " << std::setprecision(4) << bch1d -> GetQuantile(0.84) - bch1d -> GetMedian()
02556 << " - " << std::setprecision(4) << bch1d -> GetMedian() - bch1d -> GetQuantile(0.16) << std::endl;
02557 ofi << " (Marginalized) mode: " << bch1d -> GetMode() << std::endl;
02558 ofi << " Smallest interval(s) containing 68% and local modes: " << std::endl;
02559
02560 std::vector <double> v;
02561
02562 v = bch1d -> GetSmallestIntervals(0.68);
02563
02564 int ninter = int(v.size());
02565
02566 for (int j = 0; j < ninter; j+=5)
02567 ofi << " " << v.at(j) << " - " << v.at(j+1) << " (local mode at " << v.at(j+3) << " with rel. height " << v.at(j+2) << "; rel. area " << v.at(j+4) << ")" << std::endl;
02568 }
02569 ofi << std::endl;
02570 }
02571
02572 ofi << " Results of the optimization" << std::endl;
02573 ofi << " ===========================" << std::endl;
02574 ofi << " Optimization algorithm used: ";
02575 switch(this -> GetOptimizationMethod())
02576 {
02577 case BCIntegrate::kOptSimulatedAnnealing:
02578 ofi << " Simulated Annealing" << std::endl;
02579 break;
02580
02581 case BCIntegrate::kOptMinuit:
02582 ofi << " Minuit" << std::endl;
02583 break;
02584
02585 case BCIntegrate::kOptMetropolis:
02586 ofi << " MCMC " << std::endl;
02587 break;
02588 }
02589 ofi << " List of parameters and global mode: " << std::endl;
02590 for (int i = 0; i < npar; ++i)
02591 {
02592 ofi << " (" << i << ") Parameter \""
02593 << fParameterSet -> at(i) -> GetName().data() << "\": "
02594 << fBestFitParameters.at(i) << std::endl;
02595 }
02596 ofi << std::endl;
02597 if (fPValue >= 0.)
02598 {
02599 ofi << " Results of the model test" << std::endl;
02600 ofi << " =========================" << std::endl;
02601 ofi << " p-value at global mode: " << fPValue << std::endl;
02602 ofi << std::endl;
02603 }
02604
02605 ofi << " Status of the MCMC" << std::endl;
02606 ofi << " ==================" << std::endl;
02607 ofi << " Convergence reached: " << ((flag_conv)?"yes":"no") << std::endl;
02608 if (flag_conv)
02609 ofi << " Number of iterations until convergence: " << this -> MCMCGetNIterationsConvergenceGlobal() << std::endl;
02610 else
02611 ofi << " WARNING: the Markov Chain did not converge! Be\n"
02612 << " cautious using the following results!" << std::endl << std::endl;
02613 ofi << " Number of chains: " << this -> MCMCGetNChains() << std::endl;
02614 ofi << " Number of iterations of each chain: " << this -> MCMCGetNIterationsMax() << std::endl;
02615 ofi << std::endl;
02616
02617 ofi << " -----------------------------------------------------" << std::endl;
02618 ofi << std::endl;
02619 ofi << " Notes" << std::endl;
02620 ofi << " =====" << std::endl;
02621 ofi << " (i) Median +- sigma denotes the median, m, of the\n"
02622 << " marginalized distribution and the intervals from\n"
02623 << " m to the 16% and 84% quantiles. " << std::endl;
02624 ofi << " -----------------------------------------------------" << std::endl;
02625
02626
02627
02628
02629 }
02630
02631
02632
02633 void BCModel::PrintHessianMatrix(std::vector<double> parameters)
02634 {
02635
02636
02637 if (int(parameters.size()) != this -> GetNParameters())
02638 {
02639 BCLog::Out(BCLog::warning, BCLog::warning,
02640 Form("BCModel::PrintHessianMatrix. Invalid number of entries in the vector."));
02641 return;
02642 }
02643
02644
02645 std::cout << std::endl;
02646 std::cout << " Hessian matrix elements: " << std::endl;
02647 std::cout << " Point: ";
02648
02649 for (int i = 0; i < int(parameters.size()); i++)
02650 std::cout << parameters.at(i) << " ";
02651 std::cout << std::endl;
02652
02653
02654 for (int i = 0; i < this -> GetNParameters(); i++)
02655 for (int j = 0; j < i; j++)
02656 {
02657
02658 double hessianmatrixelement = this -> HessianMatrixElement(fParameterSet -> at(i),
02659 fParameterSet -> at(j), parameters);
02660
02661
02662 std::cout << " " << i << " " << j << " : " << hessianmatrixelement << std::endl;
02663 }
02664
02665 }
02666
02667
02668
02669 BCDataPoint * BCModel::VectorToDataPoint(std::vector<double> data)
02670 {
02671
02672 int sizeofvector = int(data.size());
02673
02674 BCDataPoint * datapoint = new BCDataPoint(sizeofvector);
02675
02676 datapoint -> SetValues(data);
02677
02678 return datapoint;
02679
02680 }
02681
02682
02683
02684 int BCModel::CompareStrings(const char * string1, const char * string2)
02685 {
02686
02687 int flag_same = 0;
02688
02689 if (strlen(string1) != strlen(string2))
02690 return -1;
02691
02692 for (int i = 0; i < int(strlen(string1)); i++)
02693 if (string1[i] != string2[i])
02694 flag_same = -1;
02695
02696 return flag_same;
02697
02698 }
02699
02700
02701