aGrUM  0.16.0
BNdistance_tpl.h
Go to the documentation of this file.
1 
30 #include <complex>
31 
32 #include <agrum/core/math/math.h>
33 #include <agrum/BN/IBayesNet.h>
35 
36 namespace gum {
37  template < typename GUM_SCALAR >
39  const IBayesNet< GUM_SCALAR >& Q) :
40  _p(P),
41  _q(Q), _klPQ(0.0), _klQP(0.0), _errorPQ(0), _errorQP(0),
42  __difficulty(Complexity::Heavy), __done(false) {
43  __checkCompatibility(); // may throw OperationNotAllowed
44  GUM_CONSTRUCTOR(BNdistance);
45 
46  double diff = _p.log10DomainSize();
47 
52  else
54  }
55 
56  template < typename GUM_SCALAR >
58  _p(kl._p), _q(kl._q), _klPQ(kl._klPQ), _klQP(kl._klQP),
60  __done(kl.__done) {
61  GUM_CONSTRUCTOR(BNdistance);
62  }
63 
64  template < typename GUM_SCALAR >
66  GUM_DESTRUCTOR(BNdistance);
67  }
68 
69  template < typename GUM_SCALAR >
71  return __difficulty;
72  }
73 
74  template < typename GUM_SCALAR >
76  _process();
77  return _klPQ;
78  }
79 
80  template < typename GUM_SCALAR >
82  _process();
83  return _klQP;
84  }
85 
86  template < typename GUM_SCALAR >
88  _process();
89  return _hellinger;
90  }
91 
92  template < typename GUM_SCALAR >
94  _process();
95  return _bhattacharya;
96  }
97 
98  template < typename GUM_SCALAR >
100  _process();
101  return _jsd;
102  }
103 
104  template < typename GUM_SCALAR >
106  _process();
107  return _errorPQ;
108  }
109 
110  template < typename GUM_SCALAR >
112  _process();
113  return _errorQP;
114  }
115 
116  template < typename GUM_SCALAR >
118  return _p;
119  }
120 
121  template < typename GUM_SCALAR >
123  return _q;
124  }
125 
126  // check if the 2 BNs are compatible
127  template < typename GUM_SCALAR >
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())
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&) {
148  "BNdistance : the 2 BNs are not compatible F(not the same "
149  "labels for "
150  + vp.name() + ")");
151  }
152  }
153  } catch (NotFound&) {
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())
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(
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  }
175 
176  // do the job if not already __done
177  template < typename GUM_SCALAR >
179  if (!__done) {
180  _computeKL();
181  __done = true;
182  }
183  }
184 
185  // in order to keep BNdistance instantiable
186  template < typename GUM_SCALAR >
188  GUM_ERROR(OperationNotAllowed, "No default computations");
189  }
190 } // namespace gum
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
double bhattacharya()
Complexity difficulty() const
return KL::Complexity::Heavy,KL::Complexity::Difficult,KL::Complexity::Correct depending on the BNs _...
GUM_SCALAR _klPQ
Definition: BNdistance.h:139
GUM_SCALAR _bhattacharya
Definition: BNdistance.h:142
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
GUM_SCALAR _jsd
Definition: BNdistance.h:143
const IBayesNet< GUM_SCALAR > & p() const
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:62
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
virtual void _computeKL()
Complexity
Complexity allows to characterize the awaited difficulty for an algorithm given a specific instance T...
Definition: BNdistance.h:46
virtual Size domainSize() const =0
const IBayesNet< GUM_SCALAR > & _q
Definition: BNdistance.h:137
#define GAP_COMPLEXITY_KL_DIFFICULT_CORRECT
Definition: BNdistance.h:71
GUM_SCALAR _hellinger
Definition: BNdistance.h:141
const IBayesNet< GUM_SCALAR > & q() const
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.
GUM_SCALAR _klQP
Definition: BNdistance.h:140
virtual ~BNdistance()
destructor
Complexity __difficulty
Definition: BNdistance.h:150
Size Idx
Type for indexes.
Definition: types.h:53
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
#define GAP_COMPLEXITY_KL_HEAVY_DIFFICULT
Definition: BNdistance.h:70
const std::string & name() const
returns the name of the variable
double hellinger()
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55