aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
aprioriBDeu_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 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,
39  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
42  }
43 
44 
45  /// copy constructor with a given allocator
46  template < template < typename > class ALLOC >
48  const AprioriBDeu< ALLOC >& from,
49  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
50  Apriori< ALLOC >(from, alloc) {
52  }
53 
54 
55  /// copy constructor
56  template < template < typename > class ALLOC >
59 
60 
61  /// move constructor with a given allocator
62  template < template < typename > class ALLOC >
64  AprioriBDeu< ALLOC >&& from,
65  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) :
66  Apriori< ALLOC >(std::move(from), alloc) {
68  }
69 
70 
71  /// move constructor
72  template < template < typename > class ALLOC >
75 
76 
77  /// virtual copy constructor with a given allocator
78  template < template < typename > class ALLOC >
80  const typename AprioriBDeu< ALLOC >::allocator_type& alloc) const {
83  try {
85  } catch (...) {
87  throw;
88  }
89 
90  return apriori;
91  }
92 
93 
94  /// virtual copy constructor
95  template < template < typename > class ALLOC >
96  INLINE AprioriBDeu< ALLOC >* AprioriBDeu< ALLOC >::clone() const {
97  return clone(this->getAllocator());
98  }
99 
100 
101  /// destructor
102  template < template < typename > class ALLOC >
105  }
106 
107 
108  /// copy operator
109  template < template < typename > class ALLOC >
111  Apriori< ALLOC >::operator=(from);
112  return *this;
113  }
114 
115 
116  /// move operator
117  template < template < typename > class ALLOC >
119  Apriori< ALLOC >::operator=(std::move(from));
120  return *this;
121  }
122 
123 
124  /// sets the effective sample size N' (alias of setEffectiveSampleSize ())
125  template < template < typename > class ALLOC >
126  INLINE void AprioriBDeu< ALLOC >::setWeight(const double weight) {
127  if (weight < 0.0) {
129  "A negative weight (" << weight << ") is forbidden for the BDeu apriori");
130  }
131  this->weight_ = weight;
132  }
133 
134 
135  /// sets the effective sample size N'
136  template < template < typename > class ALLOC >
137  INLINE void AprioriBDeu< ALLOC >::setEffectiveSampleSize(const double weight) {
138  setWeight(weight);
139  }
140 
141 
142  /// indicates whether an apriori is of a certain type
143  template < template < typename > class ALLOC >
144  INLINE bool AprioriBDeu< ALLOC >::isOfType(const std::string& type) {
145  return AprioriBDeuType::isOfType(type);
146  }
147 
148 
149  /// returns the type of the apriori
150  template < template < typename > class ALLOC >
151  INLINE const std::string& AprioriBDeu< ALLOC >::getType() const {
152  return AprioriBDeuType::type;
153  }
154 
155 
156  /// indicates whether the apriori is potentially informative
157  template < template < typename > class ALLOC >
158  INLINE bool AprioriBDeu< ALLOC >::isInformative() const {
159  return this->weight_ != 0.0;
160  }
161 
162 
163  /// returns the apriori vector all the variables in the idset
164  template < template < typename > class ALLOC >
165  INLINE void
167  std::vector< double, ALLOC< double > >& counts) {
168  // if the idset is empty or the weight is zero, the apriori is also empty
169  if (idset.empty() || (this->weight_ == 0.0)) return;
170 
171  // otherwise, add the weight to all the cells in the counting vector
172  const double weight = this->weight_ / counts.size();
173  for (auto& count: counts)
174  count += weight;
175  }
176 
177 
178  /// returns the apriori vector over only the conditioning set of an idset
179  template < template < typename > class ALLOC >
181  const IdCondSet< ALLOC >& idset,
182  std::vector< double, ALLOC< double > >& counts) {
183  // if the conditioning set is empty or the weight is equal to zero,
184  // the apriori is also empty
185  if ((idset.size() == idset.nbLHSIds()) || (this->weight_ == 0.0)
186  || (idset.nbLHSIds() == std::size_t(0)))
187  return;
188 
189  // add the weight to the counting vector
190  const double weight = this->weight_ / counts.size();
191  for (auto& count: counts)
192  count += weight;
193  }
194 
195 
196  } /* namespace learning */
197 
198 } /* namespace gum */
199 
200 #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)