Go to the documentation of this file.00001
00002
00003
00004
00005
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
00023 fTemporaryDataSet = 0;
00024
00025
00026 fTestModel = 0;
00027
00028
00029 fPValue = 0;
00030 fPValueAbove = 0;
00031 fPValueBelow = 0;
00032
00033
00034 fLogLikelihood = 0;
00035 fLogLikelihoodMin = 1e99;
00036 fLogLikelihoodMax = -1e99;
00037
00038
00039 fHistogramLogProb = 0;
00040
00041
00042 this -> MCMCSetNChains(5);
00043 this -> MCMCSetNIterationsMax(100000);
00044 this -> MCMCSetNIterationsRun(2000);
00045 }
00046
00047
00048
00049 BCGoFTest::~BCGoFTest()
00050 {
00051
00052
00053
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
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
00069 delete fTemporaryDataSet;
00070 }
00071
00072
00073
00074 double BCGoFTest::LogLikelihood(std::vector <double> parameters)
00075 {
00076
00077 for (int i = 0; i < int(parameters.size()); ++i)
00078 fTestModel -> GetDataSet() -> GetDataPoint(fMapDataPoint[i]) -> SetValue(fMapDataValue[i], parameters.at(i));
00079
00080
00081 double loglikelihood = fTestModel -> LogLikelihood(fDataSet -> GetDataPoint(0) -> GetValues());
00082
00083
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
00096 double loglikelihood = this -> MCMCGetLogProbx(i);
00097
00098
00099 if (loglikelihood < fLogLikelihood)
00100 fPValueBelow++;
00101 else
00102 fPValueAbove++;
00103
00104
00105 if (fHistogramLogProb)
00106 fHistogramLogProb -> Fill(loglikelihood);
00107
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
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
00130 if (fHistogramLogProb)
00131 {
00132 delete fHistogramLogProb;
00133 fHistogramLogProb = 0;
00134 }
00135
00136
00137 fPValue = 0;
00138 fPValueAbove = 0;
00139 fPValueBelow = 0;
00140
00141
00142 fTemporaryDataSet = new BCDataSet();
00143
00144
00145
00146
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
00157 fMapDataPoint.clear();
00158 fMapDataValue.clear();
00159
00160 int counter = 0;
00161
00162
00163 fParameterSet -> clear();
00164 delete fParameterSet;
00165 fParameterSet = new BCParameterSet;
00166
00167
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
00175 this -> AddParameter(
00176 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,"BCGoFTest::SetTestDataPoint(). No non-fixed data values left.");
00192 return 0;
00193 }
00194
00195
00196
00197 BCDataPoint * datapoint = new BCDataPoint(parameters);
00198 BCDataSet * dataset = new BCDataSet();
00199 dataset -> AddDataPoint(datapoint);
00200
00201
00202 fLogLikelihood = fTestModel -> LogLikelihood(parameters);
00203
00204
00205 if (fDataSet)
00206 delete fDataSet;
00207
00208
00209 fDataSet = dataset;
00210
00211
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
00226 fHistogramLogProb = 0;
00227
00228 if (flag_histogram)
00229 {
00230
00231
00232
00233
00234
00235 this -> MarginalizeAll();
00236
00237
00238
00239
00240
00241
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
00249
00250
00251 }
00252
00253
00254 this -> MarginalizeAll();
00255
00256
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
00265 fPValue = double(fPValueBelow) / double(fPValueBelow + fPValueAbove);
00266
00267
00268 return fPValue;
00269 }
00270
00271