aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
gibbsOperator.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
49
50#ifndef GUM_GIBBS_OPERATOR_H
51#define GUM_GIBBS_OPERATOR_H
52
53#include <array>
54#include <vector>
55
56#include <agrum/BN/IBayesNet.h>
57
58namespace gum {
59
68
69 template < GUM_Numeric GUM_SCALAR >
71 public:
76 const NodeProperty< Idx >* hardEv,
77 Size nbr = 1,
78 bool atRandom = false);
79
92 const NodeProperty< Idx >* hardEv,
93 const std::string& section);
94
98 virtual ~GibbsOperator();
99
101 Size nbrDrawnVar() const;
102
103 void setNbrDrawnVar(Size nbr);
104
105 bool isDrawnAtRandom() const;
106
107 void setDrawnAtRandom(bool atRandom);
108
110
136
139
140 protected:
145
148
150
151 private:
153 static constexpr Size _nbrRejectionTries_ = 100;
154
156 static constexpr Size _nbrSmoothingRestarts_ = 10;
157
160 static constexpr std::array< double, 4 > _smoothingLevels_ = {1e-3, 1e-2, 1e-1, 0.5};
161
163 static constexpr Size _nbrSweepsBySmoothingLevel_ = 50;
164
167
170
171 // computes _allowed_ by generalized arc consistency on the null probabilities of the CPTs,
172 // starting from the hard evidence. Returns false if a domain becomes empty.
173 bool _pruneDomains_();
174
175 // sets to 0 the probabilities of the values of node that are not allowed
176 void _mask_(NodeId node, Tensor< GUM_SCALAR >& p) const;
177
178 // draws a forward sample (hard evidence clamped, values restricted to the allowed ones)
180
181 // adds a node to current instantiation
183
184 // checks that the sample has a positive probability
185 bool _isCompatible_(const Instantiation& I) const;
186
187 // Gibbs move of node id in the model where the null probabilities are replaced by epsilon times
188 // _minNonZero_ (which must be computed beforehand)
189 void _smoothedGibbsSample_(NodeId id, Instantiation* I, GUM_SCALAR epsilon);
190
192 };
193
194
195#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
196
197 extern template class GUM_PUBLIC_BN GibbsOperator< double >;
198
199#endif
200
201} // namespace gum
202
204
205#endif
Class representing the minimal interface for Bayesian network with no numerical data.
NodeProperty< std::vector< bool > > _allowed_
the values of each node that may belong to a sample with a positive probability
void setDrawnAtRandom(bool atRandom)
void _smoothedGibbsSample_(NodeId id, Instantiation *I, GUM_SCALAR epsilon)
Size nbrDrawnVar() const
Getters and setters.
void _drawVarMonteCarlo_(NodeId nod, Instantiation *I)
static constexpr Size _nbrSweepsBySmoothingLevel_
maximal number of sweeps (over the non-evidence nodes) for each smoothing level
void setNbrDrawnVar(Size nbr)
bool _isCompatible_(const Instantiation &I) const
static constexpr std::array< double, 4 > _smoothingLevels_
growing smoothing levels: a null probability is replaced by epsilon times the smallest positive proba...
const IBayesNet< GUM_SCALAR > & samplingBn_
void _GibbsSample_(NodeId id, Instantiation *I)
change in Instantiation I a new drawn value for id
Instantiation nextSample(Instantiation prev)
draws next sample of Gibbs sampling
Instantiation _forwardSample_()
Sequence< NodeId > samplingNodes_
Instantiation monteCarloSample()
draws a first sample with a positive probability (starting point of a Gibbs chain)
const NodeProperty< Idx > * hardEv_
static constexpr Size _nbrSmoothingRestarts_
number of restarts (from a new forward sample) of the smoothed Gibbs moves
bool isDrawnAtRandom() const
GibbsOperator(const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false)
constructor
void _mask_(NodeId node, Tensor< GUM_SCALAR > &p) const
static constexpr Size _nbrRejectionTries_
number of forward samples drawn before searching a compatible sample by smoothed Gibbs moves
NodeProperty< GUM_SCALAR > _minNonZero_
the smallest positive probability of the CPT of each node (for the smoothed Gibbs moves)
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:79
Class for assigning/browsing values to tuples of discrete variables.
Implementation of Gibbs inference methods in Bayesian networks.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
template class GUM_PUBLIC_BN GibbsOperator< double >