25 #ifndef DOXYGEN_SHOULD_SKIP_THIS 32 template <
template <
typename >
class ALLOC >
34 const DBRowGeneratorParser< ALLOC >& parser,
35 const Apriori< ALLOC >& external_apriori,
36 const Apriori< ALLOC >& score_internal_apriori,
37 const std::vector< std::pair< std::size_t, std::size_t >,
38 ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
39 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
42 ParamEstimator< ALLOC >(parser,
44 score_internal_apriori,
53 template <
template <
typename >
class ALLOC >
55 const DBRowGeneratorParser< ALLOC >& parser,
56 const Apriori< ALLOC >& external_apriori,
57 const Apriori< ALLOC >& score_internal_apriori,
58 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
63 score_internal_apriori,
71 template <
template <
typename >
class ALLOC >
73 const ParamEstimatorML< ALLOC >& from,
81 template <
template <
typename >
class ALLOC >
83 const ParamEstimatorML< ALLOC >& from) :
88 template <
template <
typename >
class ALLOC >
90 ParamEstimatorML< ALLOC >&& from,
98 template <
template <
typename >
class ALLOC >
100 ParamEstimatorML< ALLOC >&& from) :
105 template <
template <
typename >
class ALLOC >
108 ALLOC< ParamEstimatorML< ALLOC > > allocator(alloc);
109 ParamEstimatorML< ALLOC >* new_score = allocator.allocate(1);
111 allocator.construct(new_score, *
this, alloc);
113 allocator.deallocate(new_score, 1);
122 template <
template <
typename >
class ALLOC >
129 template <
template <
typename >
class ALLOC >
136 template <
template <
typename >
class ALLOC >
138 operator=(
const ParamEstimatorML< ALLOC >& from) {
145 template <
template <
typename >
class ALLOC >
147 operator=(ParamEstimatorML< ALLOC >&& from) {
154 template <
template <
typename >
class ALLOC >
157 const std::vector<
NodeId, ALLOC< NodeId > >& conditioning_nodes) {
160 IdSet< ALLOC > idset(target_node, conditioning_nodes,
true);
164 std::vector< double, ALLOC< double > > N_ijk(
165 this->
_counter.counts(idset,
true));
166 const bool informative_external_apriori =
168 const bool informative_score_internal_apriori =
170 if (informative_external_apriori)
172 if (informative_score_internal_apriori)
180 if (!conditioning_nodes.empty()) {
183 std::vector< double, ALLOC< double > > N_ij(
184 this->
_counter.counts(idset.conditionalIdSet(),
false));
185 if (informative_external_apriori)
187 if (informative_score_internal_apriori)
190 const std::size_t conditioning_domsize = N_ij.size();
191 const std::size_t target_domsize = N_ijk.size() / conditioning_domsize;
194 for (std::size_t j = std::size_t(0); j < conditioning_domsize; ++j) {
197 const std::size_t cond_nb = conditioning_nodes.size();
198 std::vector< Idx > cond_domsize(cond_nb);
200 const auto& node2cols = this->
_counter.nodeId2Columns();
202 if (node2cols.empty()) {
203 for (std::size_t i = std::size_t(0); i < cond_nb; ++i) {
204 cond_domsize[i] =
database.domainSize(conditioning_nodes[i]);
207 for (std::size_t i = std::size_t(0); i < cond_nb; ++i) {
209 database.domainSize(node2cols.second(conditioning_nodes[i]));
214 std::vector< Idx > offsets(cond_nb);
217 for (i = std::size_t(0); i < cond_nb; ++i) {
219 offset *= cond_domsize[i];
221 std::vector< Idx > values(cond_nb);
224 for (
Idx jj = cond_nb - 1; i < cond_nb; ++i, --jj) {
225 values[jj] = offset / offsets[jj];
226 offset %= offsets[jj];
230 std::stringstream str;
231 str <<
"The conditioning set <";
233 for (i = std::size_t(0); i < cond_nb; ++i) {
238 std::size_t col = node2cols.empty()
239 ? conditioning_nodes[i]
240 : node2cols.second(conditioning_nodes[i]);
241 const DiscreteVariable& var =
242 dynamic_cast< const DiscreteVariable&
>(
database.variable(col));
243 str << var.name() <<
"=" << var.labels()[values[i]];
246 node2cols.empty() ? target_node : node2cols.second(target_node);
247 const Variable& var =
database.variable(target_col);
248 str <<
"> for target node " << var.name()
249 <<
" never appears in the database. Please consider using " 250 <<
"priors such as smoothing.";
257 for (std::size_t j = std::size_t(0), k = std::size_t(0);
258 j < conditioning_domsize;
260 for (std::size_t i = std::size_t(0); i < target_domsize; ++i, ++k) {
269 for (
const double n_ijk : N_ijk)
273 for (
double& n_ijk : N_ijk)
277 "The database being empty, it is impossible " 278 "to estimate the parameters by maximum likelihood");
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
gum is the global namespace for all aGrUM entities
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)