34 template <
class VECTORLIKE1,
class VECTORLIKE2,
class MAT>
36 const VECTORLIKE1& X,
const VECTORLIKE2& MU,
const MAT& COV)
39 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 42 ASSERT_(X.size() == COV.rows() && COV.isSquare());
44 const size_t N = X.size();
46 for (
size_t i = 0; i < N; i++) X_MU[i] = X[i] - MU[i];
47 auto z = COV.llt_solve(X_MU);
56 template <
class VECTORLIKE1,
class VECTORLIKE2,
class MAT>
58 const VECTORLIKE1& X,
const VECTORLIKE2& MU,
const MAT& COV)
69 template <
class VECTORLIKE,
class MAT1,
class MAT2,
class MAT3>
71 const VECTORLIKE& mean_diffs,
const MAT1& COV1,
const MAT2& COV2,
72 const MAT3& CROSS_COV12)
75 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 77 ASSERT_(mean_diffs.size() == COV1.rows());
78 ASSERT_(COV1.isSquare() && COV2.isSquare());
79 ASSERT_(COV1.rows() == COV2.rows());
83 COV.substract_An(CROSS_COV12, 2);
85 COV.inverse_LLt(COV_inv);
96 template <
class VECTORLIKE,
class MAT1,
class MAT2,
class MAT3>
98 const VECTORLIKE& mean_diffs,
const MAT1& COV1,
const MAT2& COV2,
99 const MAT3& CROSS_COV12)
109 template <
class VECTORLIKE,
class MATRIXLIKE>
111 const VECTORLIKE& delta_mu,
const MATRIXLIKE&
cov)
123 template <
class VECTORLIKE,
class MATRIXLIKE>
125 const VECTORLIKE& delta_mu,
const MATRIXLIKE&
cov)
135 template <
typename T>
140 const size_t vector_dim = mean_diffs.size();
145 const T cov_det = C.
det();
149 return std::pow(
M_2PI, -0.5 * vector_dim) * (1.0 / std::sqrt(cov_det)) *
150 exp(-0.5 * mean_diffs.multiply_HCHt_scalar(C_inv));
158 template <
typename T,
size_t DIM>
163 ASSERT_(mean_diffs.size() == DIM);
167 const T cov_det = C.
det();
169 C.inverse_LLt(C_inv);
171 return std::pow(
M_2PI, -0.5 * DIM) * (1.0 / std::sqrt(cov_det)) *
172 exp(-0.5 * mean_diffs.multiply_HCHt_scalar(C_inv));
179 template <
typename T,
class VECLIKE,
class MATLIKE1,
class MATLIKE2>
181 const VECLIKE& mean_diffs,
const MATLIKE1& COV1,
const MATLIKE2& COV2,
182 T& maha2_out, T& intprod_out,
const MATLIKE1* CROSS_COV12 =
nullptr)
184 const size_t vector_dim = mean_diffs.size();
194 const T cov_det = C.det();
196 C.inverse_LLt(C_inv);
199 intprod_out = std::pow(
M_2PI, -0.5 * vector_dim) *
200 (1.0 / std::sqrt(cov_det)) * exp(-0.5 * maha2_out);
208 template <
typename T,
class VECLIKE,
class MATRIXLIKE>
210 const VECLIKE& diff_mean,
const MATRIXLIKE&
cov, T& maha2_out,
221 ::log(static_cast<typename MATRIXLIKE::Scalar>(
M_2PI)) +
230 template <
typename T,
class VECLIKE,
class MATRIXLIKE>
232 const VECLIKE& diff_mean,
const MATRIXLIKE&
cov, T& maha2_out, T& pdf_out)
235 pdf_out = std::exp(pdf_out);
258 class VECTOR_OF_VECTORS,
class MATRIXLIKE,
class VECTORLIKE,
259 class VECTORLIKE2,
class VECTORLIKE3>
263 const VECTOR_OF_VECTORS& elements, MATRIXLIKE& covariances,
264 VECTORLIKE& means,
const VECTORLIKE2* weights_mean,
265 const VECTORLIKE3* weights_cov,
const bool* elem_do_wrap2pi =
nullptr)
268 elements.size() != 0,
269 "No samples provided, so there is no way to deduce the output size.");
271 const size_t DIM = elements[0].size();
273 covariances.resize(DIM, DIM);
274 const size_t nElms = elements.size();
275 const T NORM = 1.0 / nElms;
278 ASSERTDEB_(
size_t(weights_mean->size()) ==
size_t(nElms));
281 for (
size_t i = 0; i < DIM; i++)
284 if (!elem_do_wrap2pi || !elem_do_wrap2pi[i])
288 for (
size_t j = 0; j < nElms; j++)
289 accum += (*weights_mean)[j] * elements[j][i];
293 for (
size_t j = 0; j < nElms; j++) accum += elements[j][i];
300 double accum_L = 0, accum_R = 0;
301 double Waccum_L = 0, Waccum_R = 0;
302 for (
size_t j = 0; j < nElms; j++)
304 double ang = elements[j][i];
306 weights_mean !=
nullptr ? (*weights_mean)[j] : NORM;
307 if (fabs(ang) > 0.5 *
M_PI)
309 if (ang < 0) ang = (
M_2PI + ang);
319 if (Waccum_L > 0) accum_L /= Waccum_L;
320 if (Waccum_R > 0) accum_R /= Waccum_R;
324 (accum_L * Waccum_L +
330 for (
size_t i = 0; i < DIM; i++)
331 for (
size_t j = 0; j <= i; j++)
336 ASSERTDEB_(
size_t(weights_cov->size()) ==
size_t(nElms));
337 for (
size_t k = 0; k < nElms; k++)
339 const T Ai = (elements[k][i] - means[i]);
340 const T Aj = (elements[k][j] - means[j]);
341 if (!elem_do_wrap2pi || !elem_do_wrap2pi[i])
342 elem += (*weights_cov)[k] * Ai * Aj;
350 for (
size_t k = 0; k < nElms; k++)
352 const T Ai = (elements[k][i] - means[i]);
353 const T Aj = (elements[k][j] - means[j]);
354 if (!elem_do_wrap2pi || !elem_do_wrap2pi[i])
362 covariances(i, j) = elem;
363 if (i != j) covariances(j, i) = elem;
380 template <
class VECTOR_OF_VECTORS,
class MATRIXLIKE,
class VECTORLIKE>
382 const VECTOR_OF_VECTORS& elements, MATRIXLIKE& covariances,
383 VECTORLIKE& means,
const bool* elem_do_wrap2pi =
nullptr)
388 elements, covariances, means,
nullptr,
nullptr, elem_do_wrap2pi);
403 template <
class VECTORLIKE1,
class VECTORLIKE2>
405 const VECTORLIKE1&
values,
const VECTORLIKE1&
weights,
float binWidth,
406 VECTORLIKE2& out_binCenters, VECTORLIKE2& out_binValues)
415 static_cast<unsigned>(ceil((
maximum(
values) - minBin) / binWidth));
418 out_binCenters.resize(nBins);
419 out_binValues.clear();
420 out_binValues.resize(nBins, 0);
421 TNum halfBin = TNum(0.5) * binWidth;
423 VECTORLIKE2 binBorders(nBins + 1, minBin - halfBin);
424 for (
unsigned int i = 0; i < nBins; i++)
426 binBorders[i + 1] = binBorders[i] + binWidth;
427 out_binCenters[i] = binBorders[i] + halfBin;
432 typename VECTORLIKE1::const_iterator itVal, itW;
436 int idx =
round(((*itVal) - minBin) / binWidth);
437 if (idx >=
int(nBins)) idx = nBins - 1;
439 out_binValues[idx] += *itW;
443 if (totalSum) out_binValues /= totalSum;
460 template <
class VECTORLIKE1,
class VECTORLIKE2>
462 const VECTORLIKE1&
values,
const VECTORLIKE1& log_weights,
float binWidth,
463 VECTORLIKE2& out_binCenters, VECTORLIKE2& out_binValues)
472 static_cast<unsigned>(ceil((
maximum(
values) - minBin) / binWidth));
475 out_binCenters.resize(nBins);
476 out_binValues.clear();
477 out_binValues.resize(nBins, 0);
478 TNum halfBin = TNum(0.5) * binWidth;
480 VECTORLIKE2 binBorders(nBins + 1, minBin - halfBin);
481 for (
unsigned int i = 0; i < nBins; i++)
483 binBorders[i + 1] = binBorders[i] + binWidth;
484 out_binCenters[i] = binBorders[i] + halfBin;
488 const TNum max_log_weight =
maximum(log_weights);
490 typename VECTORLIKE1::const_iterator itVal, itW;
491 for (itVal =
values.begin(), itW = log_weights.begin();
492 itVal !=
values.end(); ++itVal, ++itW)
494 int idx =
round(((*itVal) - minBin) / binWidth);
495 if (idx >=
int(nBins)) idx = nBins - 1;
497 const TNum
w = exp(*itW - max_log_weight);
498 out_binValues[idx] +=
w;
502 if (totalSum) out_binValues /= totalSum;
void mahalanobisDistance2AndPDF(const VECLIKE &diff_mean, const MATRIXLIKE &cov, T &maha2_out, T &pdf_out)
Computes both, the PDF and the square Mahalanobis distance between a point (given by its difference w...
double averageWrap2Pi(const CVectorDouble &angles)
Computes the average of a sequence of angles in radians taking into account the correct wrapping in t...
MAT_C::Scalar multiply_HtCH_scalar(const VECTOR_H &H, const MAT_C &C)
r (a scalar) = H * C * H^t (with a column vector H and a symmetric matrix C)
A compile-time fixed-size numeric matrix container.
Template for column vectors of dynamic size, compatible with Eigen.
GLboolean GLenum GLenum GLvoid * values
This file implements miscelaneous matrix and matrix/vector operations, and internal functions in mrpt...
MAT_C::Scalar multiply_HCHt_scalar(const VECTOR_H &H, const MAT_C &C)
r (a scalar) = H^t * C * H (with a row vector H and a symmetric matrix C)
bool isSquare() const
returns true if matrix is NxN
void productIntegralAndMahalanobisTwoGaussians(const VECLIKE &mean_diffs, const MATLIKE1 &COV1, const MATLIKE2 &COV2, T &maha2_out, T &intprod_out, const MATLIKE1 *CROSS_COV12=nullptr)
Computes both, the integral of the product of two Gaussians and their square Mahalanobis distance...
GLubyte GLubyte GLubyte GLubyte w
#define ASSERT_(f)
Defines an assertion mechanism.
T productIntegralTwoGaussians(const std::vector< T > &mean_diffs, const CMatrixDynamic< T > &COV1, const CMatrixDynamic< T > &COV2)
Computes the integral of the product of two Gaussians, with means separated by "mean_diffs" and covar...
Derived inverse() const
Returns the inverse of a general matrix using LU.
void mahalanobisDistance2AndLogPDF(const VECLIKE &diff_mean, const MATRIXLIKE &cov, T &maha2_out, T &log_pdf_out)
Computes both, the logarithm of the PDF and the square Mahalanobis distance between a point (given by...
CVectorDynamic< double > CVectorDouble
T dot(const CVectorDynamic< T > &v) const
dot product of this \cdot v
void covariancesAndMean(const VECTOR_OF_VECTORS &elements, MATRIXLIKE &covariances, VECTORLIKE &means, const bool *elem_do_wrap2pi=nullptr)
Computes covariances and mean of any vector of containers.
double averageLogLikelihood(const CVectorDouble &logLikelihoods)
A numerically-stable method to compute average likelihood values with strongly different ranges (unwe...
CONTAINER::Scalar maximum(const CONTAINER &v)
T det() const
Determinant of matrix.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
CMatrixDynamic< T > inverse_LLt() const
Returns the inverse of a symmetric matrix using LLt.
CMatrixDouble cov(const MATRIX &v)
Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...
T wrapToPi(T a)
Modifies the given angle to translate it into the ]-pi,pi] range.
size_type cols() const
Number of columns in the matrix.
MAT::Scalar mahalanobisDistance2(const VECTORLIKE1 &X, const VECTORLIKE2 &MU, const MAT &COV)
Computes the squared mahalanobis distance of a vector X given the mean MU and the covariance inverse ...
ContainerType<T>::element_t exposes the value of any STL or Eigen container.
void weightedHistogram(const VECTORLIKE1 &values, const VECTORLIKE1 &weights, float binWidth, VECTORLIKE2 &out_binCenters, VECTORLIKE2 &out_binValues)
Computes the weighted histogram for a vector of values and their corresponding weights.
CONTAINER::Scalar minimum(const CONTAINER &v)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void covariancesAndMeanWeighted(const VECTOR_OF_VECTORS &elements, MATRIXLIKE &covariances, VECTORLIKE &means, const VECTORLIKE2 *weights_mean, const VECTORLIKE3 *weights_cov, const bool *elem_do_wrap2pi=nullptr)
Computes covariances and mean of any vector of containers, given optional weights for the different s...
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
VECTORLIKE1::Scalar mahalanobisDistance(const VECTORLIKE1 &X, const VECTORLIKE2 &MU, const MAT &COV)
Computes the mahalanobis distance of a vector X given the mean MU and the covariance inverse COV_inv ...
void weightedHistogramLog(const VECTORLIKE1 &values, const VECTORLIKE1 &log_weights, float binWidth, VECTORLIKE2 &out_binCenters, VECTORLIKE2 &out_binValues)
Computes the weighted histogram for a vector of values and their corresponding log-weights.
This template class provides the basic functionality for a general 2D any-size, resizable container o...
int round(const T value)
Returns the closer integer (int) to x.