aGrUM  0.13.2
importanceSampling_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES et Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
29 
30 
31 namespace gum {
32 
34  template < typename GUM_SCALAR >
36  const IBayesNet< GUM_SCALAR >* bn) :
37  SamplingInference< GUM_SCALAR >(bn) {
38  GUM_CONSTRUCTOR(ImportanceSampling);
39  }
40 
41 
43  template < typename GUM_SCALAR >
45  GUM_DESTRUCTOR(ImportanceSampling);
46  }
47 
49  template < typename GUM_SCALAR >
51  Instantiation I;
52  return I;
53  }
54 
55  template < typename GUM_SCALAR >
57  Instantiation prev) {
58  GUM_SCALAR pSurQ;
59 
60  do {
61  prev.clear();
62  pSurQ = 1.;
63  for (const auto ev : this->hardEvidenceNodes()) {
64  prev.add(this->BN().variable(ev));
65  prev.chgVal(this->BN().variable(ev), this->hardEvidence()[ev]);
66  }
67 
68  for (const auto nod : this->BN().topologicalOrder()) {
69  if (!this->hasHardEvidence(nod)) { this->_addVarSample(nod, &prev); }
70  auto probaP = this->BN().cpt(nod).get(prev);
71  auto probaQ = this->samplingBN().cpt(nod).get(prev);
72  if ((probaP == 0) || (probaQ == 0)) {
73  pSurQ = 0;
74  } else {
75  pSurQ = probaP / probaQ;
76  }
77  }
78  if (pSurQ > 0.0) {
79  for (const auto ev : this->hardEvidenceNodes()) {
80  pSurQ *= this->samplingBN().cpt(ev).get(prev);
81  }
82  }
83  } while (pSurQ == 0);
84 
85  *w = pSurQ;
86  return prev;
87  }
88 
89 
90  template < typename GUM_SCALAR >
93  for (const auto nod : bn->nodes().asNodeSet()) {
94  auto p = new Potential< GUM_SCALAR >();
95  *p = bn->cpt(nod).isNonZeroMap().scale(epsilon) + bn->cpt(nod);
96  p->normalizeAsCPT();
97  bn->installCPT(nod, p);
98  }
99  }
100 
101  template < typename GUM_SCALAR >
104  for (const auto ev : this->hardEvidenceNodes()) {
105  bn->uninstallCPT(ev);
106  bn->installCPT(ev, new Potential< GUM_SCALAR >(*this->evidence()[ev]));
107  // we keep the variables with hard evidence but alone
108  // bn->uninstallNode( sid[i] );
109  }
110  auto minParam = bn->minNonZeroParam();
111  auto minAccepted = this->epsilon() / bn->maxVarDomainSize();
112  if (minParam < minAccepted) this->_unsharpenBN(bn, minAccepted);
113  }
114 } // namespace gum
~ImportanceSampling() override
Destructor.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
void uninstallCPT(NodeId id) noexcept
uninstall a local CPT.
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
Instantiation _burnIn() override
draws a defined number of samples without updating the estimators
const NodeProperty< const Potential< GUM_SCALAR > * > & evidence() const
returns the set of evidence
void normalizeAsCPT() const
normalisation of this as a CPT
void installCPT(NodeId id, const Potential< GUM_SCALAR > *pot)
install a local cpt for a node into the fragment.
ImportanceSampling(const IBayesNet< GUM_SCALAR > *bn)
Default constructor.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:59
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const override
Returns the CPT of a variable.
void clear()
Erase all variables from an Instantiation.
GUM_SCALAR minNonZeroParam() const
Instantiation _draw(float *w, Instantiation prev) override
draws a sample according to Importance sampling
This file contains Importance sampling class definition.
Portion of a BN identified by the list of nodes and a BayesNet.
double epsilon() const
Returns the value of epsilon.
const NodeSet & hardEvidenceNodes() const
returns the set of nodes with hard evidence
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
void _unsharpenBN(BayesNetFragment< GUM_SCALAR > *bn, float epsilon)
modifies the cpts of a BN in order to tend to uniform distributions
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
virtual void _addVarSample(NodeId nod, Instantiation *I)
adds a node to current instantiation
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:113
Size maxVarDomainSize() const
Definition: IBayesNet_tpl.h:92
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
void _onContextualize(BayesNetFragment< GUM_SCALAR > *bn) override
fired when Bayesian network is contextualized
const IBayesNet< GUM_SCALAR > & samplingBN()
get the BayesNet which is used to really perform the sampling