aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
GibbsSampling.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 /**
23  * @file
24  * @brief This file contains Gibbs sampling class definition.
25  *
26  * @author Paul ALAM & Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 
30 #ifndef GUM_GIBBS_SAMPLING_H
31 #define GUM_GIBBS_SAMPLING_H
32 
33 #include <agrum/BN/inference/tools/gibbsOperator.h>
34 #include <agrum/BN/inference/tools/samplingInference.h>
35 
36 
37 namespace gum {
38 
39  /**
40  * @class GibbsSampling gibbsSampling.h
41  *<agrum/BN/inference/gibbsSampling.h>
42  * @brief class for making Gibbs sampling inference in Bayesian networks.
43  * @ingroup bn_approximation
44  *
45  * This class overrides pure function declared in the inherited class
46  *ApproximateInference.
47  * It defines the way Gibbs sampling draws a sample. It also inherits
48  *GibbsOperator
49  * which contains Gibbs sampling methods.
50  *
51  */
52 
53  template < typename GUM_SCALAR >
55  public SamplingInference< GUM_SCALAR >,
56  public GibbsOperator< GUM_SCALAR > {
57  public:
58  /**
59  * Default constructor
60  */
61  explicit GibbsSampling(const IBayesNet< GUM_SCALAR >* bn);
62 
63  /**
64  * Destructor
65  */
67 
68  /**
69  * @brief Number of burn in for one iteration.
70  * @param b The number of burn in.
71  * @throw OutOfLowerBound Raised if b < 1.
72  */
73  void setBurnIn(Size b) { this->burn_in_ = b; };
74 
75  /**
76  * @brief Returns the number of burn in.
77  * @return Returns the number of burn in.
78  */
79  Size burnIn() const { return this->burn_in_; };
80 
81  protected:
82  /// draws a defined number of samples without updating the estimators
83  Instantiation burnIn_() override;
84 
85  /// draws a sample given previous one according to Gibbs sampling
86  /**
87  * @param w the weight of sample being generated
88  * @param prev the previous sample generated
89  * @param bn the Bayesian network containing the evidence
90  * @param hardEvNodes hard evidence nodes
91  * @param hardEv hard evidences values
92  *
93  * Uses the Gibbs sampling method to generate a new sample given the previous
94  * one. The method is implemented in the inherited class GibbsOperator. This
95  * function only makes the call to it. It consists of choosing one node x to
96  * sample, given the instantiation of all other nodes. It requires computing of
97  * P( x \given instantiation_markovblanket(x)).
98  */
100 
101  /// draws a Monte Carlo sample
102  /**
103  * @param bn the reference Bayesian network
104  *
105  * This Monte Carlo sample generates a good starting point for Gibbs sampling,
106  * because it returns
107  * a sample consistent with the evidence, but it differs from the one
108  * implemented in the inherited
109  * class Approximate Inference because it also initializes attributes needed
110  * for Gibbs sampling.
111  */
113  };
114 
115 
116 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
117  extern template class GibbsSampling< double >;
118 #endif
119 } // namespace gum
120 
121 #include <agrum/BN/inference/GibbsSampling_tpl.h>
122 #endif
~GibbsSampling() override
Destructor.
GibbsSampling(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void setBurnIn(Size b)
Number of burn in for one iteration.
Definition: GibbsSampling.h:73
<agrum/BN/inference/gibbsSampling.h>
Definition: GibbsSampling.h:54
Instantiation draw_(GUM_SCALAR *w, Instantiation prev) override
draws a sample given previous one according to Gibbs sampling
Size burnIn() const
Returns the number of burn in.
Definition: GibbsSampling.h:79
Instantiation monteCarloSample_()
draws a Monte Carlo sample
Instantiation burnIn_() override
draws a defined number of samples without updating the estimators