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
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 unsigned int M = 4;
00030
00031
00032
00033
00034 unsigned int N = 10000;
00035
00036
00037
00038
00039 int main(int argc, char* argv[]) {
00040
00041 boost::mpi::environment env(argc, argv);
00042 boost::mpi::communicator world;
00043 int rank = world.rank();
00044
00045
00046 aux::GaussianPdf gaussian(M);
00047 aux::DiracMixturePdf sampled(M);
00048 unsigned int i, j;
00049
00050 if (rank == 0) {
00051 aux::vector mu(M);
00052 aux::symmetric_matrix sigma(M);
00053 aux::lower_triangular_matrix tmp(M,M);
00054
00055 for (i = 0; i < M; i++) {
00056 mu(i) = aux::Random::uniform(-5.0, 10.0);
00057 }
00058
00059 for (i = 0; i < M; i++) {
00060 for (j = 0; j <= i; j++) {
00061 tmp(i,j) = aux::Random::uniform(0.0, 5.0);
00062 }
00063 }
00064 noalias(sigma) = prod(tmp, trans(tmp));
00065
00066 gaussian.setExpectation(mu);
00067 gaussian.setCovariance(sigma);
00068 }
00069 boost::mpi::broadcast(world, gaussian, 0);
00070
00071
00072
00073
00074 aux::vector var = aux::diag(gaussian.getCovariance());
00075 aux::vector sample(M);
00076 double sd;
00077
00078 for (i = 0; i < N; i++) {
00079 for (j = 0; j < M; j++) {
00080 sd = sqrt(var(j));
00081 sample(j) = gaussian.getExpectation()(j) +
00082 aux::Random::uniform(-3.0*sd, 3.0*sd);
00083 }
00084 sampled.add(sample, gaussian.calculateDensity(sample));
00085 }
00086
00087 indii::ml::filter::StratifiedParticleResampler resampler;
00088 if (rank == 0) {
00089 cout << "True mean" << endl << gaussian.getExpectation() << endl;
00090 cout << "True covariance" << endl << gaussian.getCovariance() << endl;
00091
00092 cout << "Sampled mean" << endl <<
00093 sampled.getDistributedExpectation() << endl;
00094 cout << "Sampled covariance" << endl <<
00095 sampled.getDistributedCovariance() << endl;
00096
00097 sampled = resampler.resample(sampled);
00098 cout << "Resampled mean" << endl <<
00099 sampled.getDistributedExpectation() << endl;
00100 cout << "Resampled covariance" << endl <<
00101 sampled.getDistributedCovariance() << endl;
00102 } else {
00103 sampled.getDistributedExpectation();
00104 sampled.getDistributedCovariance();
00105 sampled = resampler.resample(sampled);
00106 sampled.getDistributedExpectation();
00107 sampled.getDistributedCovariance();
00108 }
00109
00110 return 0;
00111 }