aGrUM  0.16.0
importanceSampling_tpl.h
Go to the documentation of this file.
1 
32 
33 
34 namespace gum {
35 
37  template < typename GUM_SCALAR >
39  const IBayesNet< GUM_SCALAR >* bn) :
40  SamplingInference< GUM_SCALAR >(bn) {
41  GUM_CONSTRUCTOR(ImportanceSampling);
42  }
43 
44 
46  template < typename GUM_SCALAR >
48  GUM_DESTRUCTOR(ImportanceSampling);
49  }
50 
52  template < typename GUM_SCALAR >
54  Instantiation I;
55  return I;
56  }
57 
58  template < typename GUM_SCALAR >
60  Instantiation prev) {
61  GUM_SCALAR pSurQ;
62 
63  do {
64  prev.clear();
65  pSurQ = 1.;
66  for (const auto ev : this->hardEvidenceNodes()) {
67  prev.add(this->BN().variable(ev));
68  prev.chgVal(this->BN().variable(ev), this->hardEvidence()[ev]);
69  }
70 
71  for (const auto nod : this->BN().topologicalOrder()) {
72  if (!this->hasHardEvidence(nod)) { this->_addVarSample(nod, &prev); }
73  auto probaP = this->BN().cpt(nod).get(prev);
74  auto probaQ = this->samplingBN().cpt(nod).get(prev);
75  if ((probaP == 0) || (probaQ == 0)) {
76  pSurQ = 0;
77  } else {
78  pSurQ = probaP / probaQ;
79  }
80  }
81  if (pSurQ > 0.0) {
82  for (const auto ev : this->hardEvidenceNodes()) {
83  pSurQ *= this->samplingBN().cpt(ev).get(prev);
84  }
85  }
86  } while (pSurQ == 0);
87 
88  *w = pSurQ;
89  return prev;
90  }
91 
92 
93  template < typename GUM_SCALAR >
96  for (const auto nod : bn->nodes().asNodeSet()) {
97  auto p = new Potential< GUM_SCALAR >();
98  *p = bn->cpt(nod).isNonZeroMap().scale(epsilon) + bn->cpt(nod);
99  p->normalizeAsCPT();
100  bn->installCPT(nod, p);
101  }
102  }
103 
104  template < typename GUM_SCALAR >
107  for (const auto ev : this->hardEvidenceNodes()) {
108  bn->uninstallCPT(ev);
109  bn->installCPT(ev, new Potential< GUM_SCALAR >(*this->evidence()[ev]));
110  // we keep the variables with hard evidence but alone
111  // bn->uninstallNode( sid[i] );
112  }
113  GUM_SCALAR minParam = bn->minNonZeroParam();
114  GUM_SCALAR minAccepted = GUM_SCALAR(this->epsilon() / bn->maxVarDomainSize());
115  if (minParam < minAccepted) this->_unsharpenBN(bn, float(minAccepted));
116  }
117 } // namespace gum
~ImportanceSampling() override
Destructor.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
void uninstallCPT(NodeId id) noexcept
uninstall a local CPT.
const NodeProperty< const Potential< GUM_SCALAR > *> & evidence() const
returns the set of evidence
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
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.
GUM_SCALAR minNonZeroParam() const
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:62
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Instantiation _draw(GUM_SCALAR *w, Instantiation prev) override
draws a sample according to Importance sampling
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const override
Returns the CPT of a variable.
const Potential< GUM_SCALAR > & normalizeAsCPT() const
normalisation of this as a CPT
void clear()
Erase all variables from an Instantiation.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:115
const NodeProperty< Idx > & hardEvidence() const
indicate for each node with hard evidence which value it took
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Portion of a BN identified by the list of nodes and a BayesNet.
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:83
void _unsharpenBN(BayesNetFragment< GUM_SCALAR > *bn, float epsilon)
modifies the cpts of a BN in order to tend to uniform distributions
double epsilon() const
Returns the value of epsilon.
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
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
Size maxVarDomainSize() const
Definition: IBayesNet_tpl.h:95
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