aGrUM  0.16.0
gum::ExactBNdistance< GUM_SCALAR > Class Template Reference

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

#include <exactBNdistance.h>

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

Public Member Functions

 ExactBNdistance (const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
 constructor must give 2 BNs More...
 
 ExactBNdistance (const BNdistance< GUM_SCALAR > &kl)
 copy constructor More...
 
virtual ~ExactBNdistance ()
 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 ()
 
double jsd ()
 
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
 
GUM_SCALAR _jsd
 
Size _errorPQ
 
Size _errorQP
 

Protected Member Functions

void _computeKL () final
 
void _process ()
 

Detailed Description

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

ExactBNdistance computes exactly the KL divergence betweens 2 BNs.

ExactBNdistance 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 :) ExactBNdistance 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 ExactBNdistance 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::ExactBNdistance 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 70 of file exactBNdistance.h.

Constructor & Destructor Documentation

◆ ExactBNdistance() [1/2]

template<typename GUM_SCALAR >
gum::ExactBNdistance< GUM_SCALAR >::ExactBNdistance ( 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 37 of file exactBNdistance_tpl.h.

38  :
39  BNdistance< GUM_SCALAR >(P, Q) {
40  GUM_CONSTRUCTOR(ExactBNdistance);
41  }
ExactBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs

◆ ExactBNdistance() [2/2]

template<typename GUM_SCALAR >
gum::ExactBNdistance< GUM_SCALAR >::ExactBNdistance ( const BNdistance< GUM_SCALAR > &  kl)
explicit

copy constructor

Definition at line 44 of file exactBNdistance_tpl.h.

45  :
46  BNdistance< GUM_SCALAR >(kl) {
47  GUM_CONSTRUCTOR(ExactBNdistance);
48  }
ExactBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs

◆ ~ExactBNdistance()

template<typename GUM_SCALAR >
gum::ExactBNdistance< GUM_SCALAR >::~ExactBNdistance ( )
virtual

destructor

Definition at line 51 of file exactBNdistance_tpl.h.

51  {
52  GUM_DESTRUCTOR(ExactBNdistance);
53  }
ExactBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs

Member Function Documentation

◆ _computeKL()

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

Reimplemented from gum::BNdistance< GUM_SCALAR >.

Definition at line 56 of file exactBNdistance_tpl.h.

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

56  {
57  _klPQ = _klQP = _hellinger = _bhattacharya = _jsd = (GUM_SCALAR)0.0;
58  _errorPQ = _errorQP = 0;
59 
60  auto Ip = _p.completeInstantiation();
61  auto Iq = _q.completeInstantiation();
62 
63  // map between _p variables and _q variables (using name of vars)
64  HashTable< const DiscreteVariable*, const DiscreteVariable* > map;
65 
66  for (Idx ite = 0; ite < Ip.nbrDim(); ++ite) {
67  map.insert(&Ip.variable(ite), &_q.variableFromName(Ip.variable(ite).name()));
68  }
69  GUM_SCALAR pp, pq, pmid, lpp, lpq, lpmid;
70  for (Ip.setFirst(); !Ip.end(); ++Ip) {
71  Iq.setValsFrom(map, Ip);
72  pp = _p.jointProbability(Ip);
73  pq = _q.jointProbability(Iq);
74  pmid = (pp + pq) / 2.0;
75  lpmid = lpq = lpp = (GUM_SCALAR)0.0;
76  if (pmid != (GUM_SCALAR)0.0) lpmid = log2(pmid);
77  if (pp != (GUM_SCALAR)0.0) lpp = log2(pp);
78  if (pq != (GUM_SCALAR)0.0) lpq = log2(pq);
79 
80 
81  _hellinger += std::pow(std::sqrt(pp) - std::sqrt(pq), 2);
82  _bhattacharya += std::sqrt(pp * pq);
83 
84  if (pp != (GUM_SCALAR)0.0) {
85  if (pq != (GUM_SCALAR)0.0) {
86  _klPQ -= pp * (lpq - lpp); // log2(pq / pp);
87  } else {
88  _errorPQ++;
89  }
90  }
91 
92  if (pq != (GUM_SCALAR)0.0) {
93  if (pp != (GUM_SCALAR)0.0) {
94  _klQP -= pq * (lpp - lpq); // log2(pp / pq);
95  } else {
96  _errorQP++;
97  }
98  }
99  if (pmid != (GUM_SCALAR)0.0) {
100  _jsd +=
101  pp * lpp + pq * lpq
102  - (pp + pq) * lpmid; // pp* log2(pp / pmid) + pq * log2(pq / pmid);
103  }
104  }
105  _jsd /= 2.0;
106  _hellinger = std::sqrt(_hellinger);
107  _bhattacharya = -std::log(_bhattacharya);
108  }
GUM_SCALAR _klPQ
Definition: BNdistance.h:139
GUM_SCALAR _bhattacharya
Definition: BNdistance.h:142
const IBayesNet< GUM_SCALAR > & _p
Definition: BNdistance.h:136
GUM_SCALAR _jsd
Definition: BNdistance.h:143
const IBayesNet< GUM_SCALAR > & _q
Definition: BNdistance.h:137
GUM_SCALAR _hellinger
Definition: BNdistance.h:141
GUM_SCALAR _klQP
Definition: BNdistance.h:140
+ Here is the call graph for this function:

◆ _process()

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

Definition at line 178 of file BNdistance_tpl.h.

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

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

178  {
179  if (!__done) {
180  _computeKL();
181  __done = true;
182  }
183  }
virtual void _computeKL()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bhattacharya()

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

Definition at line 93 of file BNdistance_tpl.h.

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

93  {
94  _process();
95  return _bhattacharya;
96  }
GUM_SCALAR _bhattacharya
Definition: BNdistance.h:142
+ Here is the call graph for this function:

◆ difficulty()

template<typename GUM_SCALAR >
Complexity gum::BNdistance< 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 70 of file BNdistance_tpl.h.

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

70  {
71  return __difficulty;
72  }
Complexity __difficulty
Definition: BNdistance.h:150

◆ errorPQ()

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

Definition at line 105 of file BNdistance_tpl.h.

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

105  {
106  _process();
107  return _errorPQ;
108  }
+ Here is the call graph for this function:

◆ errorQP()

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

Definition at line 111 of file BNdistance_tpl.h.

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

111  {
112  _process();
113  return _errorQP;
114  }
+ Here is the call graph for this function:

◆ hellinger()

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

Definition at line 87 of file BNdistance_tpl.h.

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

87  {
88  _process();
89  return _hellinger;
90  }
GUM_SCALAR _hellinger
Definition: BNdistance.h:141
+ Here is the call graph for this function:

◆ jsd()

template<typename GUM_SCALAR >
INLINE double gum::BNdistance< GUM_SCALAR >::jsd ( )
inherited
Returns
Jensen-Shannon divergence(
See also
https://en.wikipedia.org/wiki/Jensen%E2%80%93Shannon_divergence)

Definition at line 99 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::_jsd, and gum::BNdistance< GUM_SCALAR >::_process().

99  {
100  _process();
101  return _jsd;
102  }
GUM_SCALAR _jsd
Definition: BNdistance.h:143
+ Here is the call graph for this function:

◆ klPQ()

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

Definition at line 75 of file BNdistance_tpl.h.

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

75  {
76  _process();
77  return _klPQ;
78  }
GUM_SCALAR _klPQ
Definition: BNdistance.h:139
+ Here is the call graph for this function:

◆ klQP()

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

Definition at line 81 of file BNdistance_tpl.h.

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

81  {
82  _process();
83  return _klQP;
84  }
GUM_SCALAR _klQP
Definition: BNdistance.h:140
+ Here is the call graph for this function:

◆ p()

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

Definition at line 117 of file BNdistance_tpl.h.

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

117  {
118  return _p;
119  }
const IBayesNet< GUM_SCALAR > & _p
Definition: BNdistance.h:136

◆ q()

template<typename GUM_SCALAR >
INLINE const IBayesNet< GUM_SCALAR > & gum::BNdistance< GUM_SCALAR >::q ( ) const
inherited
Returns
q

Definition at line 122 of file BNdistance_tpl.h.

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

122  {
123  return _q;
124  }
const IBayesNet< GUM_SCALAR > & _q
Definition: BNdistance.h:137

Member Data Documentation

◆ _bhattacharya

template<typename GUM_SCALAR>
GUM_SCALAR gum::BNdistance< GUM_SCALAR >::_bhattacharya
protectedinherited

◆ _errorPQ

template<typename GUM_SCALAR>
Size gum::BNdistance< GUM_SCALAR >::_errorPQ
protectedinherited

◆ _errorQP

template<typename GUM_SCALAR>
Size gum::BNdistance< GUM_SCALAR >::_errorQP
protectedinherited

◆ _hellinger

template<typename GUM_SCALAR>
GUM_SCALAR gum::BNdistance< GUM_SCALAR >::_hellinger
protectedinherited

◆ _jsd

template<typename GUM_SCALAR>
GUM_SCALAR gum::BNdistance< GUM_SCALAR >::_jsd
protectedinherited

◆ _klPQ

template<typename GUM_SCALAR>
GUM_SCALAR gum::BNdistance< GUM_SCALAR >::_klPQ
protectedinherited

◆ _klQP

template<typename GUM_SCALAR>
GUM_SCALAR gum::BNdistance< GUM_SCALAR >::_klQP
protectedinherited

◆ _p

◆ _q


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