test2.cpp

Go to the documentation of this file.
00001 #include "indii/ml/aux/GaussianPdf.hpp"
00002 #include "indii/ml/aux/vector.hpp"
00003 #include "indii/ml/aux/matrix.hpp"
00004 #include "indii/ml/aux/Random.hpp"
00005 
00006 #include <gsl/gsl_statistics_double.h>
00007 
00008 #include <iostream>
00009 
00010 using namespace std;
00011 
00012 namespace aux = indii::ml::aux;
00013 
00014 /**
00015  * @file test2.cpp
00016  *
00017  * Multivariate test of GaussianPdf.
00018  *
00019  * This test creates a multivariate Gaussian with a random mean and
00020  * covariance. It then samples from this distribution and calculates
00021  * the sample mean and variance for comparison.
00022  *
00023  * Results are as follows:
00024  *
00025  * @include test2.out
00026  */
00027 
00028 /**
00029  * Dimensionality of the Gaussian.
00030  */
00031 unsigned int M = 10;
00032 
00033 /**
00034  * Number of samples to take.
00035  */
00036 unsigned int N = 100000;
00037 
00038 /**
00039  * Run tests.
00040  */
00041 int main(int argc, const char* argv[]) {
00042   aux::vector mu(M);  // true mean
00043   aux::symmetric_matrix sigma(M);  // true covariance
00044   aux::lower_triangular_matrix tmp(M,M);  // to construct Cholesky decomp sigma
00045   aux::vector smu(M);  // sample mean
00046   aux::symmetric_matrix ssigma(M);  // sample covariance
00047 
00048   aux::vector sample(M);
00049   double data[M][N];
00050   unsigned int i, j;
00051 
00052   /* set up distribution */
00053   for (i = 0; i < M; i++) {
00054     mu(i) = aux::Random::uniform(-5.0, 5.0);
00055   }
00056 
00057   for (i = 0; i < M; i++) {
00058     for (j = 0; j <= i; j++) {
00059       tmp(i,j) = aux::Random::uniform(0.0, 5.0);
00060     }
00061   }
00062   noalias(sigma) = prod(tmp, trans(tmp)); // ensures cholesky decomposition
00063   aux::GaussianPdf pdf(mu, sigma);
00064 
00065   /* sample from distribution */
00066   for (i = 0; i < N; i++) {
00067     sample = pdf.sample();
00068 
00069     for (j = 0; j < M; j++) {
00070       data[j][i] = sample(j);
00071     }
00072   }
00073 
00074   /* calculate mean and variance of samples */
00075   for (i = 0; i < M; i++) {
00076     smu(i) = gsl_stats_mean(data[i], 1, N);
00077   }
00078   for (i = 0; i < M; i++) {
00079     for (j = 0; j < M; j++) {
00080       ssigma(i,j) = gsl_stats_covariance(data[i], 1, data[j], 1, N);
00081     }
00082   }
00083 
00084   cout << "True mean" << endl << mu << endl;
00085   cout << "True covariance" << endl << sigma << endl;
00086   cout << "Sample mean" << endl << smu << endl;
00087   cout << "Sample covariance" << endl << ssigma << endl;
00088 }

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