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