aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
MCBNDistance_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
51
55#include <agrum/BN/IBayesNet.h>
56
58
59namespace gum {
60
61 template < GUM_Numeric GUM_SCALAR >
63 const IBayesNet< GUM_SCALAR >& Q) :
64 BNdistance< GUM_SCALAR >(P, Q), ApproximationScheme() {
65 GUM_CONSTRUCTOR(MCBNDistance);
66
67 setFromConfiguration_("mcKL", true);
68 }
69
70 template < GUM_Numeric GUM_SCALAR >
72 BNdistance< GUM_SCALAR >(kl), ApproximationScheme() {
73 GUM_CONSTRUCTOR(MCBNDistance);
74
75 setFromConfiguration_("mcKL", true);
76 }
77
78 template < GUM_Numeric GUM_SCALAR >
82
83 template < GUM_Numeric GUM_SCALAR >
86
87 for (const auto nod: topoOrder) {
88 I.add(p_.variable(nod));
89 gum::Instantiation Itop(I);
90 Itop.erase(p_.variable(nod));
91 I.chgVal(p_.variable(nod), p_.cpt(nod).extract(Itop).draw());
92 }
93 return I;
94 }
95
96 template < GUM_Numeric GUM_SCALAR >
98 auto Iq = q_.completeInstantiation();
99 const auto topoOrder = p_.topologicalOrder();
101
102 // map between drawSample_() variables and q_ variables (using name of vars)
104 {
105 auto Ip = p_.completeInstantiation();
106 for (Idx ite = 0; ite < Ip.nbrDim(); ++ite) {
107 map.insert(&Ip.variable(ite), &q_.variableFromName(Ip.variable(ite).name()));
108 }
109 }
110
111 klPQ_ = klQP_ = hellinger_ = jsd_ = (GUM_SCALAR)0.0;
112 errorPQ_ = errorQP_ = 0;
113 GUM_SCALAR delta, ratio, error;
114 delta = ratio = error = (GUM_SCALAR)-1;
115 GUM_SCALAR oldPQ = 0.0;
116 GUM_SCALAR pp, pq, pmid;
117
118 do {
119 this->disableMinEpsilonRate();
120 gum::Instantiation I = drawSample_(topoOrder);
122
123 Iq.setValsFrom(map, I);
124
125 pp = p_.jointProbability(I);
126 pq = q_.jointProbability(Iq);
127 pmid = (pp + pq) / 2.0;
128
129 if (pp != (GUM_SCALAR)0.0) {
130 hellinger_ += std::pow(std::sqrt(pp) - std::sqrt(pq), 2) / pp;
131
132 if (pq != (GUM_SCALAR)0.0) {
133 bhattacharya_ += std::sqrt(pq / pp);
134 this->enableMinEpsilonRate();
135 ratio = pq / pp;
136 delta = (GUM_SCALAR)std::log2(ratio);
137 klPQ_ += delta;
138
139 // pmid!=0
140 jsd_ -= std::log2(pp / pmid) + ratio * std::log2(pq / pmid);
141 } else {
142 errorPQ_++;
143 }
144 }
145
146 if (pq != (GUM_SCALAR)0.0) {
147 if (pp != (GUM_SCALAR)0.0) {
148 // if we are here, it is certain that delta and ratio have been
149 // computed further lines above.
150 klQP_ += (GUM_SCALAR)(-delta * ratio);
151 } else {
152 errorQP_++;
153 }
154 }
155
156 if (this->isEnabledMinEpsilonRate()) {
157 // delta is used as a temporary variable
158 delta = klPQ_ / nbrIterations();
159 error = (GUM_SCALAR)std::abs(delta - oldPQ);
160 oldPQ = delta;
161 }
162 } while (continueApproximationScheme(error));
163
164 klPQ_ = -klPQ_ / (nbrIterations());
165 klQP_ = -klQP_ / (nbrIterations());
166 jsd_ = -0.5 * jsd_ / (nbrIterations());
167 hellinger_ = std::sqrt(hellinger_ / nbrIterations());
168 bhattacharya_ = -std::log(bhattacharya_ / (nbrIterations()));
169 }
170} // namespace gum
Class representing the minimal interface for Bayesian network with no numerical data.
algorithm for approximated computation KL divergence between BNs using independent Monte Carlo sampli...
This file contains general scheme for iteratively convergent algorithms.
ApproximationScheme()
Build a scheme whose parameters are read from the configuration.
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.
void setFromConfiguration_(const std::string &section, bool withMaxTime, bool withVerbosity=true)
Set the parameters of the scheme from a section of gum::Configuration.
bool isEnabledMinEpsilonRate() const override
Returns true if stopping criterion on epsilon rate is enabled, false otherwise.
void disableMinEpsilonRate() override
Disable stopping criterion on epsilon rate.
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
void initApproximationScheme()
Initialise the scheme.
Size nbrIterations() const override
Returns the number of iterations.
void enableMinEpsilonRate() override
Enable stopping criterion on epsilon rate.
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
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:79
Class for assigning/browsing values to tuples of discrete variables.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
void erase(const DiscreteVariable &v) final
Removes a variable from the Instantiation.
void computeKL_() final
Instantiation drawSample_(const Sequence< NodeId > &topoOrder) const
draws an instantiation of p_'s variables, independently, following topoOrder (no evidence to handle i...
MCBNDistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
~MCBNDistance() override
destructor
Size Idx
Type for indexes.
Definition types.h:79
Class hash tables iterators.
Useful macros for maths.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46