aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gibbsOperator_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 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-2025 *
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#pragma once
41
42
49
50
52
54
55namespace gum {
56
57 template < typename GUM_SCALAR >
59 const NodeProperty< Idx >* hardEv,
60 Size nbr,
61 bool atRandom) :
62 counting_(0), samplingBn_(BN), hardEv_(hardEv), nbr_(nbr), atRandom_(atRandom) {
63 GUM_CONSTRUCTOR(GibbsOperator);
64 }
65
66 template < typename GUM_SCALAR >
70
71 template < typename GUM_SCALAR >
73 samplingNodes_.clear();
74 for (const auto node: samplingBn_.nodes())
75 if (hardEv_ == nullptr || !hardEv_->exists(node)) { samplingNodes_.insert(node); }
76 if (samplingNodes_.size() == 0) {
77 GUM_ERROR(InvalidArgument, "No node to sample (too many nodes or too much evidence)!")
78 }
79 if (nbr_ > samplingNodes_.size()) nbr_ = samplingNodes_.size();
80 }
81
86 template < typename GUM_SCALAR >
89
90 for (const auto nod: samplingBn_.topologicalOrder()) {
91 I.add(samplingBn_.variable(nod));
92 if (hardEv_ != nullptr && hardEv_->exists(nod)) {
93 I.chgVal(samplingBn_.variable(nod), (*hardEv_)[nod]);
94 } else {
95 _drawVarMonteCarlo_(nod, &I);
96 }
97 }
98 return I;
99 }
100
101 template < typename GUM_SCALAR >
103 gum::Instantiation Itop(*I);
104 Itop.erase(samplingBn_.variable(nod));
105 I->chgVal(samplingBn_.variable(nod), samplingBn_.cpt(nod).extract(Itop).draw());
106 }
107
108 template < typename GUM_SCALAR >
110 for (Idx i = 0; i < nbr_; i++) {
111 auto pos
113 this->_GibbsSample_(samplingNodes_[pos], &prev);
114 counting_++;
115 }
116 return prev;
117 }
118
120
121 template < typename GUM_SCALAR >
123 gum::Instantiation Itop(*I);
124 Itop.erase(samplingBn_.variable(id));
125 gum::Tensor< GUM_SCALAR > p = samplingBn_.cpt(id).extract(Itop);
126 for (const auto nod: samplingBn_.children(id))
127 p *= samplingBn_.cpt(nod).extract(Itop);
128 GUM_ASSERT(p.nbrDim() == 1);
129 if (p.sum() != 0) {
130 p.normalize();
131 I->chgVal(samplingBn_.variable(id), p.draw());
132 }
133 }
134} // namespace gum
void _drawVarMonteCarlo_(NodeId nod, Instantiation *I)
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
Sequence< NodeId > samplingNodes_
Instantiation monteCarloSample()
draws a Monte Carlo sample
const NodeProperty< Idx > * hardEv_
GibbsOperator(const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false)
constructor
virtual ~GibbsOperator()
Destructor.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
Class for assigning/browsing values to tuples of discrete variables.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
void erase(const DiscreteVariable &v) final
Removes a variable from the Instantiation.
Exception: at least one argument passed to a function is not what was expected.
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
Idx draw() const
get a value at random from a 1-D distribution
Definition tensor_tpl.h:696
const Tensor< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Definition tensor_tpl.h:390
GUM_SCALAR sum() const
sum of all elements in the Tensor
Definition tensor_tpl.h:157
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
This file contains Gibbs sampling (for BNs) class definitions.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Contains useful methods for random stuff.