added sampling function for NormalN and approximation of normalN using samples + testcases for both
This commit is contained in:
77
tests/math/distribution/TestNormalN.cpp
Normal file
77
tests/math/distribution/TestNormalN.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#ifdef WITH_TESTS
|
||||
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user