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 ScoreBDeu< ALLOC >& from,
70 Score< ALLOC >(from, alloc),
71 __internal_apriori(from.__internal_apriori, alloc),
72 __gammalog2(from.__gammalog2) {
78 template <
template <
typename >
class ALLOC >
84 template <
template <
typename >
class ALLOC >
86 ScoreBDeu< ALLOC >&& from,
88 Score< ALLOC >(
std::move(from), alloc),
89 __internal_apriori(
std::move(from.__internal_apriori), alloc),
90 __gammalog2(
std::move(from.__gammalog2)) {
96 template <
template <
typename >
class ALLOC >
102 template <
template <
typename >
class ALLOC >
105 ALLOC< ScoreBDeu< ALLOC > > allocator(alloc);
106 ScoreBDeu< 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 >
126 template <
template <
typename >
class ALLOC >
133 template <
template <
typename >
class ALLOC >
135 operator=(
const ScoreBDeu< ALLOC >& from) {
138 __internal_apriori = from.__internal_apriori;
145 template <
template <
typename >
class ALLOC >
149 __internal_apriori = std::move(from.__internal_apriori);
156 template <
template <
typename >
class ALLOC >
164 return "The apriori is currently compatible with the BDeu score but " 165 "if you change the weight, it will become incompatible.";
171 return "The BDeu score already contains a different 'implicit' apriori. " 172 "Therefore, the learning will probably be biased.";
176 std::stringstream msg;
177 msg <<
"The apriori '" << apriori_type
178 <<
"' is not yet supported by method isAprioriCompatible os Score BDeu";
184 template <
template <
typename >
class ALLOC >
192 template <
template <
typename >
class ALLOC >
199 template <
template <
typename >
class ALLOC >
201 return __internal_apriori;
206 template <
template <
typename >
class ALLOC >
210 "The effective sample size of the BDeu's " 211 "internal apriori must be positive");
213 __internal_apriori.setEffectiveSampleSize(ess);
219 template <
template <
typename >
class ALLOC >
222 std::vector< double, ALLOC< double > > N_ijk(
223 this->
_counter.counts(idset,
true));
224 const std::size_t all_size = N_ijk.size();
227 const double ess = __internal_apriori.weight();
228 const bool informative_external_apriori = this->
_apriori->isInformative();
233 if (idset.hasConditioningSet()) {
235 std::vector< double, ALLOC< double > > N_ij(
237 const std::size_t conditioning_size = N_ij.size();
238 const double ess_qi = ess / conditioning_size;
239 const double ess_riqi = ess / all_size;
241 if (informative_external_apriori) {
246 std::vector< double, ALLOC< double > > N_prime_ijk(all_size, 0.0);
247 this->
_apriori->addAllApriori(idset, N_prime_ijk);
248 std::vector< double, ALLOC< double > > N_prime_ij(N_ij.size(), 0.0);
249 this->
_apriori->addConditioningApriori(idset, N_prime_ij);
256 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
257 score += __gammalog2(N_prime_ij[j] + ess_qi)
258 - __gammalog2(N_ij[j] + N_prime_ij[j] + ess_qi);
260 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
261 score += __gammalog2(N_ijk[k] + N_prime_ijk[k] + ess_riqi)
262 - __gammalog2(N_prime_ijk[k] + ess_riqi);
270 score = conditioning_size * __gammalog2(ess_qi)
271 - all_size * __gammalog2(ess_riqi);
273 for (
const auto n_ij : N_ij) {
274 score -= __gammalog2(n_ij + ess_qi);
276 for (
const auto n_ijk : N_ijk) {
277 score += __gammalog2(n_ijk + ess_riqi);
282 const double ess_ri = ess / all_size;
284 if (informative_external_apriori) {
289 std::vector< double, ALLOC< double > > N_prime_ijk(all_size, 0.0);
290 this->
_apriori->addAllApriori(idset, N_prime_ijk);
297 double N_prime = 0.0;
298 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
299 score += __gammalog2(N_ijk[k] + N_prime_ijk[k] + ess_ri)
300 - __gammalog2(N_prime_ijk[k] + ess_ri);
302 N_prime += N_prime_ijk[k];
304 score += __gammalog2(N_prime + ess) - __gammalog2(N + N_prime + ess);
311 score = __gammalog2(ess) - all_size * __gammalog2(ess_ri);
313 for (
const auto n_ijk : N_ijk) {
314 score += __gammalog2(n_ijk + ess_ri);
317 score -= __gammalog2(N + ess);
virtual ScoreBDeu< ALLOC > * clone() const
virtual copy constructor
const DatabaseTable< ALLOC > & database() const
return the database used by the score
double score(const NodeId var)
returns the score of a single node
virtual const Apriori< ALLOC > & internalApriori() const final
returns the internal apriori of the score
static const std::string type
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
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
the class for computing BDeu scores
virtual ~ScoreBDeu()
destructor
gum is the global namespace for all aGrUM entities
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
void setEffectiveSampleSize(double ess)
sets the effective sample size of the internal apriori
virtual double _score(const IdSet< ALLOC > &idset) final
returns the score for a given IdSet
Score< ALLOC > & operator=(const Score< ALLOC > &from)
copy operator
virtual std::string isAprioriCompatible() const final
indicates whether the apriori is compatible (meaningful) with the score
allocator_type getAllocator() const
returns the allocator used by the score
ScoreBDeu< ALLOC > & operator=(const ScoreBDeu< ALLOC > &from)
copy operator
Apriori< ALLOC > * _apriori
the expert knowledge a priori we add to the score
static const std::string type
ScoreBDeu(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
RecordCounter< ALLOC > _counter
the record counter used for the countings over discrete variables
Size NodeId
Type for node ids.
#define GUM_ERROR(type, msg)