aGrUM 2.3.2
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-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#pragma once
41
42
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51namespace gum {
52
53 namespace learning {
54
55 // check the coherency between the parameters passed to setParameters functions
56 template < typename GUM_SCALAR >
57 void ParamEstimator::_checkParameters_(const NodeId target_node,
58 const std::vector< NodeId >& conditioning_nodes,
59 Tensor< GUM_SCALAR >& pot) {
60 // check that the nodes passed in arguments correspond to those of pot
61 const Sequence< const DiscreteVariable* >& vars = pot.variablesSequence();
62 if (vars.size() == 0) { GUM_ERROR(SizeError, "the tensor contains no variable") }
63
64 const auto& database = counter_.database();
65 const auto& node2cols = counter_.nodeId2Columns();
66 if (node2cols.empty()) {
67 if (database.domainSize(target_node) != vars[0]->domainSize()) {
68 GUM_ERROR(SizeError,
69 "Variable " << vars[0]->name() << "of the tensor to be filled "
70 << "has a domain size of " << vars[0]->domainSize()
71 << ", which is different from that of node " << target_node
72 << " which is equal to " << database.domainSize(target_node));
73 }
74 for (std::size_t i = 1; i < vars.size(); ++i) {
75 if (database.domainSize(conditioning_nodes[i - 1]) != vars[i]->domainSize()) {
76 GUM_ERROR(SizeError,
77 "Variable " << vars[i]->name() << "of the tensor to be filled "
78 << "has a domain size of " << vars[i]->domainSize()
79 << ", which is different from that of node "
80 << conditioning_nodes[i - 1] << " which is equal to "
81 << database.domainSize(conditioning_nodes[i - 1]));
82 }
83 }
84 } else {
85 std::size_t col = node2cols.second(target_node);
86 if (database.domainSize(col) != vars[0]->domainSize()) {
87 GUM_ERROR(SizeError,
88 "Variable " << vars[0]->name() << "of the tensor to be filled "
89 << "has a domain size of " << vars[0]->domainSize()
90 << ", which is different from that of node " << target_node
91 << " which is equal to " << database.domainSize(col));
92 }
93 for (std::size_t i = 1; i < vars.size(); ++i) {
94 col = node2cols.second(conditioning_nodes[i - 1]);
95 if (database.domainSize(col) != vars[i]->domainSize()) {
96 GUM_ERROR(SizeError,
97 "Variable " << vars[i]->name() << "of the tensor to be filled "
98 << "has a domain size of " << vars[i]->domainSize()
99 << ", which is different from that of node "
100 << conditioning_nodes[i - 1] << " which is equal to "
101 << database.domainSize(col));
102 }
103 }
104 }
105 }
106
108 template < typename GUM_SCALAR >
109 INLINE typename std::enable_if< !std::is_same< GUM_SCALAR, double >::value, double >::type
110 ParamEstimator::_setParameters_(const NodeId target_node,
111 const std::vector< NodeId >& conditioning_nodes,
112 Tensor< GUM_SCALAR >& pot,
113 const bool compute_log_likelihood) {
114 _checkParameters_(target_node, conditioning_nodes, pot);
115
116 std::vector< double > params;
117 double log_likelihood = 0.0;
118 if (compute_log_likelihood) {
119 const auto xparams = parametersAndLogLikelihood(target_node, conditioning_nodes);
120 params = std::move(xparams).first;
121 log_likelihood = xparams.second;
122 } else {
123 params = parameters(target_node, conditioning_nodes);
124 }
125
126 // transform the vector of double into a vector of GUM_SCALAR
127 const std::size_t size = params.size();
128 std::vector< GUM_SCALAR > xparams(size);
129 for (std::size_t i = std::size_t(0); i < size; ++i)
130 xparams[i] = GUM_SCALAR(params[i]);
131
132 pot.fillWith(xparams);
133 return log_likelihood;
134 }
135
137 template < typename GUM_SCALAR >
138 INLINE typename std::enable_if< std::is_same< GUM_SCALAR, double >::value, double >::type
139 ParamEstimator::_setParameters_(const NodeId target_node,
140 const std::vector< NodeId >& conditioning_nodes,
141 Tensor< GUM_SCALAR >& pot,
142 const bool compute_log_likelihood) {
143 _checkParameters_(target_node, conditioning_nodes, pot);
144
145 std::vector< double > params;
146 double log_likelihood = 0.0;
147 if (compute_log_likelihood) {
148 const auto xparams = parametersAndLogLikelihood(target_node, conditioning_nodes);
149 params = std::move(xparams).first;
150 log_likelihood = xparams.second;
151 } else {
152 params = parameters(target_node, conditioning_nodes);
153 }
154
155 pot.fillWith(params);
156 return log_likelihood;
157 }
158
160 template < typename GUM_SCALAR >
161 INLINE double ParamEstimator::setParameters(const NodeId target_node,
162 const std::vector< NodeId >& conditioning_nodes,
163 Tensor< GUM_SCALAR >& pot,
164 const bool compute_log_likelihood) {
165 return _setParameters_(target_node, conditioning_nodes, pot, compute_log_likelihood);
166 }
167
169 template < typename GUM_SCALAR >
170 INLINE void ParamEstimator::setBayesNet(const BayesNet< GUM_SCALAR >& new_bn) {
171 counter_.setBayesNet(new_bn);
172 }
173
174
175 } /* namespace learning */
176
177} /* namespace gum */
178
179#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:72
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