Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "BCModelManager.h"
00011
00012 #include "BCModel.h"
00013 #include "BCDataSet.h"
00014 #include "BCDataPoint.h"
00015 #include "BCLog.h"
00016 #include "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 > 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::OutError("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::OutError("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 BCLog::OutSummary("Running normalization of all models.");
00360
00361 for (unsigned int i = 0; i < GetNModels(); i++) {
00362 fModelContainer->at(i)->Normalize();
00363
00364
00365 normalization += (fModelContainer->at(i)->GetNormalization() * fModelContainer->at(i)->GetModelAPrioriProbability());
00366 }
00367
00368
00369 for (unsigned int i = 0; i < GetNModels(); i++)
00370 fModelContainer->at(i)->SetModelAPosterioriProbability(
00371 (fModelContainer->at(i)->GetNormalization() * fModelContainer->at(i)->GetModelAPrioriProbability()) /
00372 normalization);
00373 }
00374
00375
00376
00377 double BCModelManager::BayesFactor(const unsigned int imodel1, const unsigned int imodel2)
00378 {
00379
00380
00381
00382
00383
00384 const double norm1 = fModelContainer->at(imodel1)->GetNormalization();
00385 const double norm2 = fModelContainer->at(imodel2)->GetNormalization();
00386
00387
00388 if(norm1<0.) {
00389 BCLog::OutError(
00390 Form("BCModelManager::BayesFactor : Model %s (index %d) not normalized. Cannot calculate Bayes factor.",
00391 fModelContainer->at(imodel1)->GetName().data(),imodel1));
00392 return -1.;
00393 }
00394
00395
00396 if(norm2<0.) {
00397 BCLog::OutError(
00398 Form("BCModelManager::BayesFactor : Model %s (index %d) not normalized. Cannot calculate Bayes factor.",
00399 fModelContainer->at(imodel2)->GetName().data(),imodel2));
00400 return -1.;
00401 }
00402
00403
00404 if(norm2==0. && norm1!=0.) {
00405 BCLog::OutError(
00406 Form("BCModelManager::BayesFactor : Model %s (index %d) has ZERO probability. Bayes factor is infinite.",
00407 fModelContainer->at(imodel2)->GetName().data(),imodel2));
00408 return -1.;
00409 }
00410
00411
00412 if(norm2==0. && norm1==0.) {
00413 BCLog::OutWarning(
00414 Form("BCModelManager::BayesFactor : Models %s and %s have ZERO probability. Bayes factor is unknown. Returning 1.",
00415 fModelContainer->at(imodel2)->GetName().data(),fModelContainer->at(imodel1)->GetName().data()));
00416 return 1.;
00417 }
00418
00419
00420 return norm1/norm2;
00421 }
00422
00423
00424
00425 void BCModelManager::FindMode()
00426 {
00427
00428 for (unsigned int i = 0; i < GetNModels(); i++)
00429 GetModel(i)->FindMode();
00430 }
00431
00432
00433
00434 void BCModelManager::MarginalizeAll()
00435 {
00436
00437 for (unsigned int i = 0; i < GetNModels(); i++)
00438 GetModel(i)->MarginalizeAll();
00439 }
00440
00441
00442
00443 void BCModelManager::WriteMarkovChain(bool flag)
00444 {
00445
00446 for (unsigned int i = 0; i < GetNModels(); i++)
00447 GetModel(i)->WriteMarkovChain(flag);
00448 }
00449
00450
00451
00452 void BCModelManager::CalculatePValue(bool flag_histogram)
00453 {
00454
00455 for (unsigned int i = 0; i < GetNModels(); i++)
00456 GetModel(i)->CalculatePValue(GetModel(i)->GetBestFitParameters(), flag_histogram);
00457 }
00458
00459
00460
00461 void BCModelManager::PrintSummary(const char * file)
00462 {
00463 ofstream out;
00464 std::streambuf * old_buffer = 0;
00465
00466 if(file) {
00467 out.open(file);
00468 if (!out.is_open()) {
00469 std::cerr<<"Couldn't open file "<<file<<std::endl;
00470 return;
00471 }
00472 old_buffer = std::cout.rdbuf(out.rdbuf());
00473 }
00474
00475
00476 int nmodels = fModelContainer->size();
00477 std::cout<<std::endl
00478 <<"======================================"<<std::endl
00479 <<" Summary"<<std::endl
00480 <<"======================================"<<std::endl
00481 <<std::endl
00482 <<" Number of models : "<<nmodels<<std::endl
00483 <<std::endl
00484 <<" - Models:"<<std::endl;
00485
00486 for (int i = 0; i < nmodels; i++)
00487 fModelContainer->at(i)->PrintSummary();
00488
00489
00490 std::cout<<" - Data:"<<std::endl
00491 <<std::endl
00492 <<" Number of entries: "<<fDataSet->GetNDataPoints()<<std::endl
00493 <<std::endl;
00494
00495 std::cout<<"======================================"<<std::endl
00496 <<" Model comparison"<<std::endl
00497 <<std::endl;
00498
00499
00500 std::cout<<" - A priori probabilities:"<<std::endl<<std::endl;
00501
00502 for (int i=0; i<nmodels; i++)
00503 std::cout<<" p("<< fModelContainer->at(i)->GetName()
00504 <<") = "<< fModelContainer->at(i)->GetModelAPrioriProbability()
00505 <<std::endl;
00506 std::cout<<std::endl;
00507
00508 std::cout<<" - A posteriori probabilities:"<<std::endl<<std::endl;
00509
00510 for (int i = 0; i < nmodels; i++)
00511 std::cout<<" p("<< fModelContainer->at(i)->GetName()
00512 <<" | data) = "<< fModelContainer->at(i)->GetModelAPosterioriProbability()
00513 <<std::endl;
00514 std::cout<<std::endl;
00515
00516 std::cout<<"======================================"<<std::endl<<std::endl;
00517
00518 if (file)
00519 std::cout.rdbuf(old_buffer);
00520
00521 }
00522
00523
00524
00525 void BCModelManager::PrintModelComparisonSummary(const char * file)
00526 {
00527 ofstream out;
00528 std::streambuf * old_buffer = 0;
00529
00530 if(file) {
00531 out.open(file);
00532 if (!out.is_open()) {
00533 std::cerr<<"Couldn't open file "<<file<<std::endl;
00534 return;
00535 }
00536 old_buffer = std::cout.rdbuf(out.rdbuf());
00537 }
00538
00539
00540 int nmodels = fModelContainer->size();
00541 std::cout<<std::endl
00542 <<"==========================================="<<std::endl
00543 <<" Model Comparison Summary"<<std::endl
00544 <<"==========================================="<<std::endl
00545 <<std::endl
00546 <<" Number of models : "<<nmodels<<std::endl
00547 <<std::endl;
00548
00549
00550 std::cout<<" - A priori probabilities:"<<std::endl<<std::endl;
00551
00552 for (int i=0; i<nmodels; i++)
00553 std::cout<<" p("<< fModelContainer->at(i)->GetName()
00554 <<") = "<< fModelContainer->at(i)->GetModelAPrioriProbability()
00555 <<std::endl;
00556 std::cout<<std::endl;
00557
00558 std::cout<<" - A posteriori probabilities:"<<std::endl<<std::endl;
00559
00560 for (int i = 0; i < nmodels; i++)
00561 std::cout<<" p("<< fModelContainer->at(i)->GetName()
00562 <<" | data) = "<< fModelContainer->at(i)->GetModelAPosterioriProbability()
00563 <<std::endl;
00564 std::cout<<std::endl;
00565
00566
00567 std::cout<<" - Bayes factors:"<<std::endl<<std::endl;
00568 for (int i = 0; i < nmodels-1; i++)
00569 for (int j = i+1; j < nmodels; j++)
00570 std::cout<<" K = p(data | "<<fModelContainer->at(i)->GetName()<<") / "
00571 <<"p(data | "<<fModelContainer->at(j)->GetName()<<") = "
00572 <<BayesFactor(i,j)<<std::endl;
00573 std::cout<<std::endl;
00574
00575
00576 std::cout
00577 <<" - p-values:"<<std::endl
00578 <<std::endl;
00579
00580 for (int i = 0; i < nmodels; i++)
00581 {
00582 double p = fModelContainer->at(i)->GetPValue();
00583 std::cout <<" "<< fModelContainer->at(i)->GetName();
00584 if(p>=0.)
00585 std::cout<<": p-value = "<< p;
00586 else
00587 std::cout<<": p-value not calculated";
00588 std::cout<<std::endl;
00589 }
00590 std::cout<<std::endl;
00591
00592 std::cout<<"==========================================="<<std::endl<<std::endl;
00593
00594 if (file)
00595 std::cout.rdbuf(old_buffer);
00596
00597 }
00598
00599
00600
00601 void BCModelManager::PrintResults()
00602 {
00603
00604 for (unsigned int i = 0; i < GetNModels(); i++)
00605 GetModel(i)->PrintResults(Form("%s.txt", GetModel(i)->GetName().data()));
00606 }
00607
00608
00609
00610 void BCModelManager::Copy(BCModelManager & modelmanager) const
00611 {
00612
00613 modelmanager.fModelContainer = fModelContainer;
00614 modelmanager.fDataSet = fDataSet;
00615 }
00616
00617