aGrUM 2.3.2
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-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
63 ScoreK2& ScoreK2::operator=(const ScoreK2& from) {
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 ScoreK2::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 K2 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 K2 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 'K2'.";
101 return msg.str();
102 }
103
105 double ScoreK2::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 const bool informative_external_prior = this->prior_->isInformative();
110 double score = 0.0;
111
112 // here, we distinguish idsets with conditioning nodes from those
113 // without conditioning nodes
114 if (idset.hasConditioningSet()) {
115 // get the counts for the conditioning nodes
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);
119
120 if (informative_external_prior) {
121 // the score to compute is that of BD with priors N'_ijk + 1
122 // (the + 1 is here to take into account the internal prior of K2)
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);
127
128 // the K2 score can be computed as follows:
129 // sum_j=1^qi [ gammalog2 ( N'_ij + r_i ) -
130 // gammalog2 ( N_ij + N'_ij + r_i )
131 // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk + 1 ) -
132 // gammalog2 ( N'_ijk + 1 ) } ]
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);
135 }
136 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
137 score
138 += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + 1.0) - _gammalog2_(N_prime_ijk[k] + 1.0);
139 }
140 } else {
141 // the K2 score can be computed as follows:
142 // qi log {(ri - 1)!} + sum_j=1^qi [ - log {(N_ij+ri-1)!} +
143 // sum_k=1^ri log { N_ijk! } ]
144 score = conditioning_size * _gammalog2_(ri);
145
146 for (const auto n_ij: N_ij) {
147 score -= _gammalog2_(n_ij + ri);
148 }
149 for (const auto n_ijk: N_ijk) {
150 score += _gammalog2_(n_ijk + 1);
151 }
152 }
153 } else {
154 // here, there are no conditioning nodes
155 const double ri = double(all_size);
156
157 if (informative_external_prior) {
158 // the score to compute is that of BD with priors N'_ijk + 1
159 // (the + 1 is here to take into account the internal prior of K2)
160
161 // the K2 score can be computed as follows:
162 // gammalog2 ( N' + r_i ) - gammalog2 ( N + N' + r_i )
163 // + sum_k=1^ri { gammlog2 ( N_i + N'_i + 1 ) - gammalog2 ( N'_i + 1 )
164 // }
165 std::vector< double > N_prime_ijk(all_size, 0.0);
166 this->prior_->addJointPseudoCount(idset, N_prime_ijk);
167
168 // the K2 score can be computed as follows:
169 double N = 0.0;
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);
173 N += N_ijk[k];
174 N_prime += N_prime_ijk[k];
175 }
176 score += _gammalog2_(N_prime + ri) - _gammalog2_(N + N_prime + ri);
177 } else {
178 // the K2 score can be computed as follows:
179 // log {(ri - 1)!} - log {(N + ri-1)!} + sum_k=1^ri log { N_ijk! } ]
180 score = _gammalog2_(ri);
181 double N = 0;
182 for (const auto n_ijk: N_ijk) {
183 score += _gammalog2_(n_ijk + 1);
184 N += n_ijk;
185 }
186 score -= _gammalog2_(N + ri);
187 }
188 }
189
190 return score;
191 }
192
193 } /* namespace learning */
194
195} /* namespace gum */
196
197#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 K2 scores (actually their log2 value)
Definition scoreK2.h:80
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
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 K2 scores (actually their log2 value)
the class for computing K2 scores