aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
scoreLog2Likelihood.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 DBRowGeneratorParser& parser,
68 const Prior& prior,
69 const std::vector< std::pair< std::size_t, std::size_t > >& ranges,
70 const Bijection< NodeId, std::size_t >& nodeId2columns) :
71 Score(parser, prior, ranges, nodeId2columns),
72 _internal_prior_(parser.database(), nodeId2columns) {
73 GUM_CONSTRUCTOR(ScoreLog2Likelihood);
74 }
75
77 ScoreLog2Likelihood::ScoreLog2Likelihood(
78 const DBRowGeneratorParser& parser,
79 const Prior& prior,
80 const Bijection< NodeId, std::size_t >& nodeId2columns) :
81 Score(parser, prior, nodeId2columns), _internal_prior_(parser.database(), nodeId2columns) {
82 GUM_CONSTRUCTOR(ScoreLog2Likelihood);
83 }
84
86 ScoreLog2Likelihood::ScoreLog2Likelihood(const ScoreLog2Likelihood& from) :
87 Score(from), _internal_prior_(from._internal_prior_) {
88 GUM_CONS_CPY(ScoreLog2Likelihood);
89 }
90
92 ScoreLog2Likelihood::ScoreLog2Likelihood(ScoreLog2Likelihood&& from) :
93 Score(std::move(from)), _internal_prior_(std::move(from._internal_prior_)) {
94 GUM_CONS_MOV(ScoreLog2Likelihood);
95 }
96
98 ScoreLog2Likelihood::~ScoreLog2Likelihood() { GUM_DESTRUCTOR(ScoreLog2Likelihood); }
99
101 ScoreLog2Likelihood& ScoreLog2Likelihood::operator=(const ScoreLog2Likelihood& from) {
102 if (this != &from) {
103 Score::operator=(from);
104 _internal_prior_ = from._internal_prior_;
105 }
106 return *this;
107 }
108
110 ScoreLog2Likelihood& ScoreLog2Likelihood::operator=(ScoreLog2Likelihood&& from) {
111 if (this != &from) {
112 Score::operator=(std::move(from));
113 _internal_prior_ = std::move(from._internal_prior_);
114 }
115 return *this;
116 }
117
119 std::string ScoreLog2Likelihood::isPriorCompatible(PriorType prior_type, double weight) {
120 // check that the prior is compatible with the score
121 if ((prior_type == PriorType::DirichletPriorType)
122 || (prior_type == PriorType::SmoothingPriorType)
123 || (prior_type == PriorType::NoPriorType)) {
124 return "";
125 }
126
127 // prior types unsupported by the type checker
128 return std::format("The prior '{}' is not yet compatible with the score 'Log2Likelihood'.",
129 priorTypeToString(prior_type));
130 }
131
133 double ScoreLog2Likelihood::score_(const IdCondSet& idset) {
134 // get the counts for all the nodes in the idset and add the prior
135 std::vector< double > N_ijk(this->counter_.counts(idset, true));
136 const bool informative_external_prior = this->prior_->isInformative();
137 if (informative_external_prior) this->prior_->addJointPseudoCount(idset, N_ijk);
138
139 // here, we distinguish idsets with conditioning nodes from those
140 // without conditioning nodes
141 if (idset.hasConditioningSet()) {
142 // get the counts for the conditioning nodes
143 std::vector< double > N_ij(this->marginalize_(idset[0], N_ijk));
144
145 // compute the score: it remains to compute the log likelihood, i.e.,
146 // sum_k=1^r_i sum_j=1^q_i N_ijk log (N_ijk / N_ij), which is also
147 // equivalent to:
148 // sum_j=1^q_i sum_k=1^r_i N_ijk log N_ijk - sum_j=1^q_i N_ij log N_ij
149 double score = 0.0;
150 for (const auto n_ijk: N_ijk) {
151 if (n_ijk) { score += n_ijk * std::log(n_ijk); }
152 }
153 for (const auto n_ij: N_ij) {
154 if (n_ij) { score -= n_ij * std::log(n_ij); }
155 }
156
157 // divide by log(2), since the log likelihood uses log_2
158 score *= this->one_log2_;
159
160 return score;
161 } else {
162 // here, there are no conditioning nodes
163
164 // compute the score: it remains to compute the log likelihood, i.e.,
165 // sum_k=1^r_i N_ijk log (N_ijk / N), which is also
166 // equivalent to:
167 // sum_j=1^q_i sum_k=1^r_i N_ijk log N_ijk - N log N
168 double N = 0.0;
169 double score = 0.0;
170 for (const auto n_ijk: N_ijk) {
171 if (n_ijk) {
172 score += n_ijk * std::log(n_ijk);
173 N += n_ijk;
174 }
175 }
176 score -= N * std::log(N);
177
178 // divide by log(2), since the log likelihood uses log_2
179 score *= this->one_log2_;
180
181 return score;
182 }
183 }
184
185 } /* namespace learning */
186
187} /* namespace gum */
188
189#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
ScoreLog2Likelihood(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 Log2-likelihood scores
the class for computing Log2-Likelihood scores