aGrUM  0.14.2
scoreBD_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 
29 # include <sstream>
30 
31 namespace gum {
32 
33  namespace learning {
34 
36  template < template < typename > class ALLOC >
38  const DBRowGeneratorParser< ALLOC >& parser,
39  const Apriori< ALLOC >& apriori,
40  const std::vector< std::pair< std::size_t, std::size_t >,
41  ALLOC< std::pair< std::size_t, std::size_t > > >& ranges,
42  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
43  nodeId2columns,
44  const typename ScoreBD< ALLOC >::allocator_type& alloc) :
45  Score< ALLOC >(parser, apriori, ranges, nodeId2columns, alloc),
46  __internal_apriori(parser.database(), nodeId2columns) {
47  GUM_CONSTRUCTOR(ScoreBD);
48  }
49 
50 
52  template < template < typename > class ALLOC >
54  const DBRowGeneratorParser< ALLOC >& parser,
55  const Apriori< ALLOC >& apriori,
56  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
57  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 
66  template < template < typename > class ALLOC >
68  const ScoreBD< ALLOC >& from,
69  const typename ScoreBD< ALLOC >::allocator_type& alloc) :
70  Score< ALLOC >(from, alloc),
71  __internal_apriori(from.__internal_apriori, alloc),
72  __gammalog2(from.__gammalog2) {
73  GUM_CONS_CPY(ScoreBD);
74  }
75 
76 
78  template < template < typename > class ALLOC >
79  INLINE ScoreBD< ALLOC >::ScoreBD(const ScoreBD< ALLOC >& from) :
80  ScoreBD< ALLOC >(from, from.getAllocator()) {}
81 
82 
84  template < template < typename > class ALLOC >
86  ScoreBD< ALLOC >&& from,
87  const typename ScoreBD< ALLOC >::allocator_type& alloc) :
88  Score< ALLOC >(std::move(from), alloc),
89  __internal_apriori(std::move(from.__internal_apriori), alloc),
90  __gammalog2(std::move(from.__gammalog2)) {
91  GUM_CONS_MOV(ScoreBD);
92  }
93 
94 
96  template < template < typename > class ALLOC >
97  INLINE ScoreBD< ALLOC >::ScoreBD(ScoreBD< ALLOC >&& from) :
98  ScoreBD< ALLOC >(std::move(from), from.getAllocator()) {}
99 
100 
102  template < template < typename > class ALLOC >
103  ScoreBD< ALLOC >* ScoreBD< ALLOC >::clone(
104  const typename ScoreBD< ALLOC >::allocator_type& alloc) const {
105  ALLOC< ScoreBD< ALLOC > > allocator(alloc);
106  ScoreBD< ALLOC >* new_score = allocator.allocate(1);
107  try {
108  allocator.construct(new_score, *this, alloc);
109  } catch (...) {
110  allocator.deallocate(new_score, 1);
111  throw;
112  }
113 
114  return new_score;
115  }
116 
117 
119  template < template < typename > class ALLOC >
120  ScoreBD< ALLOC >* ScoreBD< ALLOC >::clone() const {
121  return clone(this->getAllocator());
122  }
123 
124 
126  template < template < typename > class ALLOC >
128  GUM_DESTRUCTOR(ScoreBD);
129  }
130 
131 
133  template < template < typename > class ALLOC >
134  ScoreBD< ALLOC >& ScoreBD< ALLOC >::operator=(const ScoreBD< ALLOC >& from) {
135  if (this != &from) {
137  __internal_apriori = from.__internal_apriori;
138  }
139  return *this;
140  }
141 
142 
144  template < template < typename > class ALLOC >
145  ScoreBD< ALLOC >& ScoreBD< ALLOC >::operator=(ScoreBD< ALLOC >&& from) {
146  if (this != &from) {
147  Score< ALLOC >::operator=(std::move(from));
148  __internal_apriori = std::move(from.__internal_apriori);
149  }
150  return *this;
151  }
152 
153 
155  template < template < typename > class ALLOC >
156  std::string
157  ScoreBD< ALLOC >::isAprioriCompatible(const std::string& apriori_type,
158  double weight) {
159  if (apriori_type == AprioriNoAprioriType::type) {
160  return "The BD score requires an apriori";
161  }
162 
163  if (weight != 0.0) {
164  return "The apriori is currently compatible with the BD score but if "
165  "you change the weight, it may become biased";
166  }
167 
168  // apriori types unsupported by the type checker
169  std::stringstream msg;
170  msg << "The apriori '" << apriori_type
171  << "' is not yet supported by method isAprioriCompatible";
172  return msg.str();
173  }
174 
175 
177  template < template < typename > class ALLOC >
178  INLINE std::string
179  ScoreBD< ALLOC >::isAprioriCompatible(const Apriori< ALLOC >& apriori) {
180  return isAprioriCompatible(apriori.getType(), apriori.weight());
181  }
182 
183 
185  template < template < typename > class ALLOC >
186  INLINE std::string ScoreBD< ALLOC >::isAprioriCompatible() const {
187  return isAprioriCompatible(*(this->_apriori));
188  }
189 
190 
192  template < template < typename > class ALLOC >
193  INLINE const Apriori< ALLOC >& ScoreBD< ALLOC >::internalApriori() const {
194  return __internal_apriori;
195  }
196 
197 
199  template < template < typename > class ALLOC >
200  double ScoreBD< ALLOC >::_score(const IdSet< ALLOC >& idset) {
201  // if the weight of the apriori is 0, then gammaLog2 will fail
202  if (!this->_apriori->isInformative()) {
203  GUM_ERROR(OutOfBounds,
204  "The BD score requires its external apriori to "
205  << "be strictly positive");
206  }
207 
208  // get the counts for all the nodes in the idset and add the apriori
209  std::vector< double, ALLOC< double > > N_ijk(
210  this->_counter.counts(idset, true));
211  const std::size_t all_size = N_ijk.size();
212  std::vector< double, ALLOC< double > > N_prime_ijk(all_size, 0.0);
213  this->_apriori->addAllApriori(idset, N_prime_ijk);
214 
215  double score = 0.0;
216 
217  // here, we distinguish idsets with conditioning nodes from those
218  // without conditioning nodes
219  if (idset.hasConditioningSet()) {
220  // get the counts for the conditioning nodes
221  std::vector< double, ALLOC< double > > N_ij(
222  this->_marginalize(idset[0], N_ijk));
223  const std::size_t conditioning_size = N_ij.size();
224 
225  std::vector< double, ALLOC< double > > N_prime_ij(N_ij.size(), 0.0);
226  this->_apriori->addConditioningApriori(idset, N_prime_ij);
227 
228  // the BD score can be computed as follows:
229  // sum_j=1^qi [ gammalog2 ( N'_ij ) - gammalog2 ( N_ij + N'_ij )
230  // + sum_k=1^ri { gammlog2 ( N_ijk + N'_ijk ) -
231  // gammalog2 ( N'_ijk ) } ]
232  for (std::size_t j = std::size_t(0); j < conditioning_size; ++j) {
233  score +=
234  __gammalog2(N_prime_ij[j]) - __gammalog2(N_ij[j] + N_prime_ij[j]);
235  }
236  for (std::size_t k = std::size_t(0); k < all_size; ++k) {
237  score +=
238  __gammalog2(N_ijk[k] + N_prime_ijk[k]) - __gammalog2(N_prime_ijk[k]);
239  }
240  } else {
241  // the BD score can be computed as follows:
242  // gammalog2 ( N' ) - gammalog2 ( N + N' )
243  // + sum_k=1^ri { gammlog2 ( N_i + N'_i ) - gammalog2 ( N'_i ) }
244  double N = 0.0;
245  double N_prime = 0.0;
246  for (std::size_t k = std::size_t(0); k < all_size; ++k) {
247  score +=
248  __gammalog2(N_ijk[k] + N_prime_ijk[k]) - __gammalog2(N_prime_ijk[k]);
249  N += N_ijk[k];
250  N_prime += N_prime_ijk[k];
251  }
252  score += __gammalog2(N_prime) - __gammalog2(N + N_prime);
253  }
254 
255  return score;
256  }
257 
258  } /* namespace learning */
259 
260 } /* namespace gum */
261 
262 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
virtual const Apriori< ALLOC > & internalApriori() const final
returns the internal apriori of the score
const DatabaseTable< ALLOC > & database() const
return the database used by the score
double score(const NodeId var)
returns the score of a single node
ALLOC< NodeId > allocator_type
type for the allocators passed in arguments of methods
Definition: scoreBD.h:65
Score(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &external_apriori, const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > &ranges, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
static const std::string type
Definition: aprioriTypes.h:45
ScoreBD(const DBRowGeneratorParser< ALLOC > &parser, const Apriori< ALLOC > &apriori, const std::vector< std::pair< std::size_t, std::size_t >, ALLOC< std::pair< std::size_t, std::size_t > > > &ranges, const Bijection< NodeId, std::size_t, ALLOC< std::size_t > > &nodeId2columns=Bijection< NodeId, std::size_t, ALLOC< std::size_t > >(), const allocator_type &alloc=allocator_type())
default constructor
virtual double _score(const IdSet< ALLOC > &idset) final
returns the score for a given IdSet
STL namespace.
virtual ~ScoreBD()
destructor
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
std::vector< double, ALLOC< double > > _marginalize(const NodeId X_id, const std::vector< double, ALLOC< double > > &N_xyz) const
returns a counting vector where variables are marginalized from N_xyz
ScoreBD< ALLOC > & operator=(const ScoreBD< ALLOC > &from)
copy operator
Score< ALLOC > & operator=(const Score< ALLOC > &from)
copy operator
virtual std::string isAprioriCompatible() const final
indicates whether the apriori is compatible (meaningful) with the score
the class for computing Bayesian Dirichlet (BD) log2 scores
allocator_type getAllocator() const
returns the allocator used by the score
virtual ScoreBD< ALLOC > * clone() const
virtual copy constructor
Apriori< ALLOC > * _apriori
the expert knowledge a priori we add to the score
Definition: score.h:239
RecordCounter< ALLOC > _counter
the record counter used for the countings over discrete variables
Definition: score.h:242
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52