aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
aprioriDirichletFromDatabase.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 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,
83  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
85  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
87 
88  /// copy constructor
90  const AprioriDirichletFromDatabase< ALLOC >& from);
91 
92  /// copy constructor with a given allocator
94  const AprioriDirichletFromDatabase< ALLOC >& from,
95  const allocator_type& alloc);
96 
97  /// move constructor
98  AprioriDirichletFromDatabase(AprioriDirichletFromDatabase< ALLOC >&& from);
99 
100  /// move constructor with a given allocator
101  AprioriDirichletFromDatabase(AprioriDirichletFromDatabase< ALLOC >&& from,
102  const allocator_type& alloc);
103 
104  /// virtual copy constructor
105  virtual AprioriDirichletFromDatabase< ALLOC >* clone() const;
106 
107  /// virtual copy constructor with a given allocator
109  clone(const allocator_type& alloc) const;
110 
111  /// destructor
112  virtual ~AprioriDirichletFromDatabase();
113 
114  /// @}
115 
116 
117  // ##########################################################################
118  /// @name Operators
119  // ##########################################################################
120  /// @{
121 
122  /// copy operator
125 
126  /// move operator
129 
130  /// @}
131 
132 
133  // ##########################################################################
134  /// @name Accessors / Modifiers
135  // ##########################################################################
136  /// @{
137 
138  /// indicates whether an apriori is of a certain type
139  virtual bool isOfType(const std::string& type) final;
140 
141  /// returns the type of the apriori
142  virtual const std::string& getType() const final;
143 
144  /// indicates whether the apriori is potentially informative
145  /** Basically, only the NoApriori is uninformative. However, it may happen
146  * that, under some circonstances, an apriori, which is usually not equal
147  * to the NoApriori, becomes equal to it (e.g., when the weight is equal
148  * to zero). In this case, if the apriori can detect this case, it shall
149  * inform the classes that use it that it is temporarily uninformative.
150  * These classes will then be able to speed-up their code by avoiding to
151  * take into account the apriori in their computations. */
152  virtual bool isInformative() const final;
153 
154  /// sets the weight of the a priori (kind of effective sample size)
155  virtual void setWeight(const double weight) final;
156 
157  /// adds the apriori to a counting vector corresponding to the idset
158  /** adds the apriori to an already created counting vector defined over
159  * the union of the variables on both the left and right hand side of the
160  * conditioning bar of the idset.
161  * @warning the method assumes that the size of the vector is exactly
162  * the domain size of the joint variables set. */
163  virtual void
164  addAllApriori(const IdCondSet< ALLOC >& idset,
165  std::vector< double, ALLOC< double > >& counts) final;
166 
167  /** @brief adds the apriori to a counting vectordefined over the right
168  * hand side of the idset
169  *
170  * @warning the method assumes that the size of the vector is exactly
171  * the domain size of the joint RHS variables of the idset. */
172  virtual void addConditioningApriori(
173  const IdCondSet< ALLOC >& idset,
174  std::vector< double, ALLOC< double > >& counts) final;
175 
176  /// @}
177 
178 
179 #ifndef DOXYGEN_SHOULD_SKIP_THIS
180 
181  private:
182  // the record counter used to parse the apriori database
183  RecordCounter< ALLOC > counter__;
184 
185  // the internal weight is equal to weight_ / nb rows of apriori database
186  // this internal weight is used to ensure that assigning a weight of 1
187  // to the apriori is equivalent to adding just one row to the learning
188  // database
189  double internal_weight__;
190 
191 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
192  };
193 
194  } /* namespace learning */
195 
196 } /* namespace gum */
197 
198 /// include the template implementation
199 #include <agrum/BN/learning/aprioris/aprioriDirichletFromDatabase_tpl.h>
200 
201 #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:669
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