aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
scorefNML_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 fNML 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/scorefNML.h>
31 
32 namespace gum {
33 
34  namespace learning {
35 
36  /// default constructor
37  template < template < typename > class ALLOC >
38  INLINE ScorefNML< ALLOC >::ScorefNML(
40  const Apriori< ALLOC >& apriori,
41  const std::vector< std::pair< std::size_t, std::size_t >,
42  ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
44  const typename ScorefNML< ALLOC >::allocator_type& alloc) :
48  }
49 
50 
51  /// default constructor
52  template < template < typename > class ALLOC >
55  const Apriori< ALLOC >& apriori,
57  const typename ScorefNML< ALLOC >::allocator_type& alloc) :
61  }
62 
63 
64  /// copy constructor with a given allocator
65  template < template < typename > class ALLOC >
67  const typename ScorefNML< ALLOC >::allocator_type& alloc) :
68  Score< ALLOC >(from, alloc),
71  }
72 
73 
74  /// copy constructor
75  template < template < typename > class ALLOC >
78 
79 
80  /// move constructor with a given allocator
81  template < template < typename > class ALLOC >
83  const typename ScorefNML< ALLOC >::allocator_type& alloc) :
84  Score< ALLOC >(std::move(from), alloc),
87  }
88 
89 
90  /// move constructor
91  template < template < typename > class ALLOC >
94 
95 
96  /// virtual copy constructor with a given allocator
97  template < template < typename > class ALLOC >
98  ScorefNML< ALLOC >*
99  ScorefNML< ALLOC >::clone(const typename ScorefNML< ALLOC >::allocator_type& alloc) const {
102  try {
104  } catch (...) {
106  throw;
107  }
108 
109  return new_score;
110  }
111 
112 
113  /// virtual copy constructor
114  template < template < typename > class ALLOC >
115  ScorefNML< ALLOC >* ScorefNML< ALLOC >::clone() const {
116  return clone(this->getAllocator());
117  }
118 
119 
120  /// destructor
121  template < template < typename > class ALLOC >
122  ScorefNML< ALLOC >::~ScorefNML() {
124  }
125 
126 
127  /// copy operator
128  template < template < typename > class ALLOC >
129  ScorefNML< ALLOC >& ScorefNML< ALLOC >::operator=(const ScorefNML< ALLOC >& from) {
130  if (this != &from) {
131  Score< ALLOC >::operator=(from);
133  }
134  return *this;
135  }
136 
137 
138  /// move operator
139  template < template < typename > class ALLOC >
141  if (this != &from) {
142  Score< ALLOC >::operator=(std::move(from));
144  }
145  return *this;
146  }
147 
148 
149  /// indicates whether the apriori is compatible (meaningful) with the score
150  template < template < typename > class ALLOC >
152  double weight) {
153  // check that the apriori is compatible with the score
157  return "";
158  }
159 
160  // apriori types unsupported by the type checker
162  msg << "The apriori '" << apriori_type << "' is not yet compatible with the score 'fNML'.";
163  return msg.str();
164  }
165 
166 
167  /// indicates whether the apriori is compatible (meaningful) with the score
168  template < template < typename > class ALLOC >
171  }
172 
173 
174  /// indicates whether the apriori is compatible (meaningful) with the score
175  template < template < typename > class ALLOC >
177  return isAprioriCompatible(*(this->apriori_));
178  }
179 
180 
181  /// returns the internal apriori of the score
182  template < template < typename > class ALLOC >
183  INLINE const Apriori< ALLOC >& ScorefNML< ALLOC >::internalApriori() const {
184  return _internal_apriori_;
185  }
186 
187 
188  /// returns the score corresponding to a given nodeset
189  template < template < typename > class ALLOC >
190  double ScorefNML< ALLOC >::score_(const IdCondSet< ALLOC >& idset) {
191  // get the counts for all the nodes in the idset and add the apriori
192  std::vector< double, ALLOC< double > > N_ijk(this->counter_.counts(idset, true));
195  const std::size_t all_size = N_ijk.size();
196 
197  // here, we distinguish idsets with conditioning nodes from those
198  // without conditioning nodes
199  if (idset.hasConditioningSet()) {
200  // get the counts for the conditioning nodes
201  std::vector< double, ALLOC< double > > N_ij(this->marginalize_(idset[0], N_ijk));
202  const std::size_t target_domsize = all_size / N_ij.size();
203 
204  // compute the score: it remains to compute the log likelihood, i.e.,
205  // sum_k=1^r_i sum_j=1^q_i N_ijk log (N_ijk / N_ij), which is also
206  // equivalent to:
207  // 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
208  double score = 0.0;
209  for (const auto n_ijk: N_ijk) {
210  if (n_ijk) { score += n_ijk * std::log(n_ijk); }
211  }
212  for (const auto n_ij: N_ij) {
213  if (n_ij) { score -= n_ij * std::log(n_ij); }
214  }
215 
216  // divide by log(2), since the log likelihood uses log_2
217  score *= this->one_log2_;
218 
219  // finally, remove the penalty
220  double penalty = 0.0;
221  for (const auto n_ij: N_ij) {
223  }
224 
225  score -= penalty;
226 
227  return score;
228  } else {
229  // here, there are no conditioning nodes
230 
231  // compute the score: it remains to compute the log likelihood, i.e.,
232  // sum_k=1^r_i N_ijk log (N_ijk / N), which is also
233  // equivalent to:
234  // sum_j=1^q_i sum_k=1^r_i N_ijk log N_ijk - N log N
235  double N = 0.0;
236  double score = 0.0;
237  for (const auto n_ijk: N_ijk) {
238  if (n_ijk) {
239  score += n_ijk * std::log(n_ijk);
240  N += n_ijk;
241  }
242  }
243  score -= N * std::log(N);
244 
245  // divide by log(2), since the log likelihood uses log_2
246  score *= this->one_log2_;
247 
248  // finally, remove the penalty
250 
251  return score;
252  }
253  }
254 
255  } /* namespace learning */
256 
257 } /* namespace gum */
258 
259 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)