Gaussian.cpp
Go to the documentation of this file.
1 /************************************************************************
2  * Copyright (C) 2012 Eindhoven University of Technology (TU/e). *
3  * All rights reserved. *
4  ************************************************************************
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above *
10  * copyright notice, this list of conditions and the following *
11  * disclaimer. *
12  * *
13  * 2. Redistributions in binary form must reproduce the above *
14  * copyright notice, this list of conditions and the following *
15  * disclaimer in the documentation and/or other materials *
16  * provided with the distribution. *
17  * *
18  * THIS SOFTWARE IS PROVIDED BY TU/e "AS IS" AND ANY EXPRESS OR *
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
21  * ARE DISCLAIMED. IN NO EVENT SHALL TU/e OR CONTRIBUTORS BE LIABLE *
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
28  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
29  * DAMAGE. *
30  * *
31  * The views and conclusions contained in the software and *
32  * documentation are those of the authors and should not be *
33  * interpreted as representing official policies, either expressed or *
34  * implied, of TU/e. *
35  ************************************************************************/
36 
37 #include "problib/pdfs/Gaussian.h"
38 #include "problib/pdfs/Uniform.h"
39 
40 using namespace pbl;
41 
42 Gaussian::Gaussian(int dim) : PDF(dim, PDF::GAUSSIAN), ptr_(0) {
43 }
44 
45 Gaussian::Gaussian(const arma::vec& mu, const arma::mat& cov) : PDF(mu.n_elem, PDF::GAUSSIAN), ptr_(new GaussianStruct(mu, cov)) {
46 }
47 
48 Gaussian::Gaussian(const Gaussian& orig) : PDF(orig.ptr_->mu_.n_elem, PDF::GAUSSIAN), ptr_(orig.ptr_) {
49  if (ptr_) {
50  ++ptr_->n_ptrs_;
51  }
52 }
53 
55  if (ptr_) {
56  --ptr_->n_ptrs_;
57 
58  if (ptr_->n_ptrs_ == 0) {
59  delete ptr_;
60  }
61  }
62 }
63 
65  if (this != &other) {
66  if (ptr_) {
67  --ptr_->n_ptrs_;
68  if (ptr_->n_ptrs_ == 0) {
69  delete ptr_;
70  }
71  }
72  ptr_ = other.ptr_;
73  ++ptr_->n_ptrs_;
74 
75  dimensions_ = other.dimensions_;
76  }
77  return *this;
78 }
79 
81  return new Gaussian(*this);
82 }
83 
85  if (ptr_->n_ptrs_ > 1) {
86  --ptr_->n_ptrs_;
87  ptr_ = new GaussianStruct(*ptr_);
88  }
89 }
90 
91 double Gaussian::getLikelihood(const PDF& pdf) const {
93 
94  if (pdf.type() == PDF::GAUSSIAN) {
95  const Gaussian* G = static_cast<const pbl::Gaussian*>(&pdf);
96  return getDensity(*G);
97  } else if (pdf.type() == PDF::UNIFORM) {
98  const Uniform* U = static_cast<const pbl::Uniform*>(&pdf);
99  return U->getLikelihood(*this);
100  }
101 
102  assert_msg(false, "Gaussian: Likelihood can only be calculated with another Gaussian or Uniform pdf.");
103  return 0;
104 }
105 
106 double Gaussian::getDensity(const arma::vec& v, double max_mah_dist) const {
108  return getDensity(v, ptr_->mu_, ptr_->cov_, max_mah_dist);
109 }
110 
111 double Gaussian::getDensity(const Gaussian& G, double max_mah_dist) const {
113  arma::mat S = G.getCovariance() + ptr_->cov_;
114  return getDensity(ptr_->mu_, G.getMean(), S);
115 }
116 
117 double Gaussian::getMaxDensity() const {
119  return getDensity(ptr_->mu_, ptr_->mu_, ptr_->cov_);
120 }
121 
124  v = ptr_->mu_;
125  return true;
126 }
127 
128 double Gaussian::getDensity(const arma::vec& v1, const arma::vec& v2, const arma::mat& S, double max_mah_dist) const {
129  // check dimensions
130  assert(v1.n_elem == v2.n_elem && v1.n_elem == S.n_rows);
131 
132  double det = arma::det(S);
133 
134  // covariance should have non-zero determinant
135  assert(det != 0);
136 
137  // calculate difference between v1 and v2
138  arma::vec diff = v2 - v1;
139 
140  // calculate squared mahalanobis distance
141  double mahalanobis_dist_sq = arma::dot(arma::inv(S) * diff, diff);
142 
143  // mahalanobis distance should always be 0 or positive
144  assert(mahalanobis_dist_sq >= 0);
145 
146  // threshold to 0 if maximum mahalanobis distance is exceeded
147  if (max_mah_dist > 0 && mahalanobis_dist_sq > (max_mah_dist * max_mah_dist)) {
148  return 0;
149  }
150 
151  double pi2_pow = 1;
152  for(unsigned int d = 0; d < S.n_rows; ++d) {
153  pi2_pow *= 2 * M_PI;
154  }
155 
156  // calculate density
157  double pos_sqrt_pow = 1 / sqrt(pi2_pow * det);
158  return exp(-0.5 * mahalanobis_dist_sq) * pos_sqrt_pow;
159 }
160 
161 void Gaussian::setMean(const arma::vec& mu) {
162  if (ptr_) {
163  cloneStruct();
164  } else {
165  ptr_ = new GaussianStruct(mu, arma::zeros(mu.n_elem, mu.n_elem));
166  }
167 
168  ptr_->mu_ = mu;
169 }
170 
172  if (ptr_) {
173  cloneStruct();
174  } else {
175  ptr_ = new GaussianStruct(arma::zeros(cov.n_cols), cov);
176  }
177 
178  ptr_->cov_ = cov;
179 }
180 
181 const arma::vec& Gaussian::getMean() const {
183  return ptr_->mu_;
184 }
185 
188  return ptr_->cov_;
189 }
190 
191 std::string Gaussian::toString(const std::string& indent) const {
192  if (!ptr_) {
193  return "N(-)";
194  }
195 
196  std::stringstream s;
197 
198  s << "N( [";
199  for(int i = 0; i < dimensions_; ++i) {
200  s << " " << ptr_->mu_(i);
201  }
202 
203  s << "], [";
204  for(int i = 0; i < dimensions_; ++i) {
205  for(int j = 0; j < dimensions_; ++j) {
206  s << " " << ptr_->cov_(i, j);
207  }
208  s << ";";
209  }
210  s << "] )";
211 
212  return s.str();
213 }
bool getExpectedValue(arma::vec &v) const
Returns the expected value E[x] of the Gaussian, which corresponds to its mean.
Definition: Gaussian.cpp:122
d
arma_inline arma_warn_unused T1::elem_type dot(const Base< typename T1::elem_type, T1 > &A, const Base< typename T1::elem_type, T2 > &B)
arma_inline const eOp< T1, eop_exp > exp(const Base< typename T1::elem_type, T1 > &A)
PDFType type() const
Definition: PDF.cpp:56
arma_hot const Col< eT > & zeros()
arma_inline const eOp< T1, eop_sqrt > sqrt(const Base< typename T1::elem_type, T1 > &A)
int dimensions_
Definition: PDF.h:87
virtual ~Gaussian()
Destructor.
Definition: Gaussian.cpp:54
#define assert_msg(_Expression, _Msg)
Definition: globals.h:53
XmlRpcServer s
This class represents a multi-variate Gaussian (Normal) distribution.
Definition: Gaussian.h:51
Gaussian & operator=(const Gaussian &other)
Assignment operator. The operation is cheap since it only copies a pointer. A deep clone will only be...
Definition: Gaussian.cpp:64
Mat< double > mat
double getMaxDensity() const
Calculates the maximum density of the Gaussian, i.e., the density at the mean.
Definition: Gaussian.cpp:117
const arma::mat & getCovariance() const
Returns the covariance matrix of the Gaussian.
Definition: Gaussian.cpp:186
void setMean(const arma::vec &mu)
Sets the mean of the Gaussian.
Definition: Gaussian.cpp:161
const arma::vec & getMean() const
Returns the mean of the Gaussian.
Definition: Gaussian.cpp:181
static bool inv(Mat< eT > &out, const Base< eT, T1 > &X, const bool slow=false)
GaussianStruct * ptr_
Definition: Gaussian.h:167
#define CHECK_INITIALIZED
Definition: Gaussian.h:173
double getLikelihood(const PDF &pdf) const
Definition: Gaussian.cpp:91
Gaussian(int dim)
Constructs a (multi-variate) Gaussian with specific dimensionality but leaves mean and covariance uns...
Definition: Gaussian.cpp:42
void cloneStruct()
Definition: Gaussian.cpp:84
const Op< T1, op_cov > cov(const Base< typename T1::elem_type, T1 > &X, const uword norm_type=0)
double getDensity(const arma::vec &v, double max_mah_dist=0) const
Calculates the density of the Gaussian at point v.
Definition: Gaussian.cpp:106
This class represents a hyper-cube shaped uniform distribution.
Definition: Uniform.h:51
Definition: PDF.h:47
Col< double > vec
double getLikelihood(const PDF &pdf) const
Definition: Uniform.cpp:74
static eT det(const Base< eT, T1 > &X, const bool slow=false)
Gaussian * clone() const
Creates a clone of the object. The clone method is cheap since it only copies a pointer. A deep clone will only be created if the original object is modified.
Definition: Gaussian.cpp:80
void setCovariance(const arma::mat &cov)
Sets the covariance of the Gaussian.
Definition: Gaussian.cpp:171
std::string toString(const std::string &indent="") const
Represents the Gaussian as a string for easier console output.
Definition: Gaussian.cpp:191


problib
Author(s): Sjoerd van den Dries, Jos Elfring
autogenerated on Fri Apr 16 2021 02:32:19