aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
pseudoCount.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief the base class for all the independence tests used for learning
24  *
25  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26  */
27 #ifndef GUM_LEARNING_PSEUDO_COUNT_H
28 #define GUM_LEARNING_PSEUDO_COUNT_H
29 
30 #include <utility>
31 
32 #include <agrum/agrum.h>
33 #include <agrum/tools/core/math/math_utils.h>
34 #include <agrum/tools/core/OMPThreads.h>
35 
36 #include <agrum/tools/stattests/recordCounter.h>
37 #include <agrum/BN/learning/aprioris/apriori.h>
38 #include <agrum/tools/variables/discreteVariable.h>
39 #include <agrum/tools/multidim/potential.h>
40 
41 namespace gum {
42 
43  namespace learning {
44 
45  /** @class PseudoCount
46  * @brief The class for giving access to pseudo count : count in the database +
47  * prior
48  * @headerfile PseudoCount.h <agrum/BN/learning/pseudo-counts_and_tests/PseudoCount.h>
49  * @ingroup learning_pseudo-counts
50  */
51  template < template < typename > class ALLOC = std::allocator >
52  class PseudoCount {
53  public:
54  /// type for the allocators passed in arguments of methods
56 
57  // ##########################################################################
58  /// @name Constructors / Destructors
59  // ##########################################################################
60  /// @{
61 
62  /// default constructor
63  /** @param parser the parser used to parse the database
64  * @param external_apriori An apriori that we add to the computation of
65  * the pseudo-count (this should come from expert knowledge): this consists
66  * in adding numbers to countings in the contingency tables
67  * @param ranges a set of pairs {(X1,Y1),...,(Xn,Yn)} of database's rows
68  * indices. The countings are then performed only on the union of the
69  * rows [Xi,Yi), i in {1,...,n}. This is useful, e.g, when performing
70  * cross validation tasks, in which part of the database should be ignored.
71  * An empty set of ranges is equivalent to an interval [X,Y) ranging over
72  * the whole database.
73  * @param nodeId2Columns a mapping from the ids of the nodes in the
74  * graphical model to the corresponding column in the DatabaseTable
75  * parsed by the parser. This enables estimating from a database in
76  * which variable A corresponds to the 2nd column the parameters of a BN
77  * in which variable A has a NodeId of 5. An empty nodeId2Columns
78  * bijection means that the mapping is an identity, i.e., the value of a
79  * NodeId is equal to the index of the column in the DatabaseTable.
80  * @param alloc the allocator used to allocate the structures within the
81  * PseudoCount.
82  * @warning If nodeId2columns is not empty, then only the pseudo-counts over
83  * the ids belonging to this bijection can be computed: applying method
84  * pseudo-count() over other ids will raise exception NotFound. */
85  PseudoCount(const DBRowGeneratorParser< ALLOC >& parser,
86  const Apriori< ALLOC >& external_apriori,
87  const std::vector< std::pair< std::size_t, std::size_t >,
88  ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
90  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
92 
93 
94  /// default constructor
95  /** @param parser the parser used to parse the database
96  * @param external_apriori An apriori that we add to the computation of
97  * the pseudo-count (this should come from expert knowledge): this consists
98  * in adding numbers to countings in the contingency tables
99  * @param nodeId2Columns a mapping from the ids of the nodes in the
100  * graphical model to the corresponding column in the DatabaseTable
101  * parsed by the parser. This enables estimating from a database in
102  * which variable A corresponds to the 2nd column the parameters of a BN
103  * in which variable A has a NodeId of 5. An empty nodeId2Columns
104  * bijection means that the mapping is an identity, i.e., the value of a
105  * NodeId is equal to the index of the column in the DatabaseTable.
106  * @param alloc the allocator used to allocate the structures within the
107  * PseudoCount.
108  * @warning If nodeId2columns is not empty, then only the pseudo-counts over
109  * the ids belonging to this bijection can be computed: applying method
110  * pseudo-count() over other ids will raise exception NotFound. */
111  PseudoCount(const DBRowGeneratorParser< ALLOC >& parser,
112  const Apriori< ALLOC >& external_apriori,
114  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
115  const allocator_type& alloc = allocator_type());
116 
117  /// destructor
118  virtual ~PseudoCount();
119 
120 
121  /// copy constructor
122  PseudoCount(const PseudoCount< ALLOC >& from);
123 
124  /// copy constructor with a given allocator
125  PseudoCount(const PseudoCount< ALLOC >& from, const allocator_type& alloc);
126 
127  /// move constructor
128  PseudoCount(PseudoCount< ALLOC >&& from);
129 
130  /// move constructor with a given allocator
131  PseudoCount(PseudoCount< ALLOC >&& from, const allocator_type& alloc);
132 
133  /// copy operator
135 
136  /// move operator
138  /// @}
139 
140 
141  // ##########################################################################
142  /// @name Accessors / Modifiers
143  // ##########################################################################
144  /// @{
145 
146  /// changes the max number of threads used to parse the database
147  virtual void setMaxNbThreads(std::size_t nb) const;
148 
149  /// returns the number of threads used to parse the database
150  virtual std::size_t nbThreads() const;
151 
152  /** @brief changes the number min of rows a thread should process in a
153  * multithreading context
154  *
155  * When computing pseudo-count, several threads are used by record counters
156  * to perform countings on the rows of the database, the MinNbRowsPerThread
157  * method indicates how many rows each thread should at least process.
158  * This is used to compute the number of threads actually run. This number
159  * is equal to the min between the max number of threads allowed and the
160  * number of records in the database divided by nb. */
161  virtual void setMinNbRowsPerThread(const std::size_t nb) const;
162 
163  /// returns the minimum of rows that each thread should process
164  virtual std::size_t minNbRowsPerThread() const;
165 
166  /// sets new ranges to perform the countings used by the independence test
167  /** @param ranges a set of pairs {(X1,Y1),...,(Xn,Yn)} of database's rows
168  * indices. The countings are then performed only on the union of the
169  * rows [Xi,Yi), i in {1,...,n}. This is useful, e.g, when performing
170  * cross validation tasks, in which part of the database should be ignored.
171  * An empty set of ranges is equivalent to an interval [X,Y) ranging over
172  * the whole database. */
173  template < template < typename > class XALLOC >
174  void setRanges(
175  const std::vector< std::pair< std::size_t, std::size_t >,
176  XALLOC< std::pair< std::size_t, std::size_t > > >& new_ranges);
177 
178  /// reset the ranges to the one range corresponding to the whole database
179  void clearRanges();
180 
181  /// returns the current ranges
182  const std::vector< std::pair< std::size_t, std::size_t >,
183  ALLOC< std::pair< std::size_t, std::size_t > > >&
184  ranges() const;
185 
186  /// returns the pseudo-count of a pair of nodes given some other nodes
187  /** @param var1 the first variable on the left side of the conditioning bar
188  * @param var2 the second variable on the left side of the conditioning bar
189  * @param rhs_ids the set of variables on the right side of the
190  * conditioning bar */
191  std::vector< double, ALLOC< double > > get(const std::vector< NodeId, ALLOC< NodeId > >& ids);
192 
193  /// clears all the data structures from memory, including the cache
194  virtual void clear();
195 
196  /// return the mapping between the columns of the database and the node ids
197  /** @warning An empty nodeId2Columns bijection means that the mapping is
198  * an identity, i.e., the value of a NodeId is equal to the index of the
199  * column in the DatabaseTable. */
200  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >& nodeId2Columns() const;
201 
202  /// return the database used by the pseudo-count
203  const DatabaseTable< ALLOC >& database() const;
204 
205  /// returns the allocator used by the pseudo-count
207 
208  /// @}
209 
210 
211  protected:
212  /// the expert knowledge a priori we add to the contingency tables
213  Apriori< ALLOC >* apriori_{nullptr};
214 
215  /// the record counter used for the countings over discrete variables
217 
218  /// an empty vector
220  }; /* namespace learning */
221  } // namespace learning
222 } /* namespace gum */
223 
224 
225 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
226 extern template class gum::learning::PseudoCount<>;
227 #endif
228 
229 
230 /// include the template implementation
231 #include <agrum/tools/stattests/pseudoCount_tpl.h>
232 
233 #endif /* GUM_LEARNING_PSEUDO_COUNT_H */
allocator_type getAllocator() const
returns the allocator used by the pseudo-count
void clearRanges()
reset the ranges to the one range corresponding to the whole database
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
PseudoCount(const PseudoCount< ALLOC > &from)
copy constructor
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
RecordCounter< ALLOC > counter_
the record counter used for the countings over discrete variables
Definition: pseudoCount.h:216
PseudoCount< ALLOC > & operator=(PseudoCount< ALLOC > &&from)
move operator
virtual void setMaxNbThreads(std::size_t nb) const
changes the max number of threads used to parse the database
PseudoCount(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
std::vector< double, ALLOC< double > > get(const std::vector< NodeId, ALLOC< NodeId > > &ids)
returns the pseudo-count of a pair of nodes given some other nodes
virtual void clear()
clears all the data structures from memory, including the cache
PseudoCount(PseudoCount< ALLOC > &&from, const allocator_type &alloc)
move constructor with a given allocator
Apriori< ALLOC > * apriori_
the expert knowledge a priori we add to the contingency tables
Definition: pseudoCount.h:213
PseudoCount(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &external_apriori, 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
const DatabaseTable< ALLOC > & database() const
return the database used by the pseudo-count
virtual void setMinNbRowsPerThread(const std::size_t nb) const
changes the number min of rows a thread should process in a multithreading context ...
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
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
PseudoCount(const PseudoCount< ALLOC > &from, const allocator_type &alloc)
copy constructor with a given allocator
PseudoCount(PseudoCount< ALLOC > &&from)
move constructor
virtual std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
virtual std::size_t nbThreads() const
returns the number of threads used to parse the database
virtual ~PseudoCount()
destructor
PseudoCount< ALLOC > & operator=(const PseudoCount< ALLOC > &from)
copy operator