aGrUM  0.14.2
BNdistance_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #include <complex>
28 
29 #include <agrum/core/math/math.h>
30 #include <agrum/BN/IBayesNet.h>
32 
33 namespace gum {
34  template < typename GUM_SCALAR >
36  const IBayesNet< GUM_SCALAR >& Q) :
37  _p(P),
38  _q(Q), _klPQ(0.0), _klQP(0.0), _errorPQ(0), _errorQP(0),
39  __difficulty(Complexity::Heavy), __done(false) {
40  __checkCompatibility(); // may throw OperationNotAllowed
41  GUM_CONSTRUCTOR(BNdistance);
42 
43  double diff = _p.log10DomainSize();
44 
49  else
51  }
52 
53  template < typename GUM_SCALAR >
55  _p(kl._p), _q(kl._q), _klPQ(kl._klPQ), _klQP(kl._klQP),
57  __done(kl.__done) {
58  GUM_CONSTRUCTOR(BNdistance);
59  }
60 
61  template < typename GUM_SCALAR >
63  GUM_DESTRUCTOR(BNdistance);
64  }
65 
66  template < typename GUM_SCALAR >
68  return __difficulty;
69  }
70 
71  template < typename GUM_SCALAR >
73  _process();
74  return _klPQ;
75  }
76 
77  template < typename GUM_SCALAR >
79  _process();
80  return _klQP;
81  }
82 
83  template < typename GUM_SCALAR >
85  _process();
86  return _hellinger;
87  }
88 
89  template < typename GUM_SCALAR >
91  _process();
92  return _bhattacharya;
93  }
94 
95  template < typename GUM_SCALAR >
97  _process();
98  return _jsd;
99  }
100 
101  template < typename GUM_SCALAR >
103  _process();
104  return _errorPQ;
105  }
106 
107  template < typename GUM_SCALAR >
109  _process();
110  return _errorQP;
111  }
112 
113  template < typename GUM_SCALAR >
115  return _p;
116  }
117 
118  template < typename GUM_SCALAR >
120  return _q;
121  }
122 
123  // check if the 2 BNs are compatible
124  template < typename GUM_SCALAR >
126  for (auto node : _p.nodes()) {
127  const DiscreteVariable& vp = _p.variable(node);
128 
129  try {
130  const DiscreteVariable& vq = _q.variableFromName(vp.name());
131 
132  if (vp.domainSize() != vq.domainSize())
134  "BNdistance : the 2 BNs are not compatible "
135  "(not the same domainSize for "
136  + vp.name() + ")");
137 
138  for (Idx i = 0; i < vp.domainSize(); i++) {
139  try {
140  vq[vp.label(i)];
141  vp[vq.label(i)];
142 
143  } catch (OutOfBounds&) {
145  "BNdistance : the 2 BNs are not compatible F(not the same "
146  "labels for "
147  + vp.name() + ")");
148  }
149  }
150  } catch (NotFound&) {
152  "BNdistance : the 2 BNs are not compatible (not the same vars : "
153  + vp.name() + ")");
154  }
155  }
156 
157  // should not be used
158  if (_p.size() != _q.size())
160  "BNdistance : the 2 BNs are not compatible (not the same size)");
161 
162  if (std::fabs(_p.log10DomainSize() - _q.log10DomainSize()) > 1e-14) {
163  GUM_ERROR(
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  }
172 
173  // do the job if not already __done
174  template < typename GUM_SCALAR >
176  if (!__done) {
177  _computeKL();
178  __done = true;
179  }
180  }
181 
182  // in order to keep BNdistance instantiable
183  template < typename GUM_SCALAR >
185  GUM_ERROR(OperationNotAllowed, "No default computations");
186  }
187 } // namespace gum
Useful macros for maths.
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:136
GUM_SCALAR _bhattacharya
Definition: BNdistance.h:139
bool __checkCompatibility() const
const IBayesNet< GUM_SCALAR > & _p
Definition: BNdistance.h:133
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Class representing Bayesian networks.
GUM_SCALAR _jsd
Definition: BNdistance.h:140
const IBayesNet< GUM_SCALAR > & p() const
Base class for discrete random variable.
algorithm for KL divergence between BNs
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:59
gum is the global namespace for all aGrUM entities
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:43
virtual Size domainSize() const =0
const IBayesNet< GUM_SCALAR > & _q
Definition: BNdistance.h:134
#define GAP_COMPLEXITY_KL_DIFFICULT_CORRECT
Definition: BNdistance.h:68
GUM_SCALAR _hellinger
Definition: BNdistance.h:138
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:137
virtual ~BNdistance()
destructor
Complexity __difficulty
Definition: BNdistance.h:147
Size Idx
Type for indexes.
Definition: types.h:50
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
#define GAP_COMPLEXITY_KL_HEAVY_DIFFICULT
Definition: BNdistance.h:67
const std::string & name() const
returns the name of the variable
double hellinger()
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52