aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
scoreBD_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 BD 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/scoreBD.h>
31 # include <sstream>
32 
33 namespace gum {
34 
35  namespace learning {
36 
37  /// default constructor
38  template < template < typename > class ALLOC >
39  INLINE ScoreBD< ALLOC >::ScoreBD(
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 ScoreBD< ALLOC >::allocator_type& alloc) :
46  Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
47  _internal_apriori_(parser.database(), nodeId2columns) {
48  GUM_CONSTRUCTOR(ScoreBD);
49  }
50 
51 
52  /// default constructor
53  template < template < typename > class ALLOC >
54  INLINE ScoreBD< ALLOC >::ScoreBD(
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 ScoreBD< ALLOC >::allocator_type& alloc) :
59  Score< ALLOC >(parser, apriori, nodeId2columns, alloc),
60  _internal_apriori_(parser.database(), nodeId2columns) {
61  GUM_CONSTRUCTOR(ScoreBD);
62  }
63 
64 
65  /// copy constructor with a given allocator
66  template < template < typename > class ALLOC >
67  INLINE ScoreBD< ALLOC >::ScoreBD(const ScoreBD< ALLOC >& from,
68  const typename ScoreBD< ALLOC >::allocator_type& alloc) :
69  Score< ALLOC >(from, alloc),
70  _internal_apriori_(from._internal_apriori_, alloc), _gammalog2_(from._gammalog2_) {
71  GUM_CONS_CPY(ScoreBD);
72  }
73 
74 
75  /// copy constructor
76  template < template < typename > class ALLOC >
77  INLINE ScoreBD< ALLOC >::ScoreBD(const ScoreBD< ALLOC >& from) :
78  ScoreBD< ALLOC >(from, from.getAllocator()) {}
79 
80 
81  /// move constructor with a given allocator
82  template < template < typename > class ALLOC >
83  INLINE ScoreBD< ALLOC >::ScoreBD(ScoreBD< ALLOC >&& from,
84  const typename ScoreBD< 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(ScoreBD);
89  }
90 
91 
92  /// move constructor
93  template < template < typename > class ALLOC >
94  INLINE ScoreBD< ALLOC >::ScoreBD(ScoreBD< ALLOC >&& from) :
95  ScoreBD< ALLOC >(std::move(from), from.getAllocator()) {}
96 
97 
98  /// virtual copy constructor with a given allocator
99  template < template < typename > class ALLOC >
100  ScoreBD< ALLOC >*
101  ScoreBD< ALLOC >::clone(const typename ScoreBD< ALLOC >::allocator_type& alloc) const {
102  ALLOC< ScoreBD< ALLOC > > allocator(alloc);
103  ScoreBD< 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  ScoreBD< ALLOC >* ScoreBD< ALLOC >::clone() const {
118  return clone(this->getAllocator());
119  }
120 
121 
122  /// destructor
123  template < template < typename > class ALLOC >
124  ScoreBD< ALLOC >::~ScoreBD() {
125  GUM_DESTRUCTOR(ScoreBD);
126  }
127 
128 
129  /// copy operator
130  template < template < typename > class ALLOC >
131  ScoreBD< ALLOC >& ScoreBD< ALLOC >::operator=(const ScoreBD< 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  ScoreBD< ALLOC >& ScoreBD< ALLOC >::operator=(ScoreBD< 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 ScoreBD< ALLOC >::isAprioriCompatible(const std::string& apriori_type,
154  double weight) {
155  if (apriori_type == AprioriNoAprioriType::type) { return "The BD score requires an apriori"; }
156 
157  if (weight != 0.0) {
158  return "The apriori is currently compatible with the BD score but if "
159  "you change the weight, it may become biased";
160  }
161 
162  // apriori types unsupported by the type checker
163  std::stringstream msg;
164  msg << "The apriori '" << apriori_type << "' is not yet compatible with the score 'BD'.";
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 ScoreBD< 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 ScoreBD< 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 >& ScoreBD< 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 ScoreBD< ALLOC >::score_(const IdCondSet< ALLOC >& idset) {
193  // if the weight of the apriori is 0, then gammaLog2 will fail
194  if (!this->apriori_->isInformative()) {
195  GUM_ERROR(OutOfBounds,
196  "The BD score requires its external apriori to "
197  << "be strictly positive");
198  }
199 
200  // get the counts for all the nodes in the idset and add the apriori
201  std::vector< double, ALLOC< double > > N_ijk(this->counter_.counts(idset, true));
202  const std::size_t all_size = N_ijk.size();
203  std::vector< double, ALLOC< double > > N_prime_ijk(all_size, 0.0);
204  this->apriori_->addAllApriori(idset, N_prime_ijk);
205 
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 
215  std::vector< double, ALLOC< double > > N_prime_ij(N_ij.size(), 0.0);
216  this->apriori_->addConditioningApriori(idset, N_prime_ij);
217 
218  // the BD score can be computed as follows:
219  // sum_j=1^qi [ gammalog2 ( N'_ij ) - gammalog2 ( N_ij + N'_ij )
220  // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk ) -
221  // gammalog2 ( N'_ijk ) } ]
222  for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
223  score += _gammalog2_(N_prime_ij[j]) - _gammalog2_(N_ij[j] + N_prime_ij[j]);
224  }
225  for (std::size_t k = std::size_t(0); k < all_size; ++k) {
226  score += _gammalog2_(N_ijk[k] + N_prime_ijk[k]) - _gammalog2_(N_prime_ijk[k]);
227  }
228  } else {
229  // the BD score can be computed as follows:
230  // gammalog2 ( N' ) - gammalog2 ( N + N' )
231  // + sum_k=1^ri { gammlog2 ( N_i + N'_i ) - gammalog2 ( N'_i ) }
232  double N = 0.0;
233  double N_prime = 0.0;
234  for (std::size_t k = std::size_t(0); k < all_size; ++k) {
235  score += _gammalog2_(N_ijk[k] + N_prime_ijk[k]) - _gammalog2_(N_prime_ijk[k]);
236  N += N_ijk[k];
237  N_prime += N_prime_ijk[k];
238  }
239  score += _gammalog2_(N_prime) - _gammalog2_(N + N_prime);
240  }
241 
242  return score;
243  }
244 
245  } /* namespace learning */
246 
247 } /* namespace gum */
248 
249 #endif /* DOXYGEN_SHOULD_SKIP_THIS */