25 #ifndef DOXYGEN_SHOULD_SKIP_THIS 33 template <
template <
typename >
class ALLOC >
35 const DatabaseTable< ALLOC >& learning_db,
36 const DBRowGeneratorParser< ALLOC >& apriori_parser,
37 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
41 Apriori< ALLOC >(apriori_parser.database(),
42 Bijection<
NodeId,
std::size_t, ALLOC<
std::size_t > >(),
47 ALLOC<
std::pair<
std::size_t,
std::size_t > > >(alloc),
52 const DatabaseTable< ALLOC >& apriori_db = apriori_parser.database();
53 const auto& apriori_names = apriori_db.variableNames();
54 const std::size_t apriori_size = apriori_names.size();
55 HashTable< std::string, std::size_t > names2col(apriori_size);
56 for (std::size_t i = std::size_t(0); i < apriori_size; ++i)
57 names2col.insert(apriori_names[i], i);
59 const auto& learning_names = learning_db.variableNames();
60 const std::size_t learning_size = learning_names.size();
61 HashTable< std::size_t, std::size_t > learning2apriori_index(learning_size);
62 bool different_index =
false;
63 for (std::size_t i = std::size_t(0); i < learning_size; ++i) {
65 std::size_t apriori_col;
67 apriori_col = names2col[learning_names[i]];
70 "Variable " << learning_names[i]
71 <<
" could not be found in the apriori database");
75 const Variable& learning_var = learning_db.variable(i);
76 const Variable& apriori_var = apriori_db.variable(apriori_col);
77 if (learning_var.varType() != apriori_var.varType()) {
81 <<
" has not the same type in the learning database " 82 " and the apriori database");
84 if (learning_var.domain() != apriori_var.domain()) {
86 "Variable " << learning_names[i] <<
" has domain " 87 << learning_var.domain()
88 <<
" in the learning database and domain " 89 << apriori_var.domain()
90 <<
" in the aprioi database");
94 learning2apriori_index.insert(i, apriori_col);
95 if (i != apriori_col) different_index =
true;
101 if (!different_index) {
104 if (nodeId2columns.empty()) {
105 for (std::size_t i = std::size_t(0); i < learning_size; ++i) {
109 for (
auto iter = nodeId2columns.begin(); iter != nodeId2columns.end();
112 learning2apriori_index[iter.second()]);
118 std::vector< std::pair< std::size_t, std::size_t >,
119 ALLOC< std::pair< std::size_t, std::size_t > > >
121 RecordCounter< ALLOC > good_counter(
123 __counter = std::move(good_counter);
125 if (apriori_db.nbRows() == std::size_t(0))
126 __internal_weight = 0.0;
128 __internal_weight = this->
_weight / apriori_db.nbRows();
135 template <
template <
typename >
class ALLOC >
137 const AprioriDirichletFromDatabase< ALLOC >& from,
141 __counter(from.__counter, alloc),
142 __internal_weight(from.__internal_weight) {
148 template <
template <
typename >
class ALLOC >
150 const AprioriDirichletFromDatabase< ALLOC >& from) :
155 template <
template <
typename >
class ALLOC >
157 AprioriDirichletFromDatabase< ALLOC >&& from,
161 __counter(
std::move(from.__counter), alloc),
162 __internal_weight(from.__internal_weight) {
168 template <
template <
typename >
class ALLOC >
170 AprioriDirichletFromDatabase< ALLOC >&& from) :
176 template <
template <
typename >
class ALLOC >
177 AprioriDirichletFromDatabase< ALLOC >*
181 ALLOC< AprioriDirichletFromDatabase< ALLOC > > allocator(alloc);
182 AprioriDirichletFromDatabase< ALLOC >* apriori = allocator.allocate(1);
184 allocator.construct(apriori, *
this, alloc);
186 allocator.deallocate(apriori, 1);
195 template <
template <
typename >
class ALLOC >
196 INLINE AprioriDirichletFromDatabase< ALLOC >*
203 template <
template <
typename >
class ALLOC >
210 template <
template <
typename >
class ALLOC >
211 INLINE AprioriDirichletFromDatabase< ALLOC >&
213 operator=(
const AprioriDirichletFromDatabase< ALLOC >& from) {
216 __counter = from.__counter;
217 __internal_weight = from.__internal_weight;
224 template <
template <
typename >
class ALLOC >
225 INLINE AprioriDirichletFromDatabase< ALLOC >&
227 operator=(AprioriDirichletFromDatabase< ALLOC >&& from) {
230 __counter = std::move(from.__counter);
231 __internal_weight = from.__internal_weight;
238 template <
template <
typename >
class ALLOC >
246 template <
template <
typename >
class ALLOC >
247 INLINE
const std::string&
254 template <
template <
typename >
class ALLOC >
261 template <
template <
typename >
class ALLOC >
265 if (__counter.database().nbRows() == 0.0)
266 __internal_weight = 0.0;
268 __internal_weight = this->
_weight / __counter.database().nbRows();
273 template <
template <
typename >
class ALLOC >
275 const IdSet< ALLOC >& idset,
276 std::vector<
double, ALLOC< double > >& counts) {
277 if (this->
_weight == 0.0)
return;
279 const auto& apriori = __counter.counts(idset);
280 const std::size_t size = apriori.size();
281 if (__internal_weight != 1.0) {
282 for (std::size_t i = std::size_t(0); i < size; ++i) {
283 counts[i] += apriori[i] * __internal_weight;
286 for (std::size_t i = std::size_t(0); i < size; ++i) {
287 counts[i] += apriori[i];
294 template <
template <
typename >
class ALLOC >
296 const IdSet< ALLOC >& idset,
297 std::vector<
double, ALLOC< double > >& counts) {
298 if (__internal_weight == 0.0)
return;
300 const auto& apriori = __counter.counts(idset.conditionalIdSet());
301 const std::size_t size = apriori.size();
302 if (__internal_weight != 1.0) {
303 for (std::size_t i = std::size_t(0); i < size; ++i) {
304 counts[i] += apriori[i] * __internal_weight;
307 for (std::size_t i = std::size_t(0); i < size; ++i) {
308 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
gum is the global namespace for all aGrUM entities
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