aGrUM 3.1.1
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-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#pragma once
42
43
50
51
53
55
56namespace gum {
57
58 template < GUM_Numeric GUM_SCALAR >
60 const NodeProperty< Idx >* hardEv,
61 Size nbr,
62 bool atRandom) :
63 counting_(0), samplingBn_(BN), hardEv_(hardEv), nbr_(nbr), atRandom_(atRandom) {
64 GUM_CONSTRUCTOR(GibbsOperator);
65 }
66
67 template < GUM_Numeric GUM_SCALAR >
71
72 template < GUM_Numeric GUM_SCALAR >
74 samplingNodes_.clear();
75 for (const auto node: samplingBn_.nodes())
76 if (hardEv_ == nullptr || !hardEv_->exists(node)) { samplingNodes_.insert(node); }
77 if (samplingNodes_.size() == 0) {
78 GUM_ERROR(InvalidArgument, "No node to sample (too many nodes or too much evidence)!")
79 }
80 if (nbr_ > samplingNodes_.size()) nbr_ = samplingNodes_.size();
81 }
82
87 template < GUM_Numeric GUM_SCALAR >
90
91 for (const auto nod: samplingBn_.topologicalOrder()) {
92 I.add(samplingBn_.variable(nod));
93 if (hardEv_ != nullptr && hardEv_->exists(nod)) {
94 I.chgVal(samplingBn_.variable(nod), (*hardEv_)[nod]);
95 } else {
96 _drawVarMonteCarlo_(nod, &I);
97 }
98 }
99 return I;
100 }
101
102 template < GUM_Numeric GUM_SCALAR >
104 gum::Instantiation Itop(*I);
105 Itop.erase(samplingBn_.variable(nod));
106 I->chgVal(samplingBn_.variable(nod), samplingBn_.cpt(nod).extract(Itop).draw());
107 }
108
109 template < GUM_Numeric GUM_SCALAR >
111 for (Idx i = 0; i < nbr_; i++) {
112 auto pos
114 this->_GibbsSample_(samplingNodes_[pos], &prev);
115 counting_++;
116 }
117 return prev;
118 }
119
121
122 template < GUM_Numeric GUM_SCALAR >
124 gum::Instantiation Itop(*I);
125 Itop.erase(samplingBn_.variable(id));
126 gum::Tensor< GUM_SCALAR > p = samplingBn_.cpt(id).extract(Itop);
127 for (const auto nod: samplingBn_.children(id))
128 p *= samplingBn_.cpt(nod).extract(Itop);
129 GUM_ASSERT(p.nbrDim() == 1);
130 if (p.sum() != 0) {
131 p.normalize();
132 I->chgVal(samplingBn_.variable(id), p.draw());
133 }
134 }
135
136 template < GUM_Numeric GUM_SCALAR >
140
141 template < GUM_Numeric GUM_SCALAR >
145
146 template < GUM_Numeric GUM_SCALAR >
150
151 template < GUM_Numeric GUM_SCALAR >
153 atRandom_ = atRandom;
154 }
155
156} // namespace gum
void setDrawnAtRandom(bool atRandom)
Size nbrDrawnVar() const
Getters and setters.
void _drawVarMonteCarlo_(NodeId nod, Instantiation *I)
void setNbrDrawnVar(Size nbr)
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_
bool isDrawnAtRandom() const
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.
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:736
const Tensor< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Definition tensor_tpl.h:432
GUM_SCALAR sum() const
sum of all elements in the Tensor
Definition tensor_tpl.h:157
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
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.