aGrUM 3.1.1
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-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
49
50#ifndef GUM_SAMPLING_INFERENCE_H
51#define GUM_SAMPLING_INFERENCE_H
52
56
57#include <string_view>
58
59namespace gum {
75
76 template < GUM_Numeric GUM_SCALAR >
77 class SamplingInference: public ApproximateInference< GUM_SCALAR > {
78 public:
79 // ############################################################################
81 // ############################################################################
83
85
88
90
92 ~SamplingInference() override;
93
95
106 const Tensor< GUM_SCALAR >& currentPosterior(NodeId id);
107
110
123 const Tensor< GUM_SCALAR >& currentPosterior(std::string_view name);
126
127
128 // ############################################################################
130 // ############################################################################
134
136
147 const Tensor< GUM_SCALAR >& posterior_(NodeId id) override;
148
150
151
154
163 virtual void contextualize();
164
165 // ############################################################################
167 // ############################################################################
169
170
172
178 virtual void setEstimatorFromBN_();
179
181
191 GUM_SCALAR virtualLBPSize);
193
194 protected:
196 Estimator< GUM_SCALAR > _estimator_;
197
199 bool isSetEstimator = false;
200
202 bool isContextualized = false;
203
205 virtual Instantiation burnIn_() = 0;
206
208
213 virtual Instantiation draw_(GUM_SCALAR* w, Instantiation prev) = 0;
214
216 void makeInference_() override;
218
220
227 virtual void addVarSample_(NodeId nod, Instantiation* I);
228
229
231
239
240 void onEvidenceAdded_(const NodeId id, bool isHardEvidence) override;
241
242 void onEvidenceErased_(const NodeId id, bool isHardEvidence) override;
243
244 void onAllEvidenceErased_(bool contains_hard_evidence) override;
245
246 void onEvidenceChanged_(const NodeId id, bool hasChangedSoftHard) override;
247
248 void onModelChanged_(const GraphicalModel* bn) override;
249
250 void updateOutdatedStructure_() override;
251
252 void updateOutdatedTensors_() override;
253
254 void onMarginalTargetAdded_(const NodeId id) override;
255
256 void onMarginalTargetErased_(const NodeId id) override;
257
258 void onAllMarginalTargetsAdded_() override;
259
260 void onAllMarginalTargetsErased_() override;
261
262 void onStateChanged_() override;
263
264 private:
266 };
267
268
269#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
270 extern template class SamplingInference< double >;
271#endif
272
273} // namespace gum
274
276#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 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
const IBayesNet< GUM_SCALAR > & samplingBN()
get the BayesNet which is used to really perform the sampling
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.