aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
paramEstimator_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 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-2026 *
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#pragma once
42
43
49
50#include <agrum/BN/learning/paramUtils/paramEstimator.h> // to ease IDE parser
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53namespace gum {
54
55 namespace learning {
56
57 // check the coherency between the parameters passed to setParameters functions
58 template < GUM_Numeric GUM_SCALAR >
59 void ParamEstimator::_checkParameters_(const NodeId target_node,
60 const std::vector< NodeId >& conditioning_nodes,
61 Tensor< GUM_SCALAR >& pot) {
62 // check that the nodes passed in arguments correspond to those of pot
63 const Sequence< const DiscreteVariable* >& vars = pot.variablesSequence();
64 if (vars.size() == 0) { GUM_ERROR(SizeError, "the tensor contains no variable") }
65
66 const auto& database = counter_.database();
67 const auto& node2cols = counter_.nodeId2Columns();
68 if (node2cols.empty()) {
69 if (database.domainSize(target_node) != vars[0]->domainSize()) {
70 GUM_ERROR(SizeError,
71 "Variable " << vars[0]->name() << "of the tensor to be filled "
72 << "has a domain size of " << vars[0]->domainSize()
73 << ", which is different from that of node " << target_node
74 << " which is equal to " << database.domainSize(target_node));
75 }
76 for (std::size_t i = 1; i < vars.size(); ++i) {
77 if (database.domainSize(conditioning_nodes[i - 1]) != vars[i]->domainSize()) {
78 GUM_ERROR(SizeError,
79 "Variable " << vars[i]->name() << "of the tensor to be filled "
80 << "has a domain size of " << vars[i]->domainSize()
81 << ", which is different from that of node "
82 << conditioning_nodes[i - 1] << " which is equal to "
83 << database.domainSize(conditioning_nodes[i - 1]));
84 }
85 }
86 } else {
87 std::size_t col = node2cols.second(target_node);
88 if (database.domainSize(col) != vars[0]->domainSize()) {
89 GUM_ERROR(SizeError,
90 "Variable " << vars[0]->name() << "of the tensor to be filled "
91 << "has a domain size of " << vars[0]->domainSize()
92 << ", which is different from that of node " << target_node
93 << " which is equal to " << database.domainSize(col));
94 }
95 for (std::size_t i = 1; i < vars.size(); ++i) {
96 col = node2cols.second(conditioning_nodes[i - 1]);
97 if (database.domainSize(col) != vars[i]->domainSize()) {
98 GUM_ERROR(SizeError,
99 "Variable " << vars[i]->name() << "of the tensor to be filled "
100 << "has a domain size of " << vars[i]->domainSize()
101 << ", which is different from that of node "
102 << conditioning_nodes[i - 1] << " which is equal to "
103 << database.domainSize(col));
104 }
105 }
106 }
107 }
108
110 template < GUM_Numeric GUM_SCALAR >
111 typename std::enable_if< !std::is_same< GUM_SCALAR, double >::value, double >::type
112 ParamEstimator::_setParameters_(const NodeId target_node,
113 const std::vector< NodeId >& conditioning_nodes,
114 Tensor< GUM_SCALAR >& pot,
115 const bool compute_log_likelihood) {
116 _checkParameters_(target_node, conditioning_nodes, pot);
117
118 std::vector< double > params;
119 double log_likelihood = 0.0;
120 if (compute_log_likelihood) {
121 const auto xparams = parametersAndLogLikelihood(target_node, conditioning_nodes);
122 params = std::move(xparams).first;
123 log_likelihood = xparams.second;
124 } else {
125 params = parameters(target_node, conditioning_nodes);
126 }
127
128 // transform the vector of double into a vector of GUM_SCALAR
129 const std::size_t size = params.size();
130 std::vector< GUM_SCALAR > xparams(size);
131 for (std::size_t i = std::size_t(0); i < size; ++i)
132 xparams[i] = GUM_SCALAR(params[i]);
133
134 pot.fillWith(xparams);
135 return log_likelihood;
136 }
137
139 template < GUM_Numeric GUM_SCALAR >
140 typename std::enable_if< std::is_same< GUM_SCALAR, double >::value, double >::type
141 ParamEstimator::_setParameters_(const NodeId target_node,
142 const std::vector< NodeId >& conditioning_nodes,
143 Tensor< GUM_SCALAR >& pot,
144 const bool compute_log_likelihood) {
145 _checkParameters_(target_node, conditioning_nodes, pot);
146
147 std::vector< double > params;
148 double log_likelihood = 0.0;
149 if (compute_log_likelihood) {
150 const auto xparams = parametersAndLogLikelihood(target_node, conditioning_nodes);
151 params = std::move(xparams).first;
152 log_likelihood = xparams.second;
153 } else {
154 params = parameters(target_node, conditioning_nodes);
155 }
156
157 pot.fillWith(params);
158 return log_likelihood;
159 }
160
162 template < GUM_Numeric GUM_SCALAR >
163 double ParamEstimator::setParameters(const NodeId target_node,
164 const std::vector< NodeId >& conditioning_nodes,
165 Tensor< GUM_SCALAR >& pot,
166 const bool compute_log_likelihood) {
167 return _setParameters_(target_node, conditioning_nodes, pot, compute_log_likelihood);
168 }
169
171 template < GUM_Numeric GUM_SCALAR >
172 void ParamEstimator::setBayesNet(const BayesNet< GUM_SCALAR >& new_bn) {
173 counter_.setBayesNet(new_bn);
174 }
175
176
177 } /* namespace learning */
178
179} /* namespace gum */
180
181#endif /* DOXYGEN_SHOULD_SKIP_THIS */
RecordCounter counter_
the record counter used to parse the database
double setParameters(const NodeId target_node, const std::vector< NodeId > &conditioning_nodes, Tensor< GUM_SCALAR > &pot, const bool compute_log_likelihood=false)
sets a CPT's parameters and, possibly, return its log-likelihhod
void setBayesNet(const BayesNet< GUM_SCALAR > &new_bn)
assign a new Bayes net to all the counter's generators depending on a BN
std::pair< std::vector< double >, double > parametersAndLogLikelihood(const NodeId target_node)
returns the parameters of a CPT as well as its log-likelihood
std::vector< double > parameters(const NodeId target_node)
returns the CPT's parameters corresponding to a given target node
const DatabaseTable & database() const
returns the database on which we perform the counts
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size NodeId
Type for node ids.
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
the base class for estimating parameters of CPTs