aGrUM 3.2.0
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-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(ScoreBD);
73 }
74
76 ScoreBD::ScoreBD(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(ScoreBD);
81 }
82
84 ScoreBD::ScoreBD(const ScoreBD& from) :
85 Score(from), _internal_prior_(from._internal_prior_), _gammalog2_(from._gammalog2_) {
86 GUM_CONS_CPY(ScoreBD);
87 }
88
90 ScoreBD::ScoreBD(ScoreBD&& from) :
91 Score(std::move(from)), _internal_prior_(std::move(from._internal_prior_)),
92 _gammalog2_(std::move(from._gammalog2_)) {
93 GUM_CONS_MOV(ScoreBD);
94 }
95
97 ScoreBD::~ScoreBD() { GUM_DESTRUCTOR(ScoreBD); }
98
100 ScoreBD& ScoreBD::operator=(const ScoreBD& from) {
101 if (this != &from) {
102 Score::operator=(from);
103 _internal_prior_ = from._internal_prior_;
104 }
105 return *this;
106 }
107
109 ScoreBD& ScoreBD::operator=(ScoreBD&& 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 ScoreBD::isPriorCompatible(PriorType prior_type, double weight) {
119 if (prior_type == PriorType::NoPriorType) { return "The BD score requires an prior"; }
120
121 if (weight != 0.0) {
122 return "The prior is currently compatible with the BD score but if "
123 "you change the weight, it may become biased";
124 }
125
126 // prior types unsupported by the type checker
127 return std::format("The prior '{}' is not yet compatible with the score 'BD'.",
128 priorTypeToString(prior_type));
129 }
130
132 double ScoreBD::score_(const IdCondSet& idset) {
133 // if the weight of the prior is 0, then gammaLog2 will fail
134 if (!this->prior_->isInformative()) {
136 "The BD score requires its external prior to " << "be strictly positive");
137 }
138
139 // get the counts for all the nodes in the idset and add the prior
140 std::vector< double > N_ijk(this->counter_.counts(idset, true));
141 const std::size_t all_size = N_ijk.size();
142 std::vector< double > N_prime_ijk(all_size, 0.0);
143 this->prior_->addJointPseudoCount(idset, N_prime_ijk);
144
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
154 std::vector< double > N_prime_ij(N_ij.size(), 0.0);
155 this->prior_->addConditioningPseudoCount(idset, N_prime_ij);
156
157 // the BD score can be computed as follows:
158 // sum_j=1^qi [ gammalog2 ( N'_ij ) - gammalog2 ( N_ij + N'_ij )
159 // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk ) -
160 // gammalog2 ( N'_ijk ) } ]
161 for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
162 score += _gammalog2_(N_prime_ij[j]) - _gammalog2_(N_ij[j] + N_prime_ij[j]);
163 }
164 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
165 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k]) - _gammalog2_(N_prime_ijk[k]);
166 }
167 } else {
168 // the BD score can be computed as follows:
169 // gammalog2 ( N' ) - gammalog2 ( N + N' )
170 // + sum_k=1^ri { gammlog2 ( N_i + N'_i ) - gammalog2 ( N'_i ) }
171 double N = 0.0;
172 double N_prime = 0.0;
173 for (std::size_t k = std::size_t(0); k < all_size; ++k) {
174 score += _gammalog2_(N_ijk[k] + N_prime_ijk[k]) - _gammalog2_(N_prime_ijk[k]);
175 N += N_ijk[k];
176 N_prime += N_prime_ijk[k];
177 }
178 score += _gammalog2_(N_prime) - _gammalog2_(N + N_prime);
179 }
180
181 return score;
182 }
183
184 } /* namespace learning */
185
186} /* namespace gum */
187
188#endif /* DOXYGEN_SHOULD_SKIP_THIS */
Exception : out of bound.
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
ScoreBD(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
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
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 Bayesian Dirichlet (BD) log2 scores
the class for computing BD scores