aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
variableLog2ParamComplexity.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
48
49#ifndef GUM_VARIABLE_LOG2_PARAM_COMPLEXITY_H
50#define GUM_VARIABLE_LOG2_PARAM_COMPLEXITY_H
51
52#include <cstddef>
53#include <fstream>
54#include <string>
55
56#include <agrum/agrum.h>
57
59
61
62namespace gum {
63
64
65 // the CTable cache for log2(C_n^r), with n in {0,...,999} and r in {2,3,4,5}
66 extern const double VariableLog2ParamComplexityCTable[4][1000];
67
68 // the size in r of the CTable cache
69 constexpr std::size_t VariableLog2ParamComplexityCTableRSize{std::size_t(4)};
70
71 // the size in n of the CTable cache
72 constexpr std::size_t VariableLog2ParamComplexityCTableNSize{std::size_t(1000)};
73
88 public:
89 // ########################################################################
91 // ########################################################################
93
96
99
102
105
108
110
111
112 // ########################################################################
114 // ########################################################################
116
119
122
124
125
126 // ########################################################################
128 // ########################################################################
130
132 double log2Cnr(const std::size_t r, const double n);
133
135 void CnrToFile(const std::string& filename);
136
138 void useCache(const bool on_off);
139
142
144
145 private:
147 // const double _Szpankowski_threshold_{VariableLog2ParamComplexityCTableNSize};
148
149 // constants used to speed-up the computation of the Szpankowski
150 // approximation.
151 // The formula for the approximation given in Silander, Roos,
152 // Kontkanen and Myllymaki (2007) "Factorized Normalized Maximum "
153 // Likelihood Criterion for Learning Bayesian network Structures" paper
154 // is incorrect. However, the one in Kontkanen, Buntine, Myllymaki,
155 // Rissanen and Tirri (2003) "Efficient Computation of Stochastic
156 // Complexity" is correct. So we use the latter and simplify it. Thus,
157 // the approximation of log2(Cnr) is equal to:
158 // 0.5 log2(n) - 0.5 + log2(sqrt(pi)) + (sqrt(2/pi)/3) / sqrt(n) +
159 // (3/36 - 4/(9*pi)) / n.
160 // So, given the constants below, it is equal to:
161 // 0.5 * std::log2 (n) + _cst1_ + _cst2_ / std::sqrt(n) + _cst3_ / n
162 const double _cst1_ = -0.5 + std::log2(std::sqrt(M_PI));
163 const double _cst2_ = std::sqrt(2.0 / M_PI) / 3.0;
164 const double _cst3_ = 3.0 / 36.0 - 4.0 / (9.0 * M_PI);
165
166 // indicates whether we should use a cache or not
167 bool _use_cache_{true};
168
169 // the cache used, eventually, to store the log2Cnr values
171 };
172
173} /* namespace gum */
174
175
176#ifndef GUM_NO_INLINE
178#endif // GUM_NO_INLINE
179
180#endif /* GUM_VARIABLE_LOG2_PARAM_COMPLEXITY_H */
The class for generic Hash Tables.
Definition hashTable.h:637
void CnrToFile(const std::string &filename)
the function used to write the cpp file with the values of log2(Cnr)
VariableLog2ParamComplexity(VariableLog2ParamComplexity &&from)
move constructor
void useCache(const bool on_off)
indicates whether we wish to use a cache for the Cnr
const double _cst1_
the value of N above which we should use Szpankowski's approximation
VariableLog2ParamComplexity & operator=(VariableLog2ParamComplexity &&from)
move operator
VariableLog2ParamComplexity & operator=(const VariableLog2ParamComplexity &from)
copy operator
double log2Cnr(const std::size_t r, const double n)
returns the value of the log in base 2 of Cnr
virtual VariableLog2ParamComplexity * clone() const
virtual copy constructor
VariableLog2ParamComplexity()
default constructor
void clearCache()
clears the current cache
virtual ~VariableLog2ParamComplexity()
destructor
HashTable< std::pair< std::size_t, double >, double > _cache_
VariableLog2ParamComplexity(const VariableLog2ParamComplexity &from)
copy constructor
Class hash tables iterators.
Useful macros for maths.
#define M_PI
Definition math_utils.h:59
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
constexpr std::size_t VariableLog2ParamComplexityCTableNSize
const double VariableLog2ParamComplexityCTable[4][1000]
constexpr std::size_t VariableLog2ParamComplexityCTableRSize
the class for computing the log2 of the parametric complexity of an r-ary multinomial variable