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