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 > >& nodeId2columns,
45 const typename ScoreK2< ALLOC >::allocator_type& alloc) :
46 Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
47 _internal_apriori_(parser.database(), nodeId2columns) {
48 GUM_CONSTRUCTOR(ScoreK2);
53 template <
template <
typename >
class ALLOC >
54 INLINE ScoreK2< ALLOC >::ScoreK2(
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 ScoreK2< ALLOC >::allocator_type& alloc) :
59 Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
60 _internal_apriori_(parser.database(), nodeId2columns) {
61 GUM_CONSTRUCTOR(ScoreK2);
66 template <
template <
typename >
class ALLOC >
67 INLINE ScoreK2< ALLOC >::ScoreK2(
const ScoreK2< ALLOC >& from,
68 const typename ScoreK2< ALLOC >::allocator_type& alloc) :
69 Score< ALLOC >(from, alloc),
70 _internal_apriori_(from._internal_apriori_, alloc), _gammalog2_(from._gammalog2_) {
71 GUM_CONS_CPY(ScoreK2);
76 template <
template <
typename >
class ALLOC >
77 INLINE ScoreK2< ALLOC >::ScoreK2(
const ScoreK2< ALLOC >& from) :
78 ScoreK2< ALLOC >(from, from.getAllocator()) {}
82 template <
template <
typename >
class ALLOC >
83 INLINE ScoreK2< ALLOC >::ScoreK2(ScoreK2< ALLOC >&& from,
84 const typename ScoreK2< 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(ScoreK2);
93 template <
template <
typename >
class ALLOC >
94 INLINE ScoreK2< ALLOC >::ScoreK2(ScoreK2< ALLOC >&& from) :
95 ScoreK2< ALLOC >(std::move(from), from.getAllocator()) {}
99 template <
template <
typename >
class ALLOC >
101 ScoreK2< ALLOC >::clone(
const typename ScoreK2< ALLOC >::allocator_type& alloc)
const {
102 ALLOC< ScoreK2< ALLOC > > allocator(alloc);
103 ScoreK2< 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 ScoreK2< ALLOC >* ScoreK2< ALLOC >::clone()
const {
118 return clone(
this->getAllocator());
123 template <
template <
typename >
class ALLOC >
124 ScoreK2< ALLOC >::~ScoreK2() {
125 GUM_DESTRUCTOR(ScoreK2);
130 template <
template <
typename >
class ALLOC >
131 ScoreK2< ALLOC >& ScoreK2< ALLOC >::operator=(
const ScoreK2< ALLOC >& from) {
133 Score< ALLOC >::operator=(from);
134 _internal_apriori_ = from._internal_apriori_;
141 template <
template <
typename >
class ALLOC >
142 ScoreK2< ALLOC >& ScoreK2< ALLOC >::operator=(ScoreK2< 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 ScoreK2< ALLOC >::isAprioriCompatible(
const std::string& apriori_type,
156 if (apriori_type == AprioriNoAprioriType::type) {
return ""; }
159 return "The apriori is currently compatible with the K2 score but " 160 "if you change the weight, it will become incompatible.";
164 if ((apriori_type == AprioriDirichletType::type)
165 || (apriori_type == AprioriSmoothingType::type)) {
166 return "The K2 score already contains a different 'implicit' apriori. " 167 "Therefore, the learning will probably be biased.";
171 std::stringstream msg;
172 msg <<
"The apriori '" << apriori_type
173 <<
"' is not yet supported by method isAprioriCompatible os Score K2";
179 template <
template <
typename >
class ALLOC >
180 INLINE std::string ScoreK2< ALLOC >::isAprioriCompatible(
const Apriori< ALLOC >& apriori) {
181 return isAprioriCompatible(apriori.getType(), apriori.weight());
186 template <
template <
typename >
class ALLOC >
187 INLINE std::string ScoreK2< ALLOC >::isAprioriCompatible()
const {
188 return isAprioriCompatible(*(
this->apriori_));
193 template <
template <
typename >
class ALLOC >
194 INLINE
const Apriori< ALLOC >& ScoreK2< ALLOC >::internalApriori()
const {
195 return _internal_apriori_;
200 template <
template <
typename >
class ALLOC >
201 double ScoreK2< ALLOC >::score_(
const IdCondSet< ALLOC >& idset) {
203 std::vector<
double, ALLOC<
double > > N_ijk(
this->counter_.counts(idset,
true));
204 const std::size_t all_size = N_ijk.size();
205 const bool informative_external_apriori =
this->apriori_->isInformative();
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();
214 const double ri =
double(all_size / conditioning_size);
216 if (informative_external_apriori) {
219 std::vector<
double, ALLOC<
double > > N_prime_ijk(all_size, 0.0);
220 this->apriori_->addAllApriori(idset, N_prime_ijk);
221 std::vector<
double, ALLOC<
double > > N_prime_ij(N_ij.size(), 0.0);
222 this->apriori_->addConditioningApriori(idset, N_prime_ij);
229 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
230 score += _gammalog2_(N_prime_ij[j] + ri) - _gammalog2_(N_ij[j] + N_prime_ij[j] + ri);
232 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
234 += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + 1.0) - _gammalog2_(N_prime_ijk[k] + 1.0);
240 score = conditioning_size * _gammalog2_(ri);
242 for (
const auto n_ij: N_ij) {
243 score -= _gammalog2_(n_ij + ri);
245 for (
const auto n_ijk: N_ijk) {
246 score += _gammalog2_(n_ijk + 1);
251 const double ri =
double(all_size);
253 if (informative_external_apriori) {
261 std::vector<
double, ALLOC<
double > > N_prime_ijk(all_size, 0.0);
262 this->apriori_->addAllApriori(idset, N_prime_ijk);
266 double N_prime = 0.0;
267 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
268 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + 1) - _gammalog2_(N_prime_ijk[k] + 1);
270 N_prime += N_prime_ijk[k];
272 score += _gammalog2_(N_prime + ri) - _gammalog2_(N + N_prime + ri);
276 score = _gammalog2_(ri);
278 for (
const auto n_ijk: N_ijk) {
279 score += _gammalog2_(n_ijk + 1);
282 score -= _gammalog2_(N + ri);