📄 test10.cpp
字号:
#include "indii/ml/aux/GaussianMixturePdf.hpp"#include "indii/ml/aux/GaussianPdf.hpp"#include "indii/ml/aux/KDTree.hpp"#include "indii/ml/aux/GaussianKernel.hpp"#include "indii/ml/aux/PNorm.hpp"#include "indii/ml/aux/DiracMixturePdf.hpp"#include "indii/ml/aux/vector.hpp"#include "indii/ml/aux/matrix.hpp"#include "indii/ml/aux/Random.hpp"#include <gsl/gsl_statistics_double.h>#include <iostream>#include <fstream>using namespace std;namespace aux = indii::ml::aux;/** * @file test10.cpp * * Test of KDTree. * * This test: * * @li creates a random multivariate Gaussian mixture, * @li samples from this mixture and constructs a KD tree, * * Results are as follows: * * @include test10.out * * \image html test10_mixture.png "Original Gaussian mixture" * \image latex test10_mixture.eps "Original Gaussian mixture" * \image html test10_tree.png "KD tree approximation" * \image latex test10_tree.eps "KD tree approximation" *//** * Dimensionality of the distribution. */unsigned int M = 2;/** * Number of components in the Gaussian mixture. */unsigned int COMPONENTS = 4;/** * Number of samples to take. */unsigned int P = 10000;/** * Resolution of plots. */unsigned int RES = 200;/** * Scaling parameter. */double H = 16.0 / sqrt(P);/** * Create random Gaussian distribution. * * @param M Dimensionality of the Gaussian. * @param minMean Minimum value of any component of the mean. * @param maxMean Maximum value of any component of the mean. * @param minCov Minimum value of any component of the covariance. * @param maxCov Maximum value of any component of the covariance. * * @return Gaussian with given dimensionality, with mean and * covariance randomly generated uniformly from within the given * bounds. */aux::GaussianPdf createRandomGaussian(const unsigned int M, const double minMean = -1.0, const double maxMean = 1.0, const double minCov = -1.0, const double maxCov = 1.0) { aux::vector mu(M); aux::symmetric_matrix sigma(M); aux::lower_triangular_matrix L(M,M); unsigned int i, j; /* mean */ for (i = 0; i < M; i++) { mu(i) = aux::Random::uniform(minMean, maxMean); } /* covariance */ for (i = 0; i < M; i++) { for (j = 0; j <= i; j++) { L(i,j) = aux::Random::gaussian((maxCov + minCov) / 2.0, (maxCov - minCov) / 2.0); } } sigma = prod(L, trans(L)); // ensures cholesky decomposable return aux::GaussianPdf(mu, sigma);}/** * Run tests. */int main(int argc, const char* argv[]) { //int seed = static_cast<int>(aux::Random::uniform(0, 1000000)); //std::cerr << "seed = " << seed << std::endl; //aux::Random::seed(seed); aux::Random::seed(781634); unsigned int i, j; /* create distribution */ aux::GaussianMixturePdf mixture(M); for (i = 0; i < COMPONENTS; i++) { mixture.addComponent(createRandomGaussian(M), aux::Random::uniform(0.5,1.0)); } /* sample from distribution */ aux::DiracMixturePdf mixtureSamples(mixture, P); /* construct KD tree */ aux::KDTree<> tree(mixtureSamples); aux::GaussianKernel K(M, H); aux::PNorm<2> N; /* sample from norm and kernel */ aux::DiracMixturePdf kernelSamples(M); for (i = 0; i < P; i++) { kernelSamples.addComponent(aux::DiracPdf(N.sample(M))); } /* sample from KD tree */ aux::DiracMixturePdf treeSamples(M); for (i = 0; i < P; i++) { treeSamples.addComponent(tree.sample(N, K)); } /* importance sample from density tree */ aux::GaussianPdf importance(mixture.getExpectation(), mixture.getCovariance()); aux::DiracMixturePdf treeImportanceSamples(M); double treeDensity, importanceDensity; aux::vector sample(M); for (i = 0; i < P; i++) { sample = importance.sample(); importanceDensity = importance.densityAt(sample); treeDensity = tree.densityAt(sample, N, K); treeImportanceSamples.addComponent(sample, treeDensity/importanceDensity); } cout << "Mixture mean" << endl << mixture.getExpectation() << endl; cout << "Mixture covariance" << endl << mixture.getCovariance() << endl; cout << "Sample mean" << endl << mixtureSamples.getExpectation() << endl; cout << "Sample covariance" << endl << mixtureSamples.getCovariance() << endl; //cout << "KD tree mean" << endl << // tree.getExpectation() << endl; //cout << "KD tree covariance" << endl << // tree.getCovariance() << endl; cout << "Kernel & norm sample mean" << endl << kernelSamples.getExpectation() << endl; cout << "Kernel & norm sample covariance" << endl << kernelSamples.getCovariance() << endl; cout << "KD tree sample mean" << endl << treeSamples.getExpectation() << endl; cout << "KD tree sample covariance" << endl << treeSamples.getCovariance() << endl; cout << "KD tree importance sample mean" << endl << treeImportanceSamples.getExpectation() << endl; cout << "KD tree importance sample covariance" << endl << treeImportanceSamples.getCovariance() << endl; /* calculate bounds */ aux::vector lower(M), upper(M); aux::DiracMixturePdf::weighted_component_const_iterator iter, end; double s; iter = mixtureSamples.getComponents().begin(); end = mixtureSamples.getComponents().end(); assert (iter != end); noalias(lower) = iter->x; noalias(upper) = lower; iter++; while (iter != end) { for (i = 0; i < M; i++) { s = iter->x(i); if (s < lower(i)) { lower(i) = s; } else if (s > upper(i)) { upper(i) = s; } } iter++; } /* output for plots */ ofstream fMixture("results/test10_mixture.out"); ofstream fTree("results/test10_tree.out"); aux::vector coord(M); double x, y, density; for (i = 0; i < RES; i++) { x = lower(0) + (upper(0) - lower(0)) * i / RES; coord(0) = x; for (j = 0; j < RES; j++) { y = lower(1) + (upper(1) - lower(1)) * j / RES; coord(1) = y; density = mixture.densityAt(coord); fMixture << x << '\t' << y << '\t' << density << endl; density = tree.densityAt(coord, N, K); fTree << x << '\t' << y << '\t' << density << endl; } /* end isolines */ fMixture << endl; fTree << endl; } return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -