• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

BCModelManager.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008-2010, Daniel Kollar and Kevin Kroeninger.
00003  * All rights reserved.
00004  *
00005  * For the licensing terms see doc/COPYING.
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    // set data set
00063    fDataSet = dataset;
00064 
00065    // set data set of all models in the manager
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    // create new data set consisting of a single data point
00075    BCDataSet * dataset = new BCDataSet();
00076 
00077    // add the data point
00078    dataset->AddDataPoint(datapoint);
00079 
00080    // set this new data set
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    // create index
00099    unsigned int index = fModelContainer->size();
00100 
00101    // set index of new model
00102    model->SetIndex(index);
00103 
00104    // set a priori probability of new model
00105    model->SetModelAPrioriProbability(probability);
00106 
00107    // set data set
00108    model->SetDataSet(fDataSet);
00109 
00110    // fill model into container
00111    fModelContainer->push_back(model);
00112 }
00113 
00114 // ---------------------------------------------------------
00115 // DEBUG DELETE?
00116 /*
00117 void BCModelManager::SetNIterationsMax(int niterations)
00118 {
00119    // set maximum number of iterations of all models in the manager
00120 
00121    for (unsigned int i = 0; i < GetNModels(); i++)
00122       GetModel(i)->SetNIterationsMax(niterations);
00123 }
00124 
00125 // ---------------------------------------------------------
00126 */
00127 
00128 void BCModelManager::SetIntegrationMethod(BCIntegrate::BCIntegrationMethod method)
00129 {
00130    // set integration method for all models registered
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    // set marginalization method for all models registered
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    // set mode finding method for all models registered
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    // set number of iterations per dimension for all models registered
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    // set samples per 2d bin for all models registered
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    // set relative precision for all models registered
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    // set number of bins for all models registered
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    // set fit function x index for all models registered
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    // set  fit function y index for all models registered
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    // set fit function indices for all models registered
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    // set lower boundary point for all models registered
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    // set upper boundary point for all models registered
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    // set lower bounday values for all models registered
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    // set upper boundary values for all models registered
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    // set lower and upper boundary values for all models registered
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    // fix axis for all models
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    // set number of Markov chains for all models registered
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    // create data set
00298    if (!fDataSet)
00299       fDataSet = new BCDataSet();
00300    else
00301       fDataSet->Reset();
00302 
00303    // read data from tree
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    // create data set
00330    if (!fDataSet)
00331       fDataSet = new BCDataSet();
00332    else
00333       fDataSet->Reset();
00334 
00335    // read data from txt file
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    // initialize likelihood norm
00357    double normalization = 0.0;
00358 
00359 //   BCLog::Out(BCLog::summary, BCLog::summary, "Running normalization of all models.");
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       // add to total normalization
00366       normalization += (fModelContainer->at(i)->GetNormalization() * fModelContainer->at(i)->GetModelAPrioriProbability());
00367    }
00368 
00369    // set model a posteriori probabilities
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    // Bayes Factors are the likelihoods integrated over the parameters
00381    // Is this equal to the posteriors?
00382    //    NOOOO
00383    // But it is equal to normalization factors.
00384 
00385    const double norm1 = fModelContainer->at(imodel1)->GetNormalization();
00386    const double norm2 = fModelContainer->at(imodel2)->GetNormalization();
00387 
00388    // check model 1
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    // check model 2
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    // denominator cannot be zero
00405    if(norm2==0. && norm1!=0.) {// not good since norm2 is double !!!
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    // denominator cannot be zero unless also numerator is zero
00413    if(norm2==0. && norm1==0.) {// not good since norm2 and norm1 are both double !!!
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    // now calculate the factor
00421    return norm1/norm2;
00422 }
00423 
00424 // ---------------------------------------------------------
00425 
00426 void BCModelManager::FindMode()
00427 {
00428    // finds mode for all models registered
00429    for (unsigned int i = 0; i < GetNModels(); i++)
00430       GetModel(i)->FindMode();
00431 }
00432 
00433 // ---------------------------------------------------------
00434 
00435 void BCModelManager::MarginalizeAll()
00436 {
00437    // marginalizes all models registered
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    // marginalizes all models registered
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    // calculate p-value for all models
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    // model summary
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    // data summary
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    // probability summary
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    // model summary
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    // probability summary
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    // Bayes factors summary
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    // p-values summary
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    // print summary of all models
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    // don't copy the content only the pointers
00614    modelmanager.fModelContainer = fModelContainer;
00615    modelmanager.fDataSet        = fDataSet;
00616 }
00617 
00618 // ---------------------------------------------------------

Generated on Mon Aug 30 2010 22:14:54 for Bayesian Analysis Toolkit by  doxygen 1.7.1