00001 #include "indii/ml/aux/GaussianPdf.hpp"
00002 #include "indii/ml/aux/DiracPdf.hpp"
00003 #include "indii/ml/aux/UniformPdf.hpp"
00004 #include "indii/ml/aux/DensityTreePdf.hpp"
00005 #include "indii/ml/aux/GaussianMixturePdf.hpp"
00006 #include "indii/ml/aux/DiracMixturePdf.hpp"
00007 #include "indii/ml/aux/DensityTreeMixturePdf.hpp"
00008
00009 #include "boost/archive/binary_iarchive.hpp"
00010 #include "boost/archive/binary_oarchive.hpp"
00011
00012 #include <fstream>
00013 #include <iostream>
00014 #include <ios>
00015
00016 namespace aux = indii::ml::aux;
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 unsigned int N = 4;
00035
00036
00037
00038
00039 unsigned int GAUSSIAN_MIXTURE_COMPONENTS = 12;
00040
00041
00042
00043
00044 unsigned int DIRAC_MIXTURE_COMPONENTS = 1000;
00045
00046
00047
00048
00049 unsigned int TREE_MIXTURE_COMPONENTS = 12;
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 aux::DiracPdf createRandomDirac(const unsigned int M,
00062 const double minMean = -5.0, const double maxMean = 5.0) {
00063 aux::DiracPdf dirac(M);
00064 unsigned int i;
00065
00066
00067 for (i = 0; i < M; i++) {
00068 dirac(i) = aux::Random::uniform(minMean, maxMean);
00069 }
00070
00071 return dirac;
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 aux::GaussianPdf createRandomGaussian(const unsigned int M,
00088 const double minMean = -5.0, const double maxMean = 5.0,
00089 const double minCov = 0.0, const double maxCov = 5.0) {
00090 aux::vector mu(M);
00091 aux::symmetric_matrix sigma(M);
00092
00093 unsigned int i, j;
00094
00095
00096 for (i = 0; i < M; i++) {
00097 mu(i) = aux::Random::uniform(minMean, maxMean);
00098 }
00099
00100
00101 for (i = 0; i < M; i++) {
00102 for (j = 0; j <= i; j++) {
00103 sigma(i,j) = aux::Random::uniform(sqrt(minCov) / M, sqrt(maxCov) / M);
00104 }
00105 }
00106 sigma = prod(sigma, trans(sigma));
00107
00108 return aux::GaussianPdf(mu, sigma);
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 aux::UniformPdf createRandomUniform(const unsigned int M,
00122 const double minBound = -5.0, const double maxBound = 5.0) {
00123 aux::vector lower(M), upper(M);
00124 double a, b;
00125 unsigned int i;
00126
00127 for (i = 0; i < M; i++) {
00128 a = aux::Random::uniform(minBound, maxBound);
00129 b = aux::Random::uniform(minBound, maxBound);
00130
00131 if (a > b) {
00132 upper(i) = a;
00133 lower(i) = b;
00134 } else {
00135 upper(i) = b;
00136 lower(i) = a;
00137 }
00138 }
00139
00140 return aux::UniformPdf(lower, upper);
00141 }
00142
00143
00144
00145
00146 bool isZero(const aux::vector& x) {
00147 unsigned int i;
00148 for (i = 0; i < x.size(); i++) {
00149 if (x(i) != 0.0) {
00150 return false;
00151 }
00152 }
00153 return true;
00154 }
00155
00156
00157
00158
00159 bool isZero(const aux::matrix& A) {
00160 unsigned int i, j;
00161 for (i = 0; i < A.size1(); i++) {
00162 for (j = 0; j < A.size2(); j++) {
00163 if (A(i,j) != 0.0) {
00164 return false;
00165 }
00166 }
00167 }
00168 return true;
00169 }
00170
00171
00172
00173
00174 bool testGaussianPdf() {
00175 bool passed;
00176 aux::vector mu(N);
00177 aux::symmetric_matrix sigma(N);
00178
00179 aux::GaussianPdf gaussianIn;
00180 const aux::GaussianPdf gaussianOut(createRandomGaussian(N));
00181 {
00182 std::ofstream outFile("data/GaussianPdf.obj", std::ios::binary);
00183 boost::archive::binary_oarchive outArchive(outFile);
00184 outArchive << gaussianOut;
00185 }
00186 {
00187 std::ifstream inFile("data/GaussianPdf.obj", std::ios::binary);
00188 boost::archive::binary_iarchive inArchive(inFile);
00189 inArchive >> gaussianIn;
00190 }
00191
00192 passed = isZero(gaussianOut.getExpectation() - gaussianIn.getExpectation());
00193 passed &= isZero(gaussianOut.getCovariance() - gaussianIn.getCovariance());
00194
00195 return passed;
00196 }
00197
00198
00199
00200
00201 bool testDiracPdf() {
00202 bool passed;
00203
00204 aux::DiracPdf diracIn;
00205 const aux::DiracPdf diracOut(createRandomDirac(N));
00206 {
00207 std::ofstream outFile("data/DiracPdf.obj", std::ios::binary);
00208 boost::archive::binary_oarchive outArchive(outFile);
00209 outArchive << diracOut;
00210 }
00211 {
00212 std::ifstream inFile("data/DiracPdf.obj", std::ios::binary);
00213 boost::archive::binary_iarchive inArchive(inFile);
00214 inArchive >> diracIn;
00215 }
00216
00217 passed = isZero(diracOut.getExpectation() - diracIn.getExpectation());
00218
00219 return passed;
00220 }
00221
00222
00223
00224
00225 bool testUniformPdf() {
00226 bool passed;
00227
00228 aux::UniformPdf uniformIn;
00229 aux::UniformPdf uniformOut(createRandomUniform(N));
00230 {
00231 std::ofstream outFile("data/UniformPdf.obj", std::ios::binary);
00232 boost::archive::binary_oarchive outArchive(outFile);
00233 const aux::UniformPdf tmp(uniformOut);
00234 outArchive << tmp;
00235 }
00236 {
00237 std::ifstream inFile("data/UniformPdf.obj", std::ios::binary);
00238 boost::archive::binary_iarchive inArchive(inFile);
00239 inArchive >> uniformIn;
00240 }
00241
00242 passed = isZero(uniformOut.getExpectation() - uniformIn.getExpectation());
00243 passed &= isZero(uniformOut.getCovariance() - uniformIn.getCovariance());
00244
00245 return passed;
00246 }
00247
00248
00249
00250
00251 bool testDensityTreePdf() {
00252 bool passed;
00253 unsigned int i;
00254
00255 aux::DensityTreePdf treeIn;
00256 aux::DiracMixturePdf samples(N);
00257 for (i = 0; i < DIRAC_MIXTURE_COMPONENTS; i++) {
00258 samples.addComponent(createRandomDirac(N),
00259 aux::Random::uniform(0.0,1.0));
00260 }
00261 aux::DensityTreePdf treeOut(samples);
00262
00263 {
00264 std::ofstream outFile("data/DensityTreePdf.obj", std::ios::binary);
00265 boost::archive::binary_oarchive outArchive(outFile);
00266 const aux::DensityTreePdf tmp(treeOut);
00267 outArchive << tmp;
00268 }
00269 {
00270 std::ifstream inFile("data/DensityTreePdf.obj", std::ios::binary);
00271 boost::archive::binary_iarchive inArchive(inFile);
00272 inArchive >> treeIn;
00273 }
00274
00275 passed = isZero(treeOut.getExpectation() - treeIn.getExpectation());
00276 passed &= isZero(treeOut.getCovariance() - treeIn.getCovariance());
00277
00278 return passed;
00279 }
00280
00281
00282
00283
00284 bool testGaussianMixturePdf() {
00285 bool passed;
00286 unsigned int i;
00287 aux::vector mu(N);
00288 aux::symmetric_matrix sigma(N);
00289 aux::GaussianMixturePdf::weighted_component_vector::const_iterator iter1,
00290 iter2, end1, end2;
00291
00292 aux::GaussianMixturePdf gaussianMixIn;
00293 aux::GaussianMixturePdf gaussianMixOut(N);
00294 for (i = 0; i < GAUSSIAN_MIXTURE_COMPONENTS; i++) {
00295 gaussianMixOut.addComponent(createRandomGaussian(N),
00296 aux::Random::uniform(0.0,1.0));
00297 }
00298
00299 {
00300 std::ofstream outFile("data/GaussianMixturePdf.obj", std::ios::binary);
00301 boost::archive::binary_oarchive outArchive(outFile);
00302 const aux::GaussianMixturePdf tmp(gaussianMixOut);
00303 outArchive << tmp;
00304 }
00305 {
00306 std::ifstream inFile("data/GaussianMixturePdf.obj", std::ios::binary);
00307 boost::archive::binary_iarchive inArchive(inFile);
00308 inArchive >> gaussianMixIn;
00309 }
00310
00311 passed = true;
00312 iter1 = gaussianMixIn.getComponents().begin();
00313 end1 = gaussianMixIn.getComponents().end();
00314 iter2 = gaussianMixOut.getComponents().begin();
00315 end2 = gaussianMixOut.getComponents().end();
00316 while (passed && iter1 != end1 && iter2 != end2) {
00317 passed &= iter1->w == iter2->w;
00318 passed &= isZero(iter1->x.getExpectation() - iter2->x.getExpectation());
00319 passed &= isZero(iter2->x.getCovariance() - iter2->x.getCovariance());
00320
00321 iter1++;
00322 iter2++;
00323 }
00324 passed &= iter1 == end1;
00325 passed &= iter2 == end2;
00326
00327 return passed;
00328 }
00329
00330
00331
00332
00333 bool testDiracMixturePdf() {
00334 bool passed;
00335 unsigned int i;
00336 aux::vector mu(N);
00337 aux::DiracMixturePdf::weighted_component_vector::const_iterator iter1,
00338 iter2, end1, end2;
00339
00340 aux::DiracMixturePdf diracMixIn;
00341 aux::DiracMixturePdf diracMixOut(N);
00342 for (i = 0; i < DIRAC_MIXTURE_COMPONENTS; i++) {
00343 diracMixOut.addComponent(createRandomDirac(N),
00344 aux::Random::uniform(0.0,1.0));
00345 }
00346
00347 {
00348 std::ofstream outFile("data/DiracMixturePdf.obj", std::ios::binary);
00349 boost::archive::binary_oarchive outArchive(outFile);
00350 const aux::DiracMixturePdf tmp(diracMixOut);
00351 outArchive << tmp;
00352 }
00353 {
00354 std::ifstream inFile("data/DiracMixturePdf.obj", std::ios::binary);
00355 boost::archive::binary_iarchive inArchive(inFile);
00356 inArchive >> diracMixIn;
00357 }
00358
00359 passed = true;
00360 iter1 = diracMixIn.getComponents().begin();
00361 end1 = diracMixIn.getComponents().end();
00362 iter2 = diracMixOut.getComponents().begin();
00363 end2 = diracMixOut.getComponents().end();
00364 while (passed && iter1 != end1 && iter2 != end2) {
00365 passed &= iter1->w == iter2->w;
00366 passed &= isZero(iter1->x.getExpectation() - iter2->x.getExpectation());
00367
00368 iter1++;
00369 iter2++;
00370 }
00371 passed &= iter1 == end1;
00372 passed &= iter2 == end2;
00373
00374 return passed;
00375 }
00376
00377
00378
00379
00380 bool testDensityTreeMixturePdf() {
00381 bool passed;
00382 unsigned int i, j;
00383 aux::DensityTreeMixturePdf::weighted_component_iterator iter1,
00384 iter2, end1, end2;
00385 aux::DensityTreeMixturePdf treeMixIn;
00386 aux::DensityTreeMixturePdf treeMixOut(N);
00387
00388 for (i = 0; i < TREE_MIXTURE_COMPONENTS; i++) {
00389 aux::DiracMixturePdf samples(N);
00390 for (j = 0; j < DIRAC_MIXTURE_COMPONENTS; j++) {
00391 samples.addComponent(createRandomDirac(N),
00392 aux::Random::uniform(0.0,1.0));
00393 }
00394 treeMixOut.addComponent(aux::DensityTreePdf(samples),
00395 aux::Random::uniform(0.0,1.0));
00396 }
00397
00398 {
00399 std::ofstream outFile("data/DensityTreeMixturePdf.obj", std::ios::binary);
00400 boost::archive::binary_oarchive outArchive(outFile);
00401 const aux::DensityTreeMixturePdf tmp(treeMixOut);
00402 outArchive << tmp;
00403 }
00404 {
00405 std::ifstream inFile("data/DensityTreeMixturePdf.obj", std::ios::binary);
00406 boost::archive::binary_iarchive inArchive(inFile);
00407 inArchive >> treeMixIn;
00408 }
00409
00410 passed = true;
00411 iter1 = treeMixIn.getComponents().begin();
00412 end1 = treeMixIn.getComponents().end();
00413 iter2 = treeMixOut.getComponents().begin();
00414 end2 = treeMixOut.getComponents().end();
00415 while (passed && iter1 != end1 && iter2 != end2) {
00416 passed &= iter1->w == iter2->w;
00417 passed &= isZero(iter1->x.getExpectation() - iter2->x.getExpectation());
00418
00419 iter1++;
00420 iter2++;
00421 }
00422 passed &= iter1 == end1;
00423 passed &= iter2 == end2;
00424
00425 return passed;
00426 }
00427
00428
00429
00430
00431 int main(int argc, const char* argv[]) {
00432
00433 std::cout << "GaussianPdf ";
00434 if (testGaussianPdf()) {
00435 std::cout << "passed";
00436 } else {
00437 std::cout << "failed";
00438 }
00439 std::cout << std::endl;
00440
00441 std::cout << "DiracPdf ";
00442 if (testDiracPdf()) {
00443 std::cout << "passed";
00444 } else {
00445 std::cout << "failed";
00446 }
00447 std::cout << std::endl;
00448
00449 std::cout << "UniformPdf ";
00450 if (testUniformPdf()) {
00451 std::cout << "passed";
00452 } else {
00453 std::cout << "failed";
00454 }
00455 std::cout << std::endl;
00456
00457 std::cout << "DensityTreePdf ";
00458 if (testDensityTreePdf()) {
00459 std::cout << "passed";
00460 } else {
00461 std::cout << "failed";
00462 }
00463 std::cout << std::endl;
00464
00465 std::cout << "GaussianMixturePdf ";
00466 if (testGaussianMixturePdf()) {
00467 std::cout << "passed";
00468 } else {
00469 std::cout << "failed";
00470 }
00471 std::cout << std::endl;
00472
00473 std::cout << "DiracMixturePdf ";
00474 if (testDiracMixturePdf()) {
00475 std::cout << "passed";
00476 } else {
00477 std::cout << "failed";
00478 }
00479 std::cout << std::endl;
00480
00481 std::cout << "DensityTreeMixturePdf ";
00482 if (testDensityTreeMixturePdf()) {
00483 std::cout << "passed";
00484 } else {
00485 std::cout << "failed";
00486 }
00487 std::cout << std::endl;
00488
00489 return 0;
00490 }