BCGoFTest Class Reference

#include <BCGoFTest.h>

Inheritance diagram for BCGoFTest:

Inheritance graph
[legend]
Collaboration diagram for BCGoFTest:

Collaboration graph
[legend]

List of all members.


Detailed Description

The class for testing model hypotheses.

Author:
Daniel Kollar

Kevin Kröninger

Version:
1.0
Date:
08.2008 This class is used for calculating the p-value of a model.

Definition at line 33 of file BCGoFTest.h.


Public Member Functions

Constructors and destructors


 BCGoFTest (const char *name)
 ~BCGoFTest ()
Member functions (get)


double GetCalculatedPValue (bool flag_histogram=false)
TH1D * GetHistogramLogProb ()
BCModelGetTestModel ()
Member functions (miscellaneous methods)


double LogAPrioriProbability (std::vector< double > parameters)
double LogLikelihood (std::vector< double > parameters)
void MCMCUserIterationInterface ()
Member functions (set)


void SetTestModel (BCModel *testmodel)
int SetTestPoint (std::vector< double > parameters)

Private Attributes

TH1D * fHistogramLogProb
double fLogLikelihood
double fLogLikelihoodMax
double fLogLikelihoodMin
std::vector< int > fMapDataPoint
std::vector< int > fMapDataValue
int fPValueAbove
int fPValueBelow
BCDataSetfTemporaryDataSet
BCModelfTestModel

Constructor & Destructor Documentation

BCGoFTest::BCGoFTest ( const char *  name  ) 

Default constructor.

Definition at line 20 of file BCGoFTest.cxx.

00020                                      : 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 }

BCGoFTest::~BCGoFTest (  ) 

Default destructor.

Definition at line 49 of file BCGoFTest.cxx.

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 }


Member Function Documentation

double BCGoFTest::GetCalculatedPValue ( bool  flag_histogram = false  ) 

Definition at line 223 of file BCGoFTest.cxx.

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 }

TH1D* BCGoFTest::GetHistogramLogProb (  )  [inline]

Definition at line 61 of file BCGoFTest.h.

00062          { return fHistogramLogProb; };

BCModel* BCGoFTest::GetTestModel (  )  [inline]

Definition at line 66 of file BCGoFTest.h.

00067          { return fTestModel; };

double BCGoFTest::LogAPrioriProbability ( std::vector< double >  parameters  )  [inline, virtual]

Returns natural logarithm of the prior probability. Method needs to be overloaded by the user.

Parameters:
parameters A set of parameter values
Returns:
The prior probability p(parameters)
See also:
GetPrior(std::vector <double> parameters)

Reimplemented from BCModel.

Definition at line 91 of file BCGoFTest.h.

00092          { return 0; };

double BCGoFTest::LogLikelihood ( std::vector< double >  parameter  )  [virtual]

Calculates natural logarithm of the likelihood. Method needs to be overloaded by the user.

Parameters:
parameters A set of parameter values
Returns:
Natural logarithm of the likelihood

Reimplemented from BCModel.

Definition at line 74 of file BCGoFTest.cxx.

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 }

void BCGoFTest::MCMCUserIterationInterface (  )  [virtual]

Reimplemented from BCIntegrate.

Definition at line 89 of file BCGoFTest.cxx.

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 }

void BCGoFTest::SetTestModel ( BCModel testmodel  )  [inline]

Definition at line 76 of file BCGoFTest.h.

00077          { fTestModel = testmodel; };

int BCGoFTest::SetTestPoint ( std::vector< double >  parameters  ) 

Definition at line 120 of file BCGoFTest.cxx.

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 }


Member Data Documentation

Definition at line 126 of file BCGoFTest.h.

double BCGoFTest::fLogLikelihood [private]

Definition at line 120 of file BCGoFTest.h.

double BCGoFTest::fLogLikelihoodMax [private]

Definition at line 122 of file BCGoFTest.h.

double BCGoFTest::fLogLikelihoodMin [private]

Definition at line 121 of file BCGoFTest.h.

std::vector<int> BCGoFTest::fMapDataPoint [private]

Definition at line 102 of file BCGoFTest.h.

std::vector<int> BCGoFTest::fMapDataValue [private]

Definition at line 103 of file BCGoFTest.h.

int BCGoFTest::fPValueAbove [private]

Definition at line 108 of file BCGoFTest.h.

int BCGoFTest::fPValueBelow [private]

Definition at line 107 of file BCGoFTest.h.

Definition at line 116 of file BCGoFTest.h.

Definition at line 112 of file BCGoFTest.h.


Generated on Fri Jan 16 10:24:10 2009 for BAT - Bayesian Analysis Toolkit by  doxygen 1.5.6