aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
apriori.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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,
72  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
74  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
76 
77  /// virtual copy constructor
78  virtual Apriori< ALLOC >* clone() const = 0;
79 
80  /// virtual copy constructor with a given allocator
81  virtual Apriori< ALLOC >* clone(const allocator_type& alloc) const = 0;
82 
83  /// destructor
84  virtual ~Apriori();
85 
86  /// @}
87 
88  // ##########################################################################
89  /// @name Accessors / Modifiers
90  // ##########################################################################
91  /// @{
92 
93  /// sets the weight of the a priori (kind of effective sample size)
94  virtual void setWeight(const double weight);
95 
96  /// returns the weight assigned to the apriori
97  double weight() const;
98 
99  /// indicates whether an apriori is of a certain type
100  virtual bool isOfType(const std::string& type) = 0;
101 
102  /// returns the type of the apriori
103  virtual const std::string& getType() const = 0;
104 
105  /// indicates whether the apriori is potentially informative
106  /** Basically, only the NoApriori is uninformative. However, it may happen
107  * that, under some circonstances, an apriori, which is usually not equal
108  * to the NoApriori, becomes equal to it (e.g., when the weight is equal
109  * to zero). In this case, if the apriori can detect this case, it shall
110  * inform the classes that use it that it is temporarily uninformative.
111  * These classes will then be able to speed-up their code by avoiding to
112  * take into account the apriori in their computations. */
113  virtual bool isInformative() const = 0;
114 
115  /// adds the apriori to a counting vector corresponding to the idset
116  /** adds the apriori to an already created counting vector defined over
117  * the union of the variables on both the left and right hand side of the
118  * conditioning bar of the idset.
119  * @warning the method assumes that the size of the vector is exactly
120  * the domain size of the joint variables set. */
121  virtual void addAllApriori(const IdCondSet< ALLOC >& idset,
122  std::vector< double, ALLOC< double > >& counts)
123  = 0;
124 
125  /** @brief adds the apriori to a counting vectordefined over the right
126  * hand side of the idset
127  *
128  * @warning the method assumes that the size of the vector is exactly
129  * the domain size of the joint RHS variables of the idset. */
130  virtual void
131  addConditioningApriori(const IdCondSet< ALLOC >& idset,
132  std::vector< double, ALLOC< double > >& counts)
133  = 0;
134 
135  /// returns the allocator used by the internal apriori
137 
138  /// @}
139 
140 
141  protected:
142  /// the weight of the apriori
143  double weight_{1.0};
144 
145  /// a reference to the database in order to have access to its variables
147 
148  /** @brief a mapping from the NodeIds of the variables to the indices of
149  * the columns in the database */
151 
152 
153  /// copy constructor
154  Apriori(const Apriori< ALLOC >& from);
155 
156  /// copy constructor with a given allocator
157  Apriori(const Apriori< ALLOC >& from, const allocator_type& alloc);
158 
159  /// move constructor
160  Apriori(Apriori< ALLOC >&& from);
161 
162  /// move constructor with a given allocator
163  Apriori(Apriori< ALLOC >&& from, const allocator_type& alloc);
164 
165  /// copy operator
166  Apriori< ALLOC >& operator=(const Apriori< ALLOC >& from);
167 
168  /// move operator
169  Apriori< ALLOC >& operator=(Apriori< ALLOC >&& from);
170  };
171 
172  } /* namespace learning */
173 
174 } /* namespace gum */
175 
176 /// include the template implementation
177 #include <agrum/BN/learning/aprioris/apriori_tpl.h>
178 
179 #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:669
double weight_
the weight of the apriori
Definition: apriori.h:143
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:150
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:146
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