test11.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/KernelDensityMixturePdf.hpp"
00004 #include "indii/ml/aux/DiracMixturePdf.hpp"
00005 #include "indii/ml/aux/Random.hpp"
00006 #include "indii/ml/aux/kde.hpp"
00007 
00008 #include <gsl/gsl_statistics_double.h>
00009 
00010 #include <iostream>
00011 #include <fstream>
00012 
00013 using namespace std;
00014 
00015 namespace aux = indii::ml::aux;
00016 
00017 /**
00018  * @file test11.cpp
00019  *
00020  * Test of KernelDensityMixturePdf.
00021  *
00022  * This test:
00023  *
00024  * @li creates a random multivariate Gaussian mixture,
00025  * @li samples from this mixture and constructs a \f$kd\f$ tree,
00026  * @li Constructs a KernelDensityPdf approximation of the original Gaussian
00027  * mixture from this \f$kd\f$ tree.
00028  *
00029  * Results are as follows:
00030  *
00031  * @include test11.out
00032  */
00033 
00034 /**
00035  * Dimensionality of the distribution.
00036  */
00037 unsigned int M = 2;
00038 
00039 /**
00040  * Number of components in the Gaussian mixture.
00041  */
00042 unsigned int COMPONENTS = 4;
00043 
00044 /**
00045  * Number of samples to take.
00046  */
00047 unsigned int P = 1000;
00048 
00049 /**
00050  * Resolution of plots.
00051  */
00052 unsigned int RES = 200;
00053 
00054 /**
00055  * Bandwidth.
00056  */
00057 double H = 0.25 * aux::hopt(M,P);
00058 
00059 /**
00060  * Create random Gaussian distribution.
00061  *
00062  * @param M Dimensionality of the Gaussian.
00063  * @param minMean Minimum value of any component of the mean.
00064  * @param maxMean Maximum value of any component of the mean.
00065  * @param minCov Minimum value of any component of the covariance.
00066  * @param maxCov Maximum value of any component of the covariance.
00067  *
00068  * @return Gaussian with given dimensionality, with mean and
00069  * covariance randomly generated uniformly from within the given
00070  * bounds.
00071  */
00072 aux::GaussianPdf createRandomGaussian(const unsigned int M,
00073     const double minMean = -1.0, const double maxMean = 1.0,
00074     const double minCov = -1.0, const double maxCov = 1.0) {
00075   aux::vector mu(M);
00076   aux::symmetric_matrix sigma(M);
00077   aux::lower_triangular_matrix L(M,M);
00078 
00079   unsigned int i, j;
00080 
00081   /* mean */
00082   for (i = 0; i < M; i++) {
00083     mu(i) = aux::Random::uniform(minMean, maxMean);
00084   }
00085 
00086   /* covariance */
00087   for (i = 0; i < M; i++) {
00088     for (j = 0; j <= i; j++) {
00089       L(i,j) = aux::Random::gaussian((maxCov + minCov) / 2.0,
00090           (maxCov - minCov) / 2.0);
00091     }
00092   }
00093   sigma = prod(L, trans(L)); // ensures cholesky decomposable
00094 
00095   return aux::GaussianPdf(mu, sigma);
00096 }
00097 
00098 /**
00099  * Run tests.
00100  */
00101 int main(int argc, char* argv[]) {
00102   /* mpi */
00103   boost::mpi::environment env(argc, argv);
00104   boost::mpi::communicator world;
00105   unsigned int rank = world.rank();  
00106   unsigned int size = world.size();
00107 
00108   //int seed = static_cast<int>(aux::Random::uniform(0, 1000000));
00109   //std::cerr << "seed = " << seed << std::endl;
00110   //aux::Random::seed(seed);
00111   //aux::Random::seed(781634 + rank);
00112 
00113   unsigned int i;
00114 
00115   /* create distribution and broadcast */
00116   aux::GaussianMixturePdf mixture(M);
00117   if (rank == 0) {
00118     for (i = 0; i < COMPONENTS; i++) {
00119       mixture.add(createRandomGaussian(M),
00120           aux::Random::uniform(0.5,1.0));
00121     }
00122   }
00123   boost::mpi::broadcast(world, mixture, 0);
00124 
00125   /* sample from distribution */
00126   aux::DiracMixturePdf mixtureSamples(mixture, P / size);
00127   mixtureSamples.redistributeBySpace();
00128 
00129   /* construct kd tree */
00130   aux::KDTree<> tree(&mixtureSamples);
00131   aux::Almost2Norm N;
00132   aux::AlmostGaussianKernel K(M,H);
00133   aux::KernelDensityPdf<> kd(&tree, N, K);
00134   aux::KernelDensityMixturePdf<> kdMixture(kd,
00135       mixtureSamples.getTotalWeight());
00136 
00137   /* sample from kernel density mixture */
00138   std::vector<aux::vector> xs = kdMixture.distributedSample(P);
00139   aux::DiracMixturePdf kdSamples(M);
00140   for (i = 0; i < xs.size(); i++) {
00141     kdSamples.add(xs[i]);
00142   }
00143 
00144   /* importance sample from kernel density */
00145   aux::GaussianPdf importance(mixture.getExpectation(),
00146       mixture.getCovariance());
00147   aux::DiracMixturePdf kdImportanceSamples(M), querySamples(M);
00148   
00149   for (i = 0; i < P / size; i++) {
00150     querySamples.add(importance.sample());
00151   }
00152   querySamples.redistributeBySpace();
00153 
00154   aux::KDTree<aux::MedianPartitioner> queryTree(&querySamples);
00155   aux::vector kdDensities(kdMixture.distributedDensityAt(queryTree));
00156   //noalias(kdDensities) = kdMixture.distributedDensityAt(samples);
00157 
00158   for (i = 0; i < querySamples.getSize(); i++) {
00159     kdImportanceSamples.add(querySamples.get(i), kdDensities(i) /
00160         importance.densityAt(querySamples.get(i)));
00161   }
00162 
00163   if (rank == 0) {
00164     cout << "Mixture mean" << endl <<
00165         mixture.getExpectation() << endl;
00166     cout << "Mixture covariance" << endl <<
00167         mixture.getCovariance() << endl;
00168     cout << "Sample mean" << endl <<
00169         mixtureSamples.getDistributedExpectation() << endl;
00170     cout << "Sample covariance" << endl <<
00171         mixtureSamples.getDistributedCovariance() << endl;
00172     cout << "Kernel density mixture mean" << endl <<
00173         kdMixture.getDistributedExpectation() << endl;
00174     cout << "Kernel density mixture covariance" << endl <<
00175         kdMixture.getDistributedCovariance() << endl;
00176     cout << "Kernel density mixture sample mean" << endl <<
00177         kdSamples.getDistributedExpectation() << endl;
00178     cout << "Kernel density mixture sample covariance" << endl <<
00179         kdSamples.getDistributedCovariance() << endl;
00180     cout << "Kernel density mixture importance sample mean" << endl <<
00181         kdImportanceSamples.getDistributedExpectation() << endl;
00182     cout << "Kernel density mixture importance sample covariance" << endl <<
00183         kdImportanceSamples.getDistributedCovariance() << endl;
00184   } else {
00185     mixtureSamples.getDistributedExpectation();
00186     mixtureSamples.getDistributedCovariance();
00187     kdMixture.getDistributedExpectation();
00188     kdMixture.getDistributedCovariance();
00189     kdSamples.getDistributedExpectation();
00190     kdSamples.getDistributedCovariance();
00191     kdImportanceSamples.getDistributedExpectation();
00192     kdImportanceSamples.getDistributedCovariance();
00193   }
00194   
00195   return 0; 
00196 }
00197 

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