Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "BAT/BCModelManager.h"
00011
00012 #include "BAT/BCModel.h"
00013 #include "BAT/BCDataSet.h"
00014 #include "BAT/BCDataPoint.h"
00015 #include "BAT/BCLog.h"
00016 #include "BAT/BCErrorCodes.h"
00017
00018 #include <fstream>
00019 #include <iostream>
00020
00021 #include <TString.h>
00022
00023
00024
00025 BCModelManager::BCModelManager()
00026 {
00027 fModelContainer = new BCModelContainer();
00028 fDataSet = 0;
00029 }
00030
00031
00032
00033 BCModelManager::~BCModelManager()
00034 {
00035 delete fModelContainer;
00036
00037 if (fDataSet)
00038 delete fDataSet;
00039 }
00040
00041
00042
00043 BCModelManager::BCModelManager(const BCModelManager & modelmanager)
00044 {
00045 modelmanager.Copy(*this);
00046 }
00047
00048
00049
00050 BCModelManager & BCModelManager::operator = (const BCModelManager & modelmanager)
00051 {
00052 if (this != &modelmanager)
00053 modelmanager.Copy(* this);
00054
00055 return * this;
00056 }
00057
00058
00059
00060 void BCModelManager::SetDataSet(BCDataSet * dataset)
00061 {
00062
00063 fDataSet = dataset;
00064
00065
00066 for (unsigned int i = 0; i < GetNModels(); i++)
00067 GetModel(i)->SetDataSet(fDataSet);
00068 }
00069
00070
00071
00072 void BCModelManager::SetSingleDataPoint(BCDataPoint * datapoint)
00073 {
00074
00075 BCDataSet * dataset = new BCDataSet();
00076
00077
00078 dataset->AddDataPoint(datapoint);
00079
00080
00081 SetDataSet(dataset);
00082 }
00083
00084
00085
00086 void BCModelManager::SetSingleDataPoint(BCDataSet * dataset, unsigned int index)
00087 {
00088 if (index < 0 || index > dataset->GetNDataPoints())
00089 return;
00090
00091 SetSingleDataPoint(dataset->GetDataPoint(index));
00092 }
00093
00094
00095
00096 void BCModelManager::AddModel(BCModel * model, double probability)
00097 {
00098
00099 unsigned int index = fModelContainer->size();
00100
00101
00102 model->SetIndex(index);
00103
00104
00105 model->SetModelAPrioriProbability(probability);
00106
00107
00108 model->SetDataSet(fDataSet);
00109
00110
00111 fModelContainer->push_back(model);
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 void BCModelManager::SetIntegrationMethod(BCIntegrate::BCIntegrationMethod method)
00129 {
00130
00131
00132 for (unsigned int i = 0; i < GetNModels(); i++)
00133 GetModel(i)->SetIntegrationMethod(method);
00134 }
00135
00136
00137
00138 void BCModelManager::SetMarginalizationMethod(BCIntegrate::BCMarginalizationMethod method)
00139 {
00140
00141 for (unsigned int i = 0; i < GetNModels(); i++)
00142 GetModel(i)->SetMarginalizationMethod(method);
00143 };
00144
00145
00146
00147 void BCModelManager::SetOptimizationMethod(BCIntegrate::BCOptimizationMethod method)
00148 {
00149
00150 for (unsigned int i = 0; i < GetNModels(); i++)
00151 GetModel(i)->SetOptimizationMethod(method);
00152 }
00153
00154
00155
00156 void BCModelManager::SetNiterationsPerDimension(unsigned int niterations)
00157 {
00158
00159 for (unsigned int i = 0; i < GetNModels(); i++)
00160 GetModel(i)->SetNiterationsPerDimension(niterations);
00161 }
00162
00163
00164
00165 void BCModelManager::SetNSamplesPer2DBin(unsigned int n)
00166 {
00167
00168 for (unsigned int i = 0; i < GetNModels(); i++)
00169 GetModel(i)->SetNSamplesPer2DBin(n);
00170 }
00171
00172
00173
00174 void BCModelManager::SetRelativePrecision(double relprecision)
00175 {
00176
00177 for (unsigned int i = 0; i < GetNModels(); i++)
00178 GetModel(i)->SetRelativePrecision(relprecision);
00179 }
00180
00181
00182
00183 void BCModelManager::SetNbins(unsigned int n)
00184 {
00185
00186 for (unsigned int i = 0; i < GetNModels(); i++)
00187 GetModel(i)->BCIntegrate::SetNbins(n);
00188 }
00189
00190
00191
00192 void BCModelManager::SetFillErrorBand(bool flag)
00193 {
00194 for (unsigned int i = 0; i < GetNModels(); i++)
00195 GetModel(i)->SetFillErrorBand(flag);
00196 }
00197
00198
00199
00200 void BCModelManager::SetFitFunctionIndexX(int index)
00201 {
00202
00203 for (unsigned int i = 0; i < GetNModels(); i++)
00204 GetModel(i)->SetFitFunctionIndexX(index);
00205 }
00206
00207
00208
00209 void BCModelManager::SetFitFunctionIndexY(int index)
00210 {
00211
00212 for (unsigned int i = 0; i < GetNModels(); i++)
00213 GetModel(i)->SetFitFunctionIndexY(index);
00214 }
00215
00216
00217
00218 void BCModelManager::SetFitFunctionIndices(int indexx, int indexy)
00219 {
00220
00221 for (unsigned int i = 0; i < GetNModels(); i++)
00222 GetModel(i)->SetFitFunctionIndices(indexx, indexy);
00223 }
00224
00225
00226
00227 void BCModelManager::SetDataPointLowerBoundaries(BCDataPoint * datasetlowerboundaries)
00228 {
00229
00230 for (unsigned int i = 0; i < GetNModels(); i++)
00231 GetModel(i)->SetDataPointLowerBoundaries(datasetlowerboundaries);
00232 }
00233
00234
00235
00236 void BCModelManager::SetDataPointUpperBoundaries(BCDataPoint * datasetupperboundaries)
00237 {
00238
00239 for (unsigned int i = 0; i < GetNModels(); i++)
00240 GetModel(i)->SetDataPointUpperBoundaries(datasetupperboundaries);
00241 }
00242
00243
00244
00245 void BCModelManager::SetDataPointLowerBoundary(int index, double lowerboundary)
00246 {
00247
00248 for (unsigned int i = 0; i < GetNModels(); i++)
00249 GetModel(i)->SetDataPointLowerBoundary(index, lowerboundary);
00250 }
00251
00252
00253
00254 void BCModelManager::SetDataPointUpperBoundary(int index, double upperboundary)
00255 {
00256
00257 for (unsigned int i = 0; i < GetNModels(); i++)
00258 GetModel(i)->SetDataPointUpperBoundary(index, upperboundary);
00259 }
00260
00261
00262
00263 void BCModelManager::SetDataBoundaries(int index, double lowerboundary, double upperboundary)
00264 {
00265
00266 for (unsigned int i = 0; i < GetNModels(); i++)
00267 GetModel(i)->SetDataBoundaries(index, lowerboundary, upperboundary);
00268 }
00269
00270
00271
00272 void BCModelManager::FixDataAxis(int index, bool fixed)
00273 {
00274
00275 for (unsigned int i = 0; i < GetNModels(); i++)
00276 GetModel(i)->FixDataAxis(index, fixed);
00277 }
00278
00279
00280
00281 void BCModelManager::SetNChains(unsigned int n)
00282 {
00283
00284 for (unsigned int i = 0; i < GetNModels(); i++)
00285 GetModel(i)->MCMCSetNChains(n);
00286 }
00287
00288
00289
00290 int BCModelManager::ReadDataFromFileTree(const char * filename, const char * treename, const char * branchnames)
00291 {
00292 if (fModelContainer->size() < 0) {
00293 BCLog::Out(BCLog::warning, BCLog::warning, "BCModelManager::ReadDataFromFileTree : No model defined.");
00294 return ERROR_NOMODELS;
00295 }
00296
00297
00298 if (!fDataSet)
00299 fDataSet = new BCDataSet();
00300 else
00301 fDataSet->Reset();
00302
00303
00304 int read_file = fDataSet->ReadDataFromFileTree(filename, treename, branchnames);
00305
00306 if (read_file >=0) {
00307 SetDataSet(fDataSet);
00308
00309 for (unsigned int i = 0; i < GetNModels(); i++)
00310 fModelContainer->at(i)->SetDataSet(fDataSet);
00311 }
00312 else if (read_file == ERROR_FILENOTFOUND) {
00313 delete fDataSet;
00314 return ERROR_FILENOTFOUND;
00315 }
00316
00317 return 0;
00318 }
00319
00320
00321
00322 int BCModelManager::ReadDataFromFileTxt(const char * filename, int nbranches)
00323 {
00324 if (fModelContainer->size() < 0) {
00325 BCLog::Out(BCLog::warning, BCLog::warning, "BCModelManager::ReadDataFromFileTree. No model defined.");
00326 return ERROR_NOMODELS;
00327 }
00328
00329
00330 if (!fDataSet)
00331 fDataSet = new BCDataSet();
00332 else
00333 fDataSet->Reset();
00334
00335
00336 int read_file = fDataSet->ReadDataFromFileTxt(filename, nbranches);
00337
00338 if (read_file >=0) {
00339 SetDataSet(fDataSet);
00340
00341 for (unsigned int i = 0; i < GetNModels(); i++)
00342 fModelContainer->at(i)->SetDataSet(fDataSet);
00343 }
00344 else {
00345 delete fDataSet;
00346 return ERROR_FILENOTFOUND;
00347 }
00348
00349 return 0;
00350 }
00351
00352
00353
00354 void BCModelManager::Normalize()
00355 {
00356
00357 double normalization = 0.0;
00358
00359
00360 BCLog::OutSummary("Running normalization of all models.");
00361
00362 for (unsigned int i = 0; i < GetNModels(); i++) {
00363 fModelContainer->at(i)->Normalize();
00364
00365
00366 normalization += (fModelContainer->at(i)->GetNormalization() * fModelContainer->at(i)->GetModelAPrioriProbability());
00367 }
00368
00369
00370 for (unsigned int i = 0; i < GetNModels(); i++)
00371 fModelContainer->at(i)->SetModelAPosterioriProbability(
00372 (fModelContainer->at(i)->GetNormalization() * fModelContainer->at(i)->GetModelAPrioriProbability()) /
00373 normalization);
00374 }
00375
00376
00377
00378 double BCModelManager::BayesFactor(const unsigned int imodel1, const unsigned int imodel2)
00379 {
00380
00381
00382
00383
00384
00385 const double norm1 = fModelContainer->at(imodel1)->GetNormalization();
00386 const double norm2 = fModelContainer->at(imodel2)->GetNormalization();
00387
00388
00389 if(norm1<0.) {
00390 BCLog::OutError(
00391 Form("Model %s (index %d) not normalized. Cannot calculate Bayes factor.",
00392 fModelContainer->at(imodel1)->GetName().data(),imodel1));
00393 return -1.;
00394 }
00395
00396
00397 if(norm2<0.) {
00398 BCLog::OutError(
00399 Form("Model %s (index %d) not normalized. Cannot calculate Bayes factor.",
00400 fModelContainer->at(imodel2)->GetName().data(),imodel2));
00401 return -1.;
00402 }
00403
00404
00405 if(norm2==0. && norm1!=0.) {
00406 BCLog::OutError(
00407 Form("Model %s (index %d) has ZERO probability. Bayes factor is infinite.",
00408 fModelContainer->at(imodel2)->GetName().data(),imodel2));
00409 return -1.;
00410 }
00411
00412
00413 if(norm2==0. && norm1==0.) {
00414 BCLog::OutWarning(
00415 Form("Models %s and %s have ZERO probability. Bayes factor is unknown. Returning 1.",
00416 fModelContainer->at(imodel2)->GetName().data(),fModelContainer->at(imodel1)->GetName().data()));
00417 return 1.;
00418 }
00419
00420
00421 return norm1/norm2;
00422 }
00423
00424
00425
00426 void BCModelManager::FindMode()
00427 {
00428
00429 for (unsigned int i = 0; i < GetNModels(); i++)
00430 GetModel(i)->FindMode();
00431 }
00432
00433
00434
00435 void BCModelManager::MarginalizeAll()
00436 {
00437
00438 for (unsigned int i = 0; i < GetNModels(); i++)
00439 GetModel(i)->MarginalizeAll();
00440 }
00441
00442
00443
00444 void BCModelManager::WriteMarkovChain(bool flag)
00445 {
00446
00447 for (unsigned int i = 0; i < GetNModels(); i++)
00448 GetModel(i)->WriteMarkovChain(flag);
00449 }
00450
00451
00452
00453 void BCModelManager::CalculatePValue(bool flag_histogram)
00454 {
00455
00456 for (unsigned int i = 0; i < GetNModels(); i++)
00457 GetModel(i)->CalculatePValue(GetModel(i)->GetBestFitParameters(), flag_histogram);
00458 }
00459
00460
00461
00462 void BCModelManager::PrintSummary(const char * file)
00463 {
00464 ofstream out;
00465 std::streambuf * old_buffer = 0;
00466
00467 if(file) {
00468 out.open(file);
00469 if (!out.is_open()) {
00470 std::cerr<<"Couldn't open file "<<file<<std::endl;
00471 return;
00472 }
00473 old_buffer = std::cout.rdbuf(out.rdbuf());
00474 }
00475
00476
00477 int nmodels = fModelContainer->size();
00478 std::cout<<std::endl
00479 <<"======================================"<<std::endl
00480 <<" Summary"<<std::endl
00481 <<"======================================"<<std::endl
00482 <<std::endl
00483 <<" Number of models : "<<nmodels<<std::endl
00484 <<std::endl
00485 <<" - Models:"<<std::endl;
00486
00487 for (int i = 0; i < nmodels; i++)
00488 fModelContainer->at(i)->PrintSummary();
00489
00490
00491 std::cout<<" - Data:"<<std::endl
00492 <<std::endl
00493 <<" Number of entries: "<<fDataSet->GetNDataPoints()<<std::endl
00494 <<std::endl;
00495
00496 std::cout<<"======================================"<<std::endl
00497 <<" Model comparison"<<std::endl
00498 <<std::endl;
00499
00500
00501 std::cout<<" - A priori probabilities:"<<std::endl<<std::endl;
00502
00503 for (int i=0; i<nmodels; i++)
00504 std::cout<<" p("<< fModelContainer->at(i)->GetName()
00505 <<") = "<< fModelContainer->at(i)->GetModelAPrioriProbability()
00506 <<std::endl;
00507 std::cout<<std::endl;
00508
00509 std::cout<<" - A posteriori probabilities:"<<std::endl<<std::endl;
00510
00511 for (int i = 0; i < nmodels; i++)
00512 std::cout<<" p("<< fModelContainer->at(i)->GetName()
00513 <<" | data) = "<< fModelContainer->at(i)->GetModelAPosterioriProbability()
00514 <<std::endl;
00515 std::cout<<std::endl;
00516
00517 std::cout<<"======================================"<<std::endl<<std::endl;
00518
00519 if (file)
00520 std::cout.rdbuf(old_buffer);
00521
00522 }
00523
00524
00525
00526 void BCModelManager::PrintModelComparisonSummary(const char * file)
00527 {
00528 ofstream out;
00529 std::streambuf * old_buffer = 0;
00530
00531 if(file) {
00532 out.open(file);
00533 if (!out.is_open()) {
00534 std::cerr<<"Couldn't open file "<<file<<std::endl;
00535 return;
00536 }
00537 old_buffer = std::cout.rdbuf(out.rdbuf());
00538 }
00539
00540
00541 int nmodels = fModelContainer->size();
00542 std::cout<<std::endl
00543 <<"==========================================="<<std::endl
00544 <<" Model Comparison Summary"<<std::endl
00545 <<"==========================================="<<std::endl
00546 <<std::endl
00547 <<" Number of models : "<<nmodels<<std::endl
00548 <<std::endl;
00549
00550
00551 std::cout<<" - A priori probabilities:"<<std::endl<<std::endl;
00552
00553 for (int i=0; i<nmodels; i++)
00554 std::cout<<" p("<< fModelContainer->at(i)->GetName()
00555 <<") = "<< fModelContainer->at(i)->GetModelAPrioriProbability()
00556 <<std::endl;
00557 std::cout<<std::endl;
00558
00559 std::cout<<" - A posteriori probabilities:"<<std::endl<<std::endl;
00560
00561 for (int i = 0; i < nmodels; i++)
00562 std::cout<<" p("<< fModelContainer->at(i)->GetName()
00563 <<" | data) = "<< fModelContainer->at(i)->GetModelAPosterioriProbability()
00564 <<std::endl;
00565 std::cout<<std::endl;
00566
00567
00568 std::cout<<" - Bayes factors:"<<std::endl<<std::endl;
00569 for (int i = 0; i < nmodels-1; i++)
00570 for (int j = i+1; j < nmodels; j++)
00571 std::cout<<" K = p(data | "<<fModelContainer->at(i)->GetName()<<") / "
00572 <<"p(data | "<<fModelContainer->at(j)->GetName()<<") = "
00573 <<BayesFactor(i,j)<<std::endl;
00574 std::cout<<std::endl;
00575
00576
00577 std::cout
00578 <<" - p-values:"<<std::endl
00579 <<std::endl;
00580
00581 for (int i = 0; i < nmodels; i++)
00582 {
00583 double p = fModelContainer->at(i)->GetPValue();
00584 std::cout <<" "<< fModelContainer->at(i)->GetName();
00585 if(p>=0.)
00586 std::cout<<": p-value = "<< p;
00587 else
00588 std::cout<<": p-value not calculated";
00589 std::cout<<std::endl;
00590 }
00591 std::cout<<std::endl;
00592
00593 std::cout<<"==========================================="<<std::endl<<std::endl;
00594
00595 if (file)
00596 std::cout.rdbuf(old_buffer);
00597
00598 }
00599
00600
00601
00602 void BCModelManager::PrintResults()
00603 {
00604
00605 for (unsigned int i = 0; i < GetNModels(); i++)
00606 GetModel(i)->PrintResults(Form("%s.txt", GetModel(i)->GetName().data()));
00607 }
00608
00609
00610
00611 void BCModelManager::Copy(BCModelManager & modelmanager) const
00612 {
00613
00614 modelmanager.fModelContainer = fModelContainer;
00615 modelmanager.fDataSet = fDataSet;
00616 }
00617
00618