aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
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 72 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 37 of file BNdistance_tpl.h.

38  :
39  p_(P),
40  q_(Q), klPQ_(0.0), klQP_(0.0), errorPQ_(0), errorQP_(0),
42  checkCompatibility__(); // may throw OperationNotAllowed
43  GUM_CONSTRUCTOR(BNdistance);
44 
45  double diff = p_.log10DomainSize();
46 
51  else
53  }
GUM_SCALAR klQP_
Definition: BNdistance.h:144
GUM_SCALAR klPQ_
Definition: BNdistance.h:143
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
#define GAP_COMPLEXITY_KL_DIFFICULT_CORRECT
Definition: BNdistance.h:75
const IBayesNet< GUM_SCALAR > & p_
Definition: BNdistance.h:140
bool checkCompatibility__() const
Complexity difficulty__
Definition: BNdistance.h:154
#define GAP_COMPLEXITY_KL_HEAVY_DIFFICULT
Definition: BNdistance.h:74
const IBayesNet< GUM_SCALAR > & q_
Definition: BNdistance.h:141

◆ BNdistance() [2/2]

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

copy constructor

Definition at line 56 of file BNdistance_tpl.h.

56  :
57  p_(kl.p_), q_(kl.q_), klPQ_(kl.klPQ_), klQP_(kl.klQP_),
58  errorPQ_(kl.errorPQ_), errorQP_(kl.errorQP_), difficulty__(kl.difficulty__),
59  done__(kl.done__) {
60  GUM_CONSTRUCTOR(BNdistance);
61  }
GUM_SCALAR klQP_
Definition: BNdistance.h:144
GUM_SCALAR klPQ_
Definition: BNdistance.h:143
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
const IBayesNet< GUM_SCALAR > & p_
Definition: BNdistance.h:140
Complexity difficulty__
Definition: BNdistance.h:154
const IBayesNet< GUM_SCALAR > & q_
Definition: BNdistance.h:141

◆ ~BNdistance()

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

destructor

Definition at line 64 of file BNdistance_tpl.h.

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

Member Function Documentation

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

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

◆ checkCompatibility__()

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

Definition at line 127 of file BNdistance_tpl.h.

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

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

186  {
187  GUM_ERROR(OperationNotAllowed, "No default computations");
188  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ 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 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 ( )
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 ( )
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 ( )
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 ( )
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 ( )
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 ( )
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
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_ ( )
protected

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
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_
protected

Definition at line 146 of file BNdistance.h.

◆ difficulty__

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

Definition at line 154 of file BNdistance.h.

◆ done__

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

Definition at line 155 of file BNdistance.h.

◆ errorPQ_

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

Definition at line 149 of file BNdistance.h.

◆ errorQP_

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

Definition at line 150 of file BNdistance.h.

◆ hellinger_

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

Definition at line 145 of file BNdistance.h.

◆ jsd_

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

Definition at line 147 of file BNdistance.h.

◆ klPQ_

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

Definition at line 143 of file BNdistance.h.

◆ klQP_

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

Definition at line 144 of file BNdistance.h.

◆ p_

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

Definition at line 140 of file BNdistance.h.

◆ q_

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

Definition at line 141 of file BNdistance.h.


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