aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
aprioriBDeu_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 internal apriori for the BDeu score (N' / (r_i * q_i)
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
28 
29 namespace gum {
30 
31  namespace learning {
32 
33 
34  /// default constructor
35  template < template < typename > class ALLOC >
36  INLINE AprioriBDeu< ALLOC >::AprioriBDeu(
37  const DatabaseTable< ALLOC >& database,
38  const Bijection< NodeId, std::size_t, ALLOC< std::size_t > >&
40  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
43  }
44 
45 
46  /// copy constructor with a given allocator
47  template < template < typename > class ALLOC >
49  const AprioriBDeu< ALLOC >& from,
50  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
51  Apriori< ALLOC >(from, alloc) {
53  }
54 
55 
56  /// copy constructor
57  template < template < typename > class ALLOC >
60 
61 
62  /// move constructor with a given allocator
63  template < template < typename > class ALLOC >
65  AprioriBDeu< ALLOC >&& from,
66  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
67  Apriori< ALLOC >(std::move(from), alloc) {
69  }
70 
71 
72  /// move constructor
73  template < template < typename > class ALLOC >
76 
77 
78  /// virtual copy constructor with a given allocator
79  template < template < typename > class ALLOC >
81  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) const {
84  try {
86  } catch (...) {
88  throw;
89  }
90 
91  return apriori;
92  }
93 
94 
95  /// virtual copy constructor
96  template < template < typename > class ALLOC >
97  INLINE AprioriBDeu< ALLOC >* AprioriBDeu< ALLOC >::clone() const {
98  return clone(this->getAllocator());
99  }
100 
101 
102  /// destructor
103  template < template < typename > class ALLOC >
106  }
107 
108 
109  /// copy operator
110  template < template < typename > class ALLOC >
112  AprioriBDeu< ALLOC >::operator=(const AprioriBDeu< ALLOC >& from) {
113  Apriori< ALLOC >::operator=(from);
114  return *this;
115  }
116 
117 
118  /// move operator
119  template < template < typename > class ALLOC >
122  Apriori< ALLOC >::operator=(std::move(from));
123  return *this;
124  }
125 
126 
127  /// sets the effective sample size N' (alias of setEffectiveSampleSize ())
128  template < template < typename > class ALLOC >
129  INLINE void AprioriBDeu< ALLOC >::setWeight(const double weight) {
130  if (weight < 0.0) {
132  "A negative weight (" << weight
133  << ") is forbidden for the BDeu apriori");
134  }
135  this->weight_ = weight;
136  }
137 
138 
139  /// sets the effective sample size N'
140  template < template < typename > class ALLOC >
141  INLINE void AprioriBDeu< ALLOC >::setEffectiveSampleSize(const double weight) {
142  setWeight(weight);
143  }
144 
145 
146  /// indicates whether an apriori is of a certain type
147  template < template < typename > class ALLOC >
148  INLINE bool AprioriBDeu< ALLOC >::isOfType(const std::string& type) {
149  return AprioriBDeuType::isOfType(type);
150  }
151 
152 
153  /// returns the type of the apriori
154  template < template < typename > class ALLOC >
155  INLINE const std::string& AprioriBDeu< ALLOC >::getType() const {
156  return AprioriBDeuType::type;
157  }
158 
159 
160  /// indicates whether the apriori is potentially informative
161  template < template < typename > class ALLOC >
162  INLINE bool AprioriBDeu< ALLOC >::isInformative() const {
163  return this->weight_ != 0.0;
164  }
165 
166 
167  /// returns the apriori vector all the variables in the idset
168  template < template < typename > class ALLOC >
170  const IdCondSet< ALLOC >& idset,
171  std::vector< double, ALLOC< double > >& counts) {
172  // if the idset is empty or the weight is zero, the apriori is also empty
173  if (idset.empty() || (this->weight_ == 0.0)) return;
174 
175  // otherwise, add the weight to all the cells in the counting vector
176  const double weight = this->weight_ / counts.size();
177  for (auto& count: counts)
178  count += weight;
179  }
180 
181 
182  /// returns the apriori vector over only the conditioning set of an idset
183  template < template < typename > class ALLOC >
185  const IdCondSet< ALLOC >& idset,
186  std::vector< double, ALLOC< double > >& counts) {
187  // if the conditioning set is empty or the weight is equal to zero,
188  // the apriori is also empty
189  if ((idset.size() == idset.nbLHSIds()) || (this->weight_ == 0.0)
190  || (idset.nbLHSIds() == std::size_t(0)))
191  return;
192 
193  // add the weight to the counting vector
194  const double weight = this->weight_ / counts.size();
195  for (auto& count: counts)
196  count += weight;
197  }
198 
199 
200  } /* namespace learning */
201 
202 } /* namespace gum */
203 
204 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)