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

#include <BNdistance.h>

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

Public Member Functions

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

virtual void _computeKL ()
 
void _process ()
 

Detailed Description

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

Definition at line 68 of file BNdistance.h.

Constructor & Destructor Documentation

◆ BNdistance() [1/2]

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

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

39  :
40  _p(P),
41  _q(Q), _klPQ(0.0), _klQP(0.0), _errorPQ(0), _errorQP(0),
43  __checkCompatibility(); // may throw OperationNotAllowed
44  GUM_CONSTRUCTOR(BNdistance);
45 
46  double diff = _p.log10DomainSize();
47 
52  else
54  }
GUM_SCALAR _klPQ
Definition: BNdistance.h:139
bool __checkCompatibility() const
const IBayesNet< GUM_SCALAR > & _p
Definition: BNdistance.h:136
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
const IBayesNet< GUM_SCALAR > & _q
Definition: BNdistance.h:137
#define GAP_COMPLEXITY_KL_DIFFICULT_CORRECT
Definition: BNdistance.h:71
GUM_SCALAR _klQP
Definition: BNdistance.h:140
Complexity __difficulty
Definition: BNdistance.h:150
#define GAP_COMPLEXITY_KL_HEAVY_DIFFICULT
Definition: BNdistance.h:70
+ Here is the call graph for this function:

◆ BNdistance() [2/2]

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

copy constructor

Definition at line 57 of file BNdistance_tpl.h.

57  :
58  _p(kl._p), _q(kl._q), _klPQ(kl._klPQ), _klQP(kl._klQP),
59  _errorPQ(kl._errorPQ), _errorQP(kl._errorQP), __difficulty(kl.__difficulty),
60  __done(kl.__done) {
61  GUM_CONSTRUCTOR(BNdistance);
62  }
GUM_SCALAR _klPQ
Definition: BNdistance.h:139
const IBayesNet< GUM_SCALAR > & _p
Definition: BNdistance.h:136
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
const IBayesNet< GUM_SCALAR > & _q
Definition: BNdistance.h:137
GUM_SCALAR _klQP
Definition: BNdistance.h:140
Complexity __difficulty
Definition: BNdistance.h:150

◆ ~BNdistance()

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

destructor

Definition at line 65 of file BNdistance_tpl.h.

65  {
66  GUM_DESTRUCTOR(BNdistance);
67  }
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs

Member Function Documentation

◆ __checkCompatibility()

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

Definition at line 128 of file BNdistance_tpl.h.

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

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

128  {
129  for (auto node : _p.nodes()) {
130  const DiscreteVariable& vp = _p.variable(node);
131 
132  try {
133  const DiscreteVariable& vq = _q.variableFromName(vp.name());
134 
135  if (vp.domainSize() != vq.domainSize())
136  GUM_ERROR(OperationNotAllowed,
137  "BNdistance : the 2 BNs are not compatible "
138  "(not the same domainSize for "
139  + vp.name() + ")");
140 
141  for (Idx i = 0; i < vp.domainSize(); i++) {
142  try {
143  vq[vp.label(i)];
144  vp[vq.label(i)];
145 
146  } catch (OutOfBounds&) {
147  GUM_ERROR(OperationNotAllowed,
148  "BNdistance : the 2 BNs are not compatible F(not the same "
149  "labels for "
150  + vp.name() + ")");
151  }
152  }
153  } catch (NotFound&) {
154  GUM_ERROR(OperationNotAllowed,
155  "BNdistance : the 2 BNs are not compatible (not the same vars : "
156  + vp.name() + ")");
157  }
158  }
159 
160  // should not be used
161  if (_p.size() != _q.size())
162  GUM_ERROR(OperationNotAllowed,
163  "BNdistance : the 2 BNs are not compatible (not the same size)");
164 
165  if (std::fabs(_p.log10DomainSize() - _q.log10DomainSize()) > 1e-14) {
166  GUM_ERROR(
167  OperationNotAllowed,
168  "BNdistance : the 2 BNs are not compatible (not the same domainSize) : p="
169  << _p.log10DomainSize() << " q=" << _q.log10DomainSize() << " => "
170  << _p.log10DomainSize() - _q.log10DomainSize());
171  }
172 
173  return true;
174  }
const IBayesNet< GUM_SCALAR > & _p
Definition: BNdistance.h:136
const IBayesNet< GUM_SCALAR > & _q
Definition: BNdistance.h:137
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _computeKL()

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

Reimplemented in gum::GibbsBNdistance< GUM_SCALAR >, and gum::ExactBNdistance< GUM_SCALAR >.

Definition at line 187 of file BNdistance_tpl.h.

References GUM_ERROR.

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

187  {
188  GUM_ERROR(OperationNotAllowed, "No default computations");
189  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ _process()

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

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

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

◆ __difficulty

template<typename GUM_SCALAR>
Complexity gum::BNdistance< GUM_SCALAR >::__difficulty
private

◆ __done

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

Definition at line 151 of file BNdistance.h.

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

◆ _bhattacharya

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

◆ _errorPQ

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

◆ _errorQP

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

◆ _hellinger

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

◆ _jsd

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

◆ _klPQ

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

◆ _klQP

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

◆ _p

◆ _q


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