aGrUM  0.20.3
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
165  << "' is not yet supported by method isAprioriCompatible";
166  return msg.str();
167  }
168 
169 
170  /// indicates whether the apriori is compatible (meaningful) with the score
171  template < template < typename > class ALLOC >
172  INLINE std::string ScoreBD< ALLOC >::isAprioriCompatible(const Apriori< ALLOC >& apriori) {
173  return isAprioriCompatible(apriori.getType(), apriori.weight());
174  }
175 
176 
177  /// indicates whether the apriori is compatible (meaningful) with the score
178  template < template < typename > class ALLOC >
179  INLINE std::string ScoreBD< ALLOC >::isAprioriCompatible() const {
180  return isAprioriCompatible(*(this->apriori_));
181  }
182 
183 
184  /// returns the internal apriori of the score
185  template < template < typename > class ALLOC >
186  INLINE const Apriori< ALLOC >& ScoreBD< ALLOC >::internalApriori() const {
187  return _internal_apriori_;
188  }
189 
190 
191  /// returns the score corresponding to a given nodeset
192  template < template < typename > class ALLOC >
193  double ScoreBD< ALLOC >::score_(const IdCondSet< ALLOC >& idset) {
194  // if the weight of the apriori is 0, then gammaLog2 will fail
195  if (!this->apriori_->isInformative()) {
196  GUM_ERROR(OutOfBounds,
197  "The BD score requires its external apriori to "
198  << "be strictly positive");
199  }
200 
201  // get the counts for all the nodes in the idset and add the apriori
202  std::vector< double, ALLOC< double > > N_ijk(this->counter_.counts(idset, true));
203  const std::size_t all_size = N_ijk.size();
204  std::vector< double, ALLOC< double > > N_prime_ijk(all_size, 0.0);
205  this->apriori_->addAllApriori(idset, N_prime_ijk);
206 
207  double score = 0.0;
208 
209  // here, we distinguish idsets with conditioning nodes from those
210  // without conditioning nodes
211  if (idset.hasConditioningSet()) {
212  // get the counts for the conditioning nodes
213  std::vector< double, ALLOC< double > > N_ij(this->marginalize_(idset[0], N_ijk));
214  const std::size_t conditioning_size = N_ij.size();
215 
216  std::vector< double, ALLOC< double > > N_prime_ij(N_ij.size(), 0.0);
217  this->apriori_->addConditioningApriori(idset, N_prime_ij);
218 
219  // the BD score can be computed as follows:
220  // sum_j=1^qi [ gammalog2 ( N'_ij ) - gammalog2 ( N_ij + N'_ij )
221  // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk ) -
222  // gammalog2 ( N'_ijk ) } ]
223  for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
224  score += _gammalog2_(N_prime_ij[j]) - _gammalog2_(N_ij[j] + N_prime_ij[j]);
225  }
226  for (std::size_t k = std::size_t(0); k < all_size; ++k) {
227  score += _gammalog2_(N_ijk[k] + N_prime_ijk[k]) - _gammalog2_(N_prime_ijk[k]);
228  }
229  } else {
230  // the BD score can be computed as follows:
231  // gammalog2 ( N' ) - gammalog2 ( N + N' )
232  // + sum_k=1^ri { gammlog2 ( N_i + N'_i ) - gammalog2 ( N'_i ) }
233  double N = 0.0;
234  double N_prime = 0.0;
235  for (std::size_t k = std::size_t(0); k < all_size; ++k) {
236  score += _gammalog2_(N_ijk[k] + N_prime_ijk[k]) - _gammalog2_(N_prime_ijk[k]);
237  N += N_ijk[k];
238  N_prime += N_prime_ijk[k];
239  }
240  score += _gammalog2_(N_prime) - _gammalog2_(N + N_prime);
241  }
242 
243  return score;
244  }
245 
246  } /* namespace learning */
247 
248 } /* namespace gum */
249 
250 #endif /* DOXYGEN_SHOULD_SKIP_THIS */