CluE  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pointgauss.cpp
Go to the documentation of this file.
1 #include <math.h>
2 
3 #include "../point/pointgauss.h"
4 #include "../point/matrix.h"
5 
6 #include <algorithm>
7 
8 using namespace CluE;
9 
10 PointGauss::PointGauss(Point const& m, Matrix const& cov) : mean(m), covariance(cov)
11 {
12  size_t d = m.dimension();
13  this->cholesky = cov.cholesky();
15 
16  std::vector<double> temp(d);
17  for (size_t i=0; i<d; ++i)
18  temp[i] = log(this->cholesky(i,i));
19  sort(temp.begin(), temp.end());
20  this->logSqrt = accumulate(temp.begin(), temp.end(), d*log(2*M_PI)/2);
21 }
22 
23 double PointGauss::density(Point const& x) const
24 {
25  size_t d = x.dimension();
26 
27  // transform the given point
28  Point y = this->inverseCholesky*(x-this->mean);
29  std::vector<double> temp(d);
30  for (size_t i=0; i<d; ++i)
31  temp[i] = y[i]*y[i];
32  sort(temp.begin(), temp.end());
33  double qf = accumulate(temp.begin(), temp.end(), .0);
34 
35  return exp(-0.5*qf - logSqrt);
36 }
37 
38 double PointGauss::nll(Point const& x) const
39 {
40  size_t d = x.dimension();
41 
42  // transform the given point
43  Point y = this->inverseCholesky*(x-this->mean);
44  std::vector<double> temp(d);
45  for (size_t i=0; i<d; ++i)
46  temp[i] = y[i]*y[i];
47  sort(temp.begin(), temp.end());
48  double qf = accumulate(temp.begin(), temp.end(), .0);
49 
50  return 0.5*qf + logSqrt;
51 }
52 
53 double PointGauss::squaredMahalanobis(Point const& x) const
54 {
55  size_t d = x.dimension();
56 
57  // transform the given point
58  Point y = this->inverseCholesky*(x-this->mean);
59  double ret = 0;
60  for (size_t i=0; i<d; ++i)
61  ret += y[i]*y[i];
62  return ret;
63 }
virtual double density(Point const &x) const
Evaluates the density of the multivariate normal distribution at the given point x.
Definition: pointgauss.cpp:23
virtual double squaredMahalanobis(Point const &x) const
Definition: pointgauss.cpp:53
PointGauss(Point const &m, Matrix const &cov)
Definition: pointgauss.cpp:10
Matrix inverseCholesky
Definition: pointgauss.h:44
size_t dimension() const
Definition: point.h:87
static Matrix ltInverse(Matrix const &lt)
Definition: matrix.cpp:312
Weighted matrix of arbitrary dimension.
Definition: matrix.h:17
Matrix cholesky() const
Definition: matrix.cpp:49
Weighted point of arbitrary dimension.
Definition: point.h:17
virtual double nll(Point const &x) const
Computes the negative log-likelihood of the density at the given point x.
Definition: pointgauss.cpp:38
#define M_PI
Definition: unigauss.h:9