aGrUM  0.13.2
bruteForceKL_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 
29 #include <agrum/BN/IBayesNet.h>
32 
33 namespace gum {
34  template < typename GUM_SCALAR >
36  const IBayesNet< GUM_SCALAR >& Q) :
37  KL< GUM_SCALAR >(P, Q) {
38  GUM_CONSTRUCTOR(BruteForceKL);
39  }
40 
41  template < typename GUM_SCALAR >
43  KL< GUM_SCALAR >(kl) {
44  GUM_CONSTRUCTOR(BruteForceKL);
45  }
46 
47  template < typename GUM_SCALAR >
49  GUM_DESTRUCTOR(BruteForceKL);
50  }
51 
52  template < typename GUM_SCALAR >
54  _klPQ = _klQP = _hellinger = _bhattacharya = (GUM_SCALAR)0.0;
55  _errorPQ = _errorQP = 0;
56 
57  auto Ip = _p.completeInstantiation();
58  auto Iq = _q.completeInstantiation();
59 
60  // map between _p variables and _q variables (using name of vars)
62 
63  for (Idx ite = 0; ite < Ip.nbrDim(); ++ite) {
64  map.insert(&Ip.variable(ite), &_q.variableFromName(Ip.variable(ite).name()));
65  }
66 
67  for (Ip.setFirst(); !Ip.end(); ++Ip) {
68  Iq.setValsFrom(map, Ip);
69  GUM_SCALAR pp = _p.jointProbability(Ip);
70  GUM_SCALAR pq = _q.jointProbability(Iq);
71 
72  _hellinger += std::pow(std::sqrt(pp) - std::sqrt(pq), 2);
73  _bhattacharya += std::sqrt(pp * pq);
74 
75  if (pp != (GUM_SCALAR)0.0) {
76  if (pq != (GUM_SCALAR)0.0) {
77  _klPQ -= pp * log2(pq / pp);
78  } else {
79  _errorPQ++;
80  }
81  }
82 
83  if (pq != (GUM_SCALAR)0.0) {
84  if (pp != (GUM_SCALAR)0.0) {
85  _klQP -= pq * log2(pp / pq);
86  } else {
87  _errorQP++;
88  }
89  }
90  }
91 
92  _hellinger = std::sqrt(_hellinger);
93  _bhattacharya = -std::log(_bhattacharya);
94  }
95 
96 } // namespace gum
algorithm for KL divergence between BNs
GUM_SCALAR _klPQ
Definition: KL.h:132
virtual ~BruteForceKL()
destructor
const IBayesNet< GUM_SCALAR > & _p
Definition: KL.h:129
GUM_SCALAR _bhattacharya
Definition: KL.h:135
BruteForceKL(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Class representing Bayesian networks.
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
The class for generic Hash Tables.
Definition: hashTable.h:676
const IBayesNet< GUM_SCALAR > & _q
Definition: KL.h:130
BruteForceKL computes exactly the KL divergence betweens 2 BNs.
Definition: bruteForceKL.h:66
KL is the base class for KL computation betweens 2 BNs.
Definition: KL.h:65
Size _errorPQ
Definition: KL.h:137
void _computeKL() final
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
unsigned long Idx
Type for indexes.
Definition: types.h:43
GUM_SCALAR _klQP
Definition: KL.h:133
algorithm for exact computation KL divergence between BNs
Size _errorQP
Definition: KL.h:138
GUM_SCALAR _hellinger
Definition: KL.h:134