aGrUM
3.3.2
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
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
51
#include <
agrum/BN/algorithms/divergence/BNdistance.h
>
52
#include <
agrum/BN/algorithms/divergence/exactBNdistance.h
>
53
#include <
agrum/BN/IBayesNet.h
>
54
55
#include <
agrum/base/core/math/math_utils.h
>
56
57
namespace
gum
{
58
template
< GUM_Numeric GUM_SCALAR >
59
ExactBNdistance< GUM_SCALAR >::ExactBNdistance
(
const
IBayesNet< GUM_SCALAR >
& P,
60
const
IBayesNet< GUM_SCALAR >
& Q) :
61
BNdistance
< GUM_SCALAR >(P, Q) {
62
GUM_CONSTRUCTOR(
ExactBNdistance
);
63
}
64
65
template
< GUM_Numeric GUM_SCALAR >
66
ExactBNdistance< GUM_SCALAR >::ExactBNdistance
(
const
BNdistance< GUM_SCALAR >
& kl) :
67
BNdistance
< GUM_SCALAR >(kl) {
68
GUM_CONSTRUCTOR(
ExactBNdistance
);
69
}
70
71
template
< GUM_Numeric GUM_SCALAR >
72
ExactBNdistance< GUM_SCALAR >::~ExactBNdistance
() {
73
GUM_DESTRUCTOR(
ExactBNdistance
);
74
}
75
76
template
< GUM_Numeric GUM_SCALAR >
77
void
ExactBNdistance< GUM_SCALAR >::computeKL_
() {
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)
85
HashTable< const DiscreteVariable*, const DiscreteVariable* >
map;
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
BNdistance.h
algorithm for KL divergence between BNs
IBayesNet.h
Class representing the minimal interface for Bayesian network with no numerical data.
gum::BNdistance::hellinger_
GUM_SCALAR hellinger_
Definition
BNdistance.h:165
gum::BNdistance::klPQ_
GUM_SCALAR klPQ_
Definition
BNdistance.h:159
gum::BNdistance::BNdistance
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Definition
BNdistance_tpl.h:60
gum::BNdistance::jsd_
GUM_SCALAR jsd_
Definition
BNdistance.h:167
gum::BNdistance::klQP_
GUM_SCALAR klQP_
Definition
BNdistance.h:160
gum::BNdistance::errorPQ_
Size errorPQ_
Definition
BNdistance.h:162
gum::BNdistance::errorQP_
Size errorQP_
Definition
BNdistance.h:163
gum::BNdistance::bhattacharya_
GUM_SCALAR bhattacharya_
Definition
BNdistance.h:166
gum::BNdistance::q_
const IBayesNet< GUM_SCALAR > & q_
Definition
BNdistance.h:157
gum::BNdistance::p_
const IBayesNet< GUM_SCALAR > & p_
Definition
BNdistance.h:156
gum::ExactBNdistance::ExactBNdistance
ExactBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Definition
exactBNdistance_tpl.h:59
gum::ExactBNdistance::~ExactBNdistance
~ExactBNdistance() override
destructor
Definition
exactBNdistance_tpl.h:72
gum::ExactBNdistance::computeKL_
void computeKL_() final
Definition
exactBNdistance_tpl.h:77
gum::HashTable
The class for generic Hash Tables.
Definition
hashTable.h:640
gum::HashTable::insert
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Definition
hashTable_tpl.h:754
gum::IBayesNet
Class representing the minimal interface for Bayesian network with no numerical data.
Definition
IBayesNet.h:79
exactBNdistance.h
algorithm for exact computation KL divergence between BNs
gum::Idx
Size Idx
Type for indexes.
Definition
types.h:79
math_utils.h
Useful macros for maths.
GUM_LOG2_OR_0
#define GUM_LOG2_OR_0(x)
Definition
math_utils.h:72
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
aGrUM
3.3.2
© PHW&CG&others - 2022
DoXyGeN 1.18.0