aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
samplingInference.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
41
49
50#ifndef GUM_SAMPLING_INFERENCE_H
51#define GUM_SAMPLING_INFERENCE_H
52
56
57namespace gum {
73
74 template < typename GUM_SCALAR >
75 class SamplingInference: public ApproximateInference< GUM_SCALAR > {
76 public:
77 // ############################################################################
79 // ############################################################################
81
83
86
88
90 ~SamplingInference() override;
91
93
104 const Tensor< GUM_SCALAR >& currentPosterior(NodeId id);
105
108
121 const Tensor< GUM_SCALAR >& currentPosterior(const std::string& name);
124
125
126 // ############################################################################
128 // ############################################################################
132
134
145 const Tensor< GUM_SCALAR >& posterior_(NodeId id) override;
146
148
149
152
161 virtual void contextualize();
162
163 // ############################################################################
165 // ############################################################################
167
168
170
176 virtual void setEstimatorFromBN_();
177
179
189 GUM_SCALAR virtualLBPSize);
191
192 protected:
194 Estimator< GUM_SCALAR > _estimator_;
195
197 bool isSetEstimator = false;
198
200 bool isContextualized = false;
201
203 virtual Instantiation burnIn_() = 0;
204
206
211 virtual Instantiation draw_(GUM_SCALAR* w, Instantiation prev) = 0;
212
214 void makeInference_() override;
216
218
225 virtual void addVarSample_(NodeId nod, Instantiation* I);
226
227
229
237
238 void onEvidenceAdded_(const NodeId id, bool isHardEvidence) override;
239
240 void onEvidenceErased_(const NodeId id, bool isHardEvidence) override;
241
242 void onAllEvidenceErased_(bool contains_hard_evidence) override;
243
244 void onEvidenceChanged_(const NodeId id, bool hasChangedSoftHard) override;
245
246 void onModelChanged_(const GraphicalModel* bn) override;
247
248 void updateOutdatedStructure_() override;
249
250 void updateOutdatedTensors_() override;
251
252 void onMarginalTargetAdded_(const NodeId id) override;
253
254 void onMarginalTargetErased_(const NodeId id) override;
255
256 void onAllMarginalTargetsAdded_() override;
257
258 void onAllMarginalTargetsErased_() override;
259
260 void onStateChanged_() override;
261
262 private:
264 };
265
266
267#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
268 extern template class SamplingInference< double >;
269#endif
270
271} // namespace gum
272
274#endif
Class representing Fragment of Bayesian networks.
ApproximateInference(const IBayesNet< GUM_SCALAR > *bn)
Portion of a BN identified by the list of nodes and a BayesNet.
Virtual base class for probabilistic graphical models.
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.
<agrum/BN/inference/loopyBeliefPropagation.h>
<agrum/BN/inference/samplingInference.h>
void onEvidenceAdded_(const NodeId id, bool isHardEvidence) override
fired after a new evidence is inserted
const Tensor< GUM_SCALAR > & posterior_(NodeId id) override
Computes and returns the posterior of a node.
const IBayesNet< GUM_SCALAR > & samplingBN()
get the BayesNet which is used to really perform the sampling
const Tensor< GUM_SCALAR > & currentPosterior(NodeId id)
Computes and returns the actual estimation of the posterior of a node.
bool isContextualized
whether the referenced Bayesian network has been "contextualized"
void onEvidenceChanged_(const NodeId id, bool hasChangedSoftHard) override
fired after an evidence is changed, in particular when its status (soft/hard) changes
void onAllMarginalTargetsErased_() override
fired before a all marginal targets are removed
void onEvidenceErased_(const NodeId id, bool isHardEvidence) override
fired before an evidence is removed
void onStateChanged_() override
fired when the stage is changed
void onMarginalTargetAdded_(const NodeId id) override
fired after a new marginal target is inserted
virtual void addVarSample_(NodeId nod, Instantiation *I)
adds a node to current instantiation
SamplingInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
void onAllEvidenceErased_(bool contains_hard_evidence) override
fired before all the evidence are erased
void updateOutdatedStructure_() override
prepares inference when the latter is in OutdatedStructure state
virtual void setEstimatorFromLBP_(LoopyBeliefPropagation< GUM_SCALAR > *lbp, GUM_SCALAR virtualLBPSize)
Initializes the estimators object linked to the simulation.
virtual void contextualize()
Simplifying the Bayesian network with relevance reasonning to lighten the computational charge.
void onAllMarginalTargetsAdded_() override
fired after all the nodes of the BN are added as marginal targets
virtual Instantiation draw_(GUM_SCALAR *w, Instantiation prev)=0
draws a sample in the Bayesian network given a previous one
virtual void setEstimatorFromBN_()
Initializes the estimators object linked to the simulation.
virtual Instantiation burnIn_()=0
draws samples without updating the estimators
Estimator< GUM_SCALAR > _estimator_
Estimator object designed to approximate target posteriors.
void updateOutdatedTensors_() override
prepares inference when the latter is in OutdatedTensors state
virtual void onContextualize_(BayesNetFragment< GUM_SCALAR > *bn)
fired when Bayesian network is contextualized
~SamplingInference() override
destructor
void onMarginalTargetErased_(const NodeId id) override
fired before a marginal target is removed
void makeInference_() override
makes the inference by generating samples
void onModelChanged_(const GraphicalModel *bn) override
fired after a new Bayes net has been assigned to the engine
bool isSetEstimator
whether the Estimator object has been initialized
BayesNetFragment< GUM_SCALAR > * _samplingBN_
This file contains estimating tools for approximate inference.
This file contains Gibbs sampling (for BNs) class definitions.
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Implementation of the non pure virtual methods of class ApproximateInference.