aGrUM 3.0.0
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-2026 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-2026 *
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
41#pragma once
42
43
50
53#include <agrum/BN/IBayesNet.h>
54
56
57namespace gum {
58 template < GUM_Numeric GUM_SCALAR >
60 const IBayesNet< GUM_SCALAR >& Q) :
61 BNdistance< GUM_SCALAR >(P, Q) {
62 GUM_CONSTRUCTOR(ExactBNdistance);
63 }
64
65 template < GUM_Numeric GUM_SCALAR >
67 BNdistance< GUM_SCALAR >(kl) {
68 GUM_CONSTRUCTOR(ExactBNdistance);
69 }
70
71 template < GUM_Numeric GUM_SCALAR >
75
76 template < GUM_Numeric GUM_SCALAR >
78 klPQ_ = klQP_ = hellinger_ = bhattacharya_ = jsd_ = (GUM_SCALAR)0.0;
79 errorPQ_ = errorQP_ = 0;
80
81 auto Ip = p_.completeInstantiation();
82 auto Iq = q_.completeInstantiation();
83
84 // map between p_ variables and q_ variables (using name of vars)
86
87 for (Idx ite = 0; ite < Ip.nbrDim(); ++ite) {
88 map.insert(&Ip.variable(ite), &q_.variableFromName(Ip.variable(ite).name()));
89 }
90 GUM_SCALAR pp, pq, pmid, lpp, lpq, lpmid;
91 for (Ip.setFirst(); !Ip.end(); ++Ip) {
92 Iq.setValsFrom(map, Ip);
93 pp = p_.jointProbability(Ip);
94 pq = q_.jointProbability(Iq);
95 pmid = (pp + pq) / 2.0;
96 lpmid = GUM_LOG2_OR_0(pmid);
97 lpp = GUM_LOG2_OR_0(pp);
98 lpq = GUM_LOG2_OR_0(pq);
99
100
101 hellinger_ += std::pow(std::sqrt(pp) - std::sqrt(pq), 2);
102 bhattacharya_ += std::sqrt(pp * pq);
103
104 if (pp != (GUM_SCALAR)0.0) {
105 if (pq != (GUM_SCALAR)0.0) {
106 klPQ_ -= pp * (lpq - lpp); // log2(pq / pp);
107 } else {
108 errorPQ_++;
109 }
110 }
111
112 if (pq != (GUM_SCALAR)0.0) {
113 if (pp != (GUM_SCALAR)0.0) {
114 klQP_ -= pq * (lpp - lpq); // log2(pp / pq);
115 } else {
116 errorQP_++;
117 }
118 }
119 if (pmid != (GUM_SCALAR)0.0) {
120 jsd_ += pp * lpp + pq * lpq
121 - (pp + pq) * lpmid; // pp* log2(pp / pmid) + pq * log2(pq / pmid);
122 }
123 }
124 jsd_ /= 2.0;
125 hellinger_ = std::sqrt(hellinger_);
126 bhattacharya_ = -std::log(bhattacharya_);
127 }
128
129} // 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
~ExactBNdistance() override
destructor
The class for generic Hash Tables.
Definition hashTable.h:640
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