26 #ifndef DOXYGEN_SHOULD_SKIP_THIS 36 template <
template <
typename >
class ALLOC >
38 const DBRowGeneratorParser< ALLOC >& parser,
39 const Apriori< ALLOC >& apriori,
40 const std::vector< std::pair< std::size_t, std::size_t >,
41 ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
42 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
45 Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
46 __internal_apriori(parser.database(), nodeId2columns) {
52 template <
template <
typename >
class ALLOC >
54 const DBRowGeneratorParser< ALLOC >& parser,
55 const Apriori< ALLOC >& apriori,
56 const Bijection<
NodeId, std::size_t, ALLOC< std::size_t > >&
59 Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
60 __internal_apriori(parser.
database(), nodeId2columns) {
66 template <
template <
typename >
class ALLOC >
68 const ScoreBIC< ALLOC >& from,
70 Score< ALLOC >(from, alloc),
71 __internal_apriori(from.__internal_apriori, alloc) {
77 template <
template <
typename >
class ALLOC >
83 template <
template <
typename >
class ALLOC >
85 ScoreBIC< ALLOC >&& from,
87 Score< ALLOC >(
std::move(from), alloc),
88 __internal_apriori(
std::move(from.__internal_apriori), alloc) {
94 template <
template <
typename >
class ALLOC >
100 template <
template <
typename >
class ALLOC >
103 ALLOC< ScoreBIC< ALLOC > > allocator(alloc);
104 ScoreBIC< ALLOC >* new_score = allocator.allocate(1);
106 allocator.construct(new_score, *
this, alloc);
108 allocator.deallocate(new_score, 1);
117 template <
template <
typename >
class ALLOC >
124 template <
template <
typename >
class ALLOC >
131 template <
template <
typename >
class ALLOC >
133 operator=(
const ScoreBIC< ALLOC >& from) {
136 __internal_apriori = from.__internal_apriori;
143 template <
template <
typename >
class ALLOC >
147 __internal_apriori = std::move(from.__internal_apriori);
154 template <
template <
typename >
class ALLOC >
166 std::stringstream msg;
167 msg <<
"The apriori '" << apriori_type
168 <<
"' is not yet supported by method isAprioriCompatible os Score BIC";
174 template <
template <
typename >
class ALLOC >
182 template <
template <
typename >
class ALLOC >
189 template <
template <
typename >
class ALLOC >
191 return __internal_apriori;
196 template <
template <
typename >
class ALLOC >
199 std::vector< double, ALLOC< double > > N_ijk(
200 this->
_counter.counts(idset,
true));
201 const bool informative_external_apriori = this->
_apriori->isInformative();
202 if (informative_external_apriori)
203 this->
_apriori->addAllApriori(idset, N_ijk);
204 const std::size_t all_size = N_ijk.size();
208 if (idset.hasConditioningSet()) {
210 std::vector< double, ALLOC< double > > N_ij(
212 const std::size_t conditioning_size = N_ij.size();
216 const std::size_t target_domsize = all_size / conditioning_size;
217 const double penalty =
218 conditioning_size *
double(target_domsize - std::size_t(1));
225 for (
const auto n_ijk : N_ijk) {
226 if (n_ijk) { score += n_ijk * std::log(n_ijk); }
229 for (
const auto n_ij : N_ij) {
231 score -= n_ij * std::log(n_ij);
237 score -= penalty * std::log(N) * 0.5;
248 const double penalty =
double(all_size - std::size_t(1));
256 for (
const auto n_ijk : N_ijk) {
258 score += n_ijk * std::log(n_ijk);
262 score -= N * std::log(N);
265 score -= penalty * std::log(N) * 0.5;
276 template <
template <
typename >
class ALLOC >
279 std::vector< double, ALLOC< double > > N_ijk(
280 this->
_counter.counts(idset,
true));
281 if (this->
_apriori->isInformative())
282 this->
_apriori->addAllApriori(idset, N_ijk);
285 for (
const auto n_ijk : N_ijk) {
const DatabaseTable< ALLOC > & database() const
return the database used by the score
double score(const NodeId var)
returns the score of a single node
double N(const IdSet< ALLOC > &idset)
returns the size of the database w.r.t. a given idset
virtual ScoreBIC< ALLOC > * clone() const
virtual copy constructor
static const std::string type
ScoreBIC< ALLOC > & operator=(const ScoreBIC< ALLOC > &from)
copy operator
virtual ~ScoreBIC()
destructor
the class for computing BIC scores
Score(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &external_apriori, const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > &ranges, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
static const std::string type
const double _1log2
1 / log(2)
virtual std::string isAprioriCompatible() const final
indicates whether the apriori is compatible (meaningful) with the score
gum is the global namespace for all aGrUM entities
virtual double _score(const IdSet< ALLOC > &idset) final
returns the score for a given IdSet
std::vector< double, ALLOC< double > > _marginalize(const NodeId X_id, const std::vector< double, ALLOC< double > > &N_xyz) const
returns a counting vector where variables are marginalized from N_xyz
ScoreBIC(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &apriori, const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > &ranges, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
Score< ALLOC > & operator=(const Score< ALLOC > &from)
copy operator
virtual const Apriori< ALLOC > & internalApriori() const final
returns the internal apriori of the score
allocator_type getAllocator() const
returns the allocator used by the score
Apriori< ALLOC > * _apriori
the expert knowledge a priori we add to the score
static const std::string type
RecordCounter< ALLOC > _counter
the record counter used for the countings over discrete variables
Size NodeId
Type for node ids.