XMM - Probabilistic Models for Motion Recognition and Mapping

xmmGaussianDistribution.hpp
Go to the documentation of this file.
1 /*
2  * xmmGaussianDistribution.hpp
3  *
4  * Multivariate Gaussian Distribution
5  *
6  * Contact:
7  * - Jules Francoise <jules.francoise@ircam.fr>
8  *
9  * This code has been initially authored by Jules Francoise
10  * <http://julesfrancoise.com> during his PhD thesis, supervised by Frederic
11  * Bevilacqua <href="http://frederic-bevilacqua.net>, in the Sound Music
12  * Movement Interaction team <http://ismm.ircam.fr> of the
13  * STMS Lab - IRCAM, CNRS, UPMC (2011-2015).
14  *
15  * Copyright (C) 2015 UPMC, Ircam-Centre Pompidou.
16  *
17  * This File is part of XMM.
18  *
19  * XMM is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * XMM is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with XMM. If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 #ifndef xmmGaussianDistribution_h
34 #define xmmGaussianDistribution_h
35 
36 #include "../common/xmmAttribute.hpp"
37 #include "../common/xmmJson.hpp"
38 
39 namespace xmm {
48 struct Ellipse {
52  float x;
53 
57  float y;
58 
62  float width;
63 
67  float height;
68 
72  float angle;
73 };
74 
81  public:
85  enum class CovarianceMode {
89  Full = 0,
90 
94  Diagonal = 1
95  };
96 
104  GaussianDistribution(bool bimodal = false, unsigned int dimension = 1,
105  unsigned int dimension_input = 0,
106  CovarianceMode covariance_mode = CovarianceMode::Full);
107 
113 
118  explicit GaussianDistribution(Json::Value const& root);
119 
124  GaussianDistribution& operator=(GaussianDistribution const& src);
125 
127 
135  double likelihood(const float* observation) const;
136 
145  double likelihood_input(const float* observation_input) const;
146 
155  double likelihood_bimodal(const float* observation_input,
156  const float* observation_output) const;
157 
166  void regression(std::vector<float> const& observation_input,
167  std::vector<float>& predicted_output) const;
168 
170 
172 
178  void regularize(std::vector<double> regularization);
179 
184  void updateInverseCovariance();
185 
194  Ellipse toEllipse(unsigned int dimension1, unsigned int dimension2);
195 
205  void fromEllipse(Ellipse const& gaussian_ellipse, unsigned int dimension1,
206  unsigned int dimension2);
207 
209 
211 
217  Json::Value toJson() const;
218 
224  void fromJson(Json::Value const& root);
225 
227 
228  // /** @name Conversion & Extraction */
229  // ///@{
230  //
231  // /**
232  // @brief Convert to bimodal distribution in place
233  // @param dimension_input dimension of the input modality
234  // @throws runtime_error if the model is already bimodal
235  // @throws out_of_range if the requested input dimension is too
236  // large
237  // */
238  // void makeBimodal(unsigned int dimension_input);
239  //
240  // /**
241  // @brief Convert to unimodal distribution in place
242  // @throws runtime_error if the model is already unimodal
243  // */
244  // void makeUnimodal();
245  //
246  // /**
247  // @brief extract a sub-distribution with the given columns
248  // @param columns columns indices in the target order
249  // @throws runtime_error if the model is training
250  // @throws out_of_range if the number or indices of the requested
251  // columns exceeds the current dimension
252  // @return a Gaussian Distribution from the current model
253  // considering only the target columns
254  // */
255  // GaussianDistribution extractSubmodel(std::vector<unsigned int>&
256  // columns) const;
257  //
258  // /**
259  // @brief extract the sub-distribution of the input modality
260  // @throws runtime_error if the model is training or if it is not
261  // bimodal
262  // @return a unimodal Gaussian Distribution of the input modality
263  // from the current bimodal model
264  // */
265  // GaussianDistribution extractSubmodel_input() const;
266  //
267  // /**
268  // @brief extract the sub-distribution of the output modality
269  // @throws runtime_error if the model is training or if it is not
270  // bimodal
271  // @return a unimodal Gaussian Distribution of the output modality
272  // from the current bimodal model
273  // */
274  // GaussianDistribution extractSubmodel_output() const;
275  //
276  // /**
277  // @brief extract the model with reversed input and output
278  // modalities
279  // @throws runtime_error if the model is training or if it is not
280  // bimodal
281  // @return a bimodal Gaussian Distribution that swaps the input and
282  // output modalities
283  // */
284  // GaussianDistribution extract_inverse_model() const;
285  //
286  // ///@}
287 
292 
297 
301  std::vector<double> mean;
302 
307 
311  std::vector<double> covariance;
312 
317  std::vector<double> output_covariance;
318 
319  protected:
323  void allocate();
324 
328  virtual void onAttributeChange(AttributeBase* attr_pointer);
329 
335  void updateOutputCovariance();
336 
340  bool bimodal_;
341 
346 
350  std::vector<double> inverse_covariance_;
351 
356 
360  std::vector<double> inverse_covariance_input_;
361 };
362 
363 Ellipse covariance2ellipse(double c_xx, double c_xy, double c_yy);
364 
365 template <>
366 void checkLimits<GaussianDistribution::CovarianceMode>(
368  GaussianDistribution::CovarianceMode const& limit_min,
369  GaussianDistribution::CovarianceMode const& limit_max);
370 
371 template <>
374 }
375 
376 #endif
float x
x center position
Definition: xmmGaussianDistribution.hpp:52
float height
height: major axis length
Definition: xmmGaussianDistribution.hpp:67
std::vector< double > covariance
Covariance Matrix of the Gaussian Distribution.
Definition: xmmGaussianDistribution.hpp:311
Ellipse covariance2ellipse(double c_xx, double c_xy, double c_yy)
Definition: xmmGaussianDistribution.cpp:680
float angle
angle (radians)
Definition: xmmGaussianDistribution.hpp:72
float y
y center position
Definition: xmmGaussianDistribution.hpp:57
double covariance_determinant_
Determinant of the covariance matrix.
Definition: xmmGaussianDistribution.hpp:345
float width
width: minor axis length
Definition: xmmGaussianDistribution.hpp:62
Base Class for Generic Attributes.
Definition: xmmAttribute.hpp:105
std::vector< double > output_covariance
Conditional Output Variance (updated when covariances matrices are inverted)
Definition: xmmGaussianDistribution.hpp:317
Attribute< unsigned int > dimension_input
Input Dimension of the multivariate normal.
Definition: xmmGaussianDistribution.hpp:296
CovarianceMode
Covariance Mode.
Definition: xmmGaussianDistribution.hpp:85
Attribute< CovarianceMode > covariance_mode
Covariance Mode.
Definition: xmmGaussianDistribution.hpp:306
bool bimodal_
Defines if regression parameters need to be computed.
Definition: xmmGaussianDistribution.hpp:340
Abstract class for handling JSON + File I/O.
Definition: xmmJson.hpp:50
std::vector< double > inverse_covariance_input_
Inverse covariance matrix of the input modality.
Definition: xmmGaussianDistribution.hpp:360
std::vector< double > mean
Mean of the Gaussian Distribution.
Definition: xmmGaussianDistribution.hpp:301
Multivariate Gaussian Distribution.
Definition: xmmGaussianDistribution.hpp:80
Structure for storing Ellipse parameters.
Definition: xmmGaussianDistribution.hpp:48
std::vector< double > inverse_covariance_
Inverse covariance matrix.
Definition: xmmGaussianDistribution.hpp:350
Definition: xmmAttribute.hpp:42
double covariance_determinant_input_
Determinant of the covariance matrix of the input modality.
Definition: xmmGaussianDistribution.hpp:355
static T defaultLimitMax()
Attribute default maximum value.
Definition: xmmAttribute.hpp:241
Attribute< unsigned int > dimension
Convert to bimodal distribution in place.
Definition: xmmGaussianDistribution.hpp:291