aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
scoreBDeu_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief the class for computing BDeu scores
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 # include <agrum/BN/learning/scores_and_tests/scoreBDeu.h>
31 # include <sstream>
32 
33 namespace gum {
34 
35  namespace learning {
36 
37  /// default constructor
38  template < template < typename > class ALLOC >
39  INLINE ScoreBDeu< ALLOC >::ScoreBDeu(
40  const DBRowGeneratorParser< ALLOC >& parser,
41  const Apriori< ALLOC >& apriori,
42  const std::vector< std::pair< std::size_t, std::size_t >,
43  ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
44  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >& nodeId2columns,
45  const typename ScoreBDeu< ALLOC >::allocator_type& alloc) :
46  Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
47  _internal_apriori_(parser.database(), nodeId2columns) {
48  GUM_CONSTRUCTOR(ScoreBDeu);
49  }
50 
51 
52  /// default constructor
53  template < template < typename > class ALLOC >
54  INLINE ScoreBDeu< ALLOC >::ScoreBDeu(
55  const DBRowGeneratorParser< ALLOC >& parser,
56  const Apriori< ALLOC >& apriori,
57  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >& nodeId2columns,
58  const typename ScoreBDeu< ALLOC >::allocator_type& alloc) :
59  Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
60  _internal_apriori_(parser.database(), nodeId2columns) {
61  GUM_CONSTRUCTOR(ScoreBDeu);
62  }
63 
64 
65  /// copy constructor with a given allocator
66  template < template < typename > class ALLOC >
67  INLINE ScoreBDeu< ALLOC >::ScoreBDeu(const ScoreBDeu< ALLOC >& from,
68  const typename ScoreBDeu< ALLOC >::allocator_type& alloc) :
69  Score< ALLOC >(from, alloc),
70  _internal_apriori_(from._internal_apriori_, alloc), _gammalog2_(from._gammalog2_) {
71  GUM_CONS_CPY(ScoreBDeu);
72  }
73 
74 
75  /// copy constructor
76  template < template < typename > class ALLOC >
77  INLINE ScoreBDeu< ALLOC >::ScoreBDeu(const ScoreBDeu< ALLOC >& from) :
78  ScoreBDeu< ALLOC >(from, from.getAllocator()) {}
79 
80 
81  /// move constructor with a given allocator
82  template < template < typename > class ALLOC >
83  INLINE ScoreBDeu< ALLOC >::ScoreBDeu(ScoreBDeu< ALLOC >&& from,
84  const typename ScoreBDeu< ALLOC >::allocator_type& alloc) :
85  Score< ALLOC >(std::move(from), alloc),
86  _internal_apriori_(std::move(from._internal_apriori_), alloc),
87  _gammalog2_(std::move(from._gammalog2_)) {
88  GUM_CONS_MOV(ScoreBDeu);
89  }
90 
91 
92  /// move constructor
93  template < template < typename > class ALLOC >
94  INLINE ScoreBDeu< ALLOC >::ScoreBDeu(ScoreBDeu< ALLOC >&& from) :
95  ScoreBDeu< ALLOC >(std::move(from), from.getAllocator()) {}
96 
97 
98  /// virtual copy constructor with a given allocator
99  template < template < typename > class ALLOC >
100  ScoreBDeu< ALLOC >*
101  ScoreBDeu< ALLOC >::clone(const typename ScoreBDeu< ALLOC >::allocator_type& alloc) const {
102  ALLOC< ScoreBDeu< ALLOC > > allocator(alloc);
103  ScoreBDeu< ALLOC >* new_score = allocator.allocate(1);
104  try {
105  allocator.construct(new_score, *this, alloc);
106  } catch (...) {
107  allocator.deallocate(new_score, 1);
108  throw;
109  }
110 
111  return new_score;
112  }
113 
114 
115  /// virtual copy constructor
116  template < template < typename > class ALLOC >
117  ScoreBDeu< ALLOC >* ScoreBDeu< ALLOC >::clone() const {
118  return clone(this->getAllocator());
119  }
120 
121 
122  /// destructor
123  template < template < typename > class ALLOC >
124  ScoreBDeu< ALLOC >::~ScoreBDeu() {
125  GUM_DESTRUCTOR(ScoreBDeu);
126  }
127 
128 
129  /// copy operator
130  template < template < typename > class ALLOC >
131  ScoreBDeu< ALLOC >& ScoreBDeu< ALLOC >::operator=(const ScoreBDeu< ALLOC >& from) {
132  if (this != &from) {
133  Score< ALLOC >::operator=(from);
134  _internal_apriori_ = from._internal_apriori_;
135  }
136  return *this;
137  }
138 
139 
140  /// move operator
141  template < template < typename > class ALLOC >
142  ScoreBDeu< ALLOC >& ScoreBDeu< ALLOC >::operator=(ScoreBDeu< ALLOC >&& from) {
143  if (this != &from) {
144  Score< ALLOC >::operator=(std::move(from));
145  _internal_apriori_ = std::move(from._internal_apriori_);
146  }
147  return *this;
148  }
149 
150 
151  /// indicates whether the apriori is compatible (meaningful) with the score
152  template < template < typename > class ALLOC >
153  std::string ScoreBDeu< ALLOC >::isAprioriCompatible(const std::string& apriori_type,
154  double weight) {
155  // check that the apriori is compatible with the score
156  if (apriori_type == AprioriNoAprioriType::type) { return ""; }
157 
158  if (weight == 0.0) {
159  return "The apriori is currently compatible with the BDeu score but "
160  "if you change the weight, it will become incompatible.";
161  }
162 
163  // known incompatible aprioris
164  if ((apriori_type == AprioriDirichletType::type)
165  || (apriori_type == AprioriSmoothingType::type)) {
166  return "The BDeu score already contains a different 'implicit' apriori. "
167  "Therefore, the learning will probably be biased.";
168  }
169 
170  // apriori types unsupported by the type checker
171  std::stringstream msg;
172  msg << "The apriori '" << apriori_type << "' is not yet compatible with the score 'BDeu'.";
173  return msg.str();
174  }
175 
176 
177  /// indicates whether the apriori is compatible (meaningful) with the score
178  template < template < typename > class ALLOC >
179  INLINE std::string ScoreBDeu< ALLOC >::isAprioriCompatible(const Apriori< ALLOC >& apriori) {
180  return isAprioriCompatible(apriori.getType(), apriori.weight());
181  }
182 
183 
184  /// indicates whether the apriori is compatible (meaningful) with the score
185  template < template < typename > class ALLOC >
186  INLINE std::string ScoreBDeu< ALLOC >::isAprioriCompatible() const {
187  return isAprioriCompatible(*(this->apriori_));
188  }
189 
190 
191  /// returns the internal apriori of the score
192  template < template < typename > class ALLOC >
193  INLINE const Apriori< ALLOC >& ScoreBDeu< ALLOC >::internalApriori() const {
194  return _internal_apriori_;
195  }
196 
197 
198  /// sets the effective sample size of the internal apriori
199  template < template < typename > class ALLOC >
200  INLINE void ScoreBDeu< ALLOC >::setEffectiveSampleSize(double ess) {
201  if (ess < 0) {
202  GUM_ERROR(OutOfBounds,
203  "The effective sample size of the BDeu's "
204  "internal apriori must be positive");
205  } else {
206  _internal_apriori_.setEffectiveSampleSize(ess);
207  }
208  }
209 
210 
211  /// returns the score corresponding to a given nodeset
212  template < template < typename > class ALLOC >
213  double ScoreBDeu< ALLOC >::score_(const IdCondSet< ALLOC >& idset) {
214  // get the counts for all the nodes in the idset and add the apriori
215  std::vector< double, ALLOC< double > > N_ijk(this->counter_.counts(idset, true));
216  const std::size_t all_size = N_ijk.size();
217 
218  double score = 0.0;
219  const double ess = _internal_apriori_.weight();
220  const bool informative_external_apriori = this->apriori_->isInformative();
221 
222 
223  // here, we distinguish idsets with conditioning nodes from those
224  // without conditioning nodes
225  if (idset.hasConditioningSet()) {
226  // get the counts for the conditioning nodes
227  std::vector< double, ALLOC< double > > N_ij(this->marginalize_(idset[0], N_ijk));
228  const std::size_t conditioning_size = N_ij.size();
229  const double ess_qi = ess / conditioning_size;
230  const double ess_riqi = ess / all_size;
231 
232  if (informative_external_apriori) {
233  // the score to compute is that of BD with aprioris
234  // N'_ijk + ESS / (r_i * q_i )
235  // (the + ESS / (r_i * q_i ) is here to take into account the
236  // internal apriori of BDeu)
237  std::vector< double, ALLOC< double > > N_prime_ijk(all_size, 0.0);
238  this->apriori_->addAllApriori(idset, N_prime_ijk);
239  std::vector< double, ALLOC< double > > N_prime_ij(N_ij.size(), 0.0);
240  this->apriori_->addConditioningApriori(idset, N_prime_ij);
241 
242  // the BDeu score can be computed as follows:
243  // sum_j=1^qi [ gammalog2 ( N'_ij + ESS / q_i ) -
244  // gammalog2 ( N_ij + N'_ij + ESS / q_i )
245  // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk + ESS / (r_i * q_i ) )
246  // - gammalog2 ( N'_ijk + ESS / (r_i * q_i ) ) } ]
247  for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
248  score += _gammalog2_(N_prime_ij[j] + ess_qi)
249  - _gammalog2_(N_ij[j] + N_prime_ij[j] + ess_qi);
250  }
251  for (std::size_t k = std::size_t(0); k < all_size; ++k) {
252  score += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + ess_riqi)
253  - _gammalog2_(N_prime_ijk[k] + ess_riqi);
254  }
255  } else {
256  // the BDeu score can be computed as follows:
257  // qi * gammalog2 (ess / qi) - ri * qi * gammalog2 (ess / (ri * qi) )
258  // - sum_j=1^qi [ gammalog2 ( N_ij + ess / qi ) ]
259  // + sum_j=1^qi sum_k=1^ri log [ gammalog2 ( N_ijk + ess / (ri * qi) )
260  // ]
261  score = conditioning_size * _gammalog2_(ess_qi) - all_size * _gammalog2_(ess_riqi);
262 
263  for (const auto n_ij: N_ij) {
264  score -= _gammalog2_(n_ij + ess_qi);
265  }
266  for (const auto n_ijk: N_ijk) {
267  score += _gammalog2_(n_ijk + ess_riqi);
268  }
269  }
270  } else {
271  // here, there are no conditioning nodes
272  const double ess_ri = ess / all_size;
273 
274  if (informative_external_apriori) {
275  // the score to compute is that of BD with aprioris
276  // N'_ijk + ESS / ( ri * qi )
277  // (the + ESS / ( ri * qi ) is here to take into account the
278  // internal apriori of K2)
279  std::vector< double, ALLOC< double > > N_prime_ijk(all_size, 0.0);
280  this->apriori_->addAllApriori(idset, N_prime_ijk);
281 
282  // the BDeu score can be computed as follows:
283  // gammalog2 ( N' + ess ) - gammalog2 ( N + N' + ess )
284  // + sum_k=1^ri { gammlog2 ( N_i + N'_i + ESS / ri)
285  // - gammalog2 ( N'_i + ESS / ri ) }
286  double N = 0.0;
287  double N_prime = 0.0;
288  for (std::size_t k = std::size_t(0); k < all_size; ++k) {
289  score += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + ess_ri)
290  - _gammalog2_(N_prime_ijk[k] + ess_ri);
291  N += N_ijk[k];
292  N_prime += N_prime_ijk[k];
293  }
294  score += _gammalog2_(N_prime + ess) - _gammalog2_(N + N_prime + ess);
295  } else {
296  // the BDeu score can be computed as follows:
297  // gammalog2 ( ess ) - ri * gammalog2 ( ess / ri )
298  // - gammalog2 ( N + ess )
299  // + sum_k=1^ri log [ gammalog2 ( N_ijk + ess / ri ) ]
300 
301  score = _gammalog2_(ess) - all_size * _gammalog2_(ess_ri);
302  double N = 0;
303  for (const auto n_ijk: N_ijk) {
304  score += _gammalog2_(n_ijk + ess_ri);
305  N += n_ijk;
306  }
307  score -= _gammalog2_(N + ess);
308  }
309  }
310 
311  return score;
312  }
313 
314  } /* namespace learning */
315 
316 } /* namespace gum */
317 
318 #endif /* DOXYGEN_SHOULD_SKIP_THIS */