From c9ad00a2a7fdebceceeb759743f2cce5108692e5 Mon Sep 17 00:00:00 2001 From: Joris Mooij Date: Fri, 27 Nov 2009 22:05:58 +0100 Subject: [PATCH] [Frederik Eaton] Added Hellinger distance to TProb<>::DistType --- include/dai/prob.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/include/dai/prob.h b/include/dai/prob.h index dd63308..851828d 100644 --- a/include/dai/prob.h +++ b/include/dai/prob.h @@ -135,6 +135,16 @@ template struct fo_KL : public std::binary_function { }; +/// Function object useful for calculating the Hellinger distance +template struct fo_Hellinger : public std::binary_function { + /// Returns (sqrt(\a p) - sqrt(\a q))^2 + T operator()( const T &p, const T &q ) const { + T x = sqrt(p) - sqrt(q); + return x * x; + } +}; + + /// Function object that returns x to the power y template struct fo_pow : public std::binary_function { /// Returns (\a x ^ \a y) @@ -201,8 +211,9 @@ template class TProb { * - DISTLINF is the \f$\ell_\infty\f$ distance (maximum absolute value of pointwise difference); * - DISTTV is the total variation distance (half of the \f$\ell_1\f$ distance); * - DISTKL is the Kullback-Leibler distance (\f$\sum_i p_i (\log p_i - \log q_i)\f$). + * - DISTHEL is the Hellinger distance (\f$\frac{1}{2}\sum_i (\sqrt{p_i}-\sqrt{q_i})^2\f$). */ - typedef enum { DISTL1, DISTLINF, DISTTV, DISTKL } DistType; + typedef enum { DISTL1, DISTLINF, DISTTV, DISTKL, DISTHEL } DistType; /// \name Constructors and destructors //@{ @@ -661,6 +672,8 @@ template T dist( const TProb &p, const TProb &q, typename TPro return p.innerProduct( q, (T)0, std::plus(), fo_absdiff() ) / 2; case TProb::DISTKL: return p.innerProduct( q, (T)0, std::plus(), fo_KL() ); + case TProb::DISTHEL: + return p.innerProduct( q, (T)0, std::plus(), fo_Hellinger() ) / 2; default: DAI_THROW(UNKNOWN_ENUM_VALUE); return INFINITY; -- 2.20.1