aGrUM  0.14.2
exactBNdistance_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 <agrum/core/math/math.h>
28 #include <agrum/BN/IBayesNet.h>
31 
32 namespace gum {
33  template < typename GUM_SCALAR >
36  BNdistance< GUM_SCALAR >(P, Q) {
37  GUM_CONSTRUCTOR(ExactBNdistance);
38  }
39 
40  template < typename GUM_SCALAR >
42  const BNdistance< GUM_SCALAR >& kl) :
43  BNdistance< GUM_SCALAR >(kl) {
44  GUM_CONSTRUCTOR(ExactBNdistance);
45  }
46 
47  template < typename GUM_SCALAR >
49  GUM_DESTRUCTOR(ExactBNdistance);
50  }
51 
52  template < typename GUM_SCALAR >
54  _klPQ = _klQP = _hellinger = _bhattacharya = _jsd = (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  GUM_SCALAR pp, pq, pmid, lpp, lpq, lpmid;
67  for (Ip.setFirst(); !Ip.end(); ++Ip) {
68  Iq.setValsFrom(map, Ip);
69  pp = _p.jointProbability(Ip);
70  pq = _q.jointProbability(Iq);
71  pmid = (pp + pq) / 2.0;
72  lpmid = lpq = lpp = (GUM_SCALAR)0.0;
73  if (pmid != (GUM_SCALAR)0.0) lpmid = log2(pmid);
74  if (pp != (GUM_SCALAR)0.0) lpp = log2(pp);
75  if (pq != (GUM_SCALAR)0.0) lpq = log2(pq);
76 
77 
78  _hellinger += std::pow(std::sqrt(pp) - std::sqrt(pq), 2);
79  _bhattacharya += std::sqrt(pp * pq);
80 
81  if (pp != (GUM_SCALAR)0.0) {
82  if (pq != (GUM_SCALAR)0.0) {
83  _klPQ -= pp * (lpq - lpp); // log2(pq / pp);
84  } else {
85  _errorPQ++;
86  }
87  }
88 
89  if (pq != (GUM_SCALAR)0.0) {
90  if (pp != (GUM_SCALAR)0.0) {
91  _klQP -= pq * (lpp - lpq); // log2(pp / pq);
92  } else {
93  _errorQP++;
94  }
95  }
96  if (pmid != (GUM_SCALAR)0.0) {
97  _jsd +=
98  pp * lpp + pq * lpq
99  - (pp + pq) * lpmid; // pp* log2(pp / pmid) + pq * log2(pq / pmid);
100  }
101  }
102  _jsd /= 2.0;
103  _hellinger = std::sqrt(_hellinger);
104  _bhattacharya = -std::log(_bhattacharya);
105  }
106 
107 } // namespace gum
Useful macros for maths.
virtual ~ExactBNdistance()
destructor
GUM_SCALAR _klPQ
Definition: BNdistance.h:136
GUM_SCALAR _bhattacharya
Definition: BNdistance.h:139
const IBayesNet< GUM_SCALAR > & _p
Definition: BNdistance.h:133
Class representing Bayesian networks.
GUM_SCALAR _jsd
Definition: BNdistance.h:140
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
The class for generic Hash Tables.
Definition: hashTable.h:676
const IBayesNet< GUM_SCALAR > & _q
Definition: BNdistance.h:134
GUM_SCALAR _hellinger
Definition: BNdistance.h:138
algorithm for exact computation KL divergence between BNs
ExactBNdistance computes exactly the KL divergence betweens 2 BNs.
ExactBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
GUM_SCALAR _klQP
Definition: BNdistance.h:137
Size Idx
Type for indexes.
Definition: types.h:50
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.