aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
aprioriDirichletFromDatabase.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 A dirichlet priori: computes its N'_ijk from a database
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef GUM_LEARNING_A_PRIORI_DIRICHLET_FROM_DATABASE_H
28 #define GUM_LEARNING_A_PRIORI_DIRICHLET_FROM_DATABASE_H
29 
30 #include <vector>
31 
32 #include <agrum/agrum.h>
33 #include <agrum/tools/stattests/recordCounter.h>
34 #include <agrum/BN/learning/aprioris/apriori.h>
35 
36 namespace gum {
37 
38  namespace learning {
39 
40  /** @class AprioriDirichletFromDatabase
41  * @brief A dirichlet priori: computes its N'_ijk from a database
42  * @headerfile aprioriDirichletFromDatabase.h <agrum/tools/database/aprioriDirichletFromDatabase.h>
43  * @ingroup learning_apriori
44  */
45  template < template < typename > class ALLOC = std::allocator >
47  public:
48  /// the type of the a priori
50 
51  /// type for the allocators passed in arguments of methods
53 
54 
55  // ##########################################################################
56  /// @name Constructors / Destructors
57  // ##########################################################################
58  /// @{
59 
60  /// default constructor
61  /** @param learning_db the database from which learning is performed.
62  * This is useful to get access to the random variables
63  * @param apriori_parser the parser used to parse the apriori database
64  * @param nodeId2Columns a mapping from the ids of the nodes in the
65  * graphical model to the corresponding column in learning_db.
66  * This enables estimating from a database in which variable A corresponds
67  * to the 2nd column the parameters of a BN in which variable A has a
68  * NodeId of 5. An empty nodeId2Columns bijection means that the mapping
69  * is an identity, i.e., the value of a NodeId is equal to the index of
70  * the column in the DatabaseTable.
71  * @param alloc the allocator used to allocate the structures within the
72  * RecordCounter.
73  *
74  * @throws DatabaseError The apriori database may differ from the learning
75  * database, i.e., the apriori may have more nodes than the learning one.
76  * However, a check is performed to ensure that the variables within the
77  * apriori database that correspond to those in the learning database
78  * (they have the same names) are exactly identical. If this is not the
79  * case, then a DatabaseError exception is raised. */
81  const DatabaseTable< ALLOC >& learning_db,
82  const DBRowGeneratorParser< ALLOC >& apriori_parser,
84  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
86 
87  /// copy constructor
88  AprioriDirichletFromDatabase(const AprioriDirichletFromDatabase< ALLOC >& from);
89 
90  /// copy constructor with a given allocator
91  AprioriDirichletFromDatabase(const AprioriDirichletFromDatabase< ALLOC >& from,
92  const allocator_type& alloc);
93 
94  /// move constructor
95  AprioriDirichletFromDatabase(AprioriDirichletFromDatabase< ALLOC >&& from);
96 
97  /// move constructor with a given allocator
98  AprioriDirichletFromDatabase(AprioriDirichletFromDatabase< ALLOC >&& from,
99  const allocator_type& alloc);
100 
101  /// virtual copy constructor
102  virtual AprioriDirichletFromDatabase< ALLOC >* clone() const;
103 
104  /// virtual copy constructor with a given allocator
105  virtual AprioriDirichletFromDatabase< ALLOC >* clone(const allocator_type& alloc) const;
106 
107  /// destructor
108  virtual ~AprioriDirichletFromDatabase();
109 
110  /// @}
111 
112 
113  // ##########################################################################
114  /// @name Operators
115  // ##########################################################################
116  /// @{
117 
118  /// copy operator
121 
122  /// move operator
125 
126  /// @}
127 
128 
129  // ##########################################################################
130  /// @name Accessors / Modifiers
131  // ##########################################################################
132  /// @{
133 
134  /// indicates whether an apriori is of a certain type
135  virtual bool isOfType(const std::string& type) final;
136 
137  /// returns the type of the apriori
138  virtual const std::string& getType() const final;
139 
140  /// indicates whether the apriori is potentially informative
141  /** Basically, only the NoApriori is uninformative. However, it may happen
142  * that, under some circonstances, an apriori, which is usually not equal
143  * to the NoApriori, becomes equal to it (e.g., when the weight is equal
144  * to zero). In this case, if the apriori can detect this case, it shall
145  * inform the classes that use it that it is temporarily uninformative.
146  * These classes will then be able to speed-up their code by avoiding to
147  * take into account the apriori in their computations. */
148  virtual bool isInformative() const final;
149 
150  /// sets the weight of the a priori (kind of effective sample size)
151  virtual void setWeight(const double weight) final;
152 
153  /// adds the apriori to a counting vector corresponding to the idset
154  /** adds the apriori to an already created counting vector defined over
155  * the union of the variables on both the left and right hand side of the
156  * conditioning bar of the idset.
157  * @warning the method assumes that the size of the vector is exactly
158  * the domain size of the joint variables set. */
159  virtual void addAllApriori(const IdCondSet< ALLOC >& idset,
160  std::vector< double, ALLOC< double > >& counts) final;
161 
162  /** @brief adds the apriori to a counting vectordefined over the right
163  * hand side of the idset
164  *
165  * @warning the method assumes that the size of the vector is exactly
166  * the domain size of the joint RHS variables of the idset. */
167  virtual void addConditioningApriori(const IdCondSet< ALLOC >& idset,
168  std::vector< double, ALLOC< double > >& counts) final;
169 
170  /// @}
171 
172 
173 #ifndef DOXYGEN_SHOULD_SKIP_THIS
174 
175  private:
176  // the record counter used to parse the apriori database
177  RecordCounter< ALLOC > _counter_;
178 
179  // the internal weight is equal to weight_ / nb rows of apriori database
180  // this internal weight is used to ensure that assigning a weight of 1
181  // to the apriori is equivalent to adding just one row to the learning
182  // database
183  double _internal_weight_;
184 
185 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
186  };
187 
188  } /* namespace learning */
189 
190 } /* namespace gum */
191 
192 /// include the template implementation
193 #include <agrum/BN/learning/aprioris/aprioriDirichletFromDatabase_tpl.h>
194 
195 #endif /* GUM_LEARNING_A_PRIORI_DIRICHLET_FROM_DATABASE_H */
AprioriDirichletFromDatabase(const AprioriDirichletFromDatabase< ALLOC > &from)
copy constructor
virtual void addAllApriori(const IdCondSet< ALLOC > &idset, std::vector< double, ALLOC< double > > &counts) final
adds the apriori to a counting vector corresponding to the idset
virtual bool isOfType(const std::string &type) final
indicates whether an apriori is of a certain type
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
virtual AprioriDirichletFromDatabase< ALLOC > * clone() const
virtual copy constructor
virtual void addConditioningApriori(const IdCondSet< ALLOC > &idset, std::vector< double, ALLOC< double > > &counts) final
adds the apriori to a counting vectordefined over the right hand side of the idset ...
virtual bool isInformative() const final
indicates whether the apriori is potentially informative
AprioriDirichletFromDatabase(AprioriDirichletFromDatabase< ALLOC > &&from)
move constructor
A dirichlet priori: computes its N&#39;_ijk from a database.
virtual void setWeight(const double weight) final
sets the weight of the a priori (kind of effective sample size)
AprioriDirichletFromDatabase< ALLOC > & operator=(AprioriDirichletFromDatabase< ALLOC > &&from)
move operator
virtual ~AprioriDirichletFromDatabase()
destructor
AprioriDirichletFromDatabase(const AprioriDirichletFromDatabase< ALLOC > &from, const allocator_type &alloc)
copy constructor with a given allocator
AprioriDirichletFromDatabase(const DatabaseTable< ALLOC > &learning_db, const DBRowGeneratorParser< ALLOC > &apriori_parser, 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
AprioriDirichletFromDatabase(AprioriDirichletFromDatabase< ALLOC > &&from, const allocator_type &alloc)
move constructor with a given allocator
virtual const std::string & getType() const final
returns the type of the apriori
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
virtual AprioriDirichletFromDatabase< ALLOC > * clone(const allocator_type &alloc) const
virtual copy constructor with a given allocator
AprioriDirichletFromDatabase< ALLOC > & operator=(const AprioriDirichletFromDatabase< ALLOC > &from)
copy operator