aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
paramEstimatorML.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 estimating parameters of CPTs using Maximum Likelihood
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef GUM_LEARNING_PARAM_ESTIMATOR_ML_H
28 #define GUM_LEARNING_PARAM_ESTIMATOR_ML_H
29 
30 #include <sstream>
31 
32 #include <agrum/agrum.h>
33 #include <agrum/BN/learning/paramUtils/paramEstimator.h>
34 
35 namespace gum {
36 
37  namespace learning {
38 
39  /** @class ParamEstimatorML
40  * @brief The class for estimating parameters of CPTs using Maximum Likelihood
41  * @headerfile paramEstimatorML.h <agrum/BN/learning/paramUtils/paramEstimatorML.h>
42  * @ingroup learning_param_utils
43  */
44  template < template < typename > class ALLOC = std::allocator >
45  class ParamEstimatorML: public ParamEstimator< ALLOC > {
46  public:
47  /// type for the allocators passed in arguments of methods
48  using allocator_type = ALLOC< NodeId >;
49 
50  // ##########################################################################
51  /// @name Constructors / Destructors
52  // ##########################################################################
53  /// @{
54 
55  /// default constructor
56  /** @param parser the parser used to parse the database
57  * @param external_apriori An apriori that we add to the computation
58  * of the score
59  * @param score_internal_apriori The apriori within the score used
60  * to learn the data structure (might be a NoApriori)
61  * @param ranges a set of pairs {(X1,Y1),...,(Xn,Yn)} of database's rows
62  * indices. The countings are then performed only on the union of the
63  * rows [Xi,Yi), i in {1,...,n}. This is useful, e.g, when performing
64  * cross validation tasks, in which part of the database should be ignored.
65  * An empty set of ranges is equivalent to an interval [X,Y) ranging over
66  * the whole database.
67  * @param nodeId2Columns a mapping from the ids of the nodes in the
68  * graphical model to the corresponding column in the DatabaseTable
69  * parsed by the parser. This enables estimating from a database in
70  * which variable A corresponds to the 2nd column the parameters of a BN
71  * in which variable A has a NodeId of 5. An empty nodeId2Columns
72  * bijection means that the mapping is an identity, i.e., the value of a
73  * NodeId is equal to the index of the column in the DatabaseTable.
74  * @param alloc the allocator used to allocate the structures within the
75  * Score.
76  * @warning If nodeId2columns is not empty, then only the scores over the
77  * ids belonging to this bijection can be computed: applying method
78  * score() over other ids will raise exception NotFound. */
79  ParamEstimatorML(
80  const DBRowGeneratorParser< ALLOC >& parser,
81  const Apriori< ALLOC >& external_apriori,
82  const Apriori< ALLOC >& score_internal__apriori,
83  const std::vector< std::pair< std::size_t, std::size_t >,
84  ALLOC< std::pair< std::size_t, std::size_t > > >&
85  ranges,
86  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
87  nodeId2columns
88  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
89  const allocator_type& alloc = allocator_type());
90 
91  /// default constructor
92  /** @param parser the parser used to parse the database
93  * @param external_apriori An apriori that we add to the computation
94  * of the score
95  * @param score_internal_apriori The apriori within the score used
96  * to learn the data structure (might be a NoApriori)
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. */
109  ParamEstimatorML(
110  const DBRowGeneratorParser< ALLOC >& parser,
111  const Apriori< ALLOC >& external_apriori,
112  const Apriori< ALLOC >& score_internal__apriori,
113  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
114  nodeId2columns
115  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
116  const allocator_type& alloc = allocator_type());
117 
118  /// copy constructor
119  ParamEstimatorML(const ParamEstimatorML< ALLOC >& from);
120 
121  /// copy constructor with a given allocator
122  ParamEstimatorML(const ParamEstimatorML< ALLOC >& from,
123  const allocator_type& alloc);
124 
125  /// move constructor
126  ParamEstimatorML(ParamEstimatorML< ALLOC >&& from);
127 
128  /// move constructor with a given allocator
129  ParamEstimatorML(ParamEstimatorML< ALLOC >&& from,
130  const allocator_type& alloc);
131 
132  /// virtual copy constructor
133  virtual ParamEstimatorML< ALLOC >* clone() const;
134 
135  /// virtual copy constructor with a given allocator
136  virtual ParamEstimatorML< ALLOC >* clone(const allocator_type& alloc) const;
137 
138  /// destructor
139  virtual ~ParamEstimatorML();
140 
141  /// @}
142 
143 
144  // ##########################################################################
145  /// @name Operators
146  // ##########################################################################
147 
148  /// @{
149 
150  /// copy operator
151  ParamEstimatorML< ALLOC >& operator=(const ParamEstimatorML< ALLOC >& from);
152 
153  /// move operator
154  ParamEstimatorML< ALLOC >& operator=(ParamEstimatorML< ALLOC >&& from);
155 
156  /// @}
157 
158 
159  // ##########################################################################
160  /// @name Accessors / Modifiers
161  // ##########################################################################
162  /// @{
163 
164  using ParamEstimator< ALLOC >::parameters;
165 
166  /// returns the CPT's parameters corresponding to a given nodeset
167  /** The vector contains the parameters of an n-dimensional CPT. The
168  * distribution of the dimensions of the CPT within the vector is as
169  * follows:
170  * first, there is the target node, then the conditioning nodes (in the
171  * order in which they were specified).
172  * @throw DatabaseError is raised if some values of the conditioning sets
173  * were not observed in the database. */
174  virtual std::vector< double, ALLOC< double > > parameters(
175  const NodeId target_node,
176  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes);
177 
178  /// @}
179  };
180 
181  } /* namespace learning */
182 
183 } /* namespace gum */
184 
185 /// include the template implementation
186 #include <agrum/BN/learning/paramUtils/paramEstimatorML_tpl.h>
187 
188 #endif /* GUM_LEARNING_PARAM_ESTIMATOR_ML_H */