aGrUM  0.13.2
KL_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 <cmath>
28 #include <complex>
29 
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(KL);
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(KL);
59  }
60 
61  template < typename GUM_SCALAR >
63  GUM_DESTRUCTOR(KL);
64  }
65 
66  template < typename GUM_SCALAR >
68  return __difficulty;
69  }
70 
71  template < typename GUM_SCALAR >
72  INLINE double KL< GUM_SCALAR >::klPQ() {
73  _process();
74  return _klPQ;
75  }
76 
77  template < typename GUM_SCALAR >
78  INLINE double KL< GUM_SCALAR >::klQP() {
79  _process();
80  return _klQP;
81  }
82 
83  template < typename GUM_SCALAR >
84  INLINE double KL< GUM_SCALAR >::hellinger() {
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 _errorPQ;
99  }
100 
101  template < typename GUM_SCALAR >
103  _process();
104  return _errorQP;
105  }
106 
107  template < typename GUM_SCALAR >
109  return _p;
110  }
111 
112  template < typename GUM_SCALAR >
114  return _q;
115  }
116 
117  // check if the 2 BNs are compatible
118  template < typename GUM_SCALAR >
120  for (auto node : _p.nodes()) {
121  const DiscreteVariable& vp = _p.variable(node);
122 
123  try {
124  const DiscreteVariable& vq = _q.variableFromName(vp.name());
125 
126  if (vp.domainSize() != vq.domainSize())
128  "KL : the 2 BNs are not compatible "
129  "(not the same domainSize for "
130  + vp.name() + ")");
131 
132  for (Idx i = 0; i < vp.domainSize(); i++) {
133  try {
134  vq[vp.label(i)];
135  vp[vq.label(i)];
136 
137  } catch (OutOfBounds&) {
138  GUM_ERROR(
140  "KL : the 2 BNs are not compatible F(not the same labels for "
141  + vp.name() + ")");
142  }
143  }
144  } catch (NotFound&) {
146  "KL : the 2 BNs are not compatible (not the same vars : "
147  + vp.name() + ")");
148  }
149  }
150 
151  // should not be used
152  if (_p.size() != _q.size())
154  "KL : the 2 BNs are not compatible (not the same size)");
155 
156  if (std::fabs(_p.log10DomainSize() - _q.log10DomainSize()) > 1e-14) {
158  "KL : the 2 BNs are not compatible (not the same domainSize) : p="
159  << _p.log10DomainSize() << " q=" << _q.log10DomainSize()
160  << " => " << _p.log10DomainSize() - _q.log10DomainSize());
161  }
162 
163  return true;
164  }
165 
166  // do the job if not already __done
167  template < typename GUM_SCALAR >
169  if (!__done) {
170  _computeKL();
171  __done = true;
172  }
173  }
174 
175  // in order to keep KL instantiable
176  template < typename GUM_SCALAR >
178  GUM_ERROR(OperationNotAllowed, "No default computations");
179  }
180 } // namespace gum
void _process()
Definition: KL_tpl.h:168
algorithm for KL divergence between BNs
GUM_SCALAR _klPQ
Definition: KL.h:132
Complexity __difficulty
Definition: KL.h:142
const IBayesNet< GUM_SCALAR > & p() const
Definition: KL_tpl.h:108
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
#define GAP_COMPLEXITY_KL_HEAVY_DIFFICULT
Definition: KL.h:67
const IBayesNet< GUM_SCALAR > & _p
Definition: KL.h:129
double klPQ()
Definition: KL_tpl.h:72
GUM_SCALAR _bhattacharya
Definition: KL.h:135
Class representing Bayesian networks.
#define GAP_COMPLEXITY_KL_DIFFICULT_CORRECT
Definition: KL.h:68
Base class for discrete random variable.
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
const IBayesNet< GUM_SCALAR > & q() const
Definition: KL_tpl.h:113
Complexity
Complexity allows to characterize the awaited difficulty for an algorithm given a specific instance T...
Definition: KL.h:43
virtual Size domainSize() const =0
const IBayesNet< GUM_SCALAR > & _q
Definition: KL.h:130
bool __done
Definition: KL.h:143
double bhattacharya()
Definition: KL_tpl.h:90
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.
bool __checkCompatibility() const
Definition: KL_tpl.h:119
KL is the base class for KL computation betweens 2 BNs.
Definition: KL.h:65
double hellinger()
Definition: KL_tpl.h:84
Size _errorPQ
Definition: KL.h:137
Size errorPQ()
Definition: KL_tpl.h:96
virtual ~KL()
destructor
Definition: KL_tpl.h:62
Complexity difficulty() const
return KL::Complexity::Heavy,KL::Complexity::Difficult,KL::Complexity::Correct depending on the BNs _...
Definition: KL_tpl.h:67
double klQP()
Definition: KL_tpl.h:78
Size errorQP()
Definition: KL_tpl.h:102
unsigned long Idx
Type for indexes.
Definition: types.h:43
KL(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Definition: KL_tpl.h:35
const std::string & name() const
returns the name of the variable
GUM_SCALAR _klQP
Definition: KL.h:133
virtual void _computeKL()
Definition: KL_tpl.h:177
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
Size _errorQP
Definition: KL.h:138
GUM_SCALAR _hellinger
Definition: KL.h:134