aGrUM 2.3.2
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-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
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53namespace gum {
54
55 namespace learning {
56
58 INLINE void CorrectedMutualInformation::useCache(bool on_off) {
59 useICache(on_off);
60 useHCache(on_off);
61 useKCache(on_off);
62 useCnrCache(on_off);
63 }
64
66 INLINE void CorrectedMutualInformation::useICache(bool on_off) {
67 if (!on_off) _ICache_.clear();
68 _use_ICache_ = on_off;
69 }
70
72 INLINE void CorrectedMutualInformation::useHCache(bool on_off) {
73 if (!on_off) _NH_.clearCache();
74 _use_HCache_ = on_off;
75 _NH_.useCache(on_off);
76 }
77
79 INLINE void CorrectedMutualInformation::useKCache(bool on_off) {
80 if (!on_off) _KCache_.clear();
81 _use_KCache_ = on_off;
82 }
83
85 INLINE void CorrectedMutualInformation::useCnrCache(bool on_off) {
86 if (!on_off) _k_NML_.clearCache();
87 _use_CnrCache_ = on_off;
88 _k_NML_.useCache(on_off);
89 }
90
93 _NH_.clear();
94 _k_NML_.clear();
95 _score_MDL_.clear();
96 clearCache();
97 }
98
101 _NH_.clearCache();
102 _k_NML_.clearCache();
103 _ICache_.clear();
104 _KCache_.clear();
105 }
106
108 INLINE void CorrectedMutualInformation::clearICache() { _ICache_.clear(); }
109
111 INLINE void CorrectedMutualInformation::clearHCache() { _NH_.clearCache(); }
112
114 INLINE void CorrectedMutualInformation::clearKCache() { _KCache_.clear(); }
115
117 INLINE void CorrectedMutualInformation::clearCnrCache() { _k_NML_.clearCache(); }
118
121 _NH_.setNumberOfThreads(nb);
122 _k_NML_.setNumberOfThreads(nb);
123 _score_MDL_.setNumberOfThreads(nb);
124 }
125
128 return _NH_.getNumberOfThreads();
129 }
130
133 return _NH_.isGumNumberOfThreadsOverriden();
134 }
135
138 INLINE void CorrectedMutualInformation::setMinNbRowsPerThread(const std::size_t nb) const {
139 _NH_.setMinNbRowsPerThread(nb);
140 _k_NML_.setMinNbRowsPerThread(nb);
141 _score_MDL_.setMinNbRowsPerThread(nb);
142 }
143
145 INLINE std::size_t CorrectedMutualInformation::minNbRowsPerThread() const {
146 return _NH_.minNbRowsPerThread();
147 }
148
150 INLINE const std::vector< std::pair< std::size_t, std::size_t > >&
152 return _NH_.ranges();
153 }
154
157 clearCache();
158 _kmode_ = KModeTypes::MDL;
159 }
160
163 clearCache();
164 _kmode_ = KModeTypes::NML;
165 }
166
169 clearCache();
170 _kmode_ = KModeTypes::NoCorr;
171 }
172
174 INLINE double CorrectedMutualInformation::score(NodeId var1, NodeId var2) {
175 return score(var1, var2, _empty_conditioning_set_);
176 }
177
179 INLINE double CorrectedMutualInformation::score(NodeId var1,
180 NodeId var2,
181 const std::vector< NodeId >& conditioning_ids) {
182 return _NI_score_(var1, var2, conditioning_ids) - _K_score_(var1, var2, conditioning_ids);
183 }
184
186 INLINE double CorrectedMutualInformation::score(NodeId var1, NodeId var2, NodeId var3) {
187 return score(var1, var2, var3, _empty_conditioning_set_);
188 }
189
191 INLINE double CorrectedMutualInformation::score(NodeId var1,
192 NodeId var2,
193 NodeId var3,
194 const std::vector< NodeId >& conditioning_ids) {
195 return _NI_score_(var1, var2, var3, conditioning_ids)
196 + _K_score_(var1, var2, var3, conditioning_ids);
197 }
198
200 INLINE double CorrectedMutualInformation::_NI_score_(NodeId var_x,
201 NodeId var_y,
202 NodeId var_z,
203 const std::vector< NodeId >& ui_ids) {
204 // conditional 3-point mutual information formula:
205 // I(x;y;z|{ui}) = I(x;y|{ui}) - I(x;y|z,{ui})
206 std::vector< NodeId > uiz_ids = ui_ids;
207 uiz_ids.push_back(var_z);
208 return _NI_score_(var_x, var_y, ui_ids) - _NI_score_(var_x, var_y, uiz_ids);
209 }
210
212 INLINE double CorrectedMutualInformation::_K_score_(NodeId var1,
213 NodeId var2,
214 NodeId var3,
215 const std::vector< NodeId >& ui_ids) {
216 // k(x;y;z|ui) = k(x;y|ui,z) - k(x;y|ui)
217 std::vector< NodeId > uiz_ids = ui_ids;
218 uiz_ids.push_back(var3);
219 return _K_score_(var1, var2, uiz_ids) - _K_score_(var1, var2, ui_ids);
220 }
221
222
223 } /* namespace learning */
224
225} /* namespace gum */
226
227#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)
virtual void setNumberOfThreads(Size nb)
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
virtual bool isGumNumberOfThreadsOverriden() const
indicates whether the user set herself the number of threads
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)
void useHCache(bool on_off)
turn on/off the use of the HCache (the cache for the entropies)
virtual std::size_t getNumberOfThreads() const
returns the number of threads used to parse the database
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
void useMDL()
use the MDL penalty function
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:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46