aGrUM  0.20.3
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_
 
Size errorPQ_
 
Size errorQP_
 
GUM_SCALAR hellinger_
 
GUM_SCALAR bhattacharya_
 
GUM_SCALAR jsd_
 

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), _difficulty_(Complexity::Heavy),
41  _done_(false) {
42  _checkCompatibility_(); // may throw OperationNotAllowed
43  GUM_CONSTRUCTOR(BNdistance);
44 
45  double diff = p_.log10DomainSize();
46 
51  else
53  }
bool _checkCompatibility_() const
GUM_SCALAR klQP_
Definition: BNdistance.h:145
GUM_SCALAR klPQ_
Definition: BNdistance.h:144
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Complexity _difficulty_
Definition: BNdistance.h:156
#define GAP_COMPLEXITY_KL_DIFFICULT_CORRECT
Definition: BNdistance.h:75
const IBayesNet< GUM_SCALAR > & p_
Definition: BNdistance.h:141
#define GAP_COMPLEXITY_KL_HEAVY_DIFFICULT
Definition: BNdistance.h:74
const IBayesNet< GUM_SCALAR > & q_
Definition: BNdistance.h:142

◆ 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_), errorPQ_(kl.errorPQ_),
58  errorQP_(kl.errorQP_), _difficulty_(kl._difficulty_), _done_(kl._done_) {
59  GUM_CONSTRUCTOR(BNdistance);
60  }
GUM_SCALAR klQP_
Definition: BNdistance.h:145
GUM_SCALAR klPQ_
Definition: BNdistance.h:144
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Complexity _difficulty_
Definition: BNdistance.h:156
const IBayesNet< GUM_SCALAR > & p_
Definition: BNdistance.h:141
const IBayesNet< GUM_SCALAR > & q_
Definition: BNdistance.h:142

◆ ~BNdistance()

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

destructor

Definition at line 63 of file BNdistance_tpl.h.

63  {
64  GUM_DESTRUCTOR(BNdistance);
65  }
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 126 of file BNdistance_tpl.h.

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

◆ 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 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::BNdistance< GUM_SCALAR >::computeKL_ ( )
protectedvirtual

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

Definition at line 184 of file BNdistance_tpl.h.

184  {
185  GUM_ERROR(OperationNotAllowed, "No default computations")
186  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

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

◆ _difficulty_

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

Definition at line 156 of file BNdistance.h.

◆ _done_

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

Definition at line 157 of file BNdistance.h.

◆ bhattacharya_

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

Definition at line 151 of file BNdistance.h.

◆ errorPQ_

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

Definition at line 147 of file BNdistance.h.

◆ errorQP_

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

Definition at line 148 of file BNdistance.h.

◆ hellinger_

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

Definition at line 150 of file BNdistance.h.

◆ jsd_

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

Definition at line 152 of file BNdistance.h.

◆ klPQ_

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

Definition at line 144 of file BNdistance.h.

◆ klQP_

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

Definition at line 145 of file BNdistance.h.

◆ p_

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

Definition at line 141 of file BNdistance.h.

◆ q_

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

Definition at line 142 of file BNdistance.h.


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