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 > >& nodeId2columns,
45 const typename ScoreBD< ALLOC >::allocator_type& alloc) :
46 Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
47 _internal_apriori_(parser.database(), nodeId2columns) {
48 GUM_CONSTRUCTOR(ScoreBD);
53 template <
template <
typename >
class ALLOC >
54 INLINE ScoreBD< ALLOC >::ScoreBD(
55 const DBRowGeneratorParser< ALLOC >& parser,
56 const Apriori< ALLOC >& apriori,
57 const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >& nodeId2columns,
58 const typename ScoreBD< ALLOC >::allocator_type& alloc) :
59 Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
60 _internal_apriori_(parser.database(), nodeId2columns) {
61 GUM_CONSTRUCTOR(ScoreBD);
66 template <
template <
typename >
class ALLOC >
67 INLINE ScoreBD< ALLOC >::ScoreBD(
const ScoreBD< ALLOC >& from,
68 const typename ScoreBD< ALLOC >::allocator_type& alloc) :
69 Score< ALLOC >(from, alloc),
70 _internal_apriori_(from._internal_apriori_, alloc), _gammalog2_(from._gammalog2_) {
71 GUM_CONS_CPY(ScoreBD);
76 template <
template <
typename >
class ALLOC >
77 INLINE ScoreBD< ALLOC >::ScoreBD(
const ScoreBD< ALLOC >& from) :
78 ScoreBD< ALLOC >(from, from.getAllocator()) {}
82 template <
template <
typename >
class ALLOC >
83 INLINE ScoreBD< ALLOC >::ScoreBD(ScoreBD< ALLOC >&& from,
84 const typename ScoreBD< ALLOC >::allocator_type& alloc) :
85 Score< ALLOC >(std::move(from), alloc),
86 _internal_apriori_(std::move(from._internal_apriori_), alloc),
87 _gammalog2_(std::move(from._gammalog2_)) {
88 GUM_CONS_MOV(ScoreBD);
93 template <
template <
typename >
class ALLOC >
94 INLINE ScoreBD< ALLOC >::ScoreBD(ScoreBD< ALLOC >&& from) :
95 ScoreBD< ALLOC >(std::move(from), from.getAllocator()) {}
99 template <
template <
typename >
class ALLOC >
101 ScoreBD< ALLOC >::clone(
const typename ScoreBD< ALLOC >::allocator_type& alloc)
const {
102 ALLOC< ScoreBD< ALLOC > > allocator(alloc);
103 ScoreBD< ALLOC >* new_score = allocator.allocate(1);
105 allocator.construct(new_score, *
this, alloc);
107 allocator.deallocate(new_score, 1);
116 template <
template <
typename >
class ALLOC >
117 ScoreBD< ALLOC >* ScoreBD< ALLOC >::clone()
const {
118 return clone(
this->getAllocator());
123 template <
template <
typename >
class ALLOC >
124 ScoreBD< ALLOC >::~ScoreBD() {
125 GUM_DESTRUCTOR(ScoreBD);
130 template <
template <
typename >
class ALLOC >
131 ScoreBD< ALLOC >& ScoreBD< ALLOC >::operator=(
const ScoreBD< ALLOC >& from) {
133 Score< ALLOC >::operator=(from);
134 _internal_apriori_ = from._internal_apriori_;
141 template <
template <
typename >
class ALLOC >
142 ScoreBD< ALLOC >& ScoreBD< ALLOC >::operator=(ScoreBD< ALLOC >&& from) {
144 Score< ALLOC >::operator=(std::move(from));
145 _internal_apriori_ = std::move(from._internal_apriori_);
152 template <
template <
typename >
class ALLOC >
153 std::string ScoreBD< ALLOC >::isAprioriCompatible(
const std::string& apriori_type,
155 if (apriori_type == AprioriNoAprioriType::type) {
return "The BD score requires an apriori"; }
158 return "The apriori is currently compatible with the BD score but if " 159 "you change the weight, it may become biased";
163 std::stringstream msg;
164 msg <<
"The apriori '" << apriori_type <<
"' is not yet compatible with the score 'BD'.";
170 template <
template <
typename >
class ALLOC >
171 INLINE std::string ScoreBD< ALLOC >::isAprioriCompatible(
const Apriori< ALLOC >& apriori) {
172 return isAprioriCompatible(apriori.getType(), apriori.weight());
177 template <
template <
typename >
class ALLOC >
178 INLINE std::string ScoreBD< ALLOC >::isAprioriCompatible()
const {
179 return isAprioriCompatible(*(
this->apriori_));
184 template <
template <
typename >
class ALLOC >
185 INLINE
const Apriori< ALLOC >& ScoreBD< ALLOC >::internalApriori()
const {
186 return _internal_apriori_;
191 template <
template <
typename >
class ALLOC >
192 double ScoreBD< ALLOC >::score_(
const IdCondSet< ALLOC >& idset) {
194 if (!
this->apriori_->isInformative()) {
195 GUM_ERROR(OutOfBounds,
196 "The BD score requires its external apriori to " 197 <<
"be strictly positive");
201 std::vector<
double, ALLOC<
double > > N_ijk(
this->counter_.counts(idset,
true));
202 const std::size_t all_size = N_ijk.size();
203 std::vector<
double, ALLOC<
double > > N_prime_ijk(all_size, 0.0);
204 this->apriori_->addAllApriori(idset, N_prime_ijk);
210 if (idset.hasConditioningSet()) {
212 std::vector<
double, ALLOC<
double > > N_ij(
this->marginalize_(idset[0], N_ijk));
213 const std::size_t conditioning_size = N_ij.size();
215 std::vector<
double, ALLOC<
double > > N_prime_ij(N_ij.size(), 0.0);
216 this->apriori_->addConditioningApriori(idset, N_prime_ij);
222 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
223 score += _gammalog2_(N_prime_ij[j]) - _gammalog2_(N_ij[j] + N_prime_ij[j]);
225 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
226 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k]) - _gammalog2_(N_prime_ijk[k]);
233 double N_prime = 0.0;
234 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
235 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k]) - _gammalog2_(N_prime_ijk[k]);
237 N_prime += N_prime_ijk[k];
239 score += _gammalog2_(N_prime) - _gammalog2_(N + N_prime);