aGrUM  0.13.2
gum::BruteForceKL< GUM_SCALAR > Class Template Reference

BruteForceKL computes exactly the KL divergence betweens 2 BNs. More...

#include <bruteForceKL.h>

+ Inheritance diagram for gum::BruteForceKL< GUM_SCALAR >:
+ Collaboration diagram for gum::BruteForceKL< GUM_SCALAR >:

Public Member Functions

 BruteForceKL (const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
 constructor must give 2 BNs More...
 
 BruteForceKL (const KL< GUM_SCALAR > &kl)
 copy constructor More...
 
virtual ~BruteForceKL ()
 destructor More...
 
Complexity difficulty () const
 return KL::Complexity::Heavy,KL::Complexity::Difficult,KL::Complexity::Correct depending on the BNs __p and __q More...
 
Accessors to results. The first call do the computations. The

others do not.

double klPQ ()
 
Size errorPQ ()
 
double klQP ()
 
Size errorQP ()
 
double hellinger ()
 
double bhattacharya ()
 
const IBayesNet< GUM_SCALAR > & p () const
 
const IBayesNet< GUM_SCALAR > & q () const
 

Protected Attributes

const IBayesNet< GUM_SCALAR > & _p
 
const IBayesNet< GUM_SCALAR > & _q
 
GUM_SCALAR _klPQ
 
GUM_SCALAR _klQP
 
GUM_SCALAR _hellinger
 
GUM_SCALAR _bhattacharya
 
Size _errorPQ
 
Size _errorQP
 

Protected Member Functions

void _computeKL () final
 
void _process ()
 

Detailed Description

template<typename GUM_SCALAR>
class gum::BruteForceKL< GUM_SCALAR >

BruteForceKL computes exactly the KL divergence betweens 2 BNs.

BruteForceKL should be used only if difficulty() gives an estimation ( KL_CORRECT ) of the needed time. KL.process() computes KL(P||Q) using klPQ() and KL(Q||P) using klQP(). The computations are made once. The second is for free :) BruteForce allows as well to compute in the same time the Hellinger distance ( \( *\sqrt{\sum_i (\sqrt{p_i}-\sqrt{q_i})^2}\)) (Kokolakis and Nanopoulos, 2001).

It may happen that P*ln(P/Q) is not computable (Q=0 and P!=0). In such a case, KL keeps working but trace this error (errorPQ() and errorQP())? *

Warning
This BruteForceKL should be use only if difficulty()==complexity::CORRECT or at most complexity::DIFFICULT ... snippets :
gum::KL base_kl(net1,net2);
if (base_kl.difficulty()!=KL::HEAVY) {
gum::BruteForceKL kl(base_kl);
std::cout<<"KL net1||net2 :"<<kl.klPQ()<<std::endl;
} else {
gum::GibbsKL kl(base_kl);
std::cout<<"KL net1||net2 :"<<kl.klPQ()<<std::endl;
}

Definition at line 66 of file bruteForceKL.h.

Constructor & Destructor Documentation

template<typename GUM_SCALAR >
gum::BruteForceKL< GUM_SCALAR >::BruteForceKL ( const IBayesNet< GUM_SCALAR > &  P,
const IBayesNet< GUM_SCALAR > &  Q 
)

constructor must give 2 BNs

Exceptions
gum::OperationNotAllowedif the 2 BNs have not the same domainSize or compatible node sets.

Definition at line 35 of file bruteForceKL_tpl.h.

36  :
37  KL< GUM_SCALAR >(P, Q) {
38  GUM_CONSTRUCTOR(BruteForceKL);
39  }
BruteForceKL(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
template<typename GUM_SCALAR >
gum::BruteForceKL< GUM_SCALAR >::BruteForceKL ( const KL< GUM_SCALAR > &  kl)
explicit

copy constructor

Definition at line 42 of file bruteForceKL_tpl.h.

42  :
43  KL< GUM_SCALAR >(kl) {
44  GUM_CONSTRUCTOR(BruteForceKL);
45  }
BruteForceKL(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
template<typename GUM_SCALAR >
gum::BruteForceKL< GUM_SCALAR >::~BruteForceKL ( )
virtual

destructor

Definition at line 48 of file bruteForceKL_tpl.h.

48  {
49  GUM_DESTRUCTOR(BruteForceKL);
50  }
BruteForceKL(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs

Member Function Documentation

template<typename GUM_SCALAR >
void gum::BruteForceKL< GUM_SCALAR >::_computeKL ( )
finalprotectedvirtual

Reimplemented from gum::KL< GUM_SCALAR >.

Definition at line 53 of file bruteForceKL_tpl.h.

References gum::KL< GUM_SCALAR >::_bhattacharya, gum::KL< GUM_SCALAR >::_errorPQ, gum::KL< GUM_SCALAR >::_errorQP, gum::KL< GUM_SCALAR >::_hellinger, gum::KL< GUM_SCALAR >::_klPQ, gum::KL< GUM_SCALAR >::_klQP, gum::KL< GUM_SCALAR >::_p, gum::KL< GUM_SCALAR >::_q, and gum::HashTable< Key, Val, Alloc >::insert().

53  {
54  _klPQ = _klQP = _hellinger = _bhattacharya = (GUM_SCALAR)0.0;
55  _errorPQ = _errorQP = 0;
56 
57  auto Ip = _p.completeInstantiation();
58  auto Iq = _q.completeInstantiation();
59 
60  // map between _p variables and _q variables (using name of vars)
61  HashTable< const DiscreteVariable*, const DiscreteVariable* > map;
62 
63  for (Idx ite = 0; ite < Ip.nbrDim(); ++ite) {
64  map.insert(&Ip.variable(ite), &_q.variableFromName(Ip.variable(ite).name()));
65  }
66 
67  for (Ip.setFirst(); !Ip.end(); ++Ip) {
68  Iq.setValsFrom(map, Ip);
69  GUM_SCALAR pp = _p.jointProbability(Ip);
70  GUM_SCALAR pq = _q.jointProbability(Iq);
71 
72  _hellinger += std::pow(std::sqrt(pp) - std::sqrt(pq), 2);
73  _bhattacharya += std::sqrt(pp * pq);
74 
75  if (pp != (GUM_SCALAR)0.0) {
76  if (pq != (GUM_SCALAR)0.0) {
77  _klPQ -= pp * log2(pq / pp);
78  } else {
79  _errorPQ++;
80  }
81  }
82 
83  if (pq != (GUM_SCALAR)0.0) {
84  if (pp != (GUM_SCALAR)0.0) {
85  _klQP -= pq * log2(pp / pq);
86  } else {
87  _errorQP++;
88  }
89  }
90  }
91 
92  _hellinger = std::sqrt(_hellinger);
93  _bhattacharya = -std::log(_bhattacharya);
94  }
GUM_SCALAR _klPQ
Definition: KL.h:132
const IBayesNet< GUM_SCALAR > & _p
Definition: KL.h:129
GUM_SCALAR _bhattacharya
Definition: KL.h:135
const IBayesNet< GUM_SCALAR > & _q
Definition: KL.h:130
Size _errorPQ
Definition: KL.h:137
unsigned long Idx
Type for indexes.
Definition: types.h:43
GUM_SCALAR _klQP
Definition: KL.h:133
Size _errorQP
Definition: KL.h:138
GUM_SCALAR _hellinger
Definition: KL.h:134

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
void gum::KL< GUM_SCALAR >::_process ( )
protectedinherited

Definition at line 168 of file KL_tpl.h.

References gum::KL< GUM_SCALAR >::__done, and gum::KL< GUM_SCALAR >::_computeKL().

Referenced by gum::KL< GUM_SCALAR >::bhattacharya(), gum::KL< GUM_SCALAR >::errorPQ(), gum::KL< GUM_SCALAR >::errorQP(), gum::KL< GUM_SCALAR >::hellinger(), gum::KL< GUM_SCALAR >::klPQ(), and gum::KL< GUM_SCALAR >::klQP().

168  {
169  if (!__done) {
170  _computeKL();
171  __done = true;
172  }
173  }
bool __done
Definition: KL.h:143
virtual void _computeKL()
Definition: KL_tpl.h:177

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
INLINE double gum::KL< GUM_SCALAR >::bhattacharya ( )
inherited
Returns
Bhattacharya distance (
See also
http://en.wikipedia.org/wiki/Bhattacharya_distance)

Definition at line 90 of file KL_tpl.h.

References gum::KL< GUM_SCALAR >::_bhattacharya, and gum::KL< GUM_SCALAR >::_process().

90  {
91  _process();
92  return _bhattacharya;
93  }
void _process()
Definition: KL_tpl.h:168
GUM_SCALAR _bhattacharya
Definition: KL.h:135

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
Complexity gum::KL< GUM_SCALAR >::difficulty ( ) const
inherited

return KL::Complexity::Heavy,KL::Complexity::Difficult,KL::Complexity::Correct depending on the BNs __p and __q

Definition at line 67 of file KL_tpl.h.

References gum::KL< GUM_SCALAR >::__difficulty.

67  {
68  return __difficulty;
69  }
Complexity __difficulty
Definition: KL.h:142
template<typename GUM_SCALAR >
INLINE Size gum::KL< GUM_SCALAR >::errorPQ ( )
inherited
Returns
the number of errors while processing divergence KL(P||Q)

Definition at line 96 of file KL_tpl.h.

References gum::KL< GUM_SCALAR >::_errorPQ, and gum::KL< GUM_SCALAR >::_process().

96  {
97  _process();
98  return _errorPQ;
99  }
void _process()
Definition: KL_tpl.h:168
Size _errorPQ
Definition: KL.h:137

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
INLINE Size gum::KL< GUM_SCALAR >::errorQP ( )
inherited
Returns
the number of errors while processing divergence KL(Q||P)

Definition at line 102 of file KL_tpl.h.

References gum::KL< GUM_SCALAR >::_errorQP, and gum::KL< GUM_SCALAR >::_process().

102  {
103  _process();
104  return _errorQP;
105  }
void _process()
Definition: KL_tpl.h:168
Size _errorQP
Definition: KL.h:138

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
INLINE double gum::KL< GUM_SCALAR >::hellinger ( )
inherited
Returns
hellinger distance (
See also
http://en.wikipedia.org/wiki/Hellinger_distance)

Definition at line 84 of file KL_tpl.h.

References gum::KL< GUM_SCALAR >::_hellinger, and gum::KL< GUM_SCALAR >::_process().

84  {
85  _process();
86  return _hellinger;
87  }
void _process()
Definition: KL_tpl.h:168
GUM_SCALAR _hellinger
Definition: KL.h:134

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
INLINE double gum::KL< GUM_SCALAR >::klPQ ( )
inherited
Returns
divergence KL(P||Q)

Definition at line 72 of file KL_tpl.h.

References gum::KL< GUM_SCALAR >::_klPQ, and gum::KL< GUM_SCALAR >::_process().

72  {
73  _process();
74  return _klPQ;
75  }
void _process()
Definition: KL_tpl.h:168
GUM_SCALAR _klPQ
Definition: KL.h:132

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
INLINE double gum::KL< GUM_SCALAR >::klQP ( )
inherited
Returns
divergence KL(Q||P)

Definition at line 78 of file KL_tpl.h.

References gum::KL< GUM_SCALAR >::_klQP, and gum::KL< GUM_SCALAR >::_process().

78  {
79  _process();
80  return _klQP;
81  }
void _process()
Definition: KL_tpl.h:168
GUM_SCALAR _klQP
Definition: KL.h:133

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
INLINE const IBayesNet< GUM_SCALAR > & gum::KL< GUM_SCALAR >::p ( ) const
inherited
Returns
p

Definition at line 108 of file KL_tpl.h.

References gum::KL< GUM_SCALAR >::_p.

108  {
109  return _p;
110  }
const IBayesNet< GUM_SCALAR > & _p
Definition: KL.h:129
template<typename GUM_SCALAR >
INLINE const IBayesNet< GUM_SCALAR > & gum::KL< GUM_SCALAR >::q ( ) const
inherited
Returns
q

Definition at line 113 of file KL_tpl.h.

References gum::KL< GUM_SCALAR >::_q.

113  {
114  return _q;
115  }
const IBayesNet< GUM_SCALAR > & _q
Definition: KL.h:130

Member Data Documentation

template<typename GUM_SCALAR>
GUM_SCALAR gum::KL< GUM_SCALAR >::_bhattacharya
protectedinherited
template<typename GUM_SCALAR>
Size gum::KL< GUM_SCALAR >::_errorPQ
protectedinherited
template<typename GUM_SCALAR>
Size gum::KL< GUM_SCALAR >::_errorQP
protectedinherited
template<typename GUM_SCALAR>
GUM_SCALAR gum::KL< GUM_SCALAR >::_hellinger
protectedinherited
template<typename GUM_SCALAR>
GUM_SCALAR gum::KL< GUM_SCALAR >::_klPQ
protectedinherited
template<typename GUM_SCALAR>
GUM_SCALAR gum::KL< GUM_SCALAR >::_klQP
protectedinherited
template<typename GUM_SCALAR>
const IBayesNet< GUM_SCALAR >& gum::KL< GUM_SCALAR >::_p
protectedinherited
template<typename GUM_SCALAR>
const IBayesNet< GUM_SCALAR >& gum::KL< GUM_SCALAR >::_q
protectedinherited

The documentation for this class was generated from the following files: