aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
scoreK2_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 K2 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/scoreK2.h>
31 # include <sstream>
32 
33 namespace gum {
34 
35  namespace learning {
36 
37  /// default constructor
38  template < template < typename > class ALLOC >
39  INLINE ScoreK2< ALLOC >::ScoreK2(
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 ScoreK2< ALLOC >::allocator_type& alloc) :
46  Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
47  _internal_apriori_(parser.database(), nodeId2columns) {
48  GUM_CONSTRUCTOR(ScoreK2);
49  }
50 
51 
52  /// default constructor
53  template < template < typename > class ALLOC >
54  INLINE ScoreK2< ALLOC >::ScoreK2(
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 ScoreK2< ALLOC >::allocator_type& alloc) :
59  Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
60  _internal_apriori_(parser.database(), nodeId2columns) {
61  GUM_CONSTRUCTOR(ScoreK2);
62  }
63 
64 
65  /// copy constructor with a given allocator
66  template < template < typename > class ALLOC >
67  INLINE ScoreK2< ALLOC >::ScoreK2(const ScoreK2< ALLOC >& from,
68  const typename ScoreK2< ALLOC >::allocator_type& alloc) :
69  Score< ALLOC >(from, alloc),
70  _internal_apriori_(from._internal_apriori_, alloc), _gammalog2_(from._gammalog2_) {
71  GUM_CONS_CPY(ScoreK2);
72  }
73 
74 
75  /// copy constructor
76  template < template < typename > class ALLOC >
77  INLINE ScoreK2< ALLOC >::ScoreK2(const ScoreK2< ALLOC >& from) :
78  ScoreK2< ALLOC >(from, from.getAllocator()) {}
79 
80 
81  /// move constructor with a given allocator
82  template < template < typename > class ALLOC >
83  INLINE ScoreK2< ALLOC >::ScoreK2(ScoreK2< ALLOC >&& from,
84  const typename ScoreK2< 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(ScoreK2);
89  }
90 
91 
92  /// move constructor
93  template < template < typename > class ALLOC >
94  INLINE ScoreK2< ALLOC >::ScoreK2(ScoreK2< ALLOC >&& from) :
95  ScoreK2< ALLOC >(std::move(from), from.getAllocator()) {}
96 
97 
98  /// virtual copy constructor with a given allocator
99  template < template < typename > class ALLOC >
100  ScoreK2< ALLOC >*
101  ScoreK2< ALLOC >::clone(const typename ScoreK2< ALLOC >::allocator_type& alloc) const {
102  ALLOC< ScoreK2< ALLOC > > allocator(alloc);
103  ScoreK2< 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  ScoreK2< ALLOC >* ScoreK2< ALLOC >::clone() const {
118  return clone(this->getAllocator());
119  }
120 
121 
122  /// destructor
123  template < template < typename > class ALLOC >
124  ScoreK2< ALLOC >::~ScoreK2() {
125  GUM_DESTRUCTOR(ScoreK2);
126  }
127 
128 
129  /// copy operator
130  template < template < typename > class ALLOC >
131  ScoreK2< ALLOC >& ScoreK2< ALLOC >::operator=(const ScoreK2< 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  ScoreK2< ALLOC >& ScoreK2< ALLOC >::operator=(ScoreK2< 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 ScoreK2< 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 K2 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 K2 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
173  << "' is not yet supported by method isAprioriCompatible os Score K2";
174  return msg.str();
175  }
176 
177 
178  /// indicates whether the apriori is compatible (meaningful) with the score
179  template < template < typename > class ALLOC >
180  INLINE std::string ScoreK2< ALLOC >::isAprioriCompatible(const Apriori< ALLOC >& apriori) {
181  return isAprioriCompatible(apriori.getType(), apriori.weight());
182  }
183 
184 
185  /// indicates whether the apriori is compatible (meaningful) with the score
186  template < template < typename > class ALLOC >
187  INLINE std::string ScoreK2< ALLOC >::isAprioriCompatible() const {
188  return isAprioriCompatible(*(this->apriori_));
189  }
190 
191 
192  /// returns the internal apriori of the score
193  template < template < typename > class ALLOC >
194  INLINE const Apriori< ALLOC >& ScoreK2< ALLOC >::internalApriori() const {
195  return _internal_apriori_;
196  }
197 
198 
199  /// returns the score corresponding to a given nodeset
200  template < template < typename > class ALLOC >
201  double ScoreK2< ALLOC >::score_(const IdCondSet< ALLOC >& idset) {
202  // get the counts for all the nodes in the idset and add the apriori
203  std::vector< double, ALLOC< double > > N_ijk(this->counter_.counts(idset, true));
204  const std::size_t all_size = N_ijk.size();
205  const bool informative_external_apriori = this->apriori_->isInformative();
206  double score = 0.0;
207 
208  // here, we distinguish idsets with conditioning nodes from those
209  // without conditioning nodes
210  if (idset.hasConditioningSet()) {
211  // get the counts for the conditioning nodes
212  std::vector< double, ALLOC< double > > N_ij(this->marginalize_(idset[0], N_ijk));
213  const std::size_t conditioning_size = N_ij.size();
214  const double ri = double(all_size / conditioning_size);
215 
216  if (informative_external_apriori) {
217  // the score to compute is that of BD with aprioris N'_ijk + 1
218  // (the + 1 is here to take into account the internal apriori of K2)
219  std::vector< double, ALLOC< double > > N_prime_ijk(all_size, 0.0);
220  this->apriori_->addAllApriori(idset, N_prime_ijk);
221  std::vector< double, ALLOC< double > > N_prime_ij(N_ij.size(), 0.0);
222  this->apriori_->addConditioningApriori(idset, N_prime_ij);
223 
224  // the K2 score can be computed as follows:
225  // sum_j=1^qi [ gammalog2 ( N'_ij + r_i ) -
226  // gammalog2 ( N_ij + N'_ij + r_i )
227  // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk + 1 ) -
228  // gammalog2 ( N'_ijk + 1 ) } ]
229  for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
230  score += _gammalog2_(N_prime_ij[j] + ri) - _gammalog2_(N_ij[j] + N_prime_ij[j] + ri);
231  }
232  for (std::size_t k = std::size_t(0); k < all_size; ++k) {
233  score
234  += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + 1.0) - _gammalog2_(N_prime_ijk[k] + 1.0);
235  }
236  } else {
237  // the K2 score can be computed as follows:
238  // qi log {(ri - 1)!} + sum_j=1^qi [ - log {(N_ij+ri-1)!} +
239  // sum_k=1^ri log { N_ijk! } ]
240  score = conditioning_size * _gammalog2_(ri);
241 
242  for (const auto n_ij: N_ij) {
243  score -= _gammalog2_(n_ij + ri);
244  }
245  for (const auto n_ijk: N_ijk) {
246  score += _gammalog2_(n_ijk + 1);
247  }
248  }
249  } else {
250  // here, there are no conditioning nodes
251  const double ri = double(all_size);
252 
253  if (informative_external_apriori) {
254  // the score to compute is that of BD with aprioris N'_ijk + 1
255  // (the + 1 is here to take into account the internal apriori of K2)
256 
257  // the K2 score can be computed as follows:
258  // gammalog2 ( N' + r_i ) - gammalog2 ( N + N' + r_i )
259  // + sum_k=1^ri { gammlog2 ( N_i + N'_i + 1 ) - gammalog2 ( N'_i + 1 )
260  // }
261  std::vector< double, ALLOC< double > > N_prime_ijk(all_size, 0.0);
262  this->apriori_->addAllApriori(idset, N_prime_ijk);
263 
264  // the K2 score can be computed as follows:
265  double N = 0.0;
266  double N_prime = 0.0;
267  for (std::size_t k = std::size_t(0); k < all_size; ++k) {
268  score += _gammalog2_(N_ijk[k] + N_prime_ijk[k] + 1) - _gammalog2_(N_prime_ijk[k] + 1);
269  N += N_ijk[k];
270  N_prime += N_prime_ijk[k];
271  }
272  score += _gammalog2_(N_prime + ri) - _gammalog2_(N + N_prime + ri);
273  } else {
274  // the K2 score can be computed as follows:
275  // log {(ri - 1)!} - log {(N + ri-1)!} + sum_k=1^ri log { N_ijk! } ]
276  score = _gammalog2_(ri);
277  double N = 0;
278  for (const auto n_ijk: N_ijk) {
279  score += _gammalog2_(n_ijk + 1);
280  N += n_ijk;
281  }
282  score -= _gammalog2_(N + ri);
283  }
284  }
285 
286  return score;
287  }
288 
289  } /* namespace learning */
290 
291 } /* namespace gum */
292 
293 #endif /* DOXYGEN_SHOULD_SKIP_THIS */