test12.cpp

Go to the documentation of this file.
00001 #include "indii/ml/aux/GaussianMixturePdf.hpp"
00002 #include "indii/ml/aux/GaussianPdf.hpp"
00003 #include "indii/ml/aux/DiracMixturePdf.hpp"
00004 #include "indii/ml/aux/Almost2Norm.hpp"
00005 #include "indii/ml/aux/AlmostGaussianKernel.hpp"
00006 #include "indii/ml/aux/Random.hpp"
00007 #include "indii/ml/aux/kde.hpp"
00008 
00009 #include <gsl/gsl_statistics_double.h>
00010 
00011 #include <iostream>
00012 #include <fstream>
00013 
00014 using namespace std;
00015 
00016 namespace aux = indii::ml::aux;
00017 
00018 /**
00019  * @file test12.cpp
00020  *
00021  * Test of indii::ml::aux::selfTreeDensity.
00022  *
00023  * This test:
00024  *
00025  * @li creates a random multivariate Gaussian mixture,
00026  * @li samples from this mixture and constructs kernel density
00027  * approximation,
00028  * @li calculates the density at the support points of the kernel density
00029  * approximation, comparing the results of indii::ml::aux::selfTreeDensity
00030  * and indii::ml::aux::dualTreeDensity.
00031  *
00032  * Results are as follows:
00033  *
00034  * @include test11.out
00035  */
00036 
00037 /**
00038  * Dimensionality of the distribution.
00039  */
00040 unsigned int M = 2;
00041 
00042 /**
00043  * Number of components in the Gaussian mixture.
00044  */
00045 unsigned int COMPONENTS = 4;
00046 
00047 /**
00048  * Number of samples to take.
00049  */
00050 unsigned int P = 5000;
00051 
00052 /**
00053  * Resolution of plots.
00054  */
00055 unsigned int RES = 200;
00056 
00057 /**
00058  * Bandwidth.
00059  */
00060 double H = 0.25 * aux::hopt(M,P);
00061 
00062 /**
00063  * Create random Gaussian distribution.
00064  *
00065  * @param M Dimensionality of the Gaussian.
00066  * @param minMean Minimum value of any component of the mean.
00067  * @param maxMean Maximum value of any component of the mean.
00068  * @param minCov Minimum value of any component of the covariance.
00069  * @param maxCov Maximum value of any component of the covariance.
00070  *
00071  * @return Gaussian with given dimensionality, with mean and
00072  * covariance randomly generated uniformly from within the given
00073  * bounds.
00074  */
00075 aux::GaussianPdf createRandomGaussian(const unsigned int M,
00076     const double minMean = -1.0, const double maxMean = 1.0,
00077     const double minCov = -1.0, const double maxCov = 1.0) {
00078   aux::vector mu(M);
00079   aux::symmetric_matrix sigma(M);
00080   aux::lower_triangular_matrix L(M,M);
00081 
00082   unsigned int i, j;
00083 
00084   /* mean */
00085   for (i = 0; i < M; i++) {
00086     mu(i) = aux::Random::uniform(minMean, maxMean);
00087   }
00088 
00089   /* covariance */
00090   for (i = 0; i < M; i++) {
00091     for (j = 0; j <= i; j++) {
00092       L(i,j) = aux::Random::gaussian((maxCov + minCov) / 2.0,
00093           (maxCov - minCov) / 2.0);
00094     }
00095   }
00096   sigma = prod(L, trans(L)); // ensures cholesky decomposable
00097 
00098   return aux::GaussianPdf(mu, sigma);
00099 }
00100 
00101 /**
00102  * Run tests.
00103  */
00104 int main(int argc, char* argv[]) {
00105   /* mpi */
00106   boost::mpi::environment env(argc, argv);
00107   boost::mpi::communicator world;
00108   unsigned int rank = world.rank();  
00109   unsigned int size = world.size();
00110 
00111   //int seed = static_cast<int>(aux::Random::uniform(0, 1000000));
00112   //std::cerr << "seed = " << seed << std::endl;
00113   //aux::Random::seed(seed);
00114   //aux::Random::seed(781634 + rank);
00115 
00116   unsigned int i;
00117 
00118   /* create distribution and broadcast */
00119   aux::GaussianMixturePdf mixture(M);
00120   if (rank == 0) {
00121     for (i = 0; i < COMPONENTS; i++) {
00122       mixture.add(createRandomGaussian(M),
00123           aux::Random::uniform(0.5,1.0));
00124     }
00125   }
00126   boost::mpi::broadcast(world, mixture, 0);
00127 
00128   /* sample from distribution */
00129   aux::DiracMixturePdf mixtureSamples(mixture, P / size);
00130   mixtureSamples.redistributeBySpace();
00131 
00132   /* construct kd tree */
00133   aux::KDTree<> tree(&mixtureSamples);
00134   aux::KDTree<> copyTree(tree);
00135   DiracMixturePdf copyMixtureSamples(mixtureSamples);
00136   copyTree.setData(&copyMixtureSamples);
00137 
00138   aux::Almost2Norm N;
00139   aux::AlmostGaussianKernel K(M,H);
00140 
00141   /* density evaluation */
00142   aux::vector result1(aux::distributedDualTreeDensity(copyTree, tree,
00143       mixtureSamples.getWeights(), N, K));
00144   aux::vector result2(aux::distributedSelfTreeDensity(tree,
00145       mixtureSamples.getWeights(), N, K));
00146 
00147   double err = norm_inf(result1 - result2);
00148   reduce(world, err, err, boost::mpi::maximum<double>(), 0);
00149 
00150   if (rank == 0) {
00151     if (err == 0.0) {
00152       cout << "Passed" << endl;
00153     } else {
00154       cout << "Failed" << endl;
00155       cout << "Max error is " << err << endl;
00156     }
00157   }
00158   
00159   return 0; 
00160 }
00161 

Generated on Wed Dec 17 15:01:30 2008 for dysii Probability Distributions Test Suite by  doxygen 1.5.3