test3.cpp

Go to the documentation of this file.
00001 #include "indii/ml/aux/GaussianPdf.hpp"
00002 #include "indii/ml/aux/DiracMixturePdf.hpp"
00003 #include "indii/ml/aux/vector.hpp"
00004 #include "indii/ml/aux/matrix.hpp"
00005 #include "indii/ml/aux/Random.hpp"
00006 #include "indii/ml/filter/StratifiedParticleResampler.hpp"
00007 
00008 #include <iostream>
00009 
00010 using namespace std;
00011 
00012 namespace aux = indii::ml::aux;
00013 
00014 /**
00015  * @file test3.cpp
00016  *
00017  * Test of DiracMixturePdf.
00018  *
00019  * This test creates a GaussianPdf with a random mean and
00020  * covariance. It then generates uniform samples within 3 standard
00021  * deviations of the mean and weights them according to the density
00022  * function of this GaussianPdf, adding them to a DiracMixturePdf
00023  * distribution. The mean and covariance of the DiracMixturePdf can
00024  * then be compared with those of the original Gaussian. Furthermore,
00025  * the DiracMixturePdf is resampled with
00026  * indii::ml::filter::StratifiedParticleResampler and the new mean
00027  * and covariance calculated, which should be the same as before the
00028  * resampling, or very close to.
00029  *
00030  * Results are as follows:
00031  *
00032  * @include test3.out
00033  *
00034  * Note that the covariances for the DiracMixturePdf are expected to be
00035  * slightly lower than that for the GaussianPdf in general, due to the
00036  * finite interval over which the samples are taken.
00037  */
00038 
00039 /**
00040  * Dimensionality of the Gaussian.
00041  */
00042 unsigned int M = 4;
00043 
00044 /**
00045  * Number of samples to take.
00046  */
00047 unsigned int N = 10000;
00048 
00049 /**
00050  * Run tests.
00051  */
00052 int main(int argc, char* argv[]) {
00053   /* mpi */
00054   boost::mpi::environment env(argc, argv);
00055   boost::mpi::communicator world;
00056   int rank = world.rank();
00057 
00058   /* set up distributions */
00059   aux::vector mu(M);  // true mean
00060   aux::symmetric_matrix sigma(M);  // true covariance
00061   aux::GaussianPdf gaussian(M);
00062   aux::DiracMixturePdf sampled(M);
00063   unsigned int i, j;
00064 
00065   if (rank == 0) {
00066     aux::lower_triangular_matrix tmp(M,M);
00067 
00068     for (i = 0; i < M; i++) {
00069       mu(i) = aux::Random::uniform(-5.0, 10.0);
00070     }
00071 
00072     for (i = 0; i < M; i++) {
00073       for (j = 0; j <= i; j++) {
00074         tmp(i,j) = aux::Random::uniform(0.0, 5.0);
00075       }
00076     }
00077     noalias(sigma) = prod(tmp, trans(tmp)); // ensures cholesky decomposition
00078 
00079     gaussian.setExpectation(mu);
00080     gaussian.setCovariance(sigma);
00081   }
00082 
00083   boost::mpi::broadcast(world, gaussian, 0);
00084 
00085   /* uniformly sample from within 3 standard deviations of mean and
00086      add to sampled distribution, weighted by Gaussian probability
00087      density */
00088   aux::vector var = aux::diag(gaussian.getCovariance());
00089   aux::vector sample(mu.size());
00090   double sd;
00091 
00092   for (i = 0; i < N; i++) {
00093     for (j = 0; j < M; j++) {
00094       sd = sqrt(var(j));
00095       sample(j) = mu(j) + aux::Random::uniform(-3.0*sd, 3.0*sd);
00096     }
00097     sampled.add(sample, gaussian.calculateDensity(sample));
00098   }
00099 
00100   mu = sampled.getDistributedExpectation();
00101   sigma = sampled.getDistributedCovariance();
00102   if (rank == 0) {
00103     cout << "True mean" << endl << gaussian.getExpectation() << endl;
00104     cout << "True covariance" << endl << gaussian.getCovariance() << endl;
00105     cout << "Sampled mean" << endl << mu << endl;
00106     cout << "Sampled covariance" << endl << sigma << endl;
00107   }
00108 
00109   indii::ml::filter::StratifiedParticleResampler resampler;
00110   sampled = resampler.resample(sampled);
00111   mu = sampled.getDistributedExpectation();
00112   sigma = sampled.getDistributedCovariance();
00113 
00114   if (rank == 0) {
00115     cout << "Resampled mean" << endl << mu << endl;
00116     cout << "Resampled covariance" << endl << sigma << endl;
00117   }
00118 }

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