aGrUM 2.3.2
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-2025 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-2025 *
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 std::stringstream msg;
99 msg << "The prior '" << priorTypeToString(prior_type)
100 << "' is not yet compatible with the score 'BDeu'.";
101 return msg.str();
102 }
103
105 double ScoreBDeu::score_(const IdCondSet& idset) {
106 // get the counts for all the nodes in the idset and add the prior
107 std::vector< double > N_ijk(this->counter_.counts(idset, true));
108 const std::size_t all_size = N_ijk.size();
109
110 double score = 0.0;
111 const double ess = _internal_prior_.weight();
112 const bool informative_external_prior = this->prior_->isInformative();
113
114
115 // here, we distinguish idsets with conditioning nodes from those
116 // without conditioning nodes
117 if (idset.hasConditioningSet()) {
118 // get the counts for the conditioning nodes
119 std::vector< double > N_ij(this->marginalize_(idset[0], N_ijk));
120 const std::size_t conditioning_size = N_ij.size();
121 const double ess_qi = ess / conditioning_size;
122 const double ess_riqi = ess / all_size;
123
124 if (informative_external_prior) {
125 // the score to compute is that of BD with priors
126 // N'_ijk + ESS / (r_i * q_i )
127 // (the + ESS / (r_i * q_i ) is here to take into account the
128 // internal prior of BDeu)
129 std::vector< double > N_prime_ijk(all_size, 0.0);
130 this->prior_->addJointPseudoCount(idset, N_prime_ijk);
131 std::vector< double > N_prime_ij(N_ij.size(), 0.0);
132 this->prior_->addConditioningPseudoCount(idset, N_prime_ij);
133
134 // the BDeu score can be computed as follows:
135 // sum_j=1^qi [ gammalog2 ( N'_ij + ESS / q_i ) -
136 // gammalog2 ( N_ij + N'_ij + ESS / q_i )
137 // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk + ESS / (r_i * q_i ) )
138 // - gammalog2 ( N'_ijk + ESS / (r_i * q_i ) ) } ]
139 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
140 score += _gammalog2_(N_prime_ij[j] + ess_qi)
141 - _gammalog2_(N_ij[j] + N_prime_ij[j] + ess_qi);
142 }
143 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
144 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + ess_riqi)
145 - _gammalog2_(N_prime_ijk[k] + ess_riqi);
146 }
147 } else {
148 // the BDeu score can be computed as follows:
149 // qi * gammalog2 (ess / qi) - ri * qi * gammalog2 (ess / (ri * qi) )
150 // - sum_j=1^qi [ gammalog2 ( N_ij + ess / qi ) ]
151 // + sum_j=1^qi sum_k=1^ri log [ gammalog2 ( N_ijk + ess / (ri * qi) )
152 // ]
153 score = conditioning_size * _gammalog2_(ess_qi) - all_size * _gammalog2_(ess_riqi);
154
155 for (const auto n_ij: N_ij) {
156 score -= _gammalog2_(n_ij + ess_qi);
157 }
158 for (const auto n_ijk: N_ijk) {
159 score += _gammalog2_(n_ijk + ess_riqi);
160 }
161 }
162 } else {
163 // here, there are no conditioning nodes
164 const double ess_ri = ess / all_size;
165
166 if (informative_external_prior) {
167 // the score to compute is that of BD with priors
168 // N'_ijk + ESS / ( ri * qi )
169 // (the + ESS / ( ri * qi ) is here to take into account the
170 // internal prior of K2)
171 std::vector< double > N_prime_ijk(all_size, 0.0);
172 this->prior_->addJointPseudoCount(idset, N_prime_ijk);
173
174 // the BDeu score can be computed as follows:
175 // gammalog2 ( N' + ess ) - gammalog2 ( N + N' + ess )
176 // + sum_k=1^ri { gammlog2 ( N_i + N'_i + ESS / ri)
177 // - gammalog2 ( N'_i + ESS / ri ) }
178 double N = 0.0;
179 double N_prime = 0.0;
180 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
181 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + ess_ri)
182 - _gammalog2_(N_prime_ijk[k] + ess_ri);
183 N += N_ijk[k];
184 N_prime += N_prime_ijk[k];
185 }
186 score += _gammalog2_(N_prime + ess) - _gammalog2_(N + N_prime + ess);
187 } else {
188 // the BDeu score can be computed as follows:
189 // gammalog2 ( ess ) - ri * gammalog2 ( ess / ri )
190 // - gammalog2 ( N + ess )
191 // + sum_k=1^ri log [ gammalog2 ( N_ijk + ess / ri ) ]
192
193 score = _gammalog2_(ess) - all_size * _gammalog2_(ess_ri);
194 double N = 0;
195 for (const auto n_ijk: N_ijk) {
196 score += _gammalog2_(n_ijk + ess_ri);
197 N += n_ijk;
198 }
199 score -= _gammalog2_(N + ess);
200 }
201 }
202
203 return score;
204 }
205
206 } /* namespace learning */
207
208} /* namespace gum */
209
210#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:78
std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
ScoreBDeu & operator=(const ScoreBDeu &from)
copy operator
virtual double score_(const IdCondSet &idset) final
returns the score for a given IdCondSet
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:54
constexpr const char * priorTypeToString(PriorType e) noexcept
Definition prior.h:68
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