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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 unsigned int M = 4;
00043
00044
00045
00046
00047 unsigned int N = 10000;
00048
00049
00050
00051
00052 int main(int argc, char* argv[]) {
00053
00054 boost::mpi::environment env(argc, argv);
00055 boost::mpi::communicator world;
00056 int rank = world.rank();
00057
00058
00059 aux::vector mu(M);
00060 aux::symmetric_matrix sigma(M);
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));
00078
00079 gaussian.setExpectation(mu);
00080 gaussian.setCovariance(sigma);
00081 }
00082
00083 boost::mpi::broadcast(world, gaussian, 0);
00084
00085
00086
00087
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 }