aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
correctedMutualInformation_inl.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
52#include <agrum/BN/learning/correctedMutualInformation.h> // to ease IDE parser
53#ifndef DOXYGEN_SHOULD_SKIP_THIS
54
55namespace gum {
56
57 namespace learning {
58
60 INLINE void CorrectedMutualInformation::useCache(bool on_off) {
61 useICache(on_off);
62 useHCache(on_off);
63 useKCache(on_off);
64 useCnrCache(on_off);
65 }
66
68 INLINE void CorrectedMutualInformation::useICache(bool on_off) {
69 if (!on_off) _ICache_.clear();
70 _use_ICache_ = on_off;
71 }
72
74 INLINE void CorrectedMutualInformation::useHCache(bool on_off) {
75 if (!on_off) _NH_.clearCache();
76 _use_HCache_ = on_off;
77 _NH_.useCache(on_off);
78 }
79
81 INLINE void CorrectedMutualInformation::useKCache(bool on_off) {
82 if (!on_off) _KCache_.clear();
83 _use_KCache_ = on_off;
84 }
85
87 INLINE void CorrectedMutualInformation::useCnrCache(bool on_off) {
88 if (!on_off) _k_NML_.clearCache();
89 _use_CnrCache_ = on_off;
90 _k_NML_.useCache(on_off);
91 }
92
95 _NH_.clear();
96 _k_NML_.clear();
97 _score_MDL_.clear();
98 clearCache();
99 }
100
103 _NH_.clearCache();
104 _k_NML_.clearCache();
105 _ICache_.clear();
106 _KCache_.clear();
107 }
108
110 INLINE void CorrectedMutualInformation::clearICache() { _ICache_.clear(); }
111
113 INLINE void CorrectedMutualInformation::clearHCache() { _NH_.clearCache(); }
114
116 INLINE void CorrectedMutualInformation::clearKCache() { _KCache_.clear(); }
117
119 INLINE void CorrectedMutualInformation::clearCnrCache() { _k_NML_.clearCache(); }
120
123 _NH_.setNumberOfThreads(nb);
124 _k_NML_.setNumberOfThreads(nb);
125 _score_MDL_.setNumberOfThreads(nb);
126 }
127
130 return _NH_.getNumberOfThreads();
131 }
132
135 return _NH_.isGumNumberOfThreadsOverriden();
136 }
137
140 INLINE void CorrectedMutualInformation::setMinNbRowsPerThread(const std::size_t nb) const {
141 _NH_.setMinNbRowsPerThread(nb);
142 _k_NML_.setMinNbRowsPerThread(nb);
143 _score_MDL_.setMinNbRowsPerThread(nb);
144 }
145
147 INLINE std::size_t CorrectedMutualInformation::minNbRowsPerThread() const {
148 return _NH_.minNbRowsPerThread();
149 }
150
152 INLINE const std::vector< std::pair< std::size_t, std::size_t > >&
154 return _NH_.ranges();
155 }
156
159 clearCache();
160 _kmode_ = KModeTypes::MDL;
161 }
162
165 clearCache();
166 _kmode_ = KModeTypes::NML;
167 }
168
171 clearCache();
172 _kmode_ = KModeTypes::NoCorr;
173 }
174
176 INLINE double CorrectedMutualInformation::score(NodeId var1, NodeId var2) {
177 return score(var1, var2, _empty_conditioning_set_);
178 }
179
181 INLINE double CorrectedMutualInformation::score(NodeId var1,
182 NodeId var2,
183 const std::vector< NodeId >& conditioning_ids) {
184 return _NI_score_(var1, var2, conditioning_ids) - _K_score_(var1, var2, conditioning_ids);
185 }
186
188 INLINE double CorrectedMutualInformation::score(NodeId var1, NodeId var2, NodeId var3) {
189 return score(var1, var2, var3, _empty_conditioning_set_);
190 }
191
193 INLINE double CorrectedMutualInformation::score(NodeId var1,
194 NodeId var2,
195 NodeId var3,
196 const std::vector< NodeId >& conditioning_ids) {
197 return _NI_score_(var1, var2, var3, conditioning_ids)
198 + _K_score_(var1, var2, var3, conditioning_ids);
199 }
200
202 INLINE double CorrectedMutualInformation::_NI_score_(NodeId var_x,
203 NodeId var_y,
204 NodeId var_z,
205 const std::vector< NodeId >& ui_ids) {
206 // conditional 3-point mutual information formula:
207 // I(x;y;z|{ui}) = I(x;y|{ui}) - I(x;y|z,{ui})
208 std::vector< NodeId > uiz_ids = ui_ids;
209 uiz_ids.push_back(var_z);
210 return _NI_score_(var_x, var_y, ui_ids) - _NI_score_(var_x, var_y, uiz_ids);
211 }
212
214 INLINE double CorrectedMutualInformation::_K_score_(NodeId var1,
215 NodeId var2,
216 NodeId var3,
217 const std::vector< NodeId >& ui_ids) {
218 // k(x;y;z|ui) = k(x;y|ui,z) - k(x;y|ui)
219 std::vector< NodeId > uiz_ids = ui_ids;
220 uiz_ids.push_back(var3);
221 return _K_score_(var1, var2, uiz_ids) - _K_score_(var1, var2, ui_ids);
222 }
223
224
225 } /* namespace learning */
226
227} /* namespace gum */
228
229#endif /* DOXYGEN_SHOULD_SKIP_THIS */
virtual std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
void clearKCache()
clears the KCache (the cache for the penalties)
void useKCache(bool on_off)
turn on/off the use of the KCache (the cache for the penalties)
void setNumberOfThreads(Size nb) override
changes the max number of threads used to parse the database
virtual void setMinNbRowsPerThread(const std::size_t nb) const
changes the number min of rows a thread should process in a multithreading context
virtual void clearCache()
clears all the current caches
virtual void useCache(bool on_off)
turn on/off the use of all the caches
const std::vector< std::pair< std::size_t, std::size_t > > & ranges() const
returns the current ranges
double score(NodeId var1, NodeId var2)
returns the 2-point mutual information corresponding to a given nodeset
void useCnrCache(bool on_off)
turn on/off the use of the CnrCache (the cache for the Cnr formula)
void useICache(bool on_off)
turn on/off the use of the ICache (the mutual information cache)
void useNML()
use the kNML penalty function
void clearICache()
clears the ICache (the mutual information cache)
void clearHCache()
clears the HCache (the cache for the entropies)
std::size_t getNumberOfThreads() const override
returns the number of threads used to parse the database
void useHCache(bool on_off)
turn on/off the use of the HCache (the cache for the entropies)
virtual void clear()
clears all the data structures from memory
void clearCnrCache()
clears the CnrCache (the cache for the Cnr formula)
void useNoCorr()
use no correction/penalty function
bool isGumNumberOfThreadsOverriden() const override
indicates whether the user set herself the number of threads
void useMDL()
use the MDL penalty function
The class computing n times the corrected mutual information (where n is the size (or the weight) of ...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46