aGrUM 3.2.0
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
62 // Constructors and destructor are defined out-of-line (not INLINE) on
63 // purpose: see the comment in score.cpp for the MSVC LNK2005 rationale.
64
67 const Prior& prior,
68 const std::vector< std::pair< std::size_t, std::size_t > >& ranges,
69 const Bijection< NodeId, std::size_t >& nodeId2columns) :
70 Score(parser, prior, ranges, nodeId2columns),
71 _internal_prior_(parser.database(), nodeId2columns) {
72 GUM_CONSTRUCTOR(ScoreBDeu);
73 }
74
76 ScoreBDeu::ScoreBDeu(const DBRowGeneratorParser& parser,
77 const Prior& prior,
78 const Bijection< NodeId, std::size_t >& nodeId2columns) :
79 Score(parser, prior, nodeId2columns), _internal_prior_(parser.database(), nodeId2columns) {
80 GUM_CONSTRUCTOR(ScoreBDeu);
81 }
82
84 ScoreBDeu::ScoreBDeu(const ScoreBDeu& from) :
85 Score(from), _internal_prior_(from._internal_prior_), _gammalog2_(from._gammalog2_) {
86 GUM_CONS_CPY(ScoreBDeu);
87 }
88
90 ScoreBDeu::ScoreBDeu(ScoreBDeu&& from) :
91 Score(std::move(from)), _internal_prior_(std::move(from._internal_prior_)),
92 _gammalog2_(std::move(from._gammalog2_)) {
93 GUM_CONS_MOV(ScoreBDeu);
94 }
95
97 ScoreBDeu::~ScoreBDeu() { GUM_DESTRUCTOR(ScoreBDeu); }
98
100 ScoreBDeu& ScoreBDeu::operator=(const ScoreBDeu& from) {
101 if (this != &from) {
102 Score::operator=(from);
103 _internal_prior_ = from._internal_prior_;
104 }
105 return *this;
106 }
107
109 ScoreBDeu& ScoreBDeu::operator=(ScoreBDeu&& from) {
110 if (this != &from) {
111 Score::operator=(std::move(from));
112 _internal_prior_ = std::move(from._internal_prior_);
113 }
114 return *this;
115 }
116
118 std::string ScoreBDeu::isPriorCompatible(PriorType prior_type, double weight) {
119 // check that the prior is compatible with the score
120 if (prior_type == PriorType::NoPriorType) { return ""; }
121
122 if (weight == 0.0) {
123 return "The prior is currently compatible with the BDeu score but "
124 "if you change the weight, it will become incompatible.";
125 }
126
127 // known incompatible priors
128 if ((prior_type == PriorType::DirichletPriorType)
129 || (prior_type == PriorType::SmoothingPriorType)) {
130 return "The BDeu score already contains a different 'implicit' prior. "
131 "Therefore, the learning will probably be biased.";
132 }
133
134 // prior types unsupported by the type checker
135 return std::format("The prior '{}' is not yet compatible with the score 'BDeu'.",
136 priorTypeToString(prior_type));
137 }
138
140 double ScoreBDeu::score_(const IdCondSet& idset) {
141 // get the counts for all the nodes in the idset and add the prior
142 std::vector< double > N_ijk(this->counter_.counts(idset, true));
143 const std::size_t all_size = N_ijk.size();
144
145 double score = 0.0;
146 const double ess = _internal_prior_.weight();
147 const bool informative_external_prior = this->prior_->isInformative();
148
149
150 // here, we distinguish idsets with conditioning nodes from those
151 // without conditioning nodes
152 if (idset.hasConditioningSet()) {
153 // get the counts for the conditioning nodes
154 std::vector< double > N_ij(this->marginalize_(idset[0], N_ijk));
155 const std::size_t conditioning_size = N_ij.size();
156 const double ess_qi = ess / conditioning_size;
157 const double ess_riqi = ess / all_size;
158
159 if (informative_external_prior) {
160 // the score to compute is that of BD with priors
161 // N'_ijk + ESS / (r_i * q_i )
162 // (the + ESS / (r_i * q_i ) is here to take into account the
163 // internal prior of BDeu)
164 std::vector< double > N_prime_ijk(all_size, 0.0);
165 this->prior_->addJointPseudoCount(idset, N_prime_ijk);
166 std::vector< double > N_prime_ij(N_ij.size(), 0.0);
167 this->prior_->addConditioningPseudoCount(idset, N_prime_ij);
168
169 // the BDeu score can be computed as follows:
170 // sum_j=1^qi [ gammalog2 ( N'_ij + ESS / q_i ) -
171 // gammalog2 ( N_ij + N'_ij + ESS / q_i )
172 // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk + ESS / (r_i * q_i ) )
173 // - gammalog2 ( N'_ijk + ESS / (r_i * q_i ) ) } ]
174 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
175 score += _gammalog2_(N_prime_ij[j] + ess_qi)
176 - _gammalog2_(N_ij[j] + N_prime_ij[j] + ess_qi);
177 }
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_riqi)
180 - _gammalog2_(N_prime_ijk[k] + ess_riqi);
181 }
182 } else {
183 // the BDeu score can be computed as follows:
184 // qi * gammalog2 (ess / qi) - ri * qi * gammalog2 (ess / (ri * qi) )
185 // - sum_j=1^qi [ gammalog2 ( N_ij + ess / qi ) ]
186 // + sum_j=1^qi sum_k=1^ri log [ gammalog2 ( N_ijk + ess / (ri * qi) )
187 // ]
188 score = conditioning_size * _gammalog2_(ess_qi) - all_size * _gammalog2_(ess_riqi);
189
190 for (const auto n_ij: N_ij) {
191 score -= _gammalog2_(n_ij + ess_qi);
192 }
193 for (const auto n_ijk: N_ijk) {
194 score += _gammalog2_(n_ijk + ess_riqi);
195 }
196 }
197 } else {
198 // here, there are no conditioning nodes
199 const double ess_ri = ess / all_size;
200
201 if (informative_external_prior) {
202 // the score to compute is that of BD with priors
203 // N'_ijk + ESS / ( ri * qi )
204 // (the + ESS / ( ri * qi ) is here to take into account the
205 // internal prior of K2)
206 std::vector< double > N_prime_ijk(all_size, 0.0);
207 this->prior_->addJointPseudoCount(idset, N_prime_ijk);
208
209 // the BDeu score can be computed as follows:
210 // gammalog2 ( N' + ess ) - gammalog2 ( N + N' + ess )
211 // + sum_k=1^ri { gammlog2 ( N_i + N'_i + ESS / ri)
212 // - gammalog2 ( N'_i + ESS / ri ) }
213 double N = 0.0;
214 double N_prime = 0.0;
215 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
216 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + ess_ri)
217 - _gammalog2_(N_prime_ijk[k] + ess_ri);
218 N += N_ijk[k];
219 N_prime += N_prime_ijk[k];
220 }
221 score += _gammalog2_(N_prime + ess) - _gammalog2_(N + N_prime + ess);
222 } else {
223 // the BDeu score can be computed as follows:
224 // gammalog2 ( ess ) - ri * gammalog2 ( ess / ri )
225 // - gammalog2 ( N + ess )
226 // + sum_k=1^ri log [ gammalog2 ( N_ijk + ess / ri ) ]
227
228 score = _gammalog2_(ess) - all_size * _gammalog2_(ess_ri);
229 double N = 0;
230 for (const auto n_ijk: N_ijk) {
231 score += _gammalog2_(n_ijk + ess_ri);
232 N += n_ijk;
233 }
234 score -= _gammalog2_(N + ess);
235 }
236 }
237
238 return score;
239 }
240
241 } /* namespace learning */
242
243} /* namespace gum */
244
245#endif /* DOXYGEN_SHOULD_SKIP_THIS */
the class used to read a row in the database and to transform it into a set of DBRow instances that c...
the base class for all a priori
Definition prior.h:84
ScoreBDeu(const DBRowGeneratorParser &parser, const Prior &prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
The base class for all the scores used for learning (BIC, BDeu, etc).
Definition score.h:68
include the inlined functions if necessary
Definition CSVParser.h:55
constexpr const char * priorTypeToString(PriorType e) noexcept
Definition prior.h:69
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
the class for computing BDeu scores
the class for computing BDeu scores