51#ifndef DOXYGEN_SHOULD_SKIP_THIS
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) {
63 const Sequence< const DiscreteVariable* >& vars = pot.variablesSequence();
64 if (vars.size() == 0) {
GUM_ERROR(SizeError,
"the tensor contains no variable") }
67 const auto& node2cols =
counter_.nodeId2Columns();
68 if (node2cols.empty()) {
69 if (
database.domainSize(target_node) != vars[0]->domainSize()) {
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));
76 for (std::size_t i = 1; i < vars.size(); ++i) {
77 if (
database.domainSize(conditioning_nodes[i - 1]) != vars[i]->domainSize()) {
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]));
87 std::size_t col = node2cols.second(target_node);
88 if (
database.domainSize(col) != vars[0]->domainSize()) {
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));
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()) {
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 "
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);
118 std::vector< double > params;
119 double log_likelihood = 0.0;
120 if (compute_log_likelihood) {
122 params = std::move(xparams).first;
123 log_likelihood = xparams.second;
125 params =
parameters(target_node, conditioning_nodes);
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]);
134 pot.fillWith(xparams);
135 return log_likelihood;
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);
147 std::vector< double > params;
148 double log_likelihood = 0.0;
149 if (compute_log_likelihood) {
151 params = std::move(xparams).first;
152 log_likelihood = xparams.second;
154 params =
parameters(target_node, conditioning_nodes);
157 pot.fillWith(params);
158 return log_likelihood;
162 template < GUM_Numeric GUM_SCALAR >
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);
171 template < GUM_Numeric GUM_SCALAR >
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)
Size NodeId
Type for node ids.
include the inlined functions if necessary
gum is the global namespace for all aGrUM entities
the base class for estimating parameters of CPTs