28 #ifndef DOXYGEN_SHOULD_SKIP_THIS 30 # include <agrum/BN/learning/scores_and_tests/scoreBD.h> 38 template <
template <
typename >
class ALLOC >
39 INLINE ScoreBD< ALLOC >::ScoreBD(
40 const DBRowGeneratorParser< ALLOC >& parser,
41 const Apriori< ALLOC >& apriori,
42 const std::vector< std::pair< std::size_t, std::size_t >,
43 ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
44 const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
46 const typename ScoreBD< ALLOC >::allocator_type& alloc) :
47 Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
48 internal_apriori__(parser.database(), nodeId2columns) {
49 GUM_CONSTRUCTOR(ScoreBD);
54 template <
template <
typename >
class ALLOC >
55 INLINE ScoreBD< ALLOC >::ScoreBD(
56 const DBRowGeneratorParser< ALLOC >& parser,
57 const Apriori< ALLOC >& apriori,
58 const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
60 const typename ScoreBD< ALLOC >::allocator_type& alloc) :
61 Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
62 internal_apriori__(parser.database(), nodeId2columns) {
63 GUM_CONSTRUCTOR(ScoreBD);
68 template <
template <
typename >
class ALLOC >
69 INLINE ScoreBD< ALLOC >::ScoreBD(
70 const ScoreBD< ALLOC >& from,
71 const typename ScoreBD< ALLOC >::allocator_type& alloc) :
72 Score< ALLOC >(from, alloc),
73 internal_apriori__(from.internal_apriori__, alloc),
74 gammalog2__(from.gammalog2__) {
75 GUM_CONS_CPY(ScoreBD);
80 template <
template <
typename >
class ALLOC >
81 INLINE ScoreBD< ALLOC >::ScoreBD(
const ScoreBD< ALLOC >& from) :
82 ScoreBD< ALLOC >(from, from.getAllocator()) {}
86 template <
template <
typename >
class ALLOC >
87 INLINE ScoreBD< ALLOC >::ScoreBD(
88 ScoreBD< ALLOC >&& from,
89 const typename ScoreBD< ALLOC >::allocator_type& alloc) :
90 Score< ALLOC >(std::move(from), alloc),
91 internal_apriori__(std::move(from.internal_apriori__), alloc),
92 gammalog2__(std::move(from.gammalog2__)) {
93 GUM_CONS_MOV(ScoreBD);
98 template <
template <
typename >
class ALLOC >
99 INLINE ScoreBD< ALLOC >::ScoreBD(ScoreBD< ALLOC >&& from) :
100 ScoreBD< ALLOC >(std::move(from), from.getAllocator()) {}
104 template <
template <
typename >
class ALLOC >
105 ScoreBD< ALLOC >* ScoreBD< ALLOC >::clone(
106 const typename ScoreBD< ALLOC >::allocator_type& alloc)
const {
107 ALLOC< ScoreBD< ALLOC > > allocator(alloc);
108 ScoreBD< ALLOC >* new_score = allocator.allocate(1);
110 allocator.construct(new_score, *
this, alloc);
112 allocator.deallocate(new_score, 1);
121 template <
template <
typename >
class ALLOC >
122 ScoreBD< ALLOC >* ScoreBD< ALLOC >::clone()
const {
123 return clone(
this->getAllocator());
128 template <
template <
typename >
class ALLOC >
129 ScoreBD< ALLOC >::~ScoreBD() {
130 GUM_DESTRUCTOR(ScoreBD);
135 template <
template <
typename >
class ALLOC >
136 ScoreBD< ALLOC >& ScoreBD< ALLOC >::operator=(
const ScoreBD< ALLOC >& from) {
138 Score< ALLOC >::operator=(from);
139 internal_apriori__ = from.internal_apriori__;
146 template <
template <
typename >
class ALLOC >
147 ScoreBD< ALLOC >& ScoreBD< ALLOC >::operator=(ScoreBD< ALLOC >&& from) {
149 Score< ALLOC >::operator=(std::move(from));
150 internal_apriori__ = std::move(from.internal_apriori__);
157 template <
template <
typename >
class ALLOC >
159 ScoreBD< ALLOC >::isAprioriCompatible(
const std::string& apriori_type,
161 if (apriori_type == AprioriNoAprioriType::type) {
162 return "The BD score requires an apriori";
166 return "The apriori is currently compatible with the BD score but if " 167 "you change the weight, it may become biased";
171 std::stringstream msg;
172 msg <<
"The apriori '" << apriori_type
173 <<
"' is not yet supported by method isAprioriCompatible";
179 template <
template <
typename >
class ALLOC >
181 ScoreBD< ALLOC >::isAprioriCompatible(
const Apriori< ALLOC >& apriori) {
182 return isAprioriCompatible(apriori.getType(), apriori.weight());
187 template <
template <
typename >
class ALLOC >
188 INLINE std::string ScoreBD< ALLOC >::isAprioriCompatible()
const {
189 return isAprioriCompatible(*(
this->apriori_));
194 template <
template <
typename >
class ALLOC >
195 INLINE
const Apriori< ALLOC >& ScoreBD< ALLOC >::internalApriori()
const {
196 return internal_apriori__;
201 template <
template <
typename >
class ALLOC >
202 double ScoreBD< ALLOC >::score_(
const IdCondSet< ALLOC >& idset) {
204 if (!
this->apriori_->isInformative()) {
205 GUM_ERROR(OutOfBounds,
206 "The BD score requires its external apriori to " 207 <<
"be strictly positive");
211 std::vector<
double, ALLOC<
double > > N_ijk(
212 this->counter_.counts(idset,
true));
213 const std::size_t all_size = N_ijk.size();
214 std::vector<
double, ALLOC<
double > > N_prime_ijk(all_size, 0.0);
215 this->apriori_->addAllApriori(idset, N_prime_ijk);
221 if (idset.hasConditioningSet()) {
223 std::vector<
double, ALLOC<
double > > N_ij(
224 this->marginalize_(idset[0], N_ijk));
225 const std::size_t conditioning_size = N_ij.size();
227 std::vector<
double, ALLOC<
double > > N_prime_ij(N_ij.size(), 0.0);
228 this->apriori_->addConditioningApriori(idset, N_prime_ij);
234 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
236 += gammalog2__(N_prime_ij[j]) - gammalog2__(N_ij[j] + N_prime_ij[j]);
238 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
239 score += gammalog2__(N_ijk[k] + N_prime_ijk[k])
240 - gammalog2__(N_prime_ijk[k]);
247 double N_prime = 0.0;
248 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
249 score += gammalog2__(N_ijk[k] + N_prime_ijk[k])
250 - gammalog2__(N_prime_ijk[k]);
252 N_prime += N_prime_ijk[k];
254 score += gammalog2__(N_prime) - gammalog2__(N + N_prime);