aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
importanceSampling_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
53namespace gum {
54
56 template < typename GUM_SCALAR >
61
63 template < typename GUM_SCALAR >
67
69 template < typename GUM_SCALAR >
74
75 template < typename GUM_SCALAR >
77 GUM_SCALAR pSurQ;
78
79 do {
80 prev.clear();
81 pSurQ = 1.;
82 for (const auto ev: this->hardEvidenceNodes()) {
83 prev.add(this->BN().variable(ev));
84 prev.chgVal(this->BN().variable(ev), this->hardEvidence()[ev]);
85 }
86
87 for (const auto nod: this->BN().topologicalOrder()) {
88 if (!this->hasHardEvidence(nod)) { this->addVarSample_(nod, &prev); }
89 auto probaP = this->BN().cpt(nod).get(prev);
90 auto probaQ = this->samplingBN().cpt(nod).get(prev);
91 if ((probaP == 0) || (probaQ == 0)) {
92 pSurQ = 0;
93 } else {
94 pSurQ = probaP / probaQ;
95 }
96 }
97 if (pSurQ > 0.0) {
98 for (const auto ev: this->hardEvidenceNodes()) {
99 pSurQ *= this->samplingBN().cpt(ev).get(prev);
100 }
101 }
102 } while (pSurQ == 0);
103
104 *w = pSurQ;
105 return prev;
106 }
107
108 template < typename GUM_SCALAR >
110 float epsilon) {
111 for (const auto nod: bn->nodes().asNodeSet()) {
112 auto p = bn->cpt(nod).isNonZeroMap().scale(epsilon) + bn->cpt(nod);
113 p.normalizeAsCPT();
114 bn->installCPT(nod, p);
115 }
116 }
117
118 template < typename GUM_SCALAR >
120 for (const auto ev: this->hardEvidenceNodes()) {
121 bn->uninstallCPT(ev);
122 bn->installCPT(ev, *(this->evidence()[ev]));
123 // we keep the variables with hard evidence but alone
124 // bn->uninstallNode( sid[i] );
125 }
126 GUM_SCALAR minParam = bn->minNonZeroParam();
127 GUM_SCALAR minAccepted = GUM_SCALAR(this->epsilon() / bn->maxVarDomainSize());
128 if (minParam < minAccepted) this->unsharpenBN_(bn, float(minAccepted));
129 }
130} // namespace gum
double epsilon() const override
Returns the value of epsilon.
Portion of a BN identified by the list of nodes and a BayesNet.
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
void uninstallCPT(NodeId id)
uninstall a local CPT.
void installCPT(NodeId id, const Tensor< GUM_SCALAR > &pot)
install a local cpt BY COPYfor a node into the fragment.
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
const NodeProperty< const Tensor< GUM_SCALAR > * > & evidence() const
returns the set of evidence
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
GUM_SCALAR minNonZeroParam() const
Size maxVarDomainSize() const
void onContextualize_(BayesNetFragment< GUM_SCALAR > *bn) override
fired when Bayesian network is contextualized
Instantiation burnIn_() override
draws a defined number of samples without updating the estimators
void unsharpenBN_(BayesNetFragment< GUM_SCALAR > *bn, float epsilon)
modifies the cpts of a BN in order to tend to uniform distributions
ImportanceSampling(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.
~ImportanceSampling() override
Destructor.
Instantiation draw_(GUM_SCALAR *w, Instantiation prev) override
draws a sample according to Importance sampling
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 clear()
Erase all variables from an Instantiation.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
NodeSet asNodeSet() const
returns a copy of the set of nodes represented by the NodeGraphPart
const IBayesNet< GUM_SCALAR > & samplingBN()
get the BayesNet which is used to really perform the sampling
virtual void addVarSample_(NodeId nod, Instantiation *I)
adds a node to current instantiation
SamplingInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
This file contains Importance sampling class definition.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46