⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test9.cpp

📁 dysii是一款非常出色的滤波函数库
💻 CPP
字号:
#include "indii/ml/aux/GaussianMixturePdf.hpp"#include "indii/ml/aux/GaussianPdf.hpp"#include "indii/ml/aux/DensityTreePdf.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 test9.cpp * * Test of DensityTreePdf. * * This test: * * @li creates a random multivariate Gaussian mixture, * @li samples from this mixture and constructs a density tree, * @li compares the mean and covariance of the density tree with that of * the original mixture. * @li directly samples from the density tree and compares the mean and * covariance of this sample set with that of the original mixture. * @li importance samples from the density tree using a single Gaussian fit * to the original mixture and compares the mean and covariance of this * sample set with that of the original mixture. * * Results are as follows: * * @include test9.out * * \image html test9_mixture.png "Original Gaussian mixture" * \image latex test9_mixture.eps "Original Gaussian mixture" * \image html test9_tree.png "Density tree approximation" * \image latex test9_tree.eps "Density 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 = 1000000;/** * Resolution of plots. */unsigned int RES = 200;/** * 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::uniform(minCov, maxCov);    }  }  sigma = prod(L, trans(L)); // ensures cholesky decomposable  return aux::GaussianPdf(mu, sigma);}/** * Run tests. */int main(int argc, const char* argv[]) {  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 density tree */  aux::DensityTreeFactory factory(sqrt(P), 0.5*log(P)/log(2), 0.0,      aux::DensityTreeFactory::SPLIT_VARIANCE);  aux::DensityTreePdf tree(mixtureSamples, factory);  /* sample from density tree */  aux::DiracMixturePdf treeSamples(tree, P);  /* 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);    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 << "Density tree mean" << endl <<      tree.getExpectation() << endl;  cout << "Density tree covariance" << endl <<      tree.getCovariance() << endl;  cout << "Density tree sample mean" << endl <<      treeSamples.getExpectation() << endl;  cout << "Density tree sample covariance" << endl <<      treeSamples.getCovariance() << endl;  cout << "Density tree importance sample mean" << endl <<      treeImportanceSamples.getExpectation() << endl;  cout << "Density tree importance sample covariance" << endl <<      treeImportanceSamples.getCovariance() << endl;        /* output for plots */  ofstream fMixture("results/test9_mixture.out");  ofstream fTree("results/test9_tree.out");  const aux::vector& lower = tree.getLower();  const aux::vector& upper = tree.getUpper();  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);      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 + -