aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
scoreBD.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 ScoreBD& ScoreBD::operator=(const ScoreBD& 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 ScoreBD::isPriorCompatible(PriorType prior_type, double weight) {
82 if (prior_type == PriorType::NoPriorType) { return "The BD score requires an prior"; }
83
84 if (weight != 0.0) {
85 return "The prior is currently compatible with the BD score but if "
86 "you change the weight, it may become biased";
87 }
88
89 // prior types unsupported by the type checker
90 std::stringstream msg;
91 msg << "The prior '" << priorTypeToString(prior_type)
92 << "' is not yet compatible with the score 'BD'.";
93 return msg.str();
94 }
95
97 double ScoreBD::score_(const IdCondSet& idset) {
98 // if the weight of the prior is 0, then gammaLog2 will fail
99 if (!this->prior_->isInformative()) {
100 GUM_ERROR(OutOfBounds,
101 "The BD score requires its external prior to " << "be strictly positive");
102 }
103
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 std::vector< double > N_prime_ijk(all_size, 0.0);
108 this->prior_->addJointPseudoCount(idset, N_prime_ijk);
109
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
119 std::vector< double > N_prime_ij(N_ij.size(), 0.0);
120 this->prior_->addConditioningPseudoCount(idset, N_prime_ij);
121
122 // the BD score can be computed as follows:
123 // sum_j=1^qi [ gammalog2 ( N'_ij ) - gammalog2 ( N_ij + N'_ij )
124 // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk ) -
125 // gammalog2 ( N'_ijk ) } ]
126 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
127 score += _gammalog2_(N_prime_ij[j]) - _gammalog2_(N_ij[j] + N_prime_ij[j]);
128 }
129 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
130 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k]) - _gammalog2_(N_prime_ijk[k]);
131 }
132 } else {
133 // the BD score can be computed as follows:
134 // gammalog2 ( N' ) - gammalog2 ( N + N' )
135 // + sum_k=1^ri { gammlog2 ( N_i + N'_i ) - gammalog2 ( N'_i ) }
136 double N = 0.0;
137 double N_prime = 0.0;
138 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
139 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k]) - _gammalog2_(N_prime_ijk[k]);
140 N += N_ijk[k];
141 N_prime += N_prime_ijk[k];
142 }
143 score += _gammalog2_(N_prime) - _gammalog2_(N + N_prime);
144 }
145
146 return score;
147 }
148
149 } /* namespace learning */
150
151} /* namespace gum */
152
153#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 Bayesian Dirichlet (BD) log2 scores
Definition scoreBD.h:84
virtual double score_(const IdCondSet &idset) final
returns the score for a given IdCondSet
virtual std::string isPriorCompatible() const final
indicates whether the prior is compatible (meaningful) with the score
ScoreBD & operator=(const ScoreBD &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
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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 Bayesian Dirichlet (BD) log2 scores
the class for computing BD scores