MRPT  1.9.9
CProbabilityDensityFunction.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
13 #include <mrpt/math/math_frwds.h>
14 
15 namespace mrpt::math
16 {
17 /** A generic template for probability density distributions (PDFs).
18  * This template is used as base for many classes in mrpt::poses
19  * Any derived class must implement \a getMean() and a getCovarianceAndMean().
20  * Other methods such as \a getMean() or \a getCovariance() are implemented
21  * here for convenience.
22  * \sa mprt::poses::CPosePDF, mprt::poses::CPose3DPDF, mprt::poses::CPointPDF
23  * \ingroup mrpt_math_grp
24  */
25 template <class TDATA, size_t STATE_LEN>
27 {
28  public:
29  /** The length of the variable, for example, 3 for a 3D point, 6 for a 3D
30  * pose (x y z yaw pitch roll). */
31  static constexpr size_t state_length = STATE_LEN;
32  /** The type of the state the PDF represents */
33  using type_value = TDATA;
35 
36  /** Returns the mean, or mathematical expectation of the probability density
37  * distribution (PDF).
38  * \sa getCovarianceAndMean, getInformationMatrix
39  */
40  virtual void getMean(TDATA& mean_point) const = 0;
41 
42  /** Returns an estimate of the pose covariance matrix (STATE_LENxSTATE_LEN
43  * cov matrix) and the mean, both at once.
44  * \sa getMean, getInformationMatrix
45  */
46  virtual void getCovarianceAndMean(
48  TDATA& mean_point) const = 0;
49 
50  /** Returns an estimate of the pose covariance matrix (STATE_LENxSTATE_LEN
51  * cov matrix) and the mean, both at once.
52  * \sa getMean, getInformationMatrix
53  */
55  mrpt::math::CMatrixDouble& cov, TDATA& mean_point) const
56  {
59  this->getCovarianceAndMean(C, mean_point);
60  cov = C; // Convert to dynamic size matrix
61  }
62 
63  /** Returns the mean, or mathematical expectation of the probability density
64  * distribution (PDF).
65  * \sa getCovariance, getInformationMatrix
66  */
67  inline TDATA getMeanVal() const
68  {
69  TDATA p;
70  getMean(p);
71  return p;
72  }
73 
74  /** Returns the estimate of the covariance matrix (STATE_LEN x STATE_LEN
75  * covariance matrix)
76  * \sa getMean, getCovarianceAndMean, getInformationMatrix
77  */
79  {
80  TDATA p;
81  this->getCovarianceDynAndMean(cov, p);
82  }
83 
84  /** Returns the estimate of the covariance matrix (STATE_LEN x STATE_LEN
85  * covariance matrix)
86  * \sa getMean, getCovarianceAndMean, getInformationMatrix
87  */
88  inline void getCovariance(
90  const
91  {
92  TDATA p;
93  this->getCovarianceAndMean(cov, p);
94  }
95 
96  /** Returns the estimate of the covariance matrix (STATE_LEN x STATE_LEN
97  * covariance matrix)
98  * \sa getMean, getInformationMatrix
99  */
102  {
105  TDATA p;
106  this->getCovarianceAndMean(cov, p);
107  return cov;
108  }
109 
110  /** Returns whether the class instance holds the uncertainty in covariance
111  * or information form.
112  * \note By default this is going to be covariance form. *Inf classes
113  * (e.g. CPosePDFGaussianInf) store it in information form.
114  *
115  * \sa mrpt::traits::is_inf_type
116  */
117  virtual bool isInfType() const { return false; }
118  /** Returns the information (inverse covariance) matrix (a STATE_LEN x
119  * STATE_LEN matrix)
120  * Unless reimplemented in derived classes, this method first reads the
121  * covariance, then invert it.
122  * \sa getMean, getCovarianceAndMean
123  */
124  virtual void getInformationMatrix(
126  const
127  {
130  TDATA p;
131  this->getCovarianceAndMean(cov, p);
132  cov.inv_fast(
133  inf); // Destroy source cov matrix, since we don't need it anymore.
134  }
135 
136  /** Save PDF's particles to a text file. See derived classes for more
137  * information about the format of generated files.
138  * \return false on error
139  */
140  virtual bool saveToTextFile(const std::string& file) const = 0;
141 
142  /** Draws a single sample from the distribution
143  */
144  virtual void drawSingleSample(TDATA& outPart) const = 0;
145 
146  /** Draws a number of samples from the distribution, and saves as a list of
147  * 1xSTATE_LEN vectors, where each row contains a (x,y,z,yaw,pitch,roll)
148  * datum.
149  * This base method just call N times to drawSingleSample, but derived
150  * classes should implemented optimized method for each particular PDF.
151  */
152  virtual void drawManySamples(
153  size_t N, std::vector<mrpt::math::CVectorDouble>& outSamples) const
154  {
155  outSamples.resize(N);
156  TDATA pnt;
157  for (size_t i = 0; i < N; i++)
158  {
159  this->drawSingleSample(pnt);
160  pnt.getAsVector(outSamples[i]);
161  }
162  }
163 
164  /** Compute the entropy of the estimated covariance matrix.
165  * \sa
166  * http://en.wikipedia.org/wiki/Multivariate_normal_distribution#Entropy
167  */
168  double getCovarianceEntropy() const
169  {
170  static const double ln_2PI = 1.8378770664093454835606594728112;
171  return 0.5 * (STATE_LEN + STATE_LEN * ln_2PI +
172  log(std::max(
173  getCovariance().det(),
174  std::numeric_limits<double>::epsilon())));
175  }
176 
177 }; // End of class def.
178 
179 } // namespace mrpt::math
A numeric matrix of compile-time fixed size.
A generic template for probability density distributions (PDFs).
void getCovariance(mrpt::math::CMatrixFixedNumeric< double, STATE_LEN, STATE_LEN > &cov) const
Returns the estimate of the covariance matrix (STATE_LEN x STATE_LEN covariance matrix)
mrpt::math::CMatrixFixedNumeric< double, STATE_LEN, STATE_LEN > getCovariance() const
Returns the estimate of the covariance matrix (STATE_LEN x STATE_LEN covariance matrix)
void getCovarianceDynAndMean(mrpt::math::CMatrixDouble &cov, TDATA &mean_point) const
Returns an estimate of the pose covariance matrix (STATE_LENxSTATE_LEN cov matrix) and the mean,...
virtual void getMean(TDATA &mean_point) const =0
Returns the mean, or mathematical expectation of the probability density distribution (PDF).
virtual void getInformationMatrix(mrpt::math::CMatrixFixedNumeric< double, STATE_LEN, STATE_LEN > &inf) const
Returns the information (inverse covariance) matrix (a STATE_LEN x STATE_LEN matrix) Unless reimpleme...
TDATA getMeanVal() const
Returns the mean, or mathematical expectation of the probability density distribution (PDF).
virtual void getCovarianceAndMean(mrpt::math::CMatrixFixedNumeric< double, STATE_LEN, STATE_LEN > &cov, TDATA &mean_point) const =0
Returns an estimate of the pose covariance matrix (STATE_LENxSTATE_LEN cov matrix) and the mean,...
static constexpr size_t state_length
The length of the variable, for example, 3 for a 3D point, 6 for a 3D pose (x y z yaw pitch roll).
void getCovariance(mrpt::math::CMatrixDouble &cov) const
Returns the estimate of the covariance matrix (STATE_LEN x STATE_LEN covariance matrix)
virtual void drawSingleSample(TDATA &outPart) const =0
Draws a single sample from the distribution.
virtual bool isInfType() const
Returns whether the class instance holds the uncertainty in covariance or information form.
double getCovarianceEntropy() const
Compute the entropy of the estimated covariance matrix.
virtual bool saveToTextFile(const std::string &file) const =0
Save PDF's particles to a text file.
virtual void drawManySamples(size_t N, std::vector< mrpt::math::CVectorDouble > &outSamples) const
Draws a number of samples from the distribution, and saves as a list of 1xSTATE_LEN vectors,...
TDATA type_value
The type of the state the PDF represents.
EIGEN_STRONG_INLINE Scalar det() const
GLfloat GLfloat p
Definition: glext.h:6305
GLsizei const GLchar ** string
Definition: glext.h:4101
This base provides a set of functions for maths stuff.
@ UNINITIALIZED_MATRIX
Definition: math_frwds.h:75
Eigen::Matrix< typename MATRIX::Scalar, MATRIX::ColsAtCompileTime, MATRIX::ColsAtCompileTime > cov(const MATRIX &v)
Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample,...
Definition: ops_matrices.h:148



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 814d80880 Fri Aug 24 01:51:28 2018 +0200 at mar 26 may 2026 12:30:59 CEST