aGrUM 3.1.1
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-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
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#include <string_view>
62
63namespace gum {
64
65
66 // the CTable cache for log2(C_n^r), with n in {0,...,999} and r in {2,3,4,5}
67 extern const double VariableLog2ParamComplexityCTable[4][1000];
68
69 // the size in r of the CTable cache
70 constexpr std::size_t VariableLog2ParamComplexityCTableRSize{std::size_t(4)};
71
72 // the size in n of the CTable cache
73 constexpr std::size_t VariableLog2ParamComplexityCTableNSize{std::size_t(1000)};
74
89 public:
90 // ########################################################################
92 // ########################################################################
94
97
100
103
105 [[nodiscard]] virtual VariableLog2ParamComplexity* clone() const;
106
109
111
112
113 // ########################################################################
115 // ########################################################################
117
120
123
125
126
127 // ########################################################################
129 // ########################################################################
131
133 double log2Cnr(const std::size_t r, const double n);
134
136 void CnrToFile(std::string_view filename);
137
139 void useCache(const bool on_off);
140
143
145
146 private:
148 // const double _Szpankowski_threshold_{VariableLog2ParamComplexityCTableNSize};
149
150 // constants used to speed-up the computation of the Szpankowski
151 // approximation.
152 // The formula for the approximation given in Silander, Roos,
153 // Kontkanen and Myllymaki (2007) "Factorized Normalized Maximum "
154 // Likelihood Criterion for Learning Bayesian network Structures" paper
155 // is incorrect. However, the one in Kontkanen, Buntine, Myllymaki,
156 // Rissanen and Tirri (2003) "Efficient Computation of Stochastic
157 // Complexity" is correct. So we use the latter and simplify it. Thus,
158 // the approximation of log2(Cnr) is equal to:
159 // 0.5 log2(n) - 0.5 + log2(sqrt(pi)) + (sqrt(2/pi)/3) / sqrt(n) +
160 // (3/36 - 4/(9*pi)) / n.
161 // So, given the constants below, it is equal to:
162 // 0.5 * std::log2 (n) + _cst1_ + _cst2_ / std::sqrt(n) + _cst3_ / n
163 const double _cst1_ = -0.5 + std::log2(std::sqrt(M_PI));
164 const double _cst2_ = std::sqrt(2.0 / M_PI) / 3.0;
165 const double _cst3_ = 3.0 / 36.0 - 4.0 / (9.0 * M_PI);
166
167 // indicates whether we should use a cache or not
168 bool _use_cache_{true};
169
170 // the cache used, eventually, to store the log2Cnr values
172 };
173
174} /* namespace gum */
175
176
177#ifndef GUM_NO_INLINE
179#endif // GUM_NO_INLINE
180
181#endif /* GUM_VARIABLE_LOG2_PARAM_COMPLEXITY_H */
The class for generic Hash Tables.
Definition hashTable.h:640
VariableLog2ParamComplexity(VariableLog2ParamComplexity &&from)
move constructor
void CnrToFile(std::string_view filename)
the function used to write the cpp file with the values of log2(Cnr)
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