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

KL is the base class for KL computation betweens 2 BNs. More...

#include <KL.h>

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

Public Member Functions

 KL (const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
 constructor must give 2 BNs More...
 
 KL (const KL< GUM_SCALAR > &kl)
 copy constructor More...
 
virtual ~KL ()
 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

virtual void _computeKL ()
 
void _process ()
 

Detailed Description

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

KL is the base class for KL computation betweens 2 BNs.

KL is not virtual because it may be instantiated but protected methods throw gum::OperationNotAllow : we do not know here how the computation is done. Since this computation may be very difficult, KL.Complexity() give an estimation ( KL_Complexity::Heavy,KL_Complexity::Difficult,KL_Complexity::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 :)

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())?

Definition at line 65 of file KL.h.

Constructor & Destructor Documentation

template<typename GUM_SCALAR >
gum::KL< GUM_SCALAR >::KL ( 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 KL_tpl.h.

References gum::KL< GUM_SCALAR >::__checkCompatibility(), gum::KL< GUM_SCALAR >::__difficulty, gum::KL< GUM_SCALAR >::_p, gum::Correct, gum::Difficult, GAP_COMPLEXITY_KL_DIFFICULT_CORRECT, GAP_COMPLEXITY_KL_HEAVY_DIFFICULT, and gum::Heavy.

36  :
37  _p(P),
38  _q(Q), _klPQ(0.0), _klQP(0.0), _errorPQ(0), _errorQP(0),
40  __checkCompatibility(); // may throw OperationNotAllowed
41  GUM_CONSTRUCTOR(KL);
42 
43  double diff = _p.log10DomainSize();
44 
49  else
51  }
GUM_SCALAR _klPQ
Definition: KL.h:132
Complexity __difficulty
Definition: KL.h:142
#define GAP_COMPLEXITY_KL_HEAVY_DIFFICULT
Definition: KL.h:67
const IBayesNet< GUM_SCALAR > & _p
Definition: KL.h:129
#define GAP_COMPLEXITY_KL_DIFFICULT_CORRECT
Definition: KL.h:68
const IBayesNet< GUM_SCALAR > & _q
Definition: KL.h:130
bool __done
Definition: KL.h:143
bool __checkCompatibility() const
Definition: KL_tpl.h:119
Size _errorPQ
Definition: KL.h:137
KL(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Definition: KL_tpl.h:35
GUM_SCALAR _klQP
Definition: KL.h:133
Size _errorQP
Definition: KL.h:138

+ Here is the call graph for this function:

template<typename GUM_SCALAR >
gum::KL< GUM_SCALAR >::KL ( const KL< GUM_SCALAR > &  kl)

copy constructor

Definition at line 54 of file KL_tpl.h.

54  :
55  _p(kl._p), _q(kl._q), _klPQ(kl._klPQ), _klQP(kl._klQP),
56  _errorPQ(kl._errorPQ), _errorQP(kl._errorQP), __difficulty(kl.__difficulty),
57  __done(kl.__done) {
58  GUM_CONSTRUCTOR(KL);
59  }
GUM_SCALAR _klPQ
Definition: KL.h:132
Complexity __difficulty
Definition: KL.h:142
const IBayesNet< GUM_SCALAR > & _p
Definition: KL.h:129
const IBayesNet< GUM_SCALAR > & _q
Definition: KL.h:130
bool __done
Definition: KL.h:143
Size _errorPQ
Definition: KL.h:137
KL(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Definition: KL_tpl.h:35
GUM_SCALAR _klQP
Definition: KL.h:133
Size _errorQP
Definition: KL.h:138
template<typename GUM_SCALAR >
gum::KL< GUM_SCALAR >::~KL ( )
virtual

destructor

Definition at line 62 of file KL_tpl.h.

62  {
63  GUM_DESTRUCTOR(KL);
64  }
KL(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Definition: KL_tpl.h:35

Member Function Documentation

template<typename GUM_SCALAR >
bool gum::KL< GUM_SCALAR >::__checkCompatibility ( ) const
private

Definition at line 119 of file KL_tpl.h.

References gum::KL< GUM_SCALAR >::_p, gum::KL< GUM_SCALAR >::_q, gum::DiscreteVariable::domainSize(), GUM_ERROR, gum::DiscreteVariable::label(), and gum::Variable::name().

Referenced by gum::KL< GUM_SCALAR >::KL().

119  {
120  for (auto node : _p.nodes()) {
121  const DiscreteVariable& vp = _p.variable(node);
122 
123  try {
124  const DiscreteVariable& vq = _q.variableFromName(vp.name());
125 
126  if (vp.domainSize() != vq.domainSize())
127  GUM_ERROR(OperationNotAllowed,
128  "KL : the 2 BNs are not compatible "
129  "(not the same domainSize for "
130  + vp.name() + ")");
131 
132  for (Idx i = 0; i < vp.domainSize(); i++) {
133  try {
134  vq[vp.label(i)];
135  vp[vq.label(i)];
136 
137  } catch (OutOfBounds&) {
138  GUM_ERROR(
139  OperationNotAllowed,
140  "KL : the 2 BNs are not compatible F(not the same labels for "
141  + vp.name() + ")");
142  }
143  }
144  } catch (NotFound&) {
145  GUM_ERROR(OperationNotAllowed,
146  "KL : the 2 BNs are not compatible (not the same vars : "
147  + vp.name() + ")");
148  }
149  }
150 
151  // should not be used
152  if (_p.size() != _q.size())
153  GUM_ERROR(OperationNotAllowed,
154  "KL : the 2 BNs are not compatible (not the same size)");
155 
156  if (std::fabs(_p.log10DomainSize() - _q.log10DomainSize()) > 1e-14) {
157  GUM_ERROR(OperationNotAllowed,
158  "KL : the 2 BNs are not compatible (not the same domainSize) : p="
159  << _p.log10DomainSize() << " q=" << _q.log10DomainSize()
160  << " => " << _p.log10DomainSize() - _q.log10DomainSize());
161  }
162 
163  return true;
164  }
const IBayesNet< GUM_SCALAR > & _p
Definition: KL.h:129
const IBayesNet< GUM_SCALAR > & _q
Definition: KL.h:130
unsigned long Idx
Type for indexes.
Definition: types.h:43
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename GUM_SCALAR >
void gum::KL< GUM_SCALAR >::_computeKL ( )
protectedvirtual

Reimplemented in gum::GibbsKL< GUM_SCALAR >, and gum::BruteForceKL< GUM_SCALAR >.

Definition at line 177 of file KL_tpl.h.

References GUM_ERROR.

Referenced by gum::KL< GUM_SCALAR >::_process().

177  {
178  GUM_ERROR(OperationNotAllowed, "No default computations");
179  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the caller graph for this function:

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

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 ( )
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

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 ( )
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 ( )
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 ( )
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 ( )
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 ( )
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
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
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>
Complexity gum::KL< GUM_SCALAR >::__difficulty
private

Definition at line 142 of file KL.h.

Referenced by gum::KL< GUM_SCALAR >::difficulty(), and gum::KL< GUM_SCALAR >::KL().

template<typename GUM_SCALAR>
bool gum::KL< GUM_SCALAR >::__done
private

Definition at line 143 of file KL.h.

Referenced by gum::KL< GUM_SCALAR >::_process().

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

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