aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
paramEstimatorML.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 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(const DBRowGeneratorParser< ALLOC >& parser,
80  const Apriori< ALLOC >& external_apriori,
81  const Apriori< ALLOC >& _score_internal_apriori,
82  const std::vector< std::pair< std::size_t, std::size_t >,
83  ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
84  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >& nodeId2columns
85  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
86  const allocator_type& alloc = allocator_type());
87 
88  /// default constructor
89  /** @param parser the parser used to parse the database
90  * @param external_apriori An apriori that we add to the computation
91  * of the score
92  * @param score_internal_apriori The apriori within the score used
93  * to learn the data structure (might be a NoApriori)
94  * @param nodeId2Columns a mapping from the ids of the nodes in the
95  * graphical model to the corresponding column in the DatabaseTable
96  * parsed by the parser. This enables estimating from a database in
97  * which variable A corresponds to the 2nd column the parameters of a BN
98  * in which variable A has a NodeId of 5. An empty nodeId2Columns
99  * bijection means that the mapping is an identity, i.e., the value of a
100  * NodeId is equal to the index of the column in the DatabaseTable.
101  * @param alloc the allocator used to allocate the structures within the
102  * Score.
103  * @warning If nodeId2columns is not empty, then only the scores over the
104  * ids belonging to this bijection can be computed: applying method
105  * score() over other ids will raise exception NotFound. */
106  ParamEstimatorML(const DBRowGeneratorParser< ALLOC >& parser,
107  const Apriori< ALLOC >& external_apriori,
108  const Apriori< ALLOC >& _score_internal_apriori,
109  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >& nodeId2columns
110  = Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(),
111  const allocator_type& alloc = allocator_type());
112 
113  /// copy constructor
114  ParamEstimatorML(const ParamEstimatorML< ALLOC >& from);
115 
116  /// copy constructor with a given allocator
117  ParamEstimatorML(const ParamEstimatorML< ALLOC >& from, const allocator_type& alloc);
118 
119  /// move constructor
120  ParamEstimatorML(ParamEstimatorML< ALLOC >&& from);
121 
122  /// move constructor with a given allocator
123  ParamEstimatorML(ParamEstimatorML< ALLOC >&& from, const allocator_type& alloc);
124 
125  /// virtual copy constructor
126  virtual ParamEstimatorML< ALLOC >* clone() const;
127 
128  /// virtual copy constructor with a given allocator
129  virtual ParamEstimatorML< ALLOC >* clone(const allocator_type& alloc) const;
130 
131  /// destructor
132  virtual ~ParamEstimatorML();
133 
134  /// @}
135 
136 
137  // ##########################################################################
138  /// @name Operators
139  // ##########################################################################
140 
141  /// @{
142 
143  /// copy operator
144  ParamEstimatorML< ALLOC >& operator=(const ParamEstimatorML< ALLOC >& from);
145 
146  /// move operator
147  ParamEstimatorML< ALLOC >& operator=(ParamEstimatorML< ALLOC >&& from);
148 
149  /// @}
150 
151 
152  // ##########################################################################
153  /// @name Accessors / Modifiers
154  // ##########################################################################
155  /// @{
156 
157  using ParamEstimator< ALLOC >::parameters;
158 
159  /// returns the CPT's parameters corresponding to a given nodeset
160  /** The vector contains the parameters of an n-dimensional CPT. The
161  * distribution of the dimensions of the CPT within the vector is as
162  * follows:
163  * first, there is the target node, then the conditioning nodes (in the
164  * order in which they were specified).
165  * @throw DatabaseError is raised if some values of the conditioning sets
166  * were not observed in the database. */
167  virtual std::vector< double, ALLOC< double > >
168  parameters(const NodeId target_node,
169  const std::vector< NodeId, ALLOC< NodeId > >& conditioning_nodes);
170 
171  /// @}
172  };
173 
174  } /* namespace learning */
175 
176 } /* namespace gum */
177 
178 /// include the template implementation
179 #include <agrum/BN/learning/paramUtils/paramEstimatorML_tpl.h>
180 
181 #endif /* GUM_LEARNING_PARAM_ESTIMATOR_ML_H */