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