aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
scoreBDeu.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
54# ifdef GUM_NO_INLINE
56# endif /* GUM_NO_INLINE */
57
58namespace gum {
59
60 namespace learning {
61
64 if (this != &from) {
65 Score::operator=(from);
66 _internal_prior_ = from._internal_prior_;
67 }
68 return *this;
69 }
70
73 if (this != &from) {
74 Score::operator=(std::move(from));
75 _internal_prior_ = std::move(from._internal_prior_);
76 }
77 return *this;
78 }
79
81 std::string ScoreBDeu::isPriorCompatible(PriorType prior_type, double weight) {
82 // check that the prior is compatible with the score
83 if (prior_type == PriorType::NoPriorType) { return ""; }
84
85 if (weight == 0.0) {
86 return "The prior is currently compatible with the BDeu score but "
87 "if you change the weight, it will become incompatible.";
88 }
89
90 // known incompatible priors
91 if ((prior_type == PriorType::DirichletPriorType)
92 || (prior_type == PriorType::SmoothingPriorType)) {
93 return "The BDeu score already contains a different 'implicit' prior. "
94 "Therefore, the learning will probably be biased.";
95 }
96
97 // prior types unsupported by the type checker
98 return std::format("The prior '{}' is not yet compatible with the score 'BDeu'.",
99 priorTypeToString(prior_type));
100 }
101
103 double ScoreBDeu::score_(const IdCondSet& idset) {
104 // get the counts for all the nodes in the idset and add the prior
105 std::vector< double > N_ijk(this->counter_.counts(idset, true));
106 const std::size_t all_size = N_ijk.size();
107
108 double score = 0.0;
109 const double ess = _internal_prior_.weight();
110 const bool informative_external_prior = this->prior_->isInformative();
111
112
113 // here, we distinguish idsets with conditioning nodes from those
114 // without conditioning nodes
115 if (idset.hasConditioningSet()) {
116 // get the counts for the conditioning nodes
117 std::vector< double > N_ij(this->marginalize_(idset[0], N_ijk));
118 const std::size_t conditioning_size = N_ij.size();
119 const double ess_qi = ess / conditioning_size;
120 const double ess_riqi = ess / all_size;
121
122 if (informative_external_prior) {
123 // the score to compute is that of BD with priors
124 // N'_ijk + ESS / (r_i * q_i )
125 // (the + ESS / (r_i * q_i ) is here to take into account the
126 // internal prior of BDeu)
127 std::vector< double > N_prime_ijk(all_size, 0.0);
128 this->prior_->addJointPseudoCount(idset, N_prime_ijk);
129 std::vector< double > N_prime_ij(N_ij.size(), 0.0);
130 this->prior_->addConditioningPseudoCount(idset, N_prime_ij);
131
132 // the BDeu score can be computed as follows:
133 // sum_j=1^qi [ gammalog2 ( N'_ij + ESS / q_i ) -
134 // gammalog2 ( N_ij + N'_ij + ESS / q_i )
135 // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk + ESS / (r_i * q_i ) )
136 // - gammalog2 ( N'_ijk + ESS / (r_i * q_i ) ) } ]
137 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
138 score += _gammalog2_(N_prime_ij[j] + ess_qi)
139 - _gammalog2_(N_ij[j] + N_prime_ij[j] + ess_qi);
140 }
141 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
142 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + ess_riqi)
143 - _gammalog2_(N_prime_ijk[k] + ess_riqi);
144 }
145 } else {
146 // the BDeu score can be computed as follows:
147 // qi * gammalog2 (ess / qi) - ri * qi * gammalog2 (ess / (ri * qi) )
148 // - sum_j=1^qi [ gammalog2 ( N_ij + ess / qi ) ]
149 // + sum_j=1^qi sum_k=1^ri log [ gammalog2 ( N_ijk + ess / (ri * qi) )
150 // ]
151 score = conditioning_size * _gammalog2_(ess_qi) - all_size * _gammalog2_(ess_riqi);
152
153 for (const auto n_ij: N_ij) {
154 score -= _gammalog2_(n_ij + ess_qi);
155 }
156 for (const auto n_ijk: N_ijk) {
157 score += _gammalog2_(n_ijk + ess_riqi);
158 }
159 }
160 } else {
161 // here, there are no conditioning nodes
162 const double ess_ri = ess / all_size;
163
164 if (informative_external_prior) {
165 // the score to compute is that of BD with priors
166 // N'_ijk + ESS / ( ri * qi )
167 // (the + ESS / ( ri * qi ) is here to take into account the
168 // internal prior of K2)
169 std::vector< double > N_prime_ijk(all_size, 0.0);
170 this->prior_->addJointPseudoCount(idset, N_prime_ijk);
171
172 // the BDeu score can be computed as follows:
173 // gammalog2 ( N' + ess ) - gammalog2 ( N + N' + ess )
174 // + sum_k=1^ri { gammlog2 ( N_i + N'_i + ESS / ri)
175 // - gammalog2 ( N'_i + ESS / ri ) }
176 double N = 0.0;
177 double N_prime = 0.0;
178 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
179 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + ess_ri)
180 - _gammalog2_(N_prime_ijk[k] + ess_ri);
181 N += N_ijk[k];
182 N_prime += N_prime_ijk[k];
183 }
184 score += _gammalog2_(N_prime + ess) - _gammalog2_(N + N_prime + ess);
185 } else {
186 // the BDeu score can be computed as follows:
187 // gammalog2 ( ess ) - ri * gammalog2 ( ess / ri )
188 // - gammalog2 ( N + ess )
189 // + sum_k=1^ri log [ gammalog2 ( N_ijk + ess / ri ) ]
190
191 score = _gammalog2_(ess) - all_size * _gammalog2_(ess_ri);
192 double N = 0;
193 for (const auto n_ijk: N_ijk) {
194 score += _gammalog2_(n_ijk + ess_ri);
195 N += n_ijk;
196 }
197 score -= _gammalog2_(N + ess);
198 }
199 }
200
201 return score;
202 }
203
204 } /* namespace learning */
205
206} /* namespace gum */
207
208#endif /* DOXYGEN_SHOULD_SKIP_THIS */
A class for storing a pair of sets of NodeIds, the second one corresponding to a conditional set.
Definition idCondSet.h:214
the class for computing BDeu scores
Definition scoreBDeu.h:77
double score_(const IdCondSet &idset) final
returns the score for a given IdCondSet
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
ScoreBDeu & operator=(const ScoreBDeu &from)
copy operator
Prior * prior_
the expert knowledge a priorwe add to the score
Definition score.h:238
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
Definition score.h:241
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
Definition CSVParser.h:55
constexpr const char * priorTypeToString(PriorType e) noexcept
Definition prior.h:66
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
the class for computing BDeu scores
the class for computing BDeu scores