aGrUM  0.14.2
independenceTest.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  ***************************************************************************/
25 #ifndef GUM_LEARNING_INDEPENDENCE_TEST_H
26 #define GUM_LEARNING_INDEPENDENCE_TEST_H
27 
28 #include <utility>
29 
30 #include <agrum/agrum.h>
31 #include <agrum/core/math/math.h>
32 #include <agrum/core/OMPThreads.h>
33 
38 
39 namespace gum {
40 
41  namespace learning {
42 
48  template < template < typename > class ALLOC = std::allocator >
50  public:
52  using allocator_type = ALLOC< NodeId >;
53 
54  // ##########################################################################
56  // ##########################################################################
58 
60 
83  const DBRowGeneratorParser< ALLOC >& parser,
84  const Apriori< ALLOC >& external_apriori,
85  const std::vector< std::pair< std::size_t, std::size_t >,
86  ALLOC< std::pair< std::size_t, std::size_t > > >&
87  ranges,
88  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
89  nodeId2columns =
90  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
91  const allocator_type& alloc = allocator_type());
92 
93 
95 
112  const DBRowGeneratorParser< ALLOC >& parser,
113  const Apriori< ALLOC >& external_apriori,
114  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
115  nodeId2columns =
116  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
117  const allocator_type& alloc = allocator_type());
118 
120  virtual IndependenceTest< ALLOC >* clone() const = 0;
121 
124  clone(const allocator_type& alloc) const = 0;
125 
127  virtual ~IndependenceTest();
128 
130 
131 
132  // ##########################################################################
134  // ##########################################################################
136 
138  virtual void setMaxNbThreads(std::size_t nb) const;
139 
141  virtual std::size_t nbThreads() const;
142 
152  virtual void setMinNbRowsPerThread(const std::size_t nb) const;
153 
155  virtual std::size_t minNbRowsPerThread() const;
156 
158 
164  template < template < typename > class XALLOC >
165  void setRanges(
166  const std::vector< std::pair< std::size_t, std::size_t >,
167  XALLOC< std::pair< std::size_t, std::size_t > > >&
168  new_ranges);
169 
171  void clearRanges();
172 
174  const std::vector< std::pair< std::size_t, std::size_t >,
175  ALLOC< std::pair< std::size_t, std::size_t > > >&
176  ranges() const;
177 
178 
180  double score(const NodeId var1, const NodeId var2);
181 
183 
187  double score(const NodeId var1,
188  const NodeId var2,
189  const std::vector< NodeId, ALLOC< NodeId > >& rhs_ids);
190 
192  virtual void clear();
193 
195  virtual void clearCache();
196 
198  virtual void useCache(const bool on_off);
199 
201 
205  nodeId2Columns() const;
206 
208  const DatabaseTable< ALLOC >& database() const;
209 
212 
214 
215 
216  protected:
218  const double _1log2{M_LOG2E};
219 
222 
225 
228 
230  bool _use_cache{true};
231 
233  const std::vector< NodeId, ALLOC< NodeId > > _empty_ids;
234 
235 
238 
241  const allocator_type& alloc);
242 
245 
248  const allocator_type& alloc);
249 
252 
255 
257 
260  virtual double _score(const IdSet< ALLOC >& idset) = 0;
261 
263 
272  std::vector< double, ALLOC< double > >
273  _marginalize(const std::size_t node_2_marginalize,
274  const std::size_t X_size,
275  const std::size_t Y_size,
276  const std::size_t Z_size,
277  const std::vector< double, ALLOC< double > >& N_xyz) const;
278  };
279 
280  } /* namespace learning */
281 
282 } /* namespace gum */
283 
284 
285 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
286 extern template class gum::learning::IndependenceTest<>;
287 #endif
288 
289 
292 
293 #endif /* GUM_LEARNING_INDEPENDENCE_TEST_H */
ScoringCache< ALLOC > _cache
the scoring cache
Useful macros for maths.
The base class for all the independence tests used for learning.
const double _1log2
1 / log(2)
std::vector< double, ALLOC< double > > _marginalize(const std::size_t node_2_marginalize, const std::size_t X_size, const std::size_t Y_size, const std::size_t Z_size, const std::vector< double, ALLOC< double > > &N_xyz) const
returns a counting vector where variables are marginalized from N_xyz
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
RecordCounter< ALLOC > _counter
the record counter used for the countings over discrete variables
the classes to account for structure changes in a graph
virtual std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
the base class for all a priori
A class for storing a pair of sets of NodeIds, the second one corresponding to a conditional set...
Definition: idSet.h:45
the base class for all a priori
Definition: apriori.h:47
const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > & ranges() const
returns the current ranges
virtual void clearCache()
clears the current cache
const DatabaseTable< ALLOC > & database() const
return the database used by the score
const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > & nodeId2Columns() const
return the mapping between the columns of the database and the node ids
virtual void setMinNbRowsPerThread(const std::size_t nb) const
changes the number min of rows a thread should process in a multithreading context ...
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
virtual double _score(const IdSet< ALLOC > &idset)=0
returns the score for a given IdSet
const std::vector< NodeId, ALLOC< NodeId > > _empty_ids
an empty vector
the base class for all the independence tests used for learning
IndependenceTest(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &external_apriori, const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > &ranges, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
The class that computes countings of observations from the database.
allocator_type getAllocator() const
returns the allocator used by the score
The class that computes countings of observations from the database.
void clearRanges()
reset the ranges to the one range corresponding to the whole database
virtual IndependenceTest< ALLOC > * clone() const =0
virtual copy constructor
a cache for caching scores and independence tests results
#define M_LOG2E
Definition: math.h:33
virtual void setMaxNbThreads(std::size_t nb) const
changes the max number of threads used to parse the database
IndependenceTest< ALLOC > & operator=(const IndependenceTest< ALLOC > &from)
copy operator
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1803
double score(const NodeId var1, const NodeId var2)
returns the score of a pair of nodes
The class representing a tabular database as used by learning tasks.
Wrappers for OpenMP.
virtual ~IndependenceTest()
destructor
bool _use_cache
a Boolean indicating whether we wish to use the cache
a cache for caching scores and independence tests resultsCaching previously computed scores or the re...
Definition: scoringCache.h:57
virtual void useCache(const bool on_off)
turn on/off the use of a cache of the previously computed score
the class used to read a row in the database and to transform it into a set of DBRow instances that c...
virtual std::size_t nbThreads() const
returns the number of threads used to parse the database
Size NodeId
Type for node ids.
Definition: graphElements.h:97
virtual void clear()
clears all the data structures from memory, including the cache
void setRanges(const std::vector< std::pair< std::size_t, std::size_t >, XALLOC< std::pair< std::size_t, std::size_t > > > &new_ranges)
sets new ranges to perform the countings used by the independence test
Apriori< ALLOC > * _apriori
the expert knowledge a priori we add to the contongency tables