aGrUM  0.14.2
variableLog2ParamComplexity.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #ifndef GUM_VARIABLE_LOG2_PARAM_COMPLEXITY_H
28 #define GUM_VARIABLE_LOG2_PARAM_COMPLEXITY_H
29 
30 #include <cstddef>
31 #include <string>
32 #include <fstream>
33 
34 #include <agrum/agrum.h>
35 #include <agrum/core/math/math.h>
36 #include <agrum/core/hashTable.h>
37 
38 namespace gum {
39 
40 
41  // the CTable cache for log2(C_n^r), with n in {0,...,999} and r in {2,3,4,5}
42  extern const double VariableLog2ParamComplexityCTable[4][1000];
43 
44  // the size in r of the CTable cache
45  constexpr std::size_t VariableLog2ParamComplexityCTableRSize{std::size_t(4)};
46 
47  // the size in n of the CTable cache
48  constexpr std::size_t VariableLog2ParamComplexityCTableNSize{std::size_t(1000)};
49 
50 
64  template < template < typename > class ALLOC = std::allocator >
65  class VariableLog2ParamComplexity : private ALLOC< double > {
66  public:
68  using allocator_type = ALLOC< double >;
69 
70  // ########################################################################
72  // ########################################################################
74 
77 
80 
83  const allocator_type& alloc);
84 
87 
90  const allocator_type& alloc);
91 
93  virtual VariableLog2ParamComplexity* clone() const;
94 
96  virtual VariableLog2ParamComplexity* clone(const allocator_type& alloc) const;
97 
100 
102 
103 
104  // ########################################################################
106  // ########################################################################
108 
112 
115 
117 
118 
119  // ########################################################################
121  // ########################################################################
123 
125  double log2Cnr(const std::size_t r, const double n);
126 
128  void CnrToFile(const std::string& filename);
129 
131  void useCache(const bool on_off);
132 
134  void clearCache();
135 
138 
139 
141 
142  private:
145 
146  // constants used to speed-up the computation of the Szpankowski
147  // approximation.
148  // The formula for the approximation given in Silander, Roos,
149  // Kontkanen and Myllymaki (2007) "Factorized Normalized Maximum "
150  // Likelihood Criterion for Learning Bayesian Network Structures" paper
151  // is incorrect. However, the one in Kontkanen, Buntine, Myllymaki,
152  // Rissanen and Tirri (2003) "Efficient Computation of Stochastic
153  // Complexity" is correct. So we use the latter and simplify it. Thus,
154  // the approximation of log2(Cnr) is equal to:
155  // 0.5 log2(n) - 0.5 + log2(sqrt(pi)) + (sqrt(2/pi)/3) / sqrt(n) +
156  // (3/36 - 4/(9*pi)) / n.
157  // So, given the constants below, it is equal to:
158  // 0.5 * std::log2 (n) + __cst1 + __cst2 / std::sqrt(n) + __cst3 / n
159  const double __cst1 = -0.5 + std::log2(std::sqrt(M_PI));
160  const double __cst2 = std::sqrt(2.0 / M_PI) / 3.0;
161  const double __cst3 = 3.0 / 36.0 - 4.0 / (9.0 * M_PI);
162 
163  // indicates whether we should use a cache or not
164  bool __use_cache{true};
165 
166  // the cache used, eventually, to store the log2Cnr values
168  };
169 
170 } /* namespace gum */
171 
172 
173 // always include the template implementation
175 
176 #endif /* GUM_VARIABLE_LOG2_PARAM_COMPLEXITY_H */
VariableLog2ParamComplexity(const allocator_type &alloc=allocator_type())
default constructor
Useful macros for maths.
const double __Szpankowski_threshold
the value of N above which we should use Szpankowski&#39;s approximation
virtual VariableLog2ParamComplexity * clone() const
virtual copy constructor
allocator_type getAllocator() const
returns the allocator used by the parameterized complexity class
const double VariableLog2ParamComplexityCTable[4][1000]
the class for computing the log2 of the parametric complexity of an r-ary multinomial variable ...
HashTable< std::pair< std::size_t, double >, double > __cache
constexpr std::size_t VariableLog2ParamComplexityCTableRSize
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
The class for generic Hash Tables.
Definition: hashTable.h:676
void useCache(const bool on_off)
indicates whether we wish to use a cache for the Cnr
#define M_PI
Definition: math.h:37
void CnrToFile(const std::string &filename)
the function used to write the cpp file with the values of log2(Cnr)
constexpr std::size_t VariableLog2ParamComplexityCTableNSize
ALLOC< double > allocator_type
type for the allocators passed in arguments of methods
virtual ~VariableLog2ParamComplexity()
destructor
the class for computing the log2 of the parametric complexity of an r-ary multinomial variableThis cl...
VariableLog2ParamComplexity & operator=(const VariableLog2ParamComplexity &from)
copy operator
void clearCache()
clears the current cache
Class hash tables iterators.
double log2Cnr(const std::size_t r, const double n)
returns the value of the log in base 2 of Cnr