aGrUM 2.3.2
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-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
50#include <complex>
51
53#include <agrum/BN/IBayesNet.h>
54
56
57namespace gum {
58 template < typename GUM_SCALAR >
60 const IBayesNet< GUM_SCALAR >& Q) :
61 p_(P), q_(Q), klPQ_(0.0), klQP_(0.0), errorPQ_(0), errorQP_(0),
63 _checkCompatibility_(); // may throw OperationNotAllowed
64 GUM_CONSTRUCTOR(BNdistance);
65
66 double diff = p_.log10DomainSize();
67
71 }
72
73 template < typename GUM_SCALAR >
79
80 template < typename GUM_SCALAR >
84
85 template < typename GUM_SCALAR >
89
90 template < typename GUM_SCALAR >
92 process_();
93 return klPQ_;
94 }
95
96 template < typename GUM_SCALAR >
98 process_();
99 return klQP_;
100 }
101
102 template < typename GUM_SCALAR >
104 process_();
105 return hellinger_;
106 }
107
108 template < typename GUM_SCALAR >
110 process_();
111 return bhattacharya_;
112 }
113
114 template < typename GUM_SCALAR >
116 process_();
117 return jsd_;
118 }
119
120 template < typename GUM_SCALAR >
122 process_();
123 return errorPQ_;
124 }
125
126 template < typename GUM_SCALAR >
128 process_();
129 return errorQP_;
130 }
131
132 template < typename GUM_SCALAR >
134 return p_;
135 }
136
137 template < typename GUM_SCALAR >
139 return q_;
140 }
141
142 // check if the 2 BNs are compatible
143 template < typename GUM_SCALAR >
145 // should not be used
146 if (p_.size() != q_.size())
148 "BNdistance : the 2 BNs are not compatible (not the same size)")
149
150 for (auto node: p_.nodes()) {
151 const DiscreteVariable& vp = p_.variable(node);
152 try {
153 const DiscreteVariable& vq = q_.variableFromName(vp.name());
154 if (vp != vq)
156 "BNdistance : the 2 BNs are not compatible "
157 "(not the same variable for the same name : "
158 + vp.toString() + "and " + vq.toString() + ")");
159 } catch (NotFound const&) {
161 "BNdistance : the 2 BNs are not compatible (variable : " + vp.name() + ")");
162 }
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 < typename GUM_SCALAR >
178 if (!_done_) {
179 computeKL_();
180 _done_ = true;
181 }
182 }
183
184 // in order to keep BNdistance instantiable
185 template < typename 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
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
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
const IBayesNet< GUM_SCALAR > & p() const
const IBayesNet< GUM_SCALAR > & q() const
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 : the element we looked for cannot be found.
Exception : operation not allowed.
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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