28 #ifndef DOXYGEN_SHOULD_SKIP_THIS 30 # include <agrum/BN/learning/scores_and_tests/scoreLog2Likelihood.h> 38 template <
template <
typename >
class ALLOC >
39 INLINE ScoreLog2Likelihood< ALLOC >::ScoreLog2Likelihood(
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 ScoreLog2Likelihood< ALLOC >::allocator_type& alloc) :
46 Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
47 _internal_apriori_(parser.database(), nodeId2columns) {
48 GUM_CONSTRUCTOR(ScoreLog2Likelihood);
53 template <
template <
typename >
class ALLOC >
54 INLINE ScoreLog2Likelihood< ALLOC >::ScoreLog2Likelihood(
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 ScoreLog2Likelihood< ALLOC >::allocator_type& alloc) :
59 Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
60 _internal_apriori_(parser.database(), nodeId2columns) {
61 GUM_CONSTRUCTOR(ScoreLog2Likelihood);
66 template <
template <
typename >
class ALLOC >
67 INLINE ScoreLog2Likelihood< ALLOC >::ScoreLog2Likelihood(
68 const ScoreLog2Likelihood< ALLOC >& from,
69 const typename ScoreLog2Likelihood< ALLOC >::allocator_type& alloc) :
70 Score< ALLOC >(from, alloc),
71 _internal_apriori_(from._internal_apriori_, alloc) {
72 GUM_CONS_CPY(ScoreLog2Likelihood);
77 template <
template <
typename >
class ALLOC >
79 ScoreLog2Likelihood< ALLOC >::ScoreLog2Likelihood(
const ScoreLog2Likelihood< ALLOC >& from) :
80 ScoreLog2Likelihood< ALLOC >(from, from.getAllocator()) {}
84 template <
template <
typename >
class ALLOC >
85 INLINE ScoreLog2Likelihood< ALLOC >::ScoreLog2Likelihood(
86 ScoreLog2Likelihood< ALLOC >&& from,
87 const typename ScoreLog2Likelihood< ALLOC >::allocator_type& alloc) :
88 Score< ALLOC >(std::move(from), alloc),
89 _internal_apriori_(std::move(from._internal_apriori_), alloc) {
90 GUM_CONS_MOV(ScoreLog2Likelihood);
95 template <
template <
typename >
class ALLOC >
96 INLINE ScoreLog2Likelihood< ALLOC >::ScoreLog2Likelihood(ScoreLog2Likelihood< ALLOC >&& from) :
97 ScoreLog2Likelihood< ALLOC >(std::move(from), from.getAllocator()) {}
101 template <
template <
typename >
class ALLOC >
102 ScoreLog2Likelihood< ALLOC >* ScoreLog2Likelihood< ALLOC >::clone(
103 const typename ScoreLog2Likelihood< ALLOC >::allocator_type& alloc)
const {
104 ALLOC< ScoreLog2Likelihood< ALLOC > > allocator(alloc);
105 ScoreLog2Likelihood< ALLOC >* new_score = allocator.allocate(1);
107 allocator.construct(new_score, *
this, alloc);
109 allocator.deallocate(new_score, 1);
118 template <
template <
typename >
class ALLOC >
119 ScoreLog2Likelihood< ALLOC >* ScoreLog2Likelihood< ALLOC >::clone()
const {
120 return clone(
this->getAllocator());
125 template <
template <
typename >
class ALLOC >
126 ScoreLog2Likelihood< ALLOC >::~ScoreLog2Likelihood() {
127 GUM_DESTRUCTOR(ScoreLog2Likelihood);
132 template <
template <
typename >
class ALLOC >
133 ScoreLog2Likelihood< ALLOC >&
134 ScoreLog2Likelihood< ALLOC >::operator=(
const ScoreLog2Likelihood< ALLOC >& from) {
136 Score< ALLOC >::operator=(from);
137 _internal_apriori_ = from._internal_apriori_;
144 template <
template <
typename >
class ALLOC >
145 ScoreLog2Likelihood< ALLOC >&
146 ScoreLog2Likelihood< ALLOC >::operator=(ScoreLog2Likelihood< ALLOC >&& from) {
148 Score< ALLOC >::operator=(std::move(from));
149 _internal_apriori_ = std::move(from._internal_apriori_);
156 template <
template <
typename >
class ALLOC >
157 std::string ScoreLog2Likelihood< ALLOC >::isAprioriCompatible(
const std::string& apriori_type,
160 if ((apriori_type == AprioriDirichletType::type)
161 || (apriori_type == AprioriSmoothingType::type)
162 || (apriori_type == AprioriNoAprioriType::type)) {
167 std::stringstream msg;
168 msg <<
"The apriori '" << apriori_type
169 <<
"' is not yet supported by method isAprioriCompatible of " 170 <<
"Score Log2Likelihood";
176 template <
template <
typename >
class ALLOC >
178 ScoreLog2Likelihood< ALLOC >::isAprioriCompatible(
const Apriori< ALLOC >& apriori) {
179 return isAprioriCompatible(apriori.getType(), apriori.weight());
184 template <
template <
typename >
class ALLOC >
185 INLINE std::string ScoreLog2Likelihood< ALLOC >::isAprioriCompatible()
const {
186 return isAprioriCompatible(*(
this->apriori_));
191 template <
template <
typename >
class ALLOC >
192 INLINE
const Apriori< ALLOC >& ScoreLog2Likelihood< ALLOC >::internalApriori()
const {
193 return _internal_apriori_;
198 template <
template <
typename >
class ALLOC >
199 double ScoreLog2Likelihood< ALLOC >::score_(
const IdCondSet< ALLOC >& idset) {
201 std::vector<
double, ALLOC<
double > > N_ijk(
this->counter_.counts(idset,
true));
202 const bool informative_external_apriori =
this->apriori_->isInformative();
203 if (informative_external_apriori)
this->apriori_->addAllApriori(idset, N_ijk);
207 if (idset.hasConditioningSet()) {
209 std::vector<
double, ALLOC<
double > > N_ij(
this->marginalize_(idset[0], N_ijk));
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_;
236 for (
const auto n_ijk: N_ijk) {
238 score += n_ijk * std::log(n_ijk);
242 score -= N * std::log(N);
245 score *=
this->one_log2_;
253 template <
template <
typename >
class ALLOC >
254 INLINE
double ScoreLog2Likelihood< ALLOC >::score(
const IdCondSet< ALLOC >& idset) {
255 return score_(idset);