aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
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_
 
Size errorPQ_
 
Size errorQP_
 
GUM_SCALAR hellinger_
 
GUM_SCALAR bhattacharya_
 
GUM_SCALAR jsd_
 

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 69 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 36 of file exactBNdistance_tpl.h.

37  :
38  BNdistance< GUM_SCALAR >(P, Q) {
39  GUM_CONSTRUCTOR(ExactBNdistance);
40  }
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 43 of file exactBNdistance_tpl.h.

References gum::Set< Key, Alloc >::emplace().

43  :
44  BNdistance< GUM_SCALAR >(kl) {
45  GUM_CONSTRUCTOR(ExactBNdistance);
46  }
ExactBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
+ Here is the call graph for this function:

◆ ~ExactBNdistance()

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

destructor

Definition at line 49 of file exactBNdistance_tpl.h.

References gum::Set< Key, Alloc >::emplace().

49  {
50  GUM_DESTRUCTOR(ExactBNdistance);
51  }
ExactBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
+ Here is the call graph for this function:

Member Function Documentation

◆ 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 91 of file BNdistance_tpl.h.

91  {
92  process_();
93  return bhattacharya_;
94  }
GUM_SCALAR bhattacharya_
Definition: BNdistance.h:151

◆ computeKL_()

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

Reimplemented from gum::BNdistance< GUM_SCALAR >.

Definition at line 54 of file exactBNdistance_tpl.h.

References gum::Set< Key, Alloc >::emplace().

54  {
55  klPQ_ = klQP_ = hellinger_ = bhattacharya_ = jsd_ = (GUM_SCALAR)0.0;
56  errorPQ_ = errorQP_ = 0;
57 
58  auto Ip = p_.completeInstantiation();
59  auto Iq = q_.completeInstantiation();
60 
61  // map between p_ variables and q_ variables (using name of vars)
62  HashTable< const DiscreteVariable*, const DiscreteVariable* > map;
63 
64  for (Idx ite = 0; ite < Ip.nbrDim(); ++ite) {
65  map.insert(&Ip.variable(ite), &q_.variableFromName(Ip.variable(ite).name()));
66  }
67  GUM_SCALAR pp, pq, pmid, lpp, lpq, lpmid;
68  for (Ip.setFirst(); !Ip.end(); ++Ip) {
69  Iq.setValsFrom(map, Ip);
70  pp = p_.jointProbability(Ip);
71  pq = q_.jointProbability(Iq);
72  pmid = (pp + pq) / 2.0;
73  lpmid = lpq = lpp = (GUM_SCALAR)0.0;
74  if (pmid != (GUM_SCALAR)0.0) lpmid = std::log2(pmid);
75  if (pp != (GUM_SCALAR)0.0) lpp = std::log2(pp);
76  if (pq != (GUM_SCALAR)0.0) lpq = std::log2(pq);
77 
78 
79  hellinger_ += std::pow(std::sqrt(pp) - std::sqrt(pq), 2);
80  bhattacharya_ += std::sqrt(pp * pq);
81 
82  if (pp != (GUM_SCALAR)0.0) {
83  if (pq != (GUM_SCALAR)0.0) {
84  klPQ_ -= pp * (lpq - lpp); // log2(pq / pp);
85  } else {
86  errorPQ_++;
87  }
88  }
89 
90  if (pq != (GUM_SCALAR)0.0) {
91  if (pp != (GUM_SCALAR)0.0) {
92  klQP_ -= pq * (lpp - lpq); // log2(pp / pq);
93  } else {
94  errorQP_++;
95  }
96  }
97  if (pmid != (GUM_SCALAR)0.0) {
98  jsd_ += pp * lpp + pq * lpq
99  - (pp + pq) * lpmid; // pp* log2(pp / pmid) + pq * log2(pq / pmid);
100  }
101  }
102  jsd_ /= 2.0;
103  hellinger_ = std::sqrt(hellinger_);
104  bhattacharya_ = -std::log(bhattacharya_);
105  }
GUM_SCALAR klQP_
Definition: BNdistance.h:145
GUM_SCALAR klPQ_
Definition: BNdistance.h:144
GUM_SCALAR hellinger_
Definition: BNdistance.h:150
GUM_SCALAR bhattacharya_
Definition: BNdistance.h:151
const IBayesNet< GUM_SCALAR > & p_
Definition: BNdistance.h:141
GUM_SCALAR jsd_
Definition: BNdistance.h:152
const IBayesNet< GUM_SCALAR > & q_
Definition: BNdistance.h:142
Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:590
+ 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 68 of file BNdistance_tpl.h.

68  {
69  return _difficulty_;
70  }
Complexity _difficulty_
Definition: BNdistance.h:156

◆ 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 103 of file BNdistance_tpl.h.

103  {
104  process_();
105  return errorPQ_;
106  }

◆ 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 109 of file BNdistance_tpl.h.

109  {
110  process_();
111  return errorQP_;
112  }

◆ 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 85 of file BNdistance_tpl.h.

85  {
86  process_();
87  return hellinger_;
88  }
GUM_SCALAR hellinger_
Definition: BNdistance.h:150

◆ 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 97 of file BNdistance_tpl.h.

97  {
98  process_();
99  return jsd_;
100  }
GUM_SCALAR jsd_
Definition: BNdistance.h:152

◆ klPQ()

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

Definition at line 73 of file BNdistance_tpl.h.

73  {
74  process_();
75  return klPQ_;
76  }
GUM_SCALAR klPQ_
Definition: BNdistance.h:144

◆ klQP()

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

Definition at line 79 of file BNdistance_tpl.h.

79  {
80  process_();
81  return klQP_;
82  }
GUM_SCALAR klQP_
Definition: BNdistance.h:145

◆ p()

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

Definition at line 115 of file BNdistance_tpl.h.

115  {
116  return p_;
117  }
const IBayesNet< GUM_SCALAR > & p_
Definition: BNdistance.h:141

◆ process_()

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

Definition at line 175 of file BNdistance_tpl.h.

175  {
176  if (!_done_) {
177  computeKL_();
178  _done_ = true;
179  }
180  }
virtual void computeKL_()

◆ q()

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

Definition at line 120 of file BNdistance_tpl.h.

120  {
121  return q_;
122  }
const IBayesNet< GUM_SCALAR > & q_
Definition: BNdistance.h:142

Member Data Documentation

◆ bhattacharya_

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

Definition at line 151 of file BNdistance.h.

◆ errorPQ_

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

Definition at line 147 of file BNdistance.h.

◆ errorQP_

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

Definition at line 148 of file BNdistance.h.

◆ hellinger_

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

Definition at line 150 of file BNdistance.h.

◆ jsd_

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

Definition at line 152 of file BNdistance.h.

◆ klPQ_

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

Definition at line 144 of file BNdistance.h.

◆ klQP_

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

Definition at line 145 of file BNdistance.h.

◆ p_

template<typename GUM_SCALAR >
const IBayesNet< GUM_SCALAR >& gum::BNdistance< GUM_SCALAR >::p_
protectedinherited

Definition at line 141 of file BNdistance.h.

◆ q_

template<typename GUM_SCALAR >
const IBayesNet< GUM_SCALAR >& gum::BNdistance< GUM_SCALAR >::q_
protectedinherited

Definition at line 142 of file BNdistance.h.


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