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 > >&
46 const typename ScoreBIC< ALLOC >::allocator_type& alloc) :
47 Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
48 internal_apriori__(parser.database(), nodeId2columns) {
49 GUM_CONSTRUCTOR(ScoreBIC);
54 template <
template <
typename >
class ALLOC >
55 INLINE ScoreBIC< ALLOC >::ScoreBIC(
56 const DBRowGeneratorParser< ALLOC >& parser,
57 const Apriori< ALLOC >& apriori,
58 const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
60 const typename ScoreBIC< ALLOC >::allocator_type& alloc) :
61 Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
62 internal_apriori__(parser.database(), nodeId2columns) {
63 GUM_CONSTRUCTOR(ScoreBIC);
68 template <
template <
typename >
class ALLOC >
69 INLINE ScoreBIC< ALLOC >::ScoreBIC(
70 const ScoreBIC< ALLOC >& from,
71 const typename ScoreBIC< ALLOC >::allocator_type& alloc) :
72 Score< ALLOC >(from, alloc),
73 internal_apriori__(from.internal_apriori__, alloc) {
74 GUM_CONS_CPY(ScoreBIC);
79 template <
template <
typename >
class ALLOC >
80 INLINE ScoreBIC< ALLOC >::ScoreBIC(
const ScoreBIC< ALLOC >& from) :
81 ScoreBIC< ALLOC >(from, from.getAllocator()) {}
85 template <
template <
typename >
class ALLOC >
86 INLINE ScoreBIC< ALLOC >::ScoreBIC(
87 ScoreBIC< ALLOC >&& from,
88 const typename ScoreBIC< ALLOC >::allocator_type& alloc) :
89 Score< ALLOC >(std::move(from), alloc),
90 internal_apriori__(std::move(from.internal_apriori__), alloc) {
91 GUM_CONS_MOV(ScoreBIC);
96 template <
template <
typename >
class ALLOC >
97 INLINE ScoreBIC< ALLOC >::ScoreBIC(ScoreBIC< ALLOC >&& from) :
98 ScoreBIC< ALLOC >(std::move(from), from.getAllocator()) {}
102 template <
template <
typename >
class ALLOC >
103 ScoreBIC< ALLOC >* ScoreBIC< ALLOC >::clone(
104 const typename ScoreBIC< ALLOC >::allocator_type& alloc)
const {
105 ALLOC< ScoreBIC< ALLOC > > allocator(alloc);
106 ScoreBIC< ALLOC >* new_score = allocator.allocate(1);
108 allocator.construct(new_score, *
this, alloc);
110 allocator.deallocate(new_score, 1);
119 template <
template <
typename >
class ALLOC >
120 ScoreBIC< ALLOC >* ScoreBIC< ALLOC >::clone()
const {
121 return clone(
this->getAllocator());
126 template <
template <
typename >
class ALLOC >
127 ScoreBIC< ALLOC >::~ScoreBIC() {
128 GUM_DESTRUCTOR(ScoreBIC);
133 template <
template <
typename >
class ALLOC >
135 ScoreBIC< ALLOC >::operator=(
const ScoreBIC< ALLOC >& from) {
137 Score< ALLOC >::operator=(from);
138 internal_apriori__ = from.internal_apriori__;
145 template <
template <
typename >
class ALLOC >
146 ScoreBIC< ALLOC >& ScoreBIC< ALLOC >::operator=(ScoreBIC< ALLOC >&& from) {
148 Score< ALLOC >::operator=(std::move(from));
149 internal_apriori__ = std::move(from.internal_apriori__);
156 template <
template <
typename >
class ALLOC >
158 ScoreBIC< ALLOC >::isAprioriCompatible(
const std::string& apriori_type,
161 if ((apriori_type == AprioriDirichletType::type)
162 || (apriori_type == AprioriSmoothingType::type)
163 || (apriori_type == AprioriNoAprioriType::type)) {
168 std::stringstream msg;
169 msg <<
"The apriori '" << apriori_type
170 <<
"' is not yet supported by method isAprioriCompatible os Score BIC";
176 template <
template <
typename >
class ALLOC >
178 ScoreBIC< ALLOC >::isAprioriCompatible(
const Apriori< ALLOC >& apriori) {
179 return isAprioriCompatible(apriori.getType(), apriori.weight());
184 template <
template <
typename >
class ALLOC >
185 INLINE std::string ScoreBIC< ALLOC >::isAprioriCompatible()
const {
186 return isAprioriCompatible(*(
this->apriori_));
191 template <
template <
typename >
class ALLOC >
192 INLINE
const Apriori< ALLOC >& ScoreBIC< ALLOC >::internalApriori()
const {
193 return internal_apriori__;
198 template <
template <
typename >
class ALLOC >
199 double ScoreBIC< ALLOC >::score_(
const IdCondSet< ALLOC >& idset) {
201 std::vector<
double, ALLOC<
double > > N_ijk(
202 this->counter_.counts(idset,
true));
203 const bool informative_external_apriori =
this->apriori_->isInformative();
204 if (informative_external_apriori)
205 this->apriori_->addAllApriori(idset, N_ijk);
206 const std::size_t all_size = N_ijk.size();
210 if (idset.hasConditioningSet()) {
212 std::vector<
double, ALLOC<
double > > N_ij(
213 this->marginalize_(idset[0], N_ijk));
214 const std::size_t conditioning_size = N_ij.size();
218 const std::size_t target_domsize = all_size / conditioning_size;
220 = conditioning_size *
double(target_domsize - std::size_t(1));
227 for (
const auto n_ijk: N_ijk) {
228 if (n_ijk) { score += n_ijk * std::log(n_ijk); }
231 for (
const auto n_ij: N_ij) {
233 score -= n_ij * std::log(n_ij);
239 score -= penalty * std::log(N) * 0.5;
242 score *=
this->one_log2_;
250 const double penalty =
double(all_size - std::size_t(1));
258 for (
const auto n_ijk: N_ijk) {
260 score += n_ijk * std::log(n_ijk);
264 score -= N * std::log(N);
267 score -= penalty * std::log(N) * 0.5;
270 score *=
this->one_log2_;
278 template <
template <
typename >
class ALLOC >
279 double ScoreBIC< ALLOC >::N(
const IdCondSet< ALLOC >& idset) {
281 std::vector<
double, ALLOC<
double > > N_ijk(
282 this->counter_.counts(idset,
true));
283 if (
this->apriori_->isInformative())
284 this->apriori_->addAllApriori(idset, N_ijk);
287 for (
const auto n_ijk: N_ijk) {