28 #ifndef DOXYGEN_SHOULD_SKIP_THIS 36 template <
template <
typename >
class ALLOC >
38 const DatabaseTable< ALLOC >& learning_db,
39 const DBRowGeneratorParser< ALLOC >& apriori_parser,
40 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
44 Apriori< ALLOC >(apriori_parser.database(),
45 Bijection<
NodeId,
std::size_t, ALLOC<
std::size_t > >(),
50 ALLOC<
std::pair<
std::size_t,
std::size_t > > >(alloc),
55 const DatabaseTable< ALLOC >& apriori_db = apriori_parser.database();
56 const auto& apriori_names = apriori_db.variableNames();
57 const std::size_t apriori_size = apriori_names.size();
58 HashTable< std::string, std::size_t > names2col(apriori_size);
59 for (std::size_t i = std::size_t(0); i < apriori_size; ++i)
60 names2col.insert(apriori_names[i], i);
62 const auto& learning_names = learning_db.variableNames();
63 const std::size_t learning_size = learning_names.size();
64 HashTable< std::size_t, std::size_t > learning2apriori_index(learning_size);
65 bool different_index =
false;
66 for (std::size_t i = std::size_t(0); i < learning_size; ++i) {
68 std::size_t apriori_col;
70 apriori_col = names2col[learning_names[i]];
73 "Variable " << learning_names[i]
74 <<
" could not be found in the apriori database");
78 const Variable& learning_var = learning_db.variable(i);
79 const Variable& apriori_var = apriori_db.variable(apriori_col);
80 if (learning_var.varType() != apriori_var.varType()) {
84 <<
" has not the same type in the learning database " 85 " and the apriori database");
87 if (learning_var.domain() != apriori_var.domain()) {
89 "Variable " << learning_names[i] <<
" has domain " 90 << learning_var.domain()
91 <<
" in the learning database and domain " 92 << apriori_var.domain()
93 <<
" in the aprioi database");
97 learning2apriori_index.insert(i, apriori_col);
98 if (i != apriori_col) different_index =
true;
104 if (!different_index) {
107 if (nodeId2columns.empty()) {
108 for (std::size_t i = std::size_t(0); i < learning_size; ++i) {
112 for (
auto iter = nodeId2columns.begin(); iter != nodeId2columns.end();
115 learning2apriori_index[iter.second()]);
121 std::vector< std::pair< std::size_t, std::size_t >,
122 ALLOC< std::pair< std::size_t, std::size_t > > >
124 RecordCounter< ALLOC > good_counter(
126 __counter = std::move(good_counter);
128 if (apriori_db.nbRows() == std::size_t(0))
129 __internal_weight = 0.0;
131 __internal_weight = this->
_weight / apriori_db.nbRows();
138 template <
template <
typename >
class ALLOC >
140 const AprioriDirichletFromDatabase< ALLOC >& from,
144 __counter(from.__counter, alloc),
145 __internal_weight(from.__internal_weight) {
151 template <
template <
typename >
class ALLOC >
153 const AprioriDirichletFromDatabase< ALLOC >& from) :
158 template <
template <
typename >
class ALLOC >
160 AprioriDirichletFromDatabase< ALLOC >&& from,
164 __counter(
std::move(from.__counter), alloc),
165 __internal_weight(from.__internal_weight) {
171 template <
template <
typename >
class ALLOC >
173 AprioriDirichletFromDatabase< ALLOC >&& from) :
179 template <
template <
typename >
class ALLOC >
180 AprioriDirichletFromDatabase< ALLOC >*
184 ALLOC< AprioriDirichletFromDatabase< ALLOC > > allocator(alloc);
185 AprioriDirichletFromDatabase< ALLOC >* apriori = allocator.allocate(1);
187 allocator.construct(apriori, *
this, alloc);
189 allocator.deallocate(apriori, 1);
198 template <
template <
typename >
class ALLOC >
199 INLINE AprioriDirichletFromDatabase< ALLOC >*
206 template <
template <
typename >
class ALLOC >
213 template <
template <
typename >
class ALLOC >
214 INLINE AprioriDirichletFromDatabase< ALLOC >&
216 operator=(
const AprioriDirichletFromDatabase< ALLOC >& from) {
219 __counter = from.__counter;
220 __internal_weight = from.__internal_weight;
227 template <
template <
typename >
class ALLOC >
228 INLINE AprioriDirichletFromDatabase< ALLOC >&
230 operator=(AprioriDirichletFromDatabase< ALLOC >&& from) {
233 __counter = std::move(from.__counter);
234 __internal_weight = from.__internal_weight;
241 template <
template <
typename >
class ALLOC >
249 template <
template <
typename >
class ALLOC >
250 INLINE
const std::string&
257 template <
template <
typename >
class ALLOC >
264 template <
template <
typename >
class ALLOC >
268 if (__counter.database().nbRows() == 0.0)
269 __internal_weight = 0.0;
271 __internal_weight = this->
_weight / __counter.database().nbRows();
276 template <
template <
typename >
class ALLOC >
278 const IdSet< ALLOC >& idset,
279 std::vector<
double, ALLOC< double > >& counts) {
280 if (this->
_weight == 0.0)
return;
282 const auto& apriori = __counter.counts(idset);
283 const std::size_t size = apriori.size();
284 if (__internal_weight != 1.0) {
285 for (std::size_t i = std::size_t(0); i < size; ++i) {
286 counts[i] += apriori[i] * __internal_weight;
289 for (std::size_t i = std::size_t(0); i < size; ++i) {
290 counts[i] += apriori[i];
297 template <
template <
typename >
class ALLOC >
299 const IdSet< ALLOC >& idset,
300 std::vector<
double, ALLOC< double > >& counts) {
301 if (__internal_weight == 0.0)
return;
303 const auto& apriori = __counter.counts(idset.conditionalIdSet());
304 const std::size_t size = apriori.size();
305 if (__internal_weight != 1.0) {
306 for (std::size_t i = std::size_t(0); i < size; ++i) {
307 counts[i] += apriori[i] * __internal_weight;
310 for (std::size_t i = std::size_t(0); i < size; ++i) {
311 counts[i] += apriori[i];
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
virtual void setWeight(const double weight)
sets the weight of the a priori (kind of effective sample size)
virtual bool isOfType(const std::string &type) final
indicates whether an apriori is of a certain type
virtual AprioriDirichletFromDatabase< ALLOC > * clone() const
virtual copy constructor
AprioriDirichletType type
the type of the a priori
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual bool isInformative() const final
indicates whether the apriori is potentially informative
virtual void setWeight(const double weight) final
sets the weight of the a priori (kind of effective sample size)
static bool isOfType(const std::string &t)
double weight() const
returns the weight assigned to the apriori
virtual void addConditioningApriori(const IdSet< ALLOC > &idset, std::vector< double, ALLOC< double > > &counts) final
adds the apriori to a counting vectordefined over the right hand side of the idset ...
virtual ~AprioriDirichletFromDatabase()
destructor
double _weight
the weight of the apriori
Apriori< ALLOC > & operator=(const Apriori< ALLOC > &from)
copy operator
AprioriDirichletFromDatabase(const DatabaseTable< ALLOC > &learning_db, const DBRowGeneratorParser< ALLOC > &apriori_parser, 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
allocator_type getAllocator() const
returns the allocator used by the internal apriori
virtual const std::string & getType() const final
returns the type of the apriori
Bijection< NodeId, std::size_t, ALLOC< std::size_t > > _nodeId2columns
a mapping from the NodeIds of the variables to the indices of the columns in the database ...
static const std::string type
virtual void addAllApriori(const IdSet< ALLOC > &idset, std::vector< double, ALLOC< double > > &counts) final
adds the apriori to a counting vector corresponding to the idset
Size NodeId
Type for node ids.
Apriori(const DatabaseTable< ALLOC > &database, 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
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
#define GUM_ERROR(type, msg)
AprioriDirichletFromDatabase< ALLOC > & operator=(const AprioriDirichletFromDatabase< ALLOC > &from)
copy operator