28 #ifndef DOXYGEN_SHOULD_SKIP_THIS 30 # include <agrum/BN/learning/scores_and_tests/scoreBIC.h> 38 template <
template <
typename >
class ALLOC >
39 INLINE ScoreBIC< ALLOC >::ScoreBIC(
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 ScoreBIC< ALLOC >::allocator_type& alloc) :
46 Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
47 _internal_apriori_(parser.database(), nodeId2columns) {
48 GUM_CONSTRUCTOR(ScoreBIC);
53 template <
template <
typename >
class ALLOC >
54 INLINE ScoreBIC< ALLOC >::ScoreBIC(
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 ScoreBIC< ALLOC >::allocator_type& alloc) :
59 Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
60 _internal_apriori_(parser.database(), nodeId2columns) {
61 GUM_CONSTRUCTOR(ScoreBIC);
66 template <
template <
typename >
class ALLOC >
67 INLINE ScoreBIC< ALLOC >::ScoreBIC(
const ScoreBIC< ALLOC >& from,
68 const typename ScoreBIC< ALLOC >::allocator_type& alloc) :
69 Score< ALLOC >(from, alloc),
70 _internal_apriori_(from._internal_apriori_, alloc) {
71 GUM_CONS_CPY(ScoreBIC);
76 template <
template <
typename >
class ALLOC >
77 INLINE ScoreBIC< ALLOC >::ScoreBIC(
const ScoreBIC< ALLOC >& from) :
78 ScoreBIC< ALLOC >(from, from.getAllocator()) {}
82 template <
template <
typename >
class ALLOC >
83 INLINE ScoreBIC< ALLOC >::ScoreBIC(ScoreBIC< ALLOC >&& from,
84 const typename ScoreBIC< ALLOC >::allocator_type& alloc) :
85 Score< ALLOC >(std::move(from), alloc),
86 _internal_apriori_(std::move(from._internal_apriori_), alloc) {
87 GUM_CONS_MOV(ScoreBIC);
92 template <
template <
typename >
class ALLOC >
93 INLINE ScoreBIC< ALLOC >::ScoreBIC(ScoreBIC< ALLOC >&& from) :
94 ScoreBIC< ALLOC >(std::move(from), from.getAllocator()) {}
98 template <
template <
typename >
class ALLOC >
100 ScoreBIC< ALLOC >::clone(
const typename ScoreBIC< ALLOC >::allocator_type& alloc)
const {
101 ALLOC< ScoreBIC< ALLOC > > allocator(alloc);
102 ScoreBIC< 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 ScoreBIC< ALLOC >* ScoreBIC< ALLOC >::clone()
const {
117 return clone(
this->getAllocator());
122 template <
template <
typename >
class ALLOC >
123 ScoreBIC< ALLOC >::~ScoreBIC() {
124 GUM_DESTRUCTOR(ScoreBIC);
129 template <
template <
typename >
class ALLOC >
130 ScoreBIC< ALLOC >& ScoreBIC< ALLOC >::operator=(
const ScoreBIC< ALLOC >& from) {
132 Score< ALLOC >::operator=(from);
133 _internal_apriori_ = from._internal_apriori_;
140 template <
template <
typename >
class ALLOC >
141 ScoreBIC< ALLOC >& ScoreBIC< ALLOC >::operator=(ScoreBIC< 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 ScoreBIC< 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 <<
"' is not yet compatible with the score 'BIC'.";
169 template <
template <
typename >
class ALLOC >
170 INLINE std::string ScoreBIC< ALLOC >::isAprioriCompatible(
const Apriori< ALLOC >& apriori) {
171 return isAprioriCompatible(apriori.getType(), apriori.weight());
176 template <
template <
typename >
class ALLOC >
177 INLINE std::string ScoreBIC< ALLOC >::isAprioriCompatible()
const {
178 return isAprioriCompatible(*(
this->apriori_));
183 template <
template <
typename >
class ALLOC >
184 INLINE
const Apriori< ALLOC >& ScoreBIC< ALLOC >::internalApriori()
const {
185 return _internal_apriori_;
190 template <
template <
typename >
class ALLOC >
191 double ScoreBIC< ALLOC >::score_(
const IdCondSet< ALLOC >& idset) {
193 std::vector<
double, ALLOC<
double > > N_ijk(
this->counter_.counts(idset,
true));
194 const bool informative_external_apriori =
this->apriori_->isInformative();
195 if (informative_external_apriori)
this->apriori_->addAllApriori(idset, N_ijk);
196 const std::size_t all_size = N_ijk.size();
200 if (idset.hasConditioningSet()) {
202 std::vector<
double, ALLOC<
double > > N_ij(
this->marginalize_(idset[0], N_ijk));
203 const std::size_t conditioning_size = N_ij.size();
207 const std::size_t target_domsize = all_size / conditioning_size;
208 const double penalty = conditioning_size *
double(target_domsize - std::size_t(1));
215 for (
const auto n_ijk: N_ijk) {
216 if (n_ijk) { score += n_ijk * std::log(n_ijk); }
219 for (
const auto n_ij: N_ij) {
221 score -= n_ij * std::log(n_ij);
227 score -= penalty * std::log(N) * 0.5;
230 score *=
this->one_log2_;
238 const double penalty =
double(all_size - std::size_t(1));
246 for (
const auto n_ijk: N_ijk) {
248 score += n_ijk * std::log(n_ijk);
252 score -= N * std::log(N);
255 score -= penalty * std::log(N) * 0.5;
258 score *=
this->one_log2_;
266 template <
template <
typename >
class ALLOC >
267 double ScoreBIC< ALLOC >::N(
const IdCondSet< ALLOC >& idset) {
269 std::vector<
double, ALLOC<
double > > N_ijk(
this->counter_.counts(idset,
true));
270 if (
this->apriori_->isInformative())
this->apriori_->addAllApriori(idset, N_ijk);
273 for (
const auto n_ijk: N_ijk) {