aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
scoreAIC_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 AIC 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/scoreAIC.h>
31 # include <sstream>
32 
33 namespace gum {
34 
35  namespace learning {
36 
37  /// default constructor
38  template < template < typename > class ALLOC >
39  INLINE ScoreAIC< ALLOC >::ScoreAIC(
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 ScoreAIC< ALLOC >::allocator_type& alloc) :
46  Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
47  _internal_apriori_(parser.database(), nodeId2columns) {
48  GUM_CONSTRUCTOR(ScoreAIC);
49  }
50 
51 
52  /// default constructor
53  template < template < typename > class ALLOC >
54  INLINE ScoreAIC< ALLOC >::ScoreAIC(
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 ScoreAIC< ALLOC >::allocator_type& alloc) :
59  Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
60  _internal_apriori_(parser.database(), nodeId2columns) {
61  GUM_CONSTRUCTOR(ScoreAIC);
62  }
63 
64 
65  /// copy constructor with a given allocator
66  template < template < typename > class ALLOC >
67  INLINE ScoreAIC< ALLOC >::ScoreAIC(const ScoreAIC< ALLOC >& from,
68  const typename ScoreAIC< ALLOC >::allocator_type& alloc) :
69  Score< ALLOC >(from, alloc),
70  _internal_apriori_(from._internal_apriori_, alloc) {
71  GUM_CONS_CPY(ScoreAIC);
72  }
73 
74 
75  /// copy constructor
76  template < template < typename > class ALLOC >
77  INLINE ScoreAIC< ALLOC >::ScoreAIC(const ScoreAIC< ALLOC >& from) :
78  ScoreAIC< ALLOC >(from, from.getAllocator()) {}
79 
80 
81  /// move constructor with a given allocator
82  template < template < typename > class ALLOC >
83  INLINE ScoreAIC< ALLOC >::ScoreAIC(ScoreAIC< ALLOC >&& from,
84  const typename ScoreAIC< ALLOC >::allocator_type& alloc) :
85  Score< ALLOC >(std::move(from), alloc),
86  _internal_apriori_(std::move(from._internal_apriori_), alloc) {
87  GUM_CONS_MOV(ScoreAIC);
88  }
89 
90 
91  /// move constructor
92  template < template < typename > class ALLOC >
93  INLINE ScoreAIC< ALLOC >::ScoreAIC(ScoreAIC< ALLOC >&& from) :
94  ScoreAIC< ALLOC >(std::move(from), from.getAllocator()) {}
95 
96 
97  /// virtual copy constructor with a given allocator
98  template < template < typename > class ALLOC >
99  ScoreAIC< ALLOC >*
100  ScoreAIC< ALLOC >::clone(const typename ScoreAIC< ALLOC >::allocator_type& alloc) const {
101  ALLOC< ScoreAIC< ALLOC > > allocator(alloc);
102  ScoreAIC< ALLOC >* new_score = allocator.allocate(1);
103  try {
104  allocator.construct(new_score, *this, alloc);
105  } catch (...) {
106  allocator.deallocate(new_score, 1);
107  throw;
108  }
109 
110  return new_score;
111  }
112 
113 
114  /// virtual copy constructor
115  template < template < typename > class ALLOC >
116  ScoreAIC< ALLOC >* ScoreAIC< ALLOC >::clone() const {
117  return clone(this->getAllocator());
118  }
119 
120 
121  /// destructor
122  template < template < typename > class ALLOC >
123  ScoreAIC< ALLOC >::~ScoreAIC() {
124  GUM_DESTRUCTOR(ScoreAIC);
125  }
126 
127 
128  /// copy operator
129  template < template < typename > class ALLOC >
130  ScoreAIC< ALLOC >& ScoreAIC< ALLOC >::operator=(const ScoreAIC< ALLOC >& from) {
131  if (this != &from) {
132  Score< ALLOC >::operator=(from);
133  _internal_apriori_ = from._internal_apriori_;
134  }
135  return *this;
136  }
137 
138 
139  /// move operator
140  template < template < typename > class ALLOC >
141  ScoreAIC< ALLOC >& ScoreAIC< ALLOC >::operator=(ScoreAIC< ALLOC >&& from) {
142  if (this != &from) {
143  Score< ALLOC >::operator=(std::move(from));
144  _internal_apriori_ = std::move(from._internal_apriori_);
145  }
146  return *this;
147  }
148 
149 
150  /// indicates whether the apriori is compatible (meaningful) with the score
151  template < template < typename > class ALLOC >
152  std::string ScoreAIC< ALLOC >::isAprioriCompatible(const std::string& apriori_type,
153  double weight) {
154  // check that the apriori is compatible with the score
155  if ((apriori_type == AprioriDirichletType::type)
156  || (apriori_type == AprioriSmoothingType::type)
157  || (apriori_type == AprioriNoAprioriType::type)) {
158  return "";
159  }
160 
161  // apriori types unsupported by the type checker
162  std::stringstream msg;
163  msg << "The apriori '" << apriori_type
164  << "' is not yet supported by method isAprioriCompatible os Score AIC";
165  return msg.str();
166  }
167 
168 
169  /// indicates whether the apriori is compatible (meaningful) with the score
170  template < template < typename > class ALLOC >
171  INLINE std::string ScoreAIC< ALLOC >::isAprioriCompatible(const Apriori< ALLOC >& apriori) {
172  return isAprioriCompatible(apriori.getType(), apriori.weight());
173  }
174 
175 
176  /// indicates whether the apriori is compatible (meaningful) with the score
177  template < template < typename > class ALLOC >
178  INLINE std::string ScoreAIC< ALLOC >::isAprioriCompatible() const {
179  return isAprioriCompatible(*(this->apriori_));
180  }
181 
182 
183  /// returns the internal apriori of the score
184  template < template < typename > class ALLOC >
185  INLINE const Apriori< ALLOC >& ScoreAIC< ALLOC >::internalApriori() const {
186  return _internal_apriori_;
187  }
188 
189 
190  /// returns the score corresponding to a given nodeset
191  template < template < typename > class ALLOC >
192  double ScoreAIC< ALLOC >::score_(const IdCondSet< ALLOC >& idset) {
193  // get the counts for all the nodes in the idset and add the apriori
194  std::vector< double, ALLOC< double > > N_ijk(this->counter_.counts(idset, true));
195  const bool informative_external_apriori = this->apriori_->isInformative();
196  if (informative_external_apriori) this->apriori_->addAllApriori(idset, N_ijk);
197  const std::size_t all_size = N_ijk.size();
198 
199  // here, we distinguish idsets with conditioning nodes from those
200  // without conditioning nodes
201  if (idset.hasConditioningSet()) {
202  // get the counts for the conditioning nodes
203  std::vector< double, ALLOC< double > > N_ij(this->marginalize_(idset[0], N_ijk));
204  const std::size_t conditioning_size = N_ij.size();
205 
206  // initialize the score: this should be the penalty of the AIC score,
207  // i.e., -(ri-1 ) * qi
208  const std::size_t target_domsize = all_size / conditioning_size;
209  const double penalty = conditioning_size * double(target_domsize - std::size_t(1));
210 
211  // compute the score: it remains to compute the log likelihood, i.e.,
212  // sum_k=1^r_i sum_j=1^q_i N_ijk log (N_ijk / N_ij), which is also
213  // equivalent to:
214  // 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
215  double score = 0.0;
216  for (const auto n_ijk: N_ijk) {
217  if (n_ijk) { score += n_ijk * std::log(n_ijk); }
218  }
219  for (const auto n_ij: N_ij) {
220  if (n_ij) { score -= n_ij * std::log(n_ij); }
221  }
222 
223  // divide by log(2), since the log likelihood uses log_2
224  score *= this->one_log2_;
225 
226  // finally, remove the penalty
227  score -= penalty;
228 
229  return score;
230  } else {
231  // here, there are no conditioning nodes
232 
233  // initialize the score: this should be the penalty of the AIC score,
234  // i.e., -(ri-1 )
235  const double penalty = double(all_size - std::size_t(1));
236 
237  // compute the score: it remains to compute the log likelihood, i.e.,
238  // sum_k=1^r_i N_ijk log (N_ijk / N), which is also
239  // equivalent to:
240  // sum_j=1^q_i sum_k=1^r_i N_ijk log N_ijk - N log N
241  double N = 0.0;
242  double score = 0.0;
243  for (const auto n_ijk: N_ijk) {
244  if (n_ijk) {
245  score += n_ijk * std::log(n_ijk);
246  N += n_ijk;
247  }
248  }
249  score -= N * std::log(N);
250 
251  // divide by log(2), since the log likelihood uses log_2
252  score *= this->one_log2_;
253 
254  // finally, remove the penalty
255  score -= penalty;
256 
257  return score;
258  }
259  }
260 
261  } /* namespace learning */
262 
263 } /* namespace gum */
264 
265 #endif /* DOXYGEN_SHOULD_SKIP_THIS */