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
27#include <Eigen/LU>
28
30
31#include "GMMDistance.h"
33
34namespace memoryx
35{
36
37 /**
38 * @class MahalanobisDistance
39 * @ingroup CommonPlacesLearner
40 */
42 {
43 public:
44 /**
45 * Creates a new GMMDistance
46 */
50
51 /**
52 *
53 *
54 */
55 float
56 getDistance(const GaussianMixtureDistributionBasePtr& gmm1,
57 const GaussianMixtureDistributionBasePtr& gmm2) override
58 {
59 return 0; // NYI
60 }
61
62 float
64 {
65 const Eigen::VectorXf d = g2->toEigenMean() - g1->toEigenMean();
66 const Eigen::MatrixXf s = 2 * (g1->toEigenCovariance() + g2->toEigenCovariance());
67 const Eigen::MatrixXf distm = d.transpose() * s.inverse() * d;
68
69 return sqrtf(distm(0, 0));
70 }
71
72 float
73 getWeigtedDistance(const GaussianMixtureDistributionBasePtr& gmm,
74 const NormalDistributionBasePtr& g)
75 {
76 float result = 0;
77
78 NormalDistributionPtr g1 = NormalDistributionPtr::dynamicCast(g);
79
80 for (int i = 0; i < gmm->size(); ++i)
81 {
82 const GaussianMixtureComponent comp = gmm->getComponent(i);
83 NormalDistributionPtr g2 = NormalDistributionPtr::dynamicCast(comp.gaussian);
84
85 result += getDistance(g1, g2) * comp.weight;
86 }
87
88 return result;
89 }
90
91 float
92 getDistance(const GaussianMixtureComponent& comp1,
93 const GaussianMixtureComponent& comp2) override
94 {
95 NormalDistributionPtr gaussian1 = NormalDistributionPtr::dynamicCast(comp1.gaussian);
96 NormalDistributionPtr gaussian2 = NormalDistributionPtr::dynamicCast(comp2.gaussian);
97
98 Eigen::VectorXf d = gaussian2->toEigenMean() - gaussian1->toEigenMean();
99 Eigen::MatrixXf s = 2 *
100 (comp1.weight * gaussian1->toEigenCovariance() +
101 comp2.weight * gaussian2->toEigenCovariance()) /
102 (comp1.weight + comp2.weight);
103 Eigen::MatrixXf distm = d.transpose() * s.inverse() * d;
104
105 // TODO account for weights
106 // float w = (oldComp.weight + newComp.weight) / 2.;
107
108 return sqrtf(distm(0, 0));
109 }
110 };
111} // namespace memoryx
#define ARMARXCORE_IMPORT_EXPORT
GMMDistance()
Creates a new GMMDistance.
Definition GMMDistance.h:45
MahalanobisDistance()
Creates a new GMMDistance.
float getDistance(const GaussianMixtureDistributionBasePtr &gmm1, const GaussianMixtureDistributionBasePtr &gmm2) override
float getWeigtedDistance(const GaussianMixtureDistributionBasePtr &gmm, const NormalDistributionBasePtr &g)
float getDistance(const NormalDistributionPtr &g1, const NormalDistributionPtr &g2)
float getDistance(const GaussianMixtureComponent &comp1, const GaussianMixtureComponent &comp2) override
VirtualRobot headers.
IceInternal::Handle< NormalDistribution > NormalDistributionPtr