MahalanobisDistance.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package MemoryX::GaussianMixtureHelpers
17 * @author Alexey Kozlov <kozlov@kit.edu>
18 * @copyright 2013 Alexey Kozlov
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #pragma once
24 
25 #include <string>
26 #include <Eigen/LU>
27 
29 
31 #include "GMMDistance.h"
32 
33 namespace memoryx
34 {
35 
36  /**
37  * @class MahalanobisDistance
38  * @ingroup CommonPlacesLearner
39  */
41  {
42  public:
43  /**
44  * Creates a new GMMDistance
45  */
47  {
48  }
49 
50  /**
51  *
52  *
53  */
54  float getDistance(const GaussianMixtureDistributionBasePtr& gmm1, const GaussianMixtureDistributionBasePtr& gmm2) override
55  {
56  return 0; // NYI
57  }
58 
60  {
61  const Eigen::VectorXf d = g2->toEigenMean() - g1->toEigenMean();
62  const Eigen::MatrixXf s = 2 * (g1->toEigenCovariance() + g2->toEigenCovariance());
63  const Eigen::MatrixXf distm = d.transpose() * s.inverse() * d;
64 
65  return sqrtf(distm(0, 0));
66  }
67 
68  float getWeigtedDistance(const GaussianMixtureDistributionBasePtr& gmm, const NormalDistributionBasePtr& g)
69  {
70  float result = 0;
71 
72  NormalDistributionPtr g1 = NormalDistributionPtr::dynamicCast(g);
73 
74  for (int i = 0; i < gmm->size(); ++i)
75  {
76  const GaussianMixtureComponent comp = gmm->getComponent(i);
77  NormalDistributionPtr g2 = NormalDistributionPtr::dynamicCast(comp.gaussian);
78 
79  result += getDistance(g1, g2) * comp.weight;
80  }
81 
82  return result;
83  }
84 
85  float getDistance(const GaussianMixtureComponent& comp1, const GaussianMixtureComponent& comp2) override
86  {
87  NormalDistributionPtr gaussian1 = NormalDistributionPtr::dynamicCast(comp1.gaussian);
88  NormalDistributionPtr gaussian2 = NormalDistributionPtr::dynamicCast(comp2.gaussian);
89 
90  Eigen::VectorXf d = gaussian2->toEigenMean() - gaussian1->toEigenMean();
91  Eigen::MatrixXf s = 2 * (comp1.weight * gaussian1->toEigenCovariance() + comp2.weight * gaussian2->toEigenCovariance()) / (comp1.weight + comp2.weight);
92  Eigen::MatrixXf distm = d.transpose() * s.inverse() * d;
93 
94  // TODO account for weights
95  // float w = (oldComp.weight + newComp.weight) / 2.;
96 
97  return sqrtf(distm(0, 0));
98  }
99 
100  };
101 }
102 
memoryx::MahalanobisDistance::getDistance
float getDistance(const GaussianMixtureComponent &comp1, const GaussianMixtureComponent &comp2) override
Definition: MahalanobisDistance.h:85
memoryx::MahalanobisDistance
Definition: MahalanobisDistance.h:40
memoryx::GMMDistance
Definition: GMMDistance.h:39
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
GMMDistance.h
IceInternal::Handle
Definition: forward_declarations.h:8
memoryx::MahalanobisDistance::getDistance
float getDistance(const GaussianMixtureDistributionBasePtr &gmm1, const GaussianMixtureDistributionBasePtr &gmm2) override
Definition: MahalanobisDistance.h:54
memoryx::MahalanobisDistance::MahalanobisDistance
MahalanobisDistance()
Creates a new GMMDistance.
Definition: MahalanobisDistance.h:46
ProbabilityMeasures.h
memoryx::MahalanobisDistance::getWeigtedDistance
float getWeigtedDistance(const GaussianMixtureDistributionBasePtr &gmm, const NormalDistributionBasePtr &g)
Definition: MahalanobisDistance.h:68
ImportExport.h
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
memoryx::MahalanobisDistance::getDistance
float getDistance(const NormalDistributionPtr &g1, const NormalDistributionPtr &g2)
Definition: MahalanobisDistance.h:59