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

BCGoFTest.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/BCGoFTest.h"
00011 
00012 #include "BAT/BCDataSet.h"
00013 #include "BAT/BCLog.h"
00014 
00015 #include <TH1D.h>
00016 #include <TString.h>
00017 
00018 // ---------------------------------------------------------
00019 
00020 BCGoFTest::BCGoFTest(const char* name) : BCModel(name)
00021 {
00022    // set original data set to zero
00023    fTemporaryDataSet = 0;
00024 
00025    // set test mode to zero
00026    fTestModel = 0;
00027 
00028    // reset pvalue and counter
00029    fPValue = 0;
00030    fPValueAbove = 0;
00031    fPValueBelow = 0;
00032 
00033    // reset loglikelihood and range
00034    fLogLikelihood = 0;
00035    fLogLikelihoodMin = 1e99;
00036    fLogLikelihoodMax = -1e99;
00037 
00038    // define new histogram
00039    fHistogramLogProb = 0;
00040 
00041    // set defaults for the MCMC
00042    this -> MCMCSetNChains(5);
00043    this -> MCMCSetNIterationsMax(100000);
00044    this -> MCMCSetNIterationsRun(2000);
00045 }
00046 
00047 // ---------------------------------------------------------
00048 
00049 BCGoFTest::~BCGoFTest()
00050 {
00051    // restore original data set
00052 
00053    // get number of data points and values
00054    int ndatapoints = fTemporaryDataSet -> GetNDataPoints();
00055    int ndatavalues = fTemporaryDataSet -> GetDataPoint(0) -> GetNValues();
00056 
00057    for (int i = 0; i < ndatapoints; ++i)
00058       for (int j = 0; j < ndatavalues; ++j)
00059          fTestModel -> GetDataSet() -> GetDataPoint(i) -> SetValue(j, fTemporaryDataSet -> GetDataPoint(i) -> GetValue(j));
00060 
00061    // restore data point limits
00062    for (unsigned int i = 0; i < this -> GetNParameters(); ++i)
00063       fTestModel -> SetDataBoundaries(
00064             fMapDataValue[i],
00065             this -> GetParameter(i) -> GetLowerLimit(),
00066             this -> GetParameter(i) -> GetUpperLimit());
00067 
00068    // delete temporary data set
00069    delete fTemporaryDataSet;
00070 }
00071 
00072 // ---------------------------------------------------------
00073 
00074 double BCGoFTest::LogLikelihood(std::vector <double> parameters)
00075 {
00076    // set the original data set to the new parameters
00077    for (int i = 0; i < int(parameters.size()); ++i)
00078       fTestModel -> GetDataSet() -> GetDataPoint(fMapDataPoint[i]) -> SetValue(fMapDataValue[i], parameters.at(i));
00079 
00080    // calculate likelihood at the point of the original parameters
00081    double loglikelihood = fTestModel -> LogLikelihood(fDataSet -> GetDataPoint(0) -> GetValues());
00082 
00083    // return likelihood
00084    return loglikelihood;
00085 }
00086 
00087 // ---------------------------------------------------------
00088 
00089 void BCGoFTest::MCMCUserIterationInterface()
00090 {
00091    int nchains = this -> MCMCGetNChains();
00092 
00093    for (int i = 0; i < nchains; ++i)
00094    {
00095       // get likelihood at the point of the original parameters
00096       double loglikelihood = this -> MCMCGetLogProbx(i);
00097 
00098       // calculate pvalue
00099       if (loglikelihood < fLogLikelihood)
00100          fPValueBelow++;
00101       else
00102          fPValueAbove++;
00103 
00104       // if histogram exists already, then fill it ...
00105       if (fHistogramLogProb)
00106          fHistogramLogProb -> Fill(loglikelihood);
00107       // ...otherwise find range
00108       else
00109       {
00110          if (loglikelihood > fLogLikelihoodMax)
00111             fLogLikelihoodMax = loglikelihood;
00112          else if (loglikelihood < fLogLikelihoodMin)
00113             fLogLikelihoodMin = loglikelihood;
00114       }
00115    }
00116 }
00117 
00118 // ---------------------------------------------------------
00119 
00120 int BCGoFTest::SetTestPoint(std::vector<double> parameters)
00121 {
00122    // check if the boundaries of the original data set exist.
00123    if (!fTestModel -> GetFlagBoundaries())
00124    {
00125       BCLog::Out(BCLog::warning, BCLog::warning,"BCGoFTest::SetTestDataPoint(). Boundaries of the original data set are not defined.");
00126       return 0;
00127    }
00128 
00129    // reset histogram
00130    if (fHistogramLogProb)
00131    {
00132       delete fHistogramLogProb;
00133       fHistogramLogProb = 0;
00134    }
00135 
00136    // reset variables
00137    fPValue = 0;
00138    fPValueAbove = 0;
00139    fPValueBelow = 0;
00140 
00141    // create temporary data set ...
00142    fTemporaryDataSet = new BCDataSet();
00143 
00144    // ... and fill with the original one
00145 
00146    // get number of data points and values
00147    int ndatapoints = fTestModel -> GetDataSet() -> GetNDataPoints();
00148    int ndatavalues = fTestModel -> GetDataSet() -> GetDataPoint(0) -> GetNValues();
00149 
00150    for (int i = 0; i < ndatapoints; ++i)
00151    {
00152       BCDataPoint * dp = new BCDataPoint(fTestModel -> GetDataSet() -> GetDataPoint(i) -> GetValues());
00153       fTemporaryDataSet -> AddDataPoint(dp);
00154    }
00155 
00156    // clear maps
00157    fMapDataPoint.clear();
00158    fMapDataValue.clear();
00159 
00160    int counter = 0;
00161 
00162    // remove parameters
00163    fParameterSet -> clear();
00164    delete fParameterSet;
00165    fParameterSet = new BCParameterSet;
00166 
00167    // loop through data points and values
00168    for (int i = 0; i < ndatapoints; ++i)
00169       for (int j = 0; j < ndatavalues; ++j)
00170       {
00171          if (fTestModel -> GetFixedDataAxis(j))
00172             continue;
00173 
00174          // add parameter to this model
00175          this -> AddParameter(
00176                Form("parameter_%i", counter),
00177                fTestModel -> GetDataPointLowerBoundary(j),
00178                fTestModel -> GetDataPointUpperBoundary(j));
00179 
00180          // add another element to the maps
00181          fMapDataPoint.push_back(i);
00182          fMapDataValue.push_back(j);
00183 
00184          // increase counter
00185          counter ++;
00186       }
00187 
00188    // check if there are any non-fixed data values left
00189    if (counter == 0)
00190    {
00191       BCLog::Out(BCLog::warning, BCLog::warning,"BCGoFTest::SetTestDataPoint(). No non-fixed data values left.");
00192       return 0;
00193    }
00194 
00195    // create a new data set containing the vector of parameters which
00196    // are to be tested
00197    BCDataPoint * datapoint = new BCDataPoint(parameters);
00198    BCDataSet * dataset = new BCDataSet();
00199    dataset -> AddDataPoint(datapoint);
00200 
00201    // calculate likelihood of the original data set
00202    fLogLikelihood = fTestModel -> LogLikelihood(parameters);
00203 
00204    // if data set has been set before, delete
00205    if (fDataSet)
00206       delete fDataSet;
00207 
00208    // set data set of this model
00209    fDataSet = dataset;
00210 
00211    // put proper range to new data set
00212    for (int i = 0; i < int(parameters.size()); ++i)
00213       this -> SetDataBoundaries(
00214             i,
00215             fTestModel -> GetParameter(i) -> GetLowerLimit(),
00216             fTestModel -> GetParameter(i) -> GetUpperLimit());
00217 
00218    return 1;
00219 }
00220 
00221 // ---------------------------------------------------------
00222 
00223 double BCGoFTest::GetCalculatedPValue(bool flag_histogram)
00224 {
00225    // set histogram point to null
00226    fHistogramLogProb = 0;
00227 
00228    if (flag_histogram)
00229    {
00230       // modify MCMC for first run
00231 //    this -> MCMCSetNIterationsMax(100000);
00232 //    this -> MCMCSetNIterationsRun(10000);
00233 
00234       // perform first run to obtain limits for the log(likelihood)
00235       this -> MarginalizeAll();
00236 
00237       // modify MCMC for second run
00238 //    this -> MCMCSetNIterationsMax(100000);
00239 //    this -> MCMCSetNIterationsRun(10000);
00240 
00241       // create histogram
00242       double D = fLogLikelihoodMax - fLogLikelihoodMin;
00243       fHistogramLogProb = new TH1D(Form("hist_%s_logprob", this -> GetName().data()), ";ln(prob);N", 100, fLogLikelihoodMin - 0.1*D, fLogLikelihoodMax + 0.1*D);
00244       fHistogramLogProb -> SetStats(kFALSE);
00245    }
00246    else
00247    {
00248       // modify MCMC
00249 //    this -> MCMCSetNIterationsMax(100000);
00250 //    this -> MCMCSetNIterationsRun(10000);
00251    }
00252 
00253    // run MCMC
00254    this -> MarginalizeAll();
00255 
00256    // check for convergence
00257    if (this -> MCMCGetNIterationsConvergenceGlobal() < 0.)
00258    {
00259       BCLog::Out(BCLog::detail, BCLog::detail,
00260             " --> MCMC did not converge in evaluation of the p-value.");
00261       return -1;
00262    }
00263 
00264    // calculate p-value
00265    fPValue = double(fPValueBelow) / double(fPValueBelow + fPValueAbove);
00266 
00267    // return p-value
00268    return fPValue;
00269 }
00270 
00271 // ---------------------------------------------------------

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