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