aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
paramEstimatorML.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
54# ifdef GUM_NO_INLINE
56# endif /* GUM_NO_INLINE */
57
58namespace gum {
59
60 namespace learning {
61
64
68 return *this;
69 }
70
73 ParamEstimator::operator=(std::move(from));
74 return *this;
75 }
76
78 std::pair< std::vector< double >, double > ParamEstimatorML::_parametersAndLogLikelihood_(
79 const NodeId target_node,
80 const std::vector< NodeId >& conditioning_nodes,
81 const bool compute_log_likelihood) {
82 // create an idset that contains all the nodes in the following order:
83 // first, the target node, then all the conditioning nodes
84 IdCondSet idset(target_node, conditioning_nodes, true);
85
86 // get the counts for all the nodes in the idset and add the external and
87 // score internal priors
88 this->counter_.clear(); // for EM estimations, we need to disable caches
89 const std::vector< double >& original_N_ijk = this->counter_.counts(idset, true);
90 std::vector< double > N_ijk = original_N_ijk;
91 const bool informative_external_prior = this->external_prior_->isInformative();
92 const bool informative_score_internal_prior = this->score_internal_prior_->isInformative();
93
94 // add the priors pseudocounts
95 if (informative_external_prior) this->external_prior_->addJointPseudoCount(idset, N_ijk);
96 if (informative_score_internal_prior)
97 this->score_internal_prior_->addJointPseudoCount(idset, N_ijk);
98 double log_likelihood = 0.0;
99
100 // now, normalize N_ijk
101
102 // here, we distinguish nodesets with conditioning nodes from those
103 // without conditioning nodes
104 if (!conditioning_nodes.empty()) {
105 // get the counts for all the conditioning nodes, and add them the
106 // external and score internal priors
107 std::vector< double > N_ij(this->counter_.counts(idset.conditionalIdCondSet(), false));
108 if (informative_external_prior)
109 this->external_prior_->addConditioningPseudoCount(idset, N_ij);
110 if (informative_score_internal_prior)
111 this->score_internal_prior_->addConditioningPseudoCount(idset, N_ij);
112
113 const std::size_t conditioning_domsize = N_ij.size();
114 const std::size_t target_domsize = N_ijk.size() / conditioning_domsize;
115
116 // check that all conditioning nodes have strictly positive counts
117 for (std::size_t j = std::size_t(0); j < conditioning_domsize; ++j) {
118 if (N_ij[j] == 0) {
119 // get the domain sizes of the conditioning nodes
120 const std::size_t cond_nb = conditioning_nodes.size();
121 std::vector< Idx > cond_domsize(cond_nb);
122
123 const auto& node2cols = this->counter_.nodeId2Columns();
124 const auto& database = this->counter_.database();
125 if (node2cols.empty()) {
126 for (std::size_t i = std::size_t(0); i < cond_nb; ++i) {
127 cond_domsize[i] = database.domainSize(conditioning_nodes[i]);
128 }
129 } else {
130 for (std::size_t i = std::size_t(0); i < cond_nb; ++i) {
131 cond_domsize[i] = database.domainSize(node2cols.second(conditioning_nodes[i]));
132 }
133 }
134
135 // determine the value of each conditioning variable in N_ij[j]
136 std::vector< Idx > offsets(cond_nb);
137 Idx offset = 1;
138 std::size_t i;
139 for (i = std::size_t(0); i < cond_nb; ++i) {
140 offsets[i] = offset;
141 offset *= cond_domsize[i];
142 }
143 std::vector< Idx > values(cond_nb);
144 i = 0;
145 offset = j;
146 for (Idx jj = cond_nb - 1; i < cond_nb; ++i, --jj) {
147 values[jj] = offset / offsets[jj];
148 offset %= offsets[jj];
149 }
150
151 // create the error message
152 std::stringstream str;
153 str << "The conditioning set <";
154 bool deja = false;
155 for (i = std::size_t(0); i < cond_nb; ++i) {
156 if (deja) str << ", ";
157 else deja = true;
158 std::size_t col = node2cols.empty() ? conditioning_nodes[i]
159 : node2cols.second(conditioning_nodes[i]);
160 const DiscreteVariable& var
161 = dynamic_cast< const DiscreteVariable& >(database.variable(col));
162 str << var.name() << "=" << var.labels()[values[i]];
163 }
164 auto target_col = node2cols.empty() ? target_node : node2cols.second(target_node);
165 const Variable& var = database.variable(target_col);
166 str << "> for target node " << var.name()
167 << " never appears in the database. Please consider using "
168 << "priors such as smoothing.";
169
170 GUM_ERROR(DatabaseError, str.str())
171 }
172 }
173
174 // normalize the counts and compute, if needed, the log_likelihood
175 if (compute_log_likelihood) {
176 for (std::size_t j = std::size_t(0), k = std::size_t(0); j < conditioning_domsize; ++j) {
177 for (std::size_t i = std::size_t(0); i < target_domsize; ++i, ++k) {
178 N_ijk[k] /= N_ij[j];
179 if (original_N_ijk[k]) { log_likelihood += original_N_ijk[k] * std::log(N_ijk[k]); }
180 }
181 }
182 } else {
183 for (std::size_t j = std::size_t(0), k = std::size_t(0); j < conditioning_domsize; ++j) {
184 for (std::size_t i = std::size_t(0); i < target_domsize; ++i, ++k) {
185 N_ijk[k] /= N_ij[j];
186 }
187 }
188 }
189 } else {
190 // here, there are no conditioning nodes. Hence N_ijk is the marginal
191 // probability distribution over the target node. To normalize it, it
192 // is sufficient to divide each cell by the sum over all the cells
193 double sum = 0;
194 for (const double n_ijk: N_ijk)
195 sum += n_ijk;
196
197 if (sum != 0) {
198 if (compute_log_likelihood) {
199 for (std::size_t k = std::size_t(0), end = N_ijk.size(); k < end; ++k) {
200 N_ijk[k] /= sum;
201 if (original_N_ijk[k]) { log_likelihood += original_N_ijk[k] * std::log(N_ijk[k]); }
202 }
203 } else {
204 for (double& n_ijk: N_ijk)
205 n_ijk /= sum;
206 }
207 } else {
208 std::stringstream str;
209
210 const auto& node2cols = this->counter_.nodeId2Columns();
211 const auto& database = this->counter_.database();
212 auto target_col = node2cols.empty() ? target_node : node2cols.second(target_node);
213 const Variable& var = database.variable(target_col);
214 str << "No data for target node " << var.name()
215 << ". It is impossible to estimate the parameters by maximum "
216 "likelihood";
217 GUM_ERROR(DatabaseError, str.str())
218 }
219 }
220
221 return {std::move(N_ijk), log_likelihood};
222 }
223
224 } /* namespace learning */
225
226} /* namespace gum */
227
228#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The class for estimating parameters of CPTs using Maximum Likelihood.
ParamEstimatorML & operator=(const ParamEstimatorML &from)
copy operator
virtual ~ParamEstimatorML()
destructor
std::pair< std::vector< double >, double > _parametersAndLogLikelihood_(const NodeId target_node, const std::vector< NodeId > &conditioning_nodes, const bool compute_log_likelihood)
ParamEstimatorML(const DBRowGeneratorParser &parser, const Prior &external_prior, const Prior &_score_internal_prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
RecordCounter counter_
the record counter used to parse the database
ParamEstimator & operator=(const ParamEstimator &from)
copy operator
Prior * score_internal_prior_
if a score was used for learning the structure of the PGM, this is the priori internal to the score
const DatabaseTable & database() const
returns the database on which we perform the counts
Prior * external_prior_
an external a priori
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
the class for estimating parameters of CPTs using Maximum Likelihood
the class for estimating parameters of CPTs using Maximum Likelihood