aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
apriori.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 a priori
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef GUM_LEARNING_A_PRIORI_H
28 #define GUM_LEARNING_A_PRIORI_H
29 
30 #include <string>
31 #include <vector>
32 
33 #include <agrum/agrum.h>
34 #include <agrum/tools/core/bijection.h>
35 #include <agrum/BN/learning/aprioris/aprioriTypes.h>
36 #include <agrum/tools/database/databaseTable.h>
37 #include <agrum/tools/stattests/idCondSet.h>
38 
39 namespace gum {
40 
41  namespace learning {
42 
43  /** @class Apriori
44  * @brief the base class for all a priori
45  * @headerfile apriori.h <agrum/tools/database/apriori.h>
46  * @ingroup learning_apriori
47  */
48  template < template < typename > class ALLOC = std::allocator >
49  class Apriori: private ALLOC< NodeId > {
50  public:
51  /// type for the allocators passed in arguments of methods
53 
54  // ##########################################################################
55  /// @name Constructors / Destructors
56  // ##########################################################################
57  /// @{
58 
59  /// default constructor
60  /** @param database the database from which learning is performed. This is
61  * useful to get access to the random variables
62  * @param nodeId2Columns a mapping from the ids of the nodes in the
63  * graphical model to the corresponding column in the DatabaseTable.
64  * This enables estimating from a database in which variable A corresponds
65  * to the 2nd column the parameters of a BN in which variable A has a
66  * NodeId of 5. An empty nodeId2Columns bijection means that the mapping
67  * is an identity, i.e., the value of a NodeId is equal to the index of
68  * the column in the DatabaseTable.
69  * @param alloc the allocator used to allocate the structures within the
70  * RecordCounter.*/
71  Apriori(const DatabaseTable< ALLOC >& database,
73  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
75 
76  /// virtual copy constructor
77  virtual Apriori< ALLOC >* clone() const = 0;
78 
79  /// virtual copy constructor with a given allocator
80  virtual Apriori< ALLOC >* clone(const allocator_type& alloc) const = 0;
81 
82  /// destructor
83  virtual ~Apriori();
84 
85  /// @}
86 
87  // ##########################################################################
88  /// @name Accessors / Modifiers
89  // ##########################################################################
90  /// @{
91 
92  /// sets the weight of the a priori (kind of effective sample size)
93  virtual void setWeight(const double weight);
94 
95  /// returns the weight assigned to the apriori
96  double weight() const;
97 
98  /// indicates whether an apriori is of a certain type
99  virtual bool isOfType(const std::string& type) = 0;
100 
101  /// returns the type of the apriori
102  virtual const std::string& getType() const = 0;
103 
104  /// indicates whether the apriori is potentially informative
105  /** Basically, only the NoApriori is uninformative. However, it may happen
106  * that, under some circonstances, an apriori, which is usually not equal
107  * to the NoApriori, becomes equal to it (e.g., when the weight is equal
108  * to zero). In this case, if the apriori can detect this case, it shall
109  * inform the classes that use it that it is temporarily uninformative.
110  * These classes will then be able to speed-up their code by avoiding to
111  * take into account the apriori in their computations. */
112  virtual bool isInformative() const = 0;
113 
114  /// adds the apriori to a counting vector corresponding to the idset
115  /** adds the apriori to an already created counting vector defined over
116  * the union of the variables on both the left and right hand side of the
117  * conditioning bar of the idset.
118  * @warning the method assumes that the size of the vector is exactly
119  * the domain size of the joint variables set. */
120  virtual void addAllApriori(const IdCondSet< ALLOC >& idset,
121  std::vector< double, ALLOC< double > >& counts)
122  = 0;
123 
124  /** @brief adds the apriori to a counting vectordefined over the right
125  * hand side of the idset
126  *
127  * @warning the method assumes that the size of the vector is exactly
128  * the domain size of the joint RHS variables of the idset. */
129  virtual void addConditioningApriori(const IdCondSet< ALLOC >& idset,
130  std::vector< double, ALLOC< double > >& counts)
131  = 0;
132 
133  /// returns the allocator used by the internal apriori
135 
136  /// @}
137 
138 
139  protected:
140  /// the weight of the apriori
141  double weight_{1.0};
142 
143  /// a reference to the database in order to have access to its variables
145 
146  /** @brief a mapping from the NodeIds of the variables to the indices of
147  * the columns in the database */
149 
150 
151  /// copy constructor
152  Apriori(const Apriori< ALLOC >& from);
153 
154  /// copy constructor with a given allocator
155  Apriori(const Apriori< ALLOC >& from, const allocator_type& alloc);
156 
157  /// move constructor
158  Apriori(Apriori< ALLOC >&& from);
159 
160  /// move constructor with a given allocator
161  Apriori(Apriori< ALLOC >&& from, const allocator_type& alloc);
162 
163  /// copy operator
164  Apriori< ALLOC >& operator=(const Apriori< ALLOC >& from);
165 
166  /// move operator
167  Apriori< ALLOC >& operator=(Apriori< ALLOC >&& from);
168  };
169 
170  } /* namespace learning */
171 
172 } /* namespace gum */
173 
174 /// include the template implementation
175 #include <agrum/BN/learning/aprioris/apriori_tpl.h>
176 
177 #endif /* GUM_LEARNING_A_PRIORI_H */
Apriori(const Apriori< ALLOC > &from, const allocator_type &alloc)
copy constructor with a given allocator
virtual void setWeight(const double weight)
sets the weight of the a priori (kind of effective sample size)
virtual const std::string & getType() const =0
returns the type of the apriori
virtual bool isOfType(const std::string &type)=0
indicates whether an apriori is of a certain type
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
double weight_
the weight of the apriori
Definition: apriori.h:141
virtual void addConditioningApriori(const IdCondSet< ALLOC > &idset, std::vector< double, ALLOC< double > > &counts)=0
adds the apriori to a counting vectordefined over the right hand side of the idset ...
virtual Apriori< ALLOC > * clone() const =0
virtual copy constructor
Apriori(Apriori< ALLOC > &&from, const allocator_type &alloc)
move constructor with a given allocator
double weight() const
returns the weight assigned to the apriori
Apriori< ALLOC > & operator=(const Apriori< ALLOC > &from)
copy operator
virtual Apriori< ALLOC > * clone(const allocator_type &alloc) const =0
virtual copy constructor with a given allocator
Bijection< NodeId, std::size_t, ALLOC< std::size_t > > nodeId2columns_
a mapping from the NodeIds of the variables to the indices of the columns in the database ...
Definition: apriori.h:148
virtual bool isInformative() const =0
indicates whether the apriori is potentially informative
allocator_type getAllocator() const
returns the allocator used by the internal apriori
const DatabaseTable< ALLOC > * database_
a reference to the database in order to have access to its variables
Definition: apriori.h:144
Apriori(const Apriori< ALLOC > &from)
copy constructor
Apriori(Apriori< ALLOC > &&from)
move constructor
virtual void addAllApriori(const IdCondSet< ALLOC > &idset, std::vector< double, ALLOC< double > > &counts)=0
adds the apriori to a counting vector corresponding to the idset
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
Apriori(const DatabaseTable< ALLOC > &database, 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
virtual ~Apriori()
destructor
Apriori< ALLOC > & operator=(Apriori< ALLOC > &&from)
move operator