aGrUM  0.16.0
variableLog2ParamComplexity.h
Go to the documentation of this file.
1 
30 #ifndef GUM_VARIABLE_LOG2_PARAM_COMPLEXITY_H
31 #define GUM_VARIABLE_LOG2_PARAM_COMPLEXITY_H
32 
33 #include <cstddef>
34 #include <string>
35 #include <fstream>
36 
37 #include <agrum/agrum.h>
38 #include <agrum/core/math/math.h>
39 #include <agrum/core/hashTable.h>
40 
41 namespace gum {
42 
43 
44  // the CTable cache for log2(C_n^r), with n in {0,...,999} and r in {2,3,4,5}
45  extern const double VariableLog2ParamComplexityCTable[4][1000];
46 
47  // the size in r of the CTable cache
48  constexpr std::size_t VariableLog2ParamComplexityCTableRSize{std::size_t(4)};
49 
50  // the size in n of the CTable cache
51  constexpr std::size_t VariableLog2ParamComplexityCTableNSize{std::size_t(1000)};
52 
53 
67  template < template < typename > class ALLOC = std::allocator >
68  class VariableLog2ParamComplexity : private ALLOC< double > {
69  public:
71  using allocator_type = ALLOC< double >;
72 
73  // ########################################################################
75  // ########################################################################
77 
80 
83 
86  const allocator_type& alloc);
87 
90 
93  const allocator_type& alloc);
94 
96  virtual VariableLog2ParamComplexity* clone() const;
97 
99  virtual VariableLog2ParamComplexity* clone(const allocator_type& alloc) const;
100 
103 
105 
106 
107  // ########################################################################
109  // ########################################################################
111 
115 
118 
120 
121 
122  // ########################################################################
124  // ########################################################################
126 
128  double log2Cnr(const std::size_t r, const double n);
129 
131  void CnrToFile(const std::string& filename);
132 
134  void useCache(const bool on_off);
135 
137  void clearCache();
138 
141 
142 
144 
145  private:
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 // always include the template implementation
178 
179 #endif /* GUM_VARIABLE_LOG2_PARAM_COMPLEXITY_H */
VariableLog2ParamComplexity(const allocator_type &alloc=allocator_type())
default constructor
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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]
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
HashTable< std::pair< std::size_t, double >, double > __cache
constexpr std::size_t VariableLog2ParamComplexityCTableRSize
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
The class for generic Hash Tables.
Definition: hashTable.h:679
void useCache(const bool on_off)
indicates whether we wish to use a cache for the Cnr
#define M_PI
Definition: math.h:40
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
double log2Cnr(const std::size_t r, const double n)
returns the value of the log in base 2 of Cnr