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