aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
scoreK2.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(ScoreK2);
73 }
74
76 ScoreK2::ScoreK2(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(ScoreK2);
81 }
82
84 ScoreK2::ScoreK2(const ScoreK2& from) :
85 Score(from), _internal_prior_(from._internal_prior_), _gammalog2_(from._gammalog2_) {
86 GUM_CONS_CPY(ScoreK2);
87 }
88
90 ScoreK2::ScoreK2(ScoreK2&& from) :
91 Score(std::move(from)), _internal_prior_(std::move(from._internal_prior_)),
92 _gammalog2_(std::move(from._gammalog2_)) {
93 GUM_CONS_MOV(ScoreK2);
94 }
95
97 ScoreK2::~ScoreK2() { GUM_DESTRUCTOR(ScoreK2); }
98
100 ScoreK2& ScoreK2::operator=(const ScoreK2& from) {
101 if (this != &from) {
102 Score::operator=(from);
103 _internal_prior_ = from._internal_prior_;
104 }
105 return *this;
106 }
107
109 ScoreK2& ScoreK2::operator=(ScoreK2&& 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 ScoreK2::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 K2 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 K2 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 'K2'.",
136 priorTypeToString(prior_type));
137 }
138
140 double ScoreK2::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 const bool informative_external_prior = this->prior_->isInformative();
145 double score = 0.0;
146
147 // here, we distinguish idsets with conditioning nodes from those
148 // without conditioning nodes
149 if (idset.hasConditioningSet()) {
150 // get the counts for the conditioning nodes
151 std::vector< double > N_ij(this->marginalize_(idset[0], N_ijk));
152 const std::size_t conditioning_size = N_ij.size();
153 const double ri = double(all_size / conditioning_size);
154
155 if (informative_external_prior) {
156 // the score to compute is that of BD with priors N'_ijk + 1
157 // (the + 1 is here to take into account the internal prior of K2)
158 std::vector< double > N_prime_ijk(all_size, 0.0);
159 this->prior_->addJointPseudoCount(idset, N_prime_ijk);
160 std::vector< double > N_prime_ij(N_ij.size(), 0.0);
161 this->prior_->addConditioningPseudoCount(idset, N_prime_ij);
162
163 // the K2 score can be computed as follows:
164 // sum_j=1^qi [ gammalog2 ( N'_ij + r_i ) -
165 // gammalog2 ( N_ij + N'_ij + r_i )
166 // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk + 1 ) -
167 // gammalog2 ( N'_ijk + 1 ) } ]
168 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
169 score += _gammalog2_(N_prime_ij[j] + ri) - _gammalog2_(N_ij[j] + N_prime_ij[j] + ri);
170 }
171 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
172 score
173 += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + 1.0) - _gammalog2_(N_prime_ijk[k] + 1.0);
174 }
175 } else {
176 // the K2 score can be computed as follows:
177 // qi log {(ri - 1)!} + sum_j=1^qi [ - log {(N_ij+ri-1)!} +
178 // sum_k=1^ri log { N_ijk! } ]
179 score = conditioning_size * _gammalog2_(ri);
180
181 for (const auto n_ij: N_ij) {
182 score -= _gammalog2_(n_ij + ri);
183 }
184 for (const auto n_ijk: N_ijk) {
185 score += _gammalog2_(n_ijk + 1);
186 }
187 }
188 } else {
189 // here, there are no conditioning nodes
190 const double ri = double(all_size);
191
192 if (informative_external_prior) {
193 // the score to compute is that of BD with priors N'_ijk + 1
194 // (the + 1 is here to take into account the internal prior of K2)
195
196 // the K2 score can be computed as follows:
197 // gammalog2 ( N' + r_i ) - gammalog2 ( N + N' + r_i )
198 // + sum_k=1^ri { gammlog2 ( N_i + N'_i + 1 ) - gammalog2 ( N'_i + 1 )
199 // }
200 std::vector< double > N_prime_ijk(all_size, 0.0);
201 this->prior_->addJointPseudoCount(idset, N_prime_ijk);
202
203 // the K2 score can be computed as follows:
204 double N = 0.0;
205 double N_prime = 0.0;
206 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
207 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + 1) - _gammalog2_(N_prime_ijk[k] + 1);
208 N += N_ijk[k];
209 N_prime += N_prime_ijk[k];
210 }
211 score += _gammalog2_(N_prime + ri) - _gammalog2_(N + N_prime + ri);
212 } else {
213 // the K2 score can be computed as follows:
214 // log {(ri - 1)!} - log {(N + ri-1)!} + sum_k=1^ri log { N_ijk! } ]
215 score = _gammalog2_(ri);
216 double N = 0;
217 for (const auto n_ijk: N_ijk) {
218 score += _gammalog2_(n_ijk + 1);
219 N += n_ijk;
220 }
221 score -= _gammalog2_(N + ri);
222 }
223 }
224
225 return score;
226 }
227
228 } /* namespace learning */
229
230} /* namespace gum */
231
232#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
ScoreK2(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 K2 scores (actually their log2 value)
the class for computing K2 scores