aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
GibbsSampling.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 /**
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:
56  /**
57  * Default constructor
58  */
59  explicit GibbsSampling(const IBayesNet< GUM_SCALAR >* bn);
60 
61  /**
62  * Destructor
63  */
65 
66  /**
67  * @brief Number of burn in for one iteration.
68  * @param b The number of burn in.
69  * @throw OutOfLowerBound Raised if b < 1.
70  */
71  void setBurnIn(Size b) { this->burn_in_ = b; };
72 
73  /**
74  * @brief Returns the number of burn in.
75  * @return Returns the number of burn in.
76  */
77  Size burnIn() const { return this->burn_in_; };
78 
79  protected:
80  /// draws a defined number of samples without updating the estimators
81  Instantiation burnIn_() override;
82 
83  /// draws a sample given previous one according to Gibbs sampling
84  /**
85  * @param w the weight of sample being generated
86  * @param prev the previous sample generated
87  * @param bn the Bayesian network containing the evidence
88  * @param hardEvNodes hard evidence nodes
89  * @param hardEv hard evidences values
90  *
91  * Uses the Gibbs sampling method to generate a new sample given the previous
92  * one. The method is implemented in the inherited class GibbsOperator. This
93  * function only makes the call to it. It consists of choosing one node x to
94  * sample, given the instantiation of all other nodes. It requires computing of
95  * P( x \given instantiation_markovblanket(x)).
96  */
98 
99  /// draws a Monte Carlo sample
100  /**
101  * @param bn the reference Bayesian network
102  *
103  * This Monte Carlo sample generates a good starting point for Gibbs sampling,
104  * because it returns
105  * a sample consistent with the evidence, but it differs from the one
106  * implemented in the inherited
107  * class Approximate Inference because it also initializes attributes needed
108  * for Gibbs sampling.
109  */
111  };
112 
113 
114 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
115  extern template class GibbsSampling< double >;
116 #endif
117 } // namespace gum
118 
119 #include <agrum/BN/inference/GibbsSampling_tpl.h>
120 #endif
~GibbsSampling() override
Destructor.
GibbsSampling(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void setBurnIn(Size b)
Number of burn in for one iteration.
Definition: GibbsSampling.h:71
<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:77
Instantiation monteCarloSample_()
draws a Monte Carlo sample
Instantiation burnIn_() override
draws a defined number of samples without updating the estimators