aGrUM  0.20.2
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_
 
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 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.

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.

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

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
+ Here is the call graph for this function:

◆ ~ExactBNdistance()

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

destructor

Definition at line 51 of file exactBNdistance_tpl.h.

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

51  {
52  GUM_DESTRUCTOR(ExactBNdistance);
53  }
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 92 of file BNdistance_tpl.h.

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

◆ 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::Set< Key, Alloc >::emplace().

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 = std::log2(pmid);
77  if (pp != (GUM_SCALAR)0.0) lpp = std::log2(pp);
78  if (pq != (GUM_SCALAR)0.0) lpq = std::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_ += pp * lpp + pq * lpq
101  - (pp + pq) * lpmid; // pp* log2(pp / pmid) + pq * log2(pq / pmid);
102  }
103  }
104  jsd_ /= 2.0;
105  hellinger_ = std::sqrt(hellinger_);
106  bhattacharya_ = -std::log(bhattacharya_);
107  }
GUM_SCALAR klQP_
Definition: BNdistance.h:144
GUM_SCALAR klPQ_
Definition: BNdistance.h:143
GUM_SCALAR hellinger_
Definition: BNdistance.h:145
GUM_SCALAR bhattacharya_
Definition: BNdistance.h:146
const IBayesNet< GUM_SCALAR > & p_
Definition: BNdistance.h:140
GUM_SCALAR jsd_
Definition: BNdistance.h:147
const IBayesNet< GUM_SCALAR > & q_
Definition: BNdistance.h:141
Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:612
+ 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 69 of file BNdistance_tpl.h.

69  {
70  return difficulty__;
71  }
Complexity difficulty__
Definition: BNdistance.h:154

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

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

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

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

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

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

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

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

◆ klPQ()

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

Definition at line 74 of file BNdistance_tpl.h.

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

◆ klQP()

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

Definition at line 80 of file BNdistance_tpl.h.

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

◆ p()

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

Definition at line 116 of file BNdistance_tpl.h.

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

◆ process_()

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

Definition at line 177 of file BNdistance_tpl.h.

177  {
178  if (!done__) {
179  computeKL_();
180  done__ = true;
181  }
182  }
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 121 of file BNdistance_tpl.h.

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

Member Data Documentation

◆ bhattacharya_

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

Definition at line 146 of file BNdistance.h.

◆ errorPQ_

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

Definition at line 149 of file BNdistance.h.

◆ errorQP_

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

Definition at line 150 of file BNdistance.h.

◆ hellinger_

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

Definition at line 145 of file BNdistance.h.

◆ jsd_

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

Definition at line 147 of file BNdistance.h.

◆ klPQ_

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

Definition at line 143 of file BNdistance.h.

◆ klQP_

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

Definition at line 144 of file BNdistance.h.

◆ p_

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

Definition at line 140 of file BNdistance.h.

◆ q_

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

Definition at line 141 of file BNdistance.h.


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