CluE  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pointgmm.cpp
Go to the documentation of this file.
1 #include <math.h>
2 
3 #include "../exception/invalidargumentexception.h"
4 #include "../point/pointgmm.h"
5 #include "../point/point.h"
6 
7 using namespace CluE;
8 
9 PointGMM::PointGMM(std::vector<double> const& w, std::vector<PointGauss> const& g)
10  : weights(w), gaussians(g)
11 {
12  if (w.size() != g.size())
13  throw InvalidArgumentException(0, "Incompatible sizes!", "weights, gaussians");
14 }
15 
16 /*PointGMM::PointGMM(GMMProvider const& p)
17 {
18  this->weights = p.weights();
19  size_t k = this->weights.size();
20  for (size_t i=0; i<k; ++i)
21  this->gaussians.push_back(PointGauss(p.mean(i), p.covariance(i)));
22 }
23 */
24 double PointGMM::density(Point const& x) const
25 {
26  size_t k = this->weights.size();
27  double sum = 0;
28  for (size_t i=0; i<k; ++i)
29  sum += this->weights[i]*this->gaussians[i].density(x);
30 
31  return sum;
32 }
33 
34 double PointGMM::nll(Point const& x) const
35 {
36  return -log(this->density(x));
37 }
38 
39 double PointGMM::minNLL(Point const& x) const
40 {
41  size_t k = this->weights.size();
42  double ret;
43  if (k>0)
44  {
45  ret = this->gaussians[0].nll(x);
46  for (size_t i=1; i<k; ++i)
47  {
48  double c = this->gaussians[i].nll(x);
49  if (ret>c)
50  ret = c;
51  }
52  }
53  else
54  ret = 0;
55 
56  return ret;
57 }
58 
59 double PointGMM::minSquaredMahalanobis(Point const& x) const
60 {
61  size_t k = this->weights.size();
62  double ret;
63  if (k>0)
64  {
65  ret = this->gaussians[0].squaredMahalanobis(x);
66  for (size_t i=1; i<k; ++i)
67  {
68  double c = this->gaussians[i].squaredMahalanobis(x);
69  if (ret>c)
70  ret = c;
71  }
72  }
73  else
74  ret = 0;
75 
76  return ret;
77 }
virtual double density(Point const &x) const
Evaluates the density of the GMM distribution at the given point x.
Definition: pointgmm.cpp:24
virtual double nll(Point const &x) const
Computes the negative log-likelihood of the density at the given point x.
Definition: pointgmm.cpp:34
PointGMM(std::vector< double > const &, std::vector< PointGauss > const &)
Definition: pointgmm.cpp:9
std::vector< double > weights
Definition: pointgmm.h:46
virtual double minNLL(Point const &x) const
Definition: pointgmm.cpp:39
Weighted point of arbitrary dimension.
Definition: point.h:17
Indicates invalid values of arguments.
std::vector< PointGauss > gaussians
Definition: pointgmm.h:47
virtual double minSquaredMahalanobis(Point const &x) const
Definition: pointgmm.cpp:59