80 lines
1.8 KiB
C++
80 lines
1.8 KiB
C++
#ifdef WITH_TESTS
|
|
#ifdef WITH_EIGEN
|
|
|
|
#include "../../Tests.h"
|
|
#include "../../../math/divergence/KullbackLeibler.h"
|
|
#include "../../../math/Distributions.h"
|
|
|
|
#include <random>
|
|
|
|
#include <KLib/misc/gnuplot/Gnuplot.h>
|
|
#include <KLib/misc/gnuplot/GnuplotPlot.h>
|
|
#include <KLib/misc/gnuplot/GnuplotPlotElementLines.h>
|
|
#include <KLib/misc/gnuplot/GnuplotPlotElementPoints.h>
|
|
|
|
|
|
TEST(NormalN, multivariateGaussSampleData) {
|
|
|
|
Eigen::VectorXd mu(2);
|
|
mu[0] = 0.0;
|
|
mu[1] = 0.0;
|
|
|
|
Eigen::MatrixXd cov(2,2);
|
|
cov(0,0) = 1.0;
|
|
cov(0,1) = 0.0;
|
|
cov(1,0) = 0.0;
|
|
cov(1,1) = 1.0;
|
|
|
|
Distribution::NormalDistributionN normTest(mu, cov);
|
|
|
|
// draw
|
|
K::Gnuplot gp;
|
|
K::GnuplotPlot plot;
|
|
K::GnuplotPlotElementPoints pParticles;
|
|
|
|
gp << "set terminal qt size 1200,1200 \n";
|
|
gp << "set xrange [-5:5] \n set yrange [-5:5] \n";
|
|
|
|
// gen
|
|
std::vector<Eigen::VectorXd> vals;
|
|
for(int i = 0; i < 100000; ++i){
|
|
Eigen::VectorXd vec = normTest.draw();
|
|
vals.push_back(vec);
|
|
|
|
K::GnuplotPoint2 pos(vec[0], vec[1]);
|
|
pParticles.add(pos);
|
|
}
|
|
|
|
plot.add(&pParticles);
|
|
|
|
gp.draw(plot);
|
|
gp.flush();
|
|
|
|
sleep(5);
|
|
|
|
//create matrix out of the vector
|
|
Eigen::MatrixXd m(vals.size(), vals[0].rows());
|
|
|
|
for(int i = 0; i < vals.size(); ++i){
|
|
m(i,0) = vals[i][0];
|
|
m(i,1) = vals[i][1];
|
|
}
|
|
|
|
Distribution::NormalDistributionN norm = Distribution::NormalDistributionN::getNormalNFromSamples(m);
|
|
|
|
//get distance between original and approximated mu and sigma
|
|
Eigen::MatrixXd diffM = cov - norm.getSigma();
|
|
double distM = diffM.norm();
|
|
|
|
Eigen::VectorXd diffV = mu - norm.getMu();
|
|
double distV = diffV.norm();
|
|
|
|
ASSERT_NEAR(0.0, distM, 0.01);
|
|
ASSERT_NEAR(0.0, distV, 0.01);
|
|
|
|
}
|
|
|
|
#endif
|
|
#endif
|
|
|