aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
scoreBDeu.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 class for computing BDeu scores
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 
28 #ifndef GUM_LEARNING_SCORE_BDEU_H
29 #define GUM_LEARNING_SCORE_BDEU_H
30 
31 #include <string>
32 
33 #include <agrum/agrum.h>
34 #include <agrum/tools/core/math/math_utils.h>
35 #include <agrum/tools/core/math/gammaLog2.h>
36 #include <agrum/BN/learning/scores_and_tests/score.h>
37 #include <agrum/BN/learning/aprioris/aprioriBDeu.h>
38 
39 namespace gum {
40 
41  namespace learning {
42 
43  /** @class ScoreBDeu
44  * @brief the class for computing BDeu scores
45  * @headerfile scoreBDeu.h <agrum/BN/learning/scores_and_tests/scoreBDeu.h>
46  * @ingroup learning_scores
47  *
48  * @warning As BDeu already includes an implicit smoothing apriori on all
49  * the cells of contingency tables, the apriori passed to the score should
50  * be a NoApriori. But aGrUM will let you use another (certainly incompatible)
51  * apriori with the score. In this case, this apriori will be included in
52  * addition to the implicit smoothing apriori in a BD fashion, i.e., we
53  * will ressort to the Bayesian Dirichlet (BD) formula to include the sum of
54  * the two aprioris into the score.
55  *
56  */
57  template < template < typename > class ALLOC = std::allocator >
58  class ScoreBDeu: public Score< ALLOC > {
59  public:
60  /// type for the allocators passed in arguments of methods
62 
63  // ##########################################################################
64  /// @name Constructors / Destructors
65  // ##########################################################################
66  /// @{
67 
68  /// default constructor
69  /** @param parser the parser used to parse the database
70  * @param apriori An apriori that we add to the computation of the score
71  * @param ranges a set of pairs {(X1,Y1),...,(Xn,Yn)} of database's rows
72  * indices. The countings are then performed only on the union of the
73  * rows [Xi,Yi), i in {1,...,n}. This is useful, e.g, when performing
74  * cross validation tasks, in which part of the database should be ignored.
75  * An empty set of ranges is equivalent to an interval [X,Y) ranging over
76  * the whole database.
77  * @param nodeId2Columns a mapping from the ids of the nodes in the
78  * graphical model to the corresponding column in the DatabaseTable
79  * parsed by the parser. This enables estimating from a database in
80  * which variable A corresponds to the 2nd column the parameters of a BN
81  * in which variable A has a NodeId of 5. An empty nodeId2Columns
82  * bijection means that the mapping is an identity, i.e., the value of a
83  * NodeId is equal to the index of the column in the DatabaseTable.
84  * @param alloc the allocator used to allocate the structures within the
85  * Score.
86  * @warning If nodeId2columns is not empty, then only the scores over the
87  * ids belonging to this bijection can be computed: applying method
88  * score() over other ids will raise exception NotFound. */
89  ScoreBDeu(const DBRowGeneratorParser< ALLOC >& parser,
90  const Apriori< ALLOC >& apriori,
91  const std::vector< std::pair< std::size_t, std::size_t >,
92  ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
94  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
96 
97 
98  /// default constructor
99  /** @param parser the parser used to parse the database
100  * @param apriori An apriori that we add to the computation of the score
101  * @param nodeId2Columns a mapping from the ids of the nodes in the
102  * graphical model to the corresponding column in the DatabaseTable
103  * parsed by the parser. This enables estimating from a database in
104  * which variable A corresponds to the 2nd column the parameters of a BN
105  * in which variable A has a NodeId of 5. An empty nodeId2Columns
106  * bijection means that the mapping is an identity, i.e., the value of a
107  * NodeId is equal to the index of the column in the DatabaseTable.
108  * @param alloc the allocator used to allocate the structures within the
109  * Score.
110  * @warning If nodeId2columns is not empty, then only the scores over the
111  * ids belonging to this bijection can be computed: applying method
112  * score() over other ids will raise exception NotFound. */
113  ScoreBDeu(const DBRowGeneratorParser< ALLOC >& parser,
114  const Apriori< ALLOC >& apriori,
116  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
117  const allocator_type& alloc = allocator_type());
118 
119  /// copy constructor
120  ScoreBDeu(const ScoreBDeu< ALLOC >& from);
121 
122  /// copy constructor with a given allocator
123  ScoreBDeu(const ScoreBDeu< ALLOC >& from, const allocator_type& alloc);
124 
125  /// move constructor
126  ScoreBDeu(ScoreBDeu< ALLOC >&& from);
127 
128  /// move constructor with a given allocator
129  ScoreBDeu(ScoreBDeu< ALLOC >&& from, const allocator_type& alloc);
130 
131  /// virtual copy constructor
132  virtual ScoreBDeu< ALLOC >* clone() const;
133 
134  /// virtual copy constructor with a given allocator
135  virtual ScoreBDeu< ALLOC >* clone(const allocator_type& alloc) const;
136 
137  /// destructor
138  virtual ~ScoreBDeu();
139 
140  /// @}
141 
142 
143  // ##########################################################################
144  /// @name Operators
145  // ##########################################################################
146 
147  /// @{
148 
149  /// copy operator
150  ScoreBDeu< ALLOC >& operator=(const ScoreBDeu< ALLOC >& from);
151 
152  /// move operator
154 
155  /// @}
156 
157 
158  // ##########################################################################
159  /// @name Accessors / Modifiers
160  // ##########################################################################
161  /// @{
162 
163  /// indicates whether the apriori is compatible (meaningful) with the score
164  /** The combination of some scores and aprioris can be meaningless. For
165  * instance, adding a Dirichlet apriori to the K2 score is not very
166  * meaningful since K2 corresponds to a BD score with a 1-smoothing
167  * apriori.
168  * aGrUM allows you to perform such combination, but you can check with
169  * method isAprioriCompatible () whether the result the score will give
170  * you is meaningful or not.
171  * @returns a non empty string if the apriori is compatible with the
172  * score.*/
173  virtual std::string isAprioriCompatible() const final;
174 
175  /// returns the internal apriori of the score
176  /** Some scores include an apriori. For instance, the K2 score is a BD
177  * score with a Laplace Apriori ( smoothing(1) ). BDeu is a BD score with
178  * a N'/(r_i * q_i) apriori, where N' is an effective sample size and r_i
179  * is the domain size of the target variable and q_i is the domain size of
180  * the Cartesian product of its parents. The goal of the score's internal
181  * apriori classes is to enable to account for these aprioris outside the
182  * score, e.g., when performing parameter estimation. It is important to
183  * note that, to be meaningful, a structure + parameter learning requires
184  * that the same aprioris are taken into account during structure learning
185  * and parameter learning. */
186  virtual const Apriori< ALLOC >& internalApriori() const final;
187 
188  /// sets the effective sample size of the internal apriori
189  void setEffectiveSampleSize(double ess);
190 
191  /// @}
192 
193 
194  /// indicates whether the apriori is compatible (meaningful) with the score
195  /** @returns a non empty string if the apriori is compatible with the score.
196  */
197  static std::string isAprioriCompatible(const std::string& apriori_type, double weight = 1.0f);
198 
199  /// indicates whether the apriori is compatible (meaningful) with the score
200  /** a non empty string if the apriori is compatible with the score. */
201  static std::string isAprioriCompatible(const Apriori< ALLOC >& apriori);
202 
203 
204  protected:
205  /// returns the score for a given IdCondSet
206  /** @throws OperationNotAllowed is raised if the score does not support
207  * calling method score such an idset (due to too many/too few variables
208  * in the left hand side or the right hand side of the idset). */
209  virtual double score_(const IdCondSet< ALLOC >& idset) final;
210 
211 
212 #ifndef DOXYGEN_SHOULD_SKIP_THIS
213 
214  private:
215  /// the internal apriori of the score
216  AprioriBDeu< ALLOC > _internal_apriori_;
217 
218  /// the log(gamma (n)) function: generalizes log((n-1)!)
219  GammaLog2 _gammalog2_;
220 
221 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
222  };
223 
224  } /* namespace learning */
225 
226 } /* namespace gum */
227 
228 
229 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
230 extern template class gum::learning::ScoreBDeu<>;
231 #endif
232 
233 
234 // always include the template implementation
235 #include <agrum/BN/learning/scores_and_tests/scoreBDeu_tpl.h>
236 
237 #endif /* GUM_LEARNING_SCORE_BDEU_H */
virtual ScoreBDeu< ALLOC > * clone() const
virtual copy constructor
the class for computing BDeu scores
Definition: scoreBDeu.h:58
ScoreBDeu(const ScoreBDeu< ALLOC > &from)
copy constructor
virtual const Apriori< ALLOC > & internalApriori() const final
returns the internal apriori of the score
virtual ScoreBDeu< ALLOC > * clone(const allocator_type &alloc) const
virtual copy constructor with a given allocator
ScoreBDeu(ScoreBDeu< ALLOC > &&from)
move constructor
ScoreBDeu(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &apriori, 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
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
ScoreBDeu< ALLOC > & operator=(ScoreBDeu< ALLOC > &&from)
move operator
virtual ~ScoreBDeu()
destructor
ScoreBDeu(const ScoreBDeu< ALLOC > &from, const allocator_type &alloc)
copy constructor with a given allocator
ScoreBDeu(ScoreBDeu< ALLOC > &&from, const allocator_type &alloc)
move constructor with a given allocator
static std::string isAprioriCompatible(const Apriori< ALLOC > &apriori)
indicates whether the apriori is compatible (meaningful) with the score
void setEffectiveSampleSize(double ess)
sets the effective sample size of the internal apriori
virtual std::string isAprioriCompatible() const final
indicates whether the apriori is compatible (meaningful) with the score
ScoreBDeu< ALLOC > & operator=(const ScoreBDeu< ALLOC > &from)
copy operator
virtual double score_(const IdCondSet< ALLOC > &idset) final
returns the score for a given IdCondSet
static std::string isAprioriCompatible(const std::string &apriori_type, double weight=1.0f)
indicates whether the apriori is compatible (meaningful) with the score
ScoreBDeu(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &apriori, const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > &ranges, 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
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)