28 #ifndef DOXYGEN_SHOULD_SKIP_THIS 30 # include <agrum/BN/learning/scores_and_tests/scoreAIC.h> 38 template <
template <
typename >
class ALLOC >
39 INLINE ScoreAIC< ALLOC >::ScoreAIC(
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 ScoreAIC< ALLOC >::allocator_type& alloc) :
46 Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
47 _internal_apriori_(parser.database(), nodeId2columns) {
48 GUM_CONSTRUCTOR(ScoreAIC);
53 template <
template <
typename >
class ALLOC >
54 INLINE ScoreAIC< ALLOC >::ScoreAIC(
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 ScoreAIC< ALLOC >::allocator_type& alloc) :
59 Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
60 _internal_apriori_(parser.database(), nodeId2columns) {
61 GUM_CONSTRUCTOR(ScoreAIC);
66 template <
template <
typename >
class ALLOC >
67 INLINE ScoreAIC< ALLOC >::ScoreAIC(
const ScoreAIC< ALLOC >& from,
68 const typename ScoreAIC< ALLOC >::allocator_type& alloc) :
69 Score< ALLOC >(from, alloc),
70 _internal_apriori_(from._internal_apriori_, alloc) {
71 GUM_CONS_CPY(ScoreAIC);
76 template <
template <
typename >
class ALLOC >
77 INLINE ScoreAIC< ALLOC >::ScoreAIC(
const ScoreAIC< ALLOC >& from) :
78 ScoreAIC< ALLOC >(from, from.getAllocator()) {}
82 template <
template <
typename >
class ALLOC >
83 INLINE ScoreAIC< ALLOC >::ScoreAIC(ScoreAIC< ALLOC >&& from,
84 const typename ScoreAIC< ALLOC >::allocator_type& alloc) :
85 Score< ALLOC >(std::move(from), alloc),
86 _internal_apriori_(std::move(from._internal_apriori_), alloc) {
87 GUM_CONS_MOV(ScoreAIC);
92 template <
template <
typename >
class ALLOC >
93 INLINE ScoreAIC< ALLOC >::ScoreAIC(ScoreAIC< ALLOC >&& from) :
94 ScoreAIC< ALLOC >(std::move(from), from.getAllocator()) {}
98 template <
template <
typename >
class ALLOC >
100 ScoreAIC< ALLOC >::clone(
const typename ScoreAIC< ALLOC >::allocator_type& alloc)
const {
101 ALLOC< ScoreAIC< ALLOC > > allocator(alloc);
102 ScoreAIC< ALLOC >* new_score = allocator.allocate(1);
104 allocator.construct(new_score, *
this, alloc);
106 allocator.deallocate(new_score, 1);
115 template <
template <
typename >
class ALLOC >
116 ScoreAIC< ALLOC >* ScoreAIC< ALLOC >::clone()
const {
117 return clone(
this->getAllocator());
122 template <
template <
typename >
class ALLOC >
123 ScoreAIC< ALLOC >::~ScoreAIC() {
124 GUM_DESTRUCTOR(ScoreAIC);
129 template <
template <
typename >
class ALLOC >
130 ScoreAIC< ALLOC >& ScoreAIC< ALLOC >::operator=(
const ScoreAIC< ALLOC >& from) {
132 Score< ALLOC >::operator=(from);
133 _internal_apriori_ = from._internal_apriori_;
140 template <
template <
typename >
class ALLOC >
141 ScoreAIC< ALLOC >& ScoreAIC< ALLOC >::operator=(ScoreAIC< ALLOC >&& from) {
143 Score< ALLOC >::operator=(std::move(from));
144 _internal_apriori_ = std::move(from._internal_apriori_);
151 template <
template <
typename >
class ALLOC >
152 std::string ScoreAIC< ALLOC >::isAprioriCompatible(
const std::string& apriori_type,
155 if ((apriori_type == AprioriDirichletType::type)
156 || (apriori_type == AprioriSmoothingType::type)
157 || (apriori_type == AprioriNoAprioriType::type)) {
162 std::stringstream msg;
163 msg <<
"The apriori '" << apriori_type
164 <<
"' is not yet supported by method isAprioriCompatible os Score AIC";
170 template <
template <
typename >
class ALLOC >
171 INLINE std::string ScoreAIC< ALLOC >::isAprioriCompatible(
const Apriori< ALLOC >& apriori) {
172 return isAprioriCompatible(apriori.getType(), apriori.weight());
177 template <
template <
typename >
class ALLOC >
178 INLINE std::string ScoreAIC< ALLOC >::isAprioriCompatible()
const {
179 return isAprioriCompatible(*(
this->apriori_));
184 template <
template <
typename >
class ALLOC >
185 INLINE
const Apriori< ALLOC >& ScoreAIC< ALLOC >::internalApriori()
const {
186 return _internal_apriori_;
191 template <
template <
typename >
class ALLOC >
192 double ScoreAIC< ALLOC >::score_(
const IdCondSet< ALLOC >& idset) {
194 std::vector<
double, ALLOC<
double > > N_ijk(
this->counter_.counts(idset,
true));
195 const bool informative_external_apriori =
this->apriori_->isInformative();
196 if (informative_external_apriori)
this->apriori_->addAllApriori(idset, N_ijk);
197 const std::size_t all_size = N_ijk.size();
201 if (idset.hasConditioningSet()) {
203 std::vector<
double, ALLOC<
double > > N_ij(
this->marginalize_(idset[0], N_ijk));
204 const std::size_t conditioning_size = N_ij.size();
208 const std::size_t target_domsize = all_size / conditioning_size;
209 const double penalty = conditioning_size *
double(target_domsize - std::size_t(1));
216 for (
const auto n_ijk: N_ijk) {
217 if (n_ijk) { score += n_ijk * std::log(n_ijk); }
219 for (
const auto n_ij: N_ij) {
220 if (n_ij) { score -= n_ij * std::log(n_ij); }
224 score *=
this->one_log2_;
235 const double penalty =
double(all_size - std::size_t(1));
243 for (
const auto n_ijk: N_ijk) {
245 score += n_ijk * std::log(n_ijk);
249 score -= N * std::log(N);
252 score *=
this->one_log2_;