aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
exactBNdistance_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
49
52#include <agrum/BN/IBayesNet.h>
53
55
56namespace gum {
57 template < typename GUM_SCALAR >
59 const IBayesNet< GUM_SCALAR >& Q) :
60 BNdistance< GUM_SCALAR >(P, Q) {
61 GUM_CONSTRUCTOR(ExactBNdistance);
62 }
63
64 template < typename GUM_SCALAR >
66 BNdistance< GUM_SCALAR >(kl) {
67 GUM_CONSTRUCTOR(ExactBNdistance);
68 }
69
70 template < typename GUM_SCALAR >
74
75 template < typename GUM_SCALAR >
77 klPQ_ = klQP_ = hellinger_ = bhattacharya_ = jsd_ = (GUM_SCALAR)0.0;
78 errorPQ_ = errorQP_ = 0;
79
80 auto Ip = p_.completeInstantiation();
81 auto Iq = q_.completeInstantiation();
82
83 // map between p_ variables and q_ variables (using name of vars)
85
86 for (Idx ite = 0; ite < Ip.nbrDim(); ++ite) {
87 map.insert(&Ip.variable(ite), &q_.variableFromName(Ip.variable(ite).name()));
88 }
89 GUM_SCALAR pp, pq, pmid, lpp, lpq, lpmid;
90 for (Ip.setFirst(); !Ip.end(); ++Ip) {
91 Iq.setValsFrom(map, Ip);
92 pp = p_.jointProbability(Ip);
93 pq = q_.jointProbability(Iq);
94 pmid = (pp + pq) / 2.0;
95 lpmid = GUM_LOG2_OR_0(pmid);
96 lpp = GUM_LOG2_OR_0(pp);
97 lpq = GUM_LOG2_OR_0(pq);
98
99
100 hellinger_ += std::pow(std::sqrt(pp) - std::sqrt(pq), 2);
101 bhattacharya_ += std::sqrt(pp * pq);
102
103 if (pp != (GUM_SCALAR)0.0) {
104 if (pq != (GUM_SCALAR)0.0) {
105 klPQ_ -= pp * (lpq - lpp); // log2(pq / pp);
106 } else {
107 errorPQ_++;
108 }
109 }
110
111 if (pq != (GUM_SCALAR)0.0) {
112 if (pp != (GUM_SCALAR)0.0) {
113 klQP_ -= pq * (lpp - lpq); // log2(pp / pq);
114 } else {
115 errorQP_++;
116 }
117 }
118 if (pmid != (GUM_SCALAR)0.0) {
119 jsd_ += pp * lpp + pq * lpq
120 - (pp + pq) * lpmid; // pp* log2(pp / pmid) + pq * log2(pq / pmid);
121 }
122 }
123 jsd_ /= 2.0;
124 hellinger_ = std::sqrt(hellinger_);
125 bhattacharya_ = -std::log(bhattacharya_);
126 }
127
128} // namespace gum
algorithm for KL divergence between BNs
Class representing the minimal interface for Bayesian network with no numerical data.
GUM_SCALAR hellinger_
Definition BNdistance.h:165
GUM_SCALAR klPQ_
Definition BNdistance.h:159
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
GUM_SCALAR jsd_
Definition BNdistance.h:167
GUM_SCALAR klQP_
Definition BNdistance.h:160
GUM_SCALAR bhattacharya_
Definition BNdistance.h:166
const IBayesNet< GUM_SCALAR > & q_
Definition BNdistance.h:157
const IBayesNet< GUM_SCALAR > & p_
Definition BNdistance.h:156
ExactBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
virtual ~ExactBNdistance()
destructor
The class for generic Hash Tables.
Definition hashTable.h:637
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
algorithm for exact computation KL divergence between BNs
Size Idx
Type for indexes.
Definition types.h:79
Useful macros for maths.
#define GUM_LOG2_OR_0(x)
Definition math_utils.h:70
gum is the global namespace for all aGrUM entities
Definition agrum.h:46