51#ifndef DOXYGEN_SHOULD_SKIP_THIS
66 _internal_prior_ = from._internal_prior_;
75 _internal_prior_ = std::move(from._internal_prior_);
86 return "The prior is currently compatible with the K2 score but "
87 "if you change the weight, it will become incompatible.";
93 return "The K2 score already contains a different 'implicit' prior. "
94 "Therefore, the learning will probably be biased.";
98 std::stringstream msg;
100 <<
"' is not yet compatible with the score 'K2'.";
107 std::vector< double > N_ijk(this->
counter_.counts(idset,
true));
108 const std::size_t all_size = N_ijk.size();
109 const bool informative_external_prior = this->
prior_->isInformative();
114 if (idset.hasConditioningSet()) {
116 std::vector< double > N_ij(this->
marginalize_(idset[0], N_ijk));
117 const std::size_t conditioning_size = N_ij.size();
118 const double ri = double(all_size / conditioning_size);
120 if (informative_external_prior) {
123 std::vector< double > N_prime_ijk(all_size, 0.0);
124 this->
prior_->addJointPseudoCount(idset, N_prime_ijk);
125 std::vector< double > N_prime_ij(N_ij.size(), 0.0);
126 this->
prior_->addConditioningPseudoCount(idset, N_prime_ij);
133 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
134 score += _gammalog2_(N_prime_ij[j] + ri) - _gammalog2_(N_ij[j] + N_prime_ij[j] + ri);
136 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
138 += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + 1.0) - _gammalog2_(N_prime_ijk[k] + 1.0);
144 score = conditioning_size * _gammalog2_(ri);
146 for (
const auto n_ij: N_ij) {
147 score -= _gammalog2_(n_ij + ri);
149 for (
const auto n_ijk: N_ijk) {
150 score += _gammalog2_(n_ijk + 1);
155 const double ri = double(all_size);
157 if (informative_external_prior) {
165 std::vector< double > N_prime_ijk(all_size, 0.0);
166 this->
prior_->addJointPseudoCount(idset, N_prime_ijk);
170 double N_prime = 0.0;
171 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
172 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + 1) - _gammalog2_(N_prime_ijk[k] + 1);
174 N_prime += N_prime_ijk[k];
176 score += _gammalog2_(N_prime + ri) - _gammalog2_(N + N_prime + ri);
180 score = _gammalog2_(ri);
182 for (
const auto n_ijk: N_ijk) {
183 score += _gammalog2_(n_ijk + 1);
186 score -= _gammalog2_(N + ri);
A class for storing a pair of sets of NodeIds, the second one corresponding to a conditional set.
the class for computing K2 scores (actually their log2 value)
virtual std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
virtual double score_(const IdCondSet &idset) final
returns the score for a given IdCondSet
ScoreK2 & operator=(const ScoreK2 &from)
copy operator
Prior * prior_
the expert knowledge a priorwe add to the score
double score(const NodeId var)
returns the score of a single node
Score & operator=(const Score &from)
copy operator
RecordCounter counter_
the record counter used for the counts over discrete variables
std::vector< double > marginalize_(const NodeId X_id, const std::vector< double > &N_xyz) const
returns a counting vector where variables are marginalized from N_xyz
include the inlined functions if necessary
constexpr const char * priorTypeToString(PriorType e) noexcept
gum is the global namespace for all aGrUM entities
the class for computing K2 scores (actually their log2 value)
the class for computing K2 scores