aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
BNdistance_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 <complex>
52
54#include <agrum/BN/IBayesNet.h>
55
57
58namespace gum {
59 template < GUM_Numeric GUM_SCALAR >
61 const IBayesNet< GUM_SCALAR >& Q) :
62 p_(P), q_(Q), klPQ_(0.0), klQP_(0.0), errorPQ_(0), errorQP_(0),
64 _checkCompatibility_(); // may throw OperationNotAllowed
65 GUM_CONSTRUCTOR(BNdistance);
66
67 double diff = p_.log10DomainSize();
68
72 }
73
74 template < GUM_Numeric GUM_SCALAR >
80
81 template < GUM_Numeric GUM_SCALAR >
85
86 template < GUM_Numeric GUM_SCALAR >
90
91 template < GUM_Numeric GUM_SCALAR >
93 process_();
94 return klPQ_;
95 }
96
97 template < GUM_Numeric GUM_SCALAR >
99 process_();
100 return klQP_;
101 }
102
103 template < GUM_Numeric GUM_SCALAR >
105 process_();
106 return hellinger_;
107 }
108
109 template < GUM_Numeric GUM_SCALAR >
114
115 template < GUM_Numeric GUM_SCALAR >
117 process_();
118 return jsd_;
119 }
120
121 template < GUM_Numeric GUM_SCALAR >
126
127 template < GUM_Numeric GUM_SCALAR >
132
133 template < GUM_Numeric GUM_SCALAR >
135 return p_;
136 }
137
138 template < GUM_Numeric GUM_SCALAR >
140 return q_;
141 }
142
143 // check if the 2 BNs are compatible
144 template < GUM_Numeric GUM_SCALAR >
146 // should not be used
147 if (p_.size() != q_.size())
149 "BNdistance : the 2 BNs are not compatible (not the same size)")
150
151 for (auto node: p_.nodes()) {
152 const DiscreteVariable& vp = p_.variable(node);
153 if (!q_.exists(vp.name())) {
155 "BNdistance : the 2 BNs are not compatible (variable : " + vp.name() + ")");
156 }
157 const DiscreteVariable& vq = q_.variableFromName(vp.name());
158 if (vp != vq)
160 "BNdistance : the 2 BNs are not compatible "
161 "(not the same variable for the same name : "
162 + vp.toString() + "and " + vq.toString() + ")");
163 }
164
165 if (std::fabs(p_.log10DomainSize() - q_.log10DomainSize()) > 1e-14) {
167 "BNdistance : the 2 BNs are not compatible (not the same domainSize) : p="
168 << p_.log10DomainSize() << " q=" << q_.log10DomainSize() << " => "
169 << p_.log10DomainSize() - q_.log10DomainSize());
170 }
171
172 return true;
173 }
174
175 // do the job if not already _done_
176 template < GUM_Numeric GUM_SCALAR >
178 if (!_done_) {
179 computeKL_();
180 _done_ = true;
181 }
182 }
183
184 // in order to keep BNdistance instantiable
185 template < GUM_Numeric GUM_SCALAR >
187 GUM_ERROR(OperationNotAllowed, "No default computations")
188 }
189} // namespace gum
algorithm for KL divergence between BNs
#define GAP_COMPLEXITY_KL_DIFFICULT_CORRECT
Definition BNdistance.h:89
#define GAP_COMPLEXITY_KL_HEAVY_DIFFICULT
Definition BNdistance.h:88
Class representing the minimal interface for Bayesian network with no numerical data.
GUM_SCALAR hellinger_
Definition BNdistance.h:165
const IBayesNet< GUM_SCALAR > & q() const
GUM_SCALAR klPQ_
Definition BNdistance.h:159
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Complexity _difficulty_
Definition BNdistance.h:171
virtual void computeKL_()
GUM_SCALAR jsd_
Definition BNdistance.h:167
const IBayesNet< GUM_SCALAR > & p() const
GUM_SCALAR klQP_
Definition BNdistance.h:160
bool _checkCompatibility_() const
virtual ~BNdistance()
destructor
GUM_SCALAR bhattacharya_
Definition BNdistance.h:166
const IBayesNet< GUM_SCALAR > & q_
Definition BNdistance.h:157
Complexity difficulty() const
return KL::Complexity::Heavy,KL::Complexity::Difficult,KL::Complexity::Correct depending on the BNs p...
const IBayesNet< GUM_SCALAR > & p_
Definition BNdistance.h:156
Base class for discrete random variable.
std::string toString() const
string version of *this
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
Exception : operation not allowed.
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Useful macros for maths.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Complexity
Complexity allows to characterize the awaited difficulty for an algorithm given a specific instance T...
Definition BNdistance.h:64