add the Eigen library implementations of normal distributions computations
This commit is contained in:
parent
3eefe9cd25
commit
f3e1562a14
5 changed files with 301 additions and 106 deletions
|
|
@ -34,8 +34,8 @@ Authors:
|
|||
#include "edoNormalMulti.h"
|
||||
|
||||
#ifdef WITH_BOOST
|
||||
//! edoEstimatorNormalMulti< EOT >
|
||||
|
||||
//! edoEstimatorNormalMulti< EOT >
|
||||
template < typename EOT >
|
||||
class edoEstimatorNormalMulti : public edoEstimator< edoNormalMulti< EOT > >
|
||||
{
|
||||
|
|
@ -43,95 +43,95 @@ public:
|
|||
class CovMatrix
|
||||
{
|
||||
public:
|
||||
typedef typename EOT::AtomType AtomType;
|
||||
typedef typename EOT::AtomType AtomType;
|
||||
|
||||
CovMatrix( const eoPop< EOT >& pop )
|
||||
{
|
||||
//-------------------------------------------------------------
|
||||
// Some checks before starting to estimate covar
|
||||
//-------------------------------------------------------------
|
||||
CovMatrix( const eoPop< EOT >& pop )
|
||||
{
|
||||
//-------------------------------------------------------------
|
||||
// Some checks before starting to estimate covar
|
||||
//-------------------------------------------------------------
|
||||
|
||||
unsigned int p_size = pop.size(); // population size
|
||||
assert(p_size > 0);
|
||||
unsigned int p_size = pop.size(); // population size
|
||||
assert(p_size > 0);
|
||||
|
||||
unsigned int s_size = pop[0].size(); // solution size
|
||||
assert(s_size > 0);
|
||||
unsigned int s_size = pop[0].size(); // solution size
|
||||
assert(s_size > 0);
|
||||
|
||||
//-------------------------------------------------------------
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// Copy the population to an ublas matrix
|
||||
//-------------------------------------------------------------
|
||||
//-------------------------------------------------------------
|
||||
// Copy the population to an ublas matrix
|
||||
//-------------------------------------------------------------
|
||||
|
||||
ublas::matrix< AtomType > sample( p_size, s_size );
|
||||
ublas::matrix< AtomType > sample( p_size, s_size );
|
||||
|
||||
for (unsigned int i = 0; i < p_size; ++i)
|
||||
{
|
||||
for (unsigned int j = 0; j < s_size; ++j)
|
||||
{
|
||||
sample(i, j) = pop[i][j];
|
||||
}
|
||||
}
|
||||
for (unsigned int i = 0; i < p_size; ++i)
|
||||
{
|
||||
for (unsigned int j = 0; j < s_size; ++j)
|
||||
{
|
||||
sample(i, j) = pop[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
_varcovar.resize(s_size);
|
||||
_varcovar.resize(s_size);
|
||||
|
||||
|
||||
//-------------------------------------------------------------
|
||||
// variance-covariance matrix are symmetric (and semi-definite
|
||||
// positive), thus a triangular storage is sufficient
|
||||
//
|
||||
// variance-covariance matrix computation : transpose(A) * A
|
||||
//-------------------------------------------------------------
|
||||
//-------------------------------------------------------------
|
||||
// variance-covariance matrix are symmetric (and semi-definite
|
||||
// positive), thus a triangular storage is sufficient
|
||||
//
|
||||
// variance-covariance matrix computation : transpose(A) * A
|
||||
//-------------------------------------------------------------
|
||||
|
||||
ublas::symmetric_matrix< AtomType, ublas::lower > var = ublas::prod( ublas::trans( sample ), sample );
|
||||
ublas::symmetric_matrix< AtomType, ublas::lower > var = ublas::prod( ublas::trans( sample ), sample );
|
||||
|
||||
// Be sure that the symmetric matrix got the good size
|
||||
// Be sure that the symmetric matrix got the good size
|
||||
|
||||
assert(var.size1() == s_size);
|
||||
assert(var.size2() == s_size);
|
||||
assert(var.size1() == _varcovar.size1());
|
||||
assert(var.size2() == _varcovar.size2());
|
||||
assert(var.size1() == s_size);
|
||||
assert(var.size2() == s_size);
|
||||
assert(var.size1() == _varcovar.size1());
|
||||
assert(var.size2() == _varcovar.size2());
|
||||
|
||||
//-------------------------------------------------------------
|
||||
//-------------------------------------------------------------
|
||||
|
||||
|
||||
// TODO: to remove the comment below
|
||||
// TODO: to remove the comment below
|
||||
|
||||
// for (unsigned int i = 0; i < s_size; ++i)
|
||||
// {
|
||||
// // triangular LOWER matrix, thus j is not going further than i
|
||||
// for (unsigned int j = 0; j <= i; ++j)
|
||||
// {
|
||||
// // we want a reducted covariance matrix
|
||||
// _varcovar(i, j) = var(i, j) / p_size;
|
||||
// }
|
||||
// }
|
||||
// for (unsigned int i = 0; i < s_size; ++i)
|
||||
// {
|
||||
// // triangular LOWER matrix, thus j is not going further than i
|
||||
// for (unsigned int j = 0; j <= i; ++j)
|
||||
// {
|
||||
// // we want a reducted covariance matrix
|
||||
// _varcovar(i, j) = var(i, j) / p_size;
|
||||
// }
|
||||
// }
|
||||
|
||||
_varcovar = var / p_size;
|
||||
_varcovar = var / p_size;
|
||||
|
||||
_mean.resize(s_size); // FIXME: check if it is really used because of the assignation below
|
||||
_mean.resize(s_size); // FIXME: check if it is really used because of the assignation below
|
||||
|
||||
// unit vector
|
||||
ublas::scalar_vector< AtomType > u( p_size, 1 );
|
||||
// unit vector
|
||||
ublas::scalar_vector< AtomType > u( p_size, 1 );
|
||||
|
||||
// sum over columns
|
||||
_mean = ublas::prod( ublas::trans( sample ), u );
|
||||
// sum over columns
|
||||
_mean = ublas::prod( ublas::trans( sample ), u );
|
||||
|
||||
// division by n
|
||||
_mean /= p_size;
|
||||
}
|
||||
// division by n
|
||||
_mean /= p_size;
|
||||
}
|
||||
|
||||
const ublas::symmetric_matrix< AtomType, ublas::lower >& get_varcovar() const {return _varcovar;}
|
||||
const ublas::symmetric_matrix< AtomType, ublas::lower >& get_varcovar() const {return _varcovar;}
|
||||
|
||||
const ublas::vector< AtomType >& get_mean() const {return _mean;}
|
||||
const ublas::vector< AtomType >& get_mean() const {return _mean;}
|
||||
|
||||
private:
|
||||
ublas::symmetric_matrix< AtomType, ublas::lower > _varcovar;
|
||||
ublas::vector< AtomType > _mean;
|
||||
ublas::symmetric_matrix< AtomType, ublas::lower > _varcovar;
|
||||
ublas::vector< AtomType > _mean;
|
||||
};
|
||||
|
||||
public:
|
||||
|
|
@ -139,21 +139,109 @@ public:
|
|||
|
||||
edoNormalMulti< EOT > operator()(eoPop<EOT>& pop)
|
||||
{
|
||||
unsigned int popsize = pop.size();
|
||||
assert(popsize > 0);
|
||||
unsigned int popsize = pop.size();
|
||||
assert(popsize > 0);
|
||||
|
||||
unsigned int dimsize = pop[0].size();
|
||||
assert(dimsize > 0);
|
||||
unsigned int dimsize = pop[0].size();
|
||||
assert(dimsize > 0);
|
||||
|
||||
CovMatrix cov( pop );
|
||||
CovMatrix cov( pop );
|
||||
|
||||
return edoNormalMulti< EOT >( cov.get_mean(), cov.get_varcovar() );
|
||||
return edoNormalMulti< EOT >( cov.get_mean(), cov.get_varcovar() );
|
||||
}
|
||||
};
|
||||
|
||||
#else
|
||||
#ifdef WITH_EIGEN
|
||||
|
||||
//! edoEstimatorNormalMulti< EOT >
|
||||
template < typename EOT >
|
||||
class edoEstimatorNormalMulti : public edoEstimator< edoNormalMulti< EOT > >
|
||||
{
|
||||
public:
|
||||
class CovMatrix
|
||||
{
|
||||
public:
|
||||
typedef typename EOT::AtomType AtomType;
|
||||
typedef Eigen::Matrix< AtomType, Eigen::Dynamic, 1> Vector;
|
||||
typedef Eigen::Matrix< AtomType, Eigen::Dynamic, Eigen::Dynamic> Matrix;
|
||||
|
||||
CovMatrix( const eoPop< EOT >& pop )
|
||||
{
|
||||
// Some checks before starting to estimate covar
|
||||
unsigned int p_size = pop.size(); // population size
|
||||
assert(p_size > 0);
|
||||
unsigned int s_size = pop[0].size(); // solution size
|
||||
assert(s_size > 0);
|
||||
|
||||
// Copy the population to an ublas matrix
|
||||
//ublas::matrix< AtomType > sample( p_size, s_size );
|
||||
Matrix sample( p_size, s_size );
|
||||
|
||||
for (unsigned int i = 0; i < p_size; ++i) {
|
||||
for (unsigned int j = 0; j < s_size; ++j) {
|
||||
sample(i, j) = pop[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
// _varcovar.resize(s_size);
|
||||
|
||||
// variance-covariance matrix are symmetric, thus a triangular storage is sufficient
|
||||
// variance-covariance matrix computation : transpose(A) * A
|
||||
//ublas::symmetric_matrix< AtomType, ublas::lower > var = ublas::prod( ublas::trans( sample ), sample );
|
||||
Matrix var = sample.transpose() * sample;
|
||||
|
||||
// Be sure that the symmetric matrix got the good size
|
||||
assert(var.innerSize() == s_size);
|
||||
assert(var.outerSize() == s_size);
|
||||
assert(var.innerSize() == _varcovar.innerSize());
|
||||
assert(var.outerSize() == _varcovar.outerSize());
|
||||
|
||||
_varcovar = var / p_size;
|
||||
|
||||
// _mean.resize(s_size); // FIXME: check if it is really used because of the assignation below
|
||||
|
||||
// unit vector
|
||||
// ublas::scalar_vector< AtomType > u( p_size, 1 );
|
||||
Vector u( p_size, 1);
|
||||
|
||||
// sum over columns
|
||||
// _mean = ublas::prod( ublas::trans( sample ), u );
|
||||
_mean = sample.transpose() * u;
|
||||
|
||||
// division by n
|
||||
_mean /= p_size;
|
||||
}
|
||||
|
||||
// const ublas::symmetric_matrix< AtomType, ublas::lower >& get_varcovar() const {return _varcovar;}
|
||||
const Matrix& get_varcovar() const {return _varcovar;}
|
||||
|
||||
// const ublas::vector< AtomType >& get_mean() const {return _mean;}
|
||||
const Vector& get_mean() const {return _mean;}
|
||||
|
||||
private:
|
||||
// ublas::symmetric_matrix< AtomType, ublas::lower > _varcovar;
|
||||
Matrix _varcovar;
|
||||
// ublas::vector< AtomType > _mean;
|
||||
Vector _mean;
|
||||
};
|
||||
|
||||
public:
|
||||
typedef typename EOT::AtomType AtomType;
|
||||
|
||||
edoNormalMulti< EOT > operator()(eoPop<EOT>& pop)
|
||||
{
|
||||
unsigned int popsize = pop.size();
|
||||
assert(popsize > 0);
|
||||
|
||||
unsigned int dimsize = pop[0].size();
|
||||
assert(dimsize > 0);
|
||||
|
||||
CovMatrix cov( pop );
|
||||
|
||||
return edoNormalMulti< EOT >( cov.get_mean(), cov.get_varcovar() );
|
||||
}
|
||||
};
|
||||
#endif // WITH_EIGEN
|
||||
#endif // WITH_BOOST
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue