test6.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/vector.hpp"
00004 #include "indii/ml/aux/matrix.hpp"
00005 #include "indii/ml/aux/Random.hpp"
00006 
00007 #include <gsl/gsl_statistics_double.h>
00008 
00009 #include <iostream>
00010 
00011 using namespace std;
00012 
00013 namespace aux = indii::ml::aux;
00014 
00015 /**
00016  * @file test6.cpp
00017  *
00018  * Test of GaussianMixturePdf.
00019  *
00020  * This test creates a random multivariate Gaussian mixture. It then
00021  * samples from this mixture and compares the mean and covariance of
00022  * the original mixture with the mean and covariance of the sample
00023  * set.
00024  *
00025  * Results are as follows:
00026  *
00027  * @include test6.out
00028  */
00029 
00030 /**
00031  * Dimensionality of the Gaussian mixture.
00032  */
00033 unsigned int M = 10;
00034 
00035 /**
00036  * Number of components in the Gaussian mixture.
00037  */
00038 unsigned int COMPONENTS = 12;
00039 
00040 /**
00041  * Number of samples to take.
00042  */
00043 unsigned int N = 100000;
00044 
00045 /**
00046  * Create random Gaussian distribution.
00047  *
00048  * @param M Dimensionality of the Gaussian.
00049  * @param minMean Minimum value of any component of the mean.
00050  * @param maxMean Maximum value of any component of the mean.
00051  * @param minCov Minimum value of any component of the covariance.
00052  * @param maxCov Maximum value of any component of the covariance.
00053  *
00054  * @return Gaussian with given dimensionality, with mean and
00055  * covariance randomly generated uniformly from within the given
00056  * bounds.
00057  */
00058 aux::GaussianPdf createRandomGaussian(const unsigned int M,
00059     const double minMean = -5.0, const double maxMean = 5.0,
00060     const double minCov = 0.0, const double maxCov = 5.0) {
00061   aux::vector mu(M);
00062   aux::symmetric_matrix sigma(M);
00063 
00064   unsigned int i, j;
00065 
00066   /* mean */
00067   for (i = 0; i < M; i++) {
00068     mu(i) = aux::Random::uniform(minMean, maxMean);
00069   }
00070 
00071   /* covariance */
00072   for (i = 0; i < M; i++) {
00073     for (j = 0; j <= i; j++) {
00074       sigma(i,j) = aux::Random::uniform(sqrt(minCov) / M, sqrt(maxCov) / M);
00075     }
00076   }
00077   sigma = prod(sigma, trans(sigma)); // ensures cholesky decomposable
00078 
00079   return aux::GaussianPdf(mu, sigma);
00080 }
00081 
00082 /**
00083  * Run tests.
00084  */
00085 int main(int argc, const char* argv[]) {
00086   aux::vector sample(M);
00087   double data[M][N];
00088   unsigned int i, j;
00089   aux::vector smu(M);  // sample mean
00090   aux::symmetric_matrix ssigma(M);  // sample covariance
00091 
00092   /* create distribution */
00093   aux::GaussianMixturePdf pdf(M);
00094   for (i = 0; i < COMPONENTS; i++) {
00095     pdf.add(createRandomGaussian(M), aux::Random::uniform(0.0,1.0));
00096   }
00097 
00098   /* sample from distribution */
00099   for (i = 0; i < N; i++) {
00100     sample = pdf.sample();
00101 
00102     for (j = 0; j < M; j++) {
00103       data[j][i] = sample(j);
00104     }
00105   }
00106 
00107   /* calculate mean and variance of samples */
00108   for (i = 0; i < M; i++) {
00109     smu(i) = gsl_stats_mean(data[i], 1, N);
00110   }
00111   for (i = 0; i < M; i++) {
00112     for (j = 0; j < M; j++) {
00113       ssigma(i,j) = gsl_stats_covariance(data[i], 1, data[j], 1, N);
00114     }
00115   }
00116 
00117   cout << "True mean" << endl << pdf.getExpectation() << endl;
00118   cout << "True covariance" << endl << pdf.getCovariance() << endl;
00119   cout << "Sample mean" << endl << smu << endl;
00120   cout << "Sample covariance" << endl << ssigma << endl;
00121 }

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