28 #ifndef DOXYGEN_SHOULD_SKIP_THIS 30 # include <agrum/BN/learning/scores_and_tests/scoreK2.h> 38 template <
template <
typename >
class ALLOC >
39 INLINE ScoreK2< ALLOC >::ScoreK2(
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 ScoreK2< ALLOC >::allocator_type& alloc) :
47 Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
48 internal_apriori__(parser.database(), nodeId2columns) {
49 GUM_CONSTRUCTOR(ScoreK2);
54 template <
template <
typename >
class ALLOC >
55 INLINE ScoreK2< ALLOC >::ScoreK2(
56 const DBRowGeneratorParser< ALLOC >& parser,
57 const Apriori< ALLOC >& apriori,
58 const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
60 const typename ScoreK2< ALLOC >::allocator_type& alloc) :
61 Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
62 internal_apriori__(parser.database(), nodeId2columns) {
63 GUM_CONSTRUCTOR(ScoreK2);
68 template <
template <
typename >
class ALLOC >
69 INLINE ScoreK2< ALLOC >::ScoreK2(
70 const ScoreK2< ALLOC >& from,
71 const typename ScoreK2< ALLOC >::allocator_type& alloc) :
72 Score< ALLOC >(from, alloc),
73 internal_apriori__(from.internal_apriori__, alloc),
74 gammalog2__(from.gammalog2__) {
75 GUM_CONS_CPY(ScoreK2);
80 template <
template <
typename >
class ALLOC >
81 INLINE ScoreK2< ALLOC >::ScoreK2(
const ScoreK2< ALLOC >& from) :
82 ScoreK2< ALLOC >(from, from.getAllocator()) {}
86 template <
template <
typename >
class ALLOC >
87 INLINE ScoreK2< ALLOC >::ScoreK2(
88 ScoreK2< ALLOC >&& from,
89 const typename ScoreK2< 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(ScoreK2);
98 template <
template <
typename >
class ALLOC >
99 INLINE ScoreK2< ALLOC >::ScoreK2(ScoreK2< ALLOC >&& from) :
100 ScoreK2< ALLOC >(std::move(from), from.getAllocator()) {}
104 template <
template <
typename >
class ALLOC >
105 ScoreK2< ALLOC >* ScoreK2< ALLOC >::clone(
106 const typename ScoreK2< ALLOC >::allocator_type& alloc)
const {
107 ALLOC< ScoreK2< ALLOC > > allocator(alloc);
108 ScoreK2< 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 ScoreK2< ALLOC >* ScoreK2< ALLOC >::clone()
const {
123 return clone(
this->getAllocator());
128 template <
template <
typename >
class ALLOC >
129 ScoreK2< ALLOC >::~ScoreK2() {
130 GUM_DESTRUCTOR(ScoreK2);
135 template <
template <
typename >
class ALLOC >
136 ScoreK2< ALLOC >& ScoreK2< ALLOC >::operator=(
const ScoreK2< ALLOC >& from) {
138 Score< ALLOC >::operator=(from);
139 internal_apriori__ = from.internal_apriori__;
146 template <
template <
typename >
class ALLOC >
147 ScoreK2< ALLOC >& ScoreK2< ALLOC >::operator=(ScoreK2< ALLOC >&& from) {
149 Score< ALLOC >::operator=(std::move(from));
150 internal_apriori__ = std::move(from.internal_apriori__);
157 template <
template <
typename >
class ALLOC >
159 ScoreK2< ALLOC >::isAprioriCompatible(
const std::string& apriori_type,
162 if (apriori_type == AprioriNoAprioriType::type) {
return ""; }
165 return "The apriori is currently compatible with the K2 score but " 166 "if you change the weight, it will become incompatible.";
170 if ((apriori_type == AprioriDirichletType::type)
171 || (apriori_type == AprioriSmoothingType::type)) {
172 return "The K2 score already contains a different 'implicit' apriori. " 173 "Therefore, the learning will probably be biased.";
177 std::stringstream msg;
178 msg <<
"The apriori '" << apriori_type
179 <<
"' is not yet supported by method isAprioriCompatible os Score K2";
185 template <
template <
typename >
class ALLOC >
187 ScoreK2< ALLOC >::isAprioriCompatible(
const Apriori< ALLOC >& apriori) {
188 return isAprioriCompatible(apriori.getType(), apriori.weight());
193 template <
template <
typename >
class ALLOC >
194 INLINE std::string ScoreK2< ALLOC >::isAprioriCompatible()
const {
195 return isAprioriCompatible(*(
this->apriori_));
200 template <
template <
typename >
class ALLOC >
201 INLINE
const Apriori< ALLOC >& ScoreK2< ALLOC >::internalApriori()
const {
202 return internal_apriori__;
207 template <
template <
typename >
class ALLOC >
208 double ScoreK2< ALLOC >::score_(
const IdCondSet< ALLOC >& idset) {
210 std::vector<
double, ALLOC<
double > > N_ijk(
211 this->counter_.counts(idset,
true));
212 const std::size_t all_size = N_ijk.size();
213 const bool informative_external_apriori =
this->apriori_->isInformative();
218 if (idset.hasConditioningSet()) {
220 std::vector<
double, ALLOC<
double > > N_ij(
221 this->marginalize_(idset[0], N_ijk));
222 const std::size_t conditioning_size = N_ij.size();
223 const double ri =
double(all_size / conditioning_size);
225 if (informative_external_apriori) {
228 std::vector<
double, ALLOC<
double > > N_prime_ijk(all_size, 0.0);
229 this->apriori_->addAllApriori(idset, N_prime_ijk);
230 std::vector<
double, ALLOC<
double > > N_prime_ij(N_ij.size(), 0.0);
231 this->apriori_->addConditioningApriori(idset, N_prime_ij);
238 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
239 score += gammalog2__(N_prime_ij[j] + ri)
240 - gammalog2__(N_ij[j] + N_prime_ij[j] + ri);
242 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
243 score += gammalog2__(N_ijk[k] + N_prime_ijk[k] + 1.0)
244 - gammalog2__(N_prime_ijk[k] + 1.0);
250 score = conditioning_size * gammalog2__(ri);
252 for (
const auto n_ij: N_ij) {
253 score -= gammalog2__(n_ij + ri);
255 for (
const auto n_ijk: N_ijk) {
256 score += gammalog2__(n_ijk + 1);
261 const double ri =
double(all_size);
263 if (informative_external_apriori) {
271 std::vector<
double, ALLOC<
double > > N_prime_ijk(all_size, 0.0);
272 this->apriori_->addAllApriori(idset, N_prime_ijk);
276 double N_prime = 0.0;
277 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
278 score += gammalog2__(N_ijk[k] + N_prime_ijk[k] + 1)
279 - gammalog2__(N_prime_ijk[k] + 1);
281 N_prime += N_prime_ijk[k];
283 score += gammalog2__(N_prime + ri) - gammalog2__(N + N_prime + ri);
287 score = gammalog2__(ri);
289 for (
const auto n_ijk: N_ijk) {
290 score += gammalog2__(n_ijk + 1);
293 score -= gammalog2__(N + ri);