aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
BNdistance.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
41
49#ifndef GUM_KL_H
50#define GUM_KL_H
51
52#include <agrum/BN/IBayesNet.h>
53
54namespace gum {
55
64 enum class Complexity : char { Heavy, Difficult, Correct };
65
85 template < typename GUM_SCALAR >
86 class BNdistance {
87// difficulty is chosen w.r.t the log10DomainSize of the BN
88#define GAP_COMPLEXITY_KL_HEAVY_DIFFICULT double(12.0)
89#define GAP_COMPLEXITY_KL_DIFFICULT_CORRECT double(7.0)
90
91 public:
97 BNdistance(const IBayesNet< GUM_SCALAR >& P, const IBayesNet< GUM_SCALAR >& Q);
98
102
104 virtual ~BNdistance();
105
111 Complexity difficulty() const;
112
117
119 double klPQ();
120
122 Size errorPQ();
123
125 double klQP();
126
128 Size errorQP();
129
132 double hellinger();
133
136 double bhattacharya();
137
140 double jsd();
141
143 const IBayesNet< GUM_SCALAR >& p() const;
144
146 const IBayesNet< GUM_SCALAR >& q() const;
148
149 protected:
150 // should be pure virtual but using BNdistance directly is a way to delay the
151 // choice between different computation scheme (@see ExactBNdistance)
152 virtual void computeKL_();
153
154 void process_();
155
158
159 GUM_SCALAR klPQ_;
160 GUM_SCALAR klQP_;
161
164
165 GUM_SCALAR hellinger_;
166 GUM_SCALAR bhattacharya_;
167 GUM_SCALAR jsd_;
168
169 private:
170 bool _checkCompatibility_() const;
172 bool _done_;
173 };
174
175
176#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
177
178 extern template class BNdistance< double >;
179
180#endif
181
182} // namespace gum
183
185
186#endif // GUM_KL_H
KL divergence between BNs implementation.
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
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
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