27 #ifndef DOXYGEN_SHOULD_SKIP_THIS 39 template <
template <
typename >
class ALLOC >
41 const DBRowGeneratorParser< ALLOC >& parser,
42 const Apriori< ALLOC >& external_apriori,
43 const Apriori< ALLOC >& score_internal_apriori,
44 const std::vector< std::pair< std::size_t, std::size_t >,
45 ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
46 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
49 ParamEstimator< ALLOC >(parser,
51 score_internal_apriori,
60 template <
template <
typename >
class ALLOC >
62 const DBRowGeneratorParser< ALLOC >& parser,
63 const Apriori< ALLOC >& external_apriori,
64 const Apriori< ALLOC >& score_internal_apriori,
65 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
70 score_internal_apriori,
78 template <
template <
typename >
class ALLOC >
80 const ParamEstimatorML< ALLOC >& from,
88 template <
template <
typename >
class ALLOC >
90 const ParamEstimatorML< ALLOC >& from) :
95 template <
template <
typename >
class ALLOC >
97 ParamEstimatorML< ALLOC >&& from,
105 template <
template <
typename >
class ALLOC >
107 ParamEstimatorML< ALLOC >&& from) :
112 template <
template <
typename >
class ALLOC >
115 ALLOC< ParamEstimatorML< ALLOC > > allocator(alloc);
116 ParamEstimatorML< ALLOC >* new_score = allocator.allocate(1);
118 allocator.construct(new_score, *
this, alloc);
120 allocator.deallocate(new_score, 1);
129 template <
template <
typename >
class ALLOC >
136 template <
template <
typename >
class ALLOC >
143 template <
template <
typename >
class ALLOC >
145 operator=(
const ParamEstimatorML< ALLOC >& from) {
152 template <
template <
typename >
class ALLOC >
154 operator=(ParamEstimatorML< ALLOC >&& from) {
161 template <
template <
typename >
class ALLOC >
164 const std::vector<
NodeId, ALLOC< NodeId > >& conditioning_nodes) {
167 IdSet< ALLOC > idset(target_node, conditioning_nodes,
true);
171 std::vector< double, ALLOC< double > > N_ijk(
172 this->
_counter.counts(idset,
true));
173 const bool informative_external_apriori =
175 const bool informative_score_internal_apriori =
177 if (informative_external_apriori)
179 if (informative_score_internal_apriori)
187 if (!conditioning_nodes.empty()) {
190 std::vector< double, ALLOC< double > > N_ij(
191 this->
_counter.counts(idset.conditionalIdSet(),
false));
192 if (informative_external_apriori)
194 if (informative_score_internal_apriori)
197 const std::size_t conditioning_domsize = N_ij.size();
198 const std::size_t target_domsize = N_ijk.size() / conditioning_domsize;
200 # ifdef GUM_PARAMESTIMATOR_ERROR_WHEN_NIJ_IS_NULL 202 for (std::size_t j = std::size_t(0); j < conditioning_domsize; ++j) {
205 const std::size_t cond_nb = conditioning_nodes.size();
206 std::vector< Idx > cond_domsize(cond_nb);
208 const auto& node2cols = this->
_counter.nodeId2Columns();
210 if (node2cols.empty()) {
211 for (std::size_t i = std::size_t(0); i < cond_nb; ++i) {
212 cond_domsize[i] =
database.domainSize(conditioning_nodes[i]);
215 for (std::size_t i = std::size_t(0); i < cond_nb; ++i) {
217 database.domainSize(node2cols.second(conditioning_nodes[i]));
222 std::vector< Idx > offsets(cond_nb);
225 for (i = std::size_t(0); i < cond_nb; ++i) {
227 offset *= cond_domsize[i];
229 std::vector< Idx > values(cond_nb);
232 for (
Idx jj = cond_nb - 1; i < cond_nb; ++i, --jj) {
233 values[jj] = offset / offsets[jj];
234 offset %= offsets[jj];
238 std::stringstream str;
239 str <<
"The conditioning set <";
241 for (i = std::size_t(0); i < cond_nb; ++i) {
246 std::size_t col = node2cols.empty()
247 ? conditioning_nodes[i]
248 : node2cols.second(conditioning_nodes[i]);
249 const DiscreteVariable& var =
250 dynamic_cast< const DiscreteVariable&
>(
database.variable(col));
251 str << var.name() <<
"=" << var.labels()[values[i]];
254 node2cols.empty() ? target_node : node2cols.second(target_node);
255 const Variable& var =
database.variable(target_col);
256 str <<
"> for target node " << var.name()
257 <<
" never appears in the database. Please consider using " 258 <<
"priors such as smoothing.";
263 # endif // GUM_PARAMESTIMATOR_ERROR_WHEN_NIJ_IS_NULL 266 for (std::size_t j = std::size_t(0), k = std::size_t(0);
267 j < conditioning_domsize;
269 for (std::size_t i = std::size_t(0); i < target_domsize; ++i, ++k) {
270 # ifdef GUM_PARAMESTIMATOR_ERROR_WHEN_NIJ_IS_NULL 272 # else // GUM_PARAMESTIMATOR_ERROR_WHEN_NIJ_IS_NULL 276 N_ijk[k] = 1.0 / target_domsize;
278 # endif // GUM_PARAMESTIMATOR_ERROR_WHEN_NIJ_IS_NULL 286 for (
const double n_ijk : N_ijk)
290 for (
double& n_ijk : N_ijk)
293 # ifdef GUM_PARAMESTIMATOR_ERROR_WHEN_NIJ_IS_NULL 294 std::stringstream str;
296 const auto& node2cols = this->
_counter.nodeId2Columns();
299 node2cols.empty() ? target_node : node2cols.second(target_node);
300 const Variable& var =
database.variable(target_col);
301 str <<
"No data for target node " << var.name()
302 <<
". It is impossible to estimate the parameters by maximum " 305 # else // GUM_PARAMESTIMATOR_ERROR_WHEN_NIJ_IS_NULL 306 for (
double& n_ijk : N_ijk)
307 n_ijk = 1.0 / N_ijk.size();
308 # endif // GUM_PARAMESTIMATOR_ERROR_WHEN_NIJ_IS_NULL ParamEstimator< ALLOC > & operator=(const ParamEstimator< ALLOC > &from)
copy operator
ParamEstimator(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &external_apriori, const Apriori< ALLOC > &score_internal__apriori, const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > &ranges, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
virtual ~ParamEstimatorML()
destructor
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Apriori< ALLOC > * _score_internal_apriori
if a score was used for learning the structure of the PGM, this is the a priori internal to the score...
Apriori< ALLOC > * _external_apriori
an external a priori
allocator_type getAllocator() const
returns the allocator used by the score
ParamEstimatorML(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &external_apriori, const Apriori< ALLOC > &score_internal__apriori, const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > &ranges, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
ParamEstimatorML< ALLOC > & operator=(const ParamEstimatorML< ALLOC > &from)
copy operator
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
virtual ParamEstimatorML< ALLOC > * clone() const
virtual copy constructor
const DatabaseTable< ALLOC > & database() const
returns the database on which we perform the counts
virtual std::vector< double, ALLOC< double > > parameters(const NodeId target_node, const std::vector< NodeId, ALLOC< NodeId > > &conditioning_nodes)
returns the CPT's parameters corresponding to a given nodeset
Size NodeId
Type for node ids.
RecordCounter< ALLOC > _counter
the record counter used to parse the database
#define GUM_ERROR(type, msg)