Main MRPT website > C++ reference for MRPT 1.9.9
transform_gaussian.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-2017, 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 #ifndef transform_gaussian_H
10 #define transform_gaussian_H
11 
12 #include <mrpt/utils/utils_defs.h>
13 #include <mrpt/math/math_frwds.h>
16 #include <mrpt/math/ops_matrices.h>
17 #include <mrpt/math/jacobians.h>
18 #include <mrpt/math/data_utils.h>
19 #include <mrpt/random.h>
20 #include <functional>
21 
22 namespace mrpt
23 {
24 namespace math
25 {
26 /** @addtogroup gausspdf_transform_grp Gaussian PDF transformation functions
27  * \ingroup mrpt_base_grp
28  * @{ */
29 
30 /** Scaled unscented transformation (SUT) for estimating the Gaussian
31  * distribution of a variable Y=f(X) for an arbitrary function f() provided by
32  * the user.
33  * The user must supply the function in "functor" which takes points in the X
34  * space and returns the mapped point in Y, optionally using an extra, constant
35  * parameter ("fixed_param") which remains constant.
36  *
37  * The parameters alpha, K and beta are the common names of the SUT method,
38  * and the default values are those recommended in most papers.
39  *
40  * \param elem_do_wrap2pi If !=nullptr; it must point to an array of "bool" of
41  * size()==dimension of each element, stating if it's needed to do a wrap to
42  * [-pi,pi] to each dimension.
43  * \sa The example in MRPT/samples/unscented_transform_test
44  * \sa transform_gaussian_montecarlo, transform_gaussian_linear
45  */
46 template <class VECTORLIKE1, class MATLIKE1, class USERPARAM, class VECTORLIKE2,
47  class VECTORLIKE3, class MATLIKE2>
49  const VECTORLIKE1& x_mean, const MATLIKE1& x_cov,
50  void (*functor)(
51  const VECTORLIKE1& x, const USERPARAM& fixed_param, VECTORLIKE3& y),
52  const USERPARAM& fixed_param, VECTORLIKE2& y_mean, MATLIKE2& y_cov,
53  const bool* elem_do_wrap2pi = nullptr, const double alpha = 1e-3,
54  const double K = 0, const double beta = 2.0)
55 {
57  const size_t Nx = x_mean.size(); // Dimensionality of inputs X
58  const double lambda = alpha * alpha * (Nx + K) - Nx;
59  const double c = Nx + lambda;
60 
61  // Generate weights:
62  const double Wi = 0.5 / c;
63  std::vector<double> W_mean(1 + 2 * Nx, Wi), W_cov(1 + 2 * Nx, Wi);
64  W_mean[0] = lambda / c;
65  W_cov[0] = W_mean[0] + (1 - alpha * alpha + beta);
66 
67  // Generate X_i samples:
68  MATLIKE1 L;
69  const bool valid = x_cov.chol(L);
70  if (!valid)
71  throw std::runtime_error(
72  "transform_gaussian_unscented: Singular covariance matrix in "
73  "Cholesky.");
74  L *= sqrt(c);
75 
76  // Propagate the samples X_i -> Y_i:
77  // We don't need to store the X sigma points: just use one vector to compute
78  // all the Y sigma points:
80  1 + 2 * Nx); // 2Nx+1 sigma points
81  VECTORLIKE1 X = x_mean;
82  functor(X, fixed_param, Y[0]);
83  VECTORLIKE1 delta; // i'th row of L:
84  delta.resize(Nx);
85  size_t row = 1;
86  for (size_t i = 0; i < Nx; i++)
87  {
88  L.extractRowAsCol(i, delta);
89  X = x_mean;
90  X -= delta;
91  functor(X, fixed_param, Y[row++]);
92  X = x_mean;
93  X += delta;
94  functor(X, fixed_param, Y[row++]);
95  }
96 
97  // Estimate weighted cov & mean:
99  Y, y_cov, y_mean, &W_mean, &W_cov, elem_do_wrap2pi);
100  MRPT_END
101 }
102 
103 /** Simple Montecarlo-base estimation of the Gaussian distribution of a variable
104  * Y=f(X) for an arbitrary function f() provided by the user.
105  * The user must supply the function in "functor" which takes points in the X
106  * space and returns the mapped point in Y, optionally using an extra, constant
107  * parameter ("fixed_param") which remains constant.
108  * \param out_samples_y If !=nullptr, this vector will contain, upon return,
109  * the sequence of random samples generated and propagated through the
110  * functor().
111  * \sa The example in MRPT/samples/unscented_transform_test
112  * \sa transform_gaussian_unscented, transform_gaussian_linear
113  */
114 template <class VECTORLIKE1, class MATLIKE1, class USERPARAM, class VECTORLIKE2,
115  class VECTORLIKE3, class MATLIKE2>
117  const VECTORLIKE1& x_mean, const MATLIKE1& x_cov,
118  void (*functor)(
119  const VECTORLIKE1& x, const USERPARAM& fixed_param, VECTORLIKE3& y),
120  const USERPARAM& fixed_param, VECTORLIKE2& y_mean, MATLIKE2& y_cov,
121  const size_t num_samples = 1000,
122  typename mrpt::aligned_containers<VECTORLIKE3>::vector_t* out_samples_y =
123  nullptr)
124 {
125  MRPT_START
128  samples_x, num_samples, x_cov, &x_mean);
130  num_samples);
131  for (size_t i = 0; i < num_samples; i++)
132  functor(samples_x[i], fixed_param, samples_y[i]);
133  mrpt::math::covariancesAndMean(samples_y, y_cov, y_mean);
134  if (out_samples_y)
135  {
136  out_samples_y->clear();
137  samples_y.swap(*out_samples_y);
138  }
139  MRPT_END
140 }
141 
142 /** First order uncertainty propagation estimator of the Gaussian distribution
143  * of a variable Y=f(X) for an arbitrary function f() provided by the user.
144  * The user must supply the function in "functor" which takes points in the X
145  * space and returns the mapped point in Y, optionally using an extra, constant
146  * parameter ("fixed_param") which remains constant.
147  * The Jacobians are estimated numerically using the vector of small
148  * increments "x_increments".
149  * \sa The example in MRPT/samples/unscented_transform_test
150  * \sa transform_gaussian_unscented, transform_gaussian_montecarlo
151  */
152 template <class VECTORLIKE1, class MATLIKE1, class USERPARAM, class VECTORLIKE2,
153  class VECTORLIKE3, class MATLIKE2>
155  const VECTORLIKE1& x_mean, const MATLIKE1& x_cov,
156  void (*functor)(
157  const VECTORLIKE1& x, const USERPARAM& fixed_param, VECTORLIKE3& y),
158  const USERPARAM& fixed_param, VECTORLIKE2& y_mean, MATLIKE2& y_cov,
159  const VECTORLIKE1& x_increments)
160 {
161  MRPT_START
162  // Mean: simple propagation:
163  functor(x_mean, fixed_param, y_mean);
164  // Cov: COV = H C Ht
165  Eigen::Matrix<double, VECTORLIKE3::RowsAtCompileTime,
166  VECTORLIKE1::RowsAtCompileTime>
167  H;
169  x_mean, std::function<void(
170  const VECTORLIKE1& x, const USERPARAM& fixed_param,
171  VECTORLIKE3& y)>(functor),
172  x_increments, fixed_param, H);
173  H.multiply_HCHt(x_cov, y_cov);
174  MRPT_END
175 }
176 
177 /** @} */
178 
179 } // End of MATH namespace
180 
181 } // End of namespace
182 
183 #endif
GLclampf GLclampf GLclampf alpha
Definition: glext.h:3525
This file implements miscelaneous matrix and matrix/vector operations, and internal functions in mrpt...
void jacob_numeric_estimate(const VECTORLIKE &x, std::function< void(const VECTORLIKE &x, const USERPARAM &y, VECTORLIKE3 &out)> functor, const VECTORLIKE2 &increments, const USERPARAM &userParam, MATRIXLIKE &out_Jacobian)
Numerical estimation of the Jacobian of a user-supplied function - this template redirects to mrpt::m...
Definition: jacobians.h:144
#define MRPT_END
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.
Definition: data_utils.h:386
const GLubyte * c
Definition: glext.h:6313
void drawGaussianMultivariateMany(VECTOR_OF_VECTORS &ret, size_t desiredSamples, const COVMATRIX &cov, const typename VECTOR_OF_VECTORS::value_type *mean=nullptr)
Generate a given number of multidimensional random samples according to a given covariance matrix...
void transform_gaussian_linear(const VECTORLIKE1 &x_mean, const MATLIKE1 &x_cov, void(*functor)(const VECTORLIKE1 &x, const USERPARAM &fixed_param, VECTORLIKE3 &y), const USERPARAM &fixed_param, VECTORLIKE2 &y_mean, MATLIKE2 &y_cov, const VECTORLIKE1 &x_increments)
First order uncertainty propagation estimator of the Gaussian distribution of a variable Y=f(X) for a...
void transform_gaussian_unscented(const VECTORLIKE1 &x_mean, const MATLIKE1 &x_cov, void(*functor)(const VECTORLIKE1 &x, const USERPARAM &fixed_param, VECTORLIKE3 &y), const USERPARAM &fixed_param, VECTORLIKE2 &y_mean, MATLIKE2 &y_cov, const bool *elem_do_wrap2pi=nullptr, const double alpha=1e-3, const double K=0, const double beta=2.0)
Scaled unscented transformation (SUT) for estimating the Gaussian distribution of a variable Y=f(X) f...
#define MRPT_START
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...
Definition: data_utils.h:263
GLenum GLenum GLvoid * row
Definition: glext.h:3576
GLenum GLint GLint y
Definition: glext.h:3538
void transform_gaussian_montecarlo(const VECTORLIKE1 &x_mean, const MATLIKE1 &x_cov, void(*functor)(const VECTORLIKE1 &x, const USERPARAM &fixed_param, VECTORLIKE3 &y), const USERPARAM &fixed_param, VECTORLIKE2 &y_mean, MATLIKE2 &y_cov, const size_t num_samples=1000, typename mrpt::aligned_containers< VECTORLIKE3 >::vector_t *out_samples_y=nullptr)
Simple Montecarlo-base estimation of the Gaussian distribution of a variable Y=f(X) for an arbitrary ...
GLenum GLint x
Definition: glext.h:3538
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
std::vector< TYPE1, Eigen::aligned_allocator< TYPE1 > > vector_t



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019