aGrUM  0.16.0
independenceTest.h
Go to the documentation of this file.
1 
28 #ifndef GUM_LEARNING_INDEPENDENCE_TEST_H
29 #define GUM_LEARNING_INDEPENDENCE_TEST_H
30 
31 #include <utility>
32 
33 #include <agrum/agrum.h>
34 #include <agrum/core/math/math.h>
35 #include <agrum/core/OMPThreads.h>
36 
41 
42 namespace gum {
43 
44  namespace learning {
45 
51  template < template < typename > class ALLOC = std::allocator >
53  public:
55  using allocator_type = ALLOC< NodeId >;
56 
57  // ##########################################################################
59  // ##########################################################################
61 
63 
86  const DBRowGeneratorParser< ALLOC >& parser,
87  const Apriori< ALLOC >& external_apriori,
88  const std::vector< std::pair< std::size_t, std::size_t >,
89  ALLOC< std::pair< std::size_t, std::size_t > > >&
90  ranges,
91  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
92  nodeId2columns =
93  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
94  const allocator_type& alloc = allocator_type());
95 
96 
98 
115  const DBRowGeneratorParser< ALLOC >& parser,
116  const Apriori< ALLOC >& external_apriori,
117  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
118  nodeId2columns =
119  Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
120  const allocator_type& alloc = allocator_type());
121 
123  virtual IndependenceTest< ALLOC >* clone() const = 0;
124 
127  clone(const allocator_type& alloc) const = 0;
128 
130  virtual ~IndependenceTest();
131 
133 
134 
135  // ##########################################################################
137  // ##########################################################################
139 
141  virtual void setMaxNbThreads(std::size_t nb) const;
142 
144  virtual std::size_t nbThreads() const;
145 
155  virtual void setMinNbRowsPerThread(const std::size_t nb) const;
156 
158  virtual std::size_t minNbRowsPerThread() const;
159 
161 
167  template < template < typename > class XALLOC >
168  void setRanges(
169  const std::vector< std::pair< std::size_t, std::size_t >,
170  XALLOC< std::pair< std::size_t, std::size_t > > >&
171  new_ranges);
172 
174  void clearRanges();
175 
177  const std::vector< std::pair< std::size_t, std::size_t >,
178  ALLOC< std::pair< std::size_t, std::size_t > > >&
179  ranges() const;
180 
181 
183  double score(const NodeId var1, const NodeId var2);
184 
186 
190  double score(const NodeId var1,
191  const NodeId var2,
192  const std::vector< NodeId, ALLOC< NodeId > >& rhs_ids);
193 
195  virtual void clear();
196 
198  virtual void clearCache();
199 
201  virtual void useCache(const bool on_off);
202 
204 
208  nodeId2Columns() const;
209 
211  const DatabaseTable< ALLOC >& database() const;
212 
215 
217 
218 
219  protected:
221  const double _1log2{M_LOG2E};
222 
225 
228 
231 
233  bool _use_cache{true};
234 
236  const std::vector< NodeId, ALLOC< NodeId > > _empty_ids;
237 
238 
241 
244  const allocator_type& alloc);
245 
248 
251  const allocator_type& alloc);
252 
255 
258 
260 
263  virtual double _score(const IdSet< ALLOC >& idset) = 0;
264 
266 
275  std::vector< double, ALLOC< double > >
276  _marginalize(const std::size_t node_2_marginalize,
277  const std::size_t X_size,
278  const std::size_t Y_size,
279  const std::size_t Z_size,
280  const std::vector< double, ALLOC< double > >& N_xyz) const;
281  };
282 
283  } /* namespace learning */
284 
285 } /* namespace gum */
286 
287 
288 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
289 extern template class gum::learning::IndependenceTest<>;
290 #endif
291 
292 
295 
296 #endif /* GUM_LEARNING_INDEPENDENCE_TEST_H */
ScoringCache< ALLOC > _cache
the scoring cache
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
A class for storing a pair of sets of NodeIds, the second one corresponding to a conditional set...
Definition: idSet.h:48
the base class for all a priori
Definition: apriori.h:50
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 ...
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
#define M_LOG2E
Definition: math.h:36
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:1805
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:60
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:98
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