aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
correctedMutualInformation.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
50#ifndef GUM_LEARNING_CORRECTED_MUTUAL_INFORMATION_H
51#define GUM_LEARNING_CORRECTED_MUTUAL_INFORMATION_H
52
53#include <vector>
54
55#include <agrum/config.h>
56
60
61namespace gum {
62 namespace learning {
79 public:
80 // ##########################################################################
82 // ##########################################################################
84
86
107 const Prior& prior,
108 const std::vector< std::pair< std::size_t, std::size_t > >& ranges,
109 const Bijection< NodeId, std::size_t >& nodeId2columns
111
113
128 const Prior& prior,
129 const Bijection< NodeId, std::size_t >& nodeId2columns
131
134
137
139 [[nodiscard]] virtual CorrectedMutualInformation* clone() const;
140
143
145
146
147 // ##########################################################################
149 // ##########################################################################
150
152
155
158
160
161
162 // ##########################################################################
164 // ##########################################################################
166
168 virtual void clear();
169
171
179 virtual void clearCache();
180
182
190 virtual void useCache(bool on_off);
191
193 void useICache(bool on_off);
194
197
199 void useHCache(bool on_off);
200
203
205 void useKCache(bool on_off);
206
209
211 void useCnrCache(bool on_off);
212
215
217
218
219 // ##########################################################################
221 // ##########################################################################
223
225 double score(NodeId var1, NodeId var2);
226
228 double score(NodeId var1, NodeId var2, const std::vector< NodeId >& conditioning_ids);
229
231 double score(NodeId var1, NodeId var2, NodeId var3);
232
234 double score(NodeId var1,
235 NodeId var2,
236 NodeId var3,
237 const std::vector< NodeId >& conditioning_ids);
238
240
241
242 // ##########################################################################
244 // ##########################################################################
246
248 void useMDL();
249
251 void useNML();
252
254 void useNoCorr();
255
257 void setNumberOfThreads(Size nb) override;
258
260 std::size_t getNumberOfThreads() const override;
261
263 bool isGumNumberOfThreadsOverriden() const override;
264
274 virtual void setMinNbRowsPerThread(const std::size_t nb) const;
275
277 virtual std::size_t minNbRowsPerThread() const;
278
280
286 void setRanges(const std::vector< std::pair< std::size_t, std::size_t > >& new_ranges);
287
290
292 const std::vector< std::pair< std::size_t, std::size_t > >& ranges() const;
293
295
296
298 enum class KModeTypes { MDL, NML, NoCorr };
299
300
301#ifndef DOXYGEN_SHOULD_SKIP_THIS
302
303 private:
305 /* Note that the log2-likelihood is equal to N times the entropy H */
307
309 KNML _k_NML_;
310
313 ScoreMDL _score_MDL_;
314
316 KModeTypes _kmode_{KModeTypes::MDL};
317
318
320
322 bool _use_ICache_{true};
323
325
328 bool _use_HCache_{true};
329
331
334 bool _use_KCache_{true};
335
337
341 bool _use_CnrCache_{true};
342
343
345 ScoringCache _ICache_;
346
348 ScoringCache _KCache_;
349
350
352 const std::vector< NodeId > _empty_conditioning_set_;
353
355 const double _threshold_{1e-10};
356
357
359 double _NI_score_(NodeId var_x, NodeId var_y, const std::vector< NodeId >& vars_z);
360
362 double _NI_score_(NodeId var_x,
363 NodeId var_y,
364 NodeId var_z,
365 const std::vector< NodeId >& vars_ui);
366
368 double _K_score_(NodeId var_x, NodeId var_y, const std::vector< NodeId >& vars_z);
369
371 double
372 _K_score_(NodeId var_x, NodeId var_y, NodeId var_z, const std::vector< NodeId >& vars_ui);
373
374#endif /* DOXYGEN_SHOULD_SKIP_THIS */
375 };
376
377 } /* namespace learning */
378
379} /* namespace gum */
380
381// include the inlined functions if necessary
382#ifndef GUM_NO_INLINE
384#endif /* GUM_NO_INLINE */
385
386#endif /* GUM_LEARNING_CORRECTED_MUTUAL_INFORMATION_H */
double score(NodeId var1, NodeId var2, NodeId var3)
returns the 3-point mutual information corresponding to a given nodeset
virtual CorrectedMutualInformation * clone() const
virtual copy constructor
virtual std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
virtual ~CorrectedMutualInformation()
destructor
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
CorrectedMutualInformation & operator=(const CorrectedMutualInformation &from)
copy operator
KModeTypes
the description type for the complexity correction
virtual void useCache(bool on_off)
turn on/off the use of all the caches
void clearRanges()
reset the ranges to the one range corresponding to the whole database
CorrectedMutualInformation & operator=(CorrectedMutualInformation &&from)
move operator
CorrectedMutualInformation(const CorrectedMutualInformation &from)
copy constructor
double score(NodeId var1, NodeId var2, NodeId var3, const std::vector< NodeId > &conditioning_ids)
returns the 3-point mutual information corresponding to a given nodeset
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)
double score(NodeId var1, NodeId var2, const std::vector< NodeId > &conditioning_ids)
returns the 2-point mutual information corresponding to a given nodeset
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)
CorrectedMutualInformation(const DBRowGeneratorParser &parser, const Prior &prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
CorrectedMutualInformation(const DBRowGeneratorParser &parser, const Prior &prior, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
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)
CorrectedMutualInformation(CorrectedMutualInformation &&from)
move constructor
void setRanges(const std::vector< std::pair< std::size_t, std::size_t > > &new_ranges)
sets new ranges to perform the counts used by the mutual information
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 used to read a row in the database and to transform it into a set of DBRow instances that c...
the class for computing the NML penalty used by MIIC
Definition kNML.h:67
the base class for all a priori
Definition prior.h:81
the class for computing Log2-likelihood scores
The class computing n times the corrected mutual information, as used in the MIIC algorithm.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
The class for the NML penalty used in MIIC.
include the inlined functions if necessary
Definition CSVParser.h:55
ScoreBIC ScoreMDL
Definition scoreMDL.h:67
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
the class for computing Log2-likelihood scores
the class for computing MDL scores