aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
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
58
#include <
agrum/base/core/hashTable.h
>
59
60
#include <
agrum/base/core/math/math_utils.h
>
61
#include <string_view>
62
63
namespace
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
88
class
GUM_SHARED_PUBLIC
VariableLog2ParamComplexity
{
89
public
:
90
// ########################################################################
92
// ########################################################################
94
96
VariableLog2ParamComplexity
();
97
99
VariableLog2ParamComplexity
(
const
VariableLog2ParamComplexity
& from);
100
102
VariableLog2ParamComplexity
(
VariableLog2ParamComplexity
&& from);
103
105
[[nodiscard]]
virtual
VariableLog2ParamComplexity
*
clone
()
const
;
106
108
virtual
~VariableLog2ParamComplexity
();
109
111
112
113
// ########################################################################
115
// ########################################################################
117
119
VariableLog2ParamComplexity
&
operator=
(
const
VariableLog2ParamComplexity
& from);
120
122
VariableLog2ParamComplexity
&
operator=
(
VariableLog2ParamComplexity
&& from);
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
142
void
clearCache
();
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
171
HashTable< std::pair< std::size_t, double >
,
double
>
_cache_
;
172
};
173
174
}
/* namespace gum */
175
176
177
#ifndef GUM_NO_INLINE
178
# include <
agrum/base/core/math/variableLog2ParamComplexity_inl.h
>
179
#endif
// GUM_NO_INLINE
180
181
#endif
/* GUM_VARIABLE_LOG2_PARAM_COMPLEXITY_H */
agrum.h
gum::HashTable
The class for generic Hash Tables.
Definition
hashTable.h:640
gum::VariableLog2ParamComplexity::VariableLog2ParamComplexity
VariableLog2ParamComplexity(VariableLog2ParamComplexity &&from)
move constructor
gum::VariableLog2ParamComplexity::CnrToFile
void CnrToFile(std::string_view filename)
the function used to write the cpp file with the values of log2(Cnr)
Definition
variableLog2ParamComplexity.cpp:154
gum::VariableLog2ParamComplexity::useCache
void useCache(const bool on_off)
indicates whether we wish to use a cache for the Cnr
gum::VariableLog2ParamComplexity::_cst1_
const double _cst1_
the value of N above which we should use Szpankowski's approximation
Definition
variableLog2ParamComplexity.h:163
gum::VariableLog2ParamComplexity::_use_cache_
bool _use_cache_
Definition
variableLog2ParamComplexity.h:168
gum::VariableLog2ParamComplexity::operator=
VariableLog2ParamComplexity & operator=(VariableLog2ParamComplexity &&from)
move operator
gum::VariableLog2ParamComplexity::operator=
VariableLog2ParamComplexity & operator=(const VariableLog2ParamComplexity &from)
copy operator
gum::VariableLog2ParamComplexity::log2Cnr
double log2Cnr(const std::size_t r, const double n)
returns the value of the log in base 2 of Cnr
Definition
variableLog2ParamComplexity.cpp:59
gum::VariableLog2ParamComplexity::_cst3_
const double _cst3_
Definition
variableLog2ParamComplexity.h:165
gum::VariableLog2ParamComplexity::clone
virtual VariableLog2ParamComplexity * clone() const
virtual copy constructor
gum::VariableLog2ParamComplexity::_cst2_
const double _cst2_
Definition
variableLog2ParamComplexity.h:164
gum::VariableLog2ParamComplexity::VariableLog2ParamComplexity
VariableLog2ParamComplexity()
default constructor
gum::VariableLog2ParamComplexity::clearCache
void clearCache()
clears the current cache
gum::VariableLog2ParamComplexity::~VariableLog2ParamComplexity
virtual ~VariableLog2ParamComplexity()
destructor
gum::VariableLog2ParamComplexity::_cache_
HashTable< std::pair< std::size_t, double >, double > _cache_
Definition
variableLog2ParamComplexity.h:171
gum::VariableLog2ParamComplexity::VariableLog2ParamComplexity
VariableLog2ParamComplexity(const VariableLog2ParamComplexity &from)
copy constructor
hashTable.h
Class hash tables iterators.
math_utils.h
Useful macros for maths.
M_PI
#define M_PI
Definition
math_utils.h:59
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
gum::VariableLog2ParamComplexityCTableNSize
constexpr std::size_t VariableLog2ParamComplexityCTableNSize
Definition
variableLog2ParamComplexity.h:73
gum::VariableLog2ParamComplexityCTable
const double VariableLog2ParamComplexityCTable[4][1000]
Definition
variableLog2ParamComplexity.cpp:246
gum::VariableLog2ParamComplexityCTableRSize
constexpr std::size_t VariableLog2ParamComplexityCTableRSize
Definition
variableLog2ParamComplexity.h:70
variableLog2ParamComplexity_inl.h
the class for computing the log2 of the parametric complexity of an r-ary multinomial variable
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0