aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
importanceSampling_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Implementation of Importance Sampling for inference in Bayesian networks.
25  *
26  * @author Paul ALAM & Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 
30 #include <agrum/BN/inference/importanceSampling.h>
31 
32 
33 namespace gum {
34 
35  /// default constructor
36  template < typename GUM_SCALAR >
37  ImportanceSampling< GUM_SCALAR >::ImportanceSampling(const IBayesNet< GUM_SCALAR >* bn) :
38  SamplingInference< GUM_SCALAR >(bn) {
39  GUM_CONSTRUCTOR(ImportanceSampling);
40  }
41 
42 
43  /// destructor
44  template < typename GUM_SCALAR >
47  }
48 
49  /// no burn in needed for Importance sampling
50  template < typename GUM_SCALAR >
53  return I;
54  }
55 
56  template < typename GUM_SCALAR >
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 >
92  float epsilon) {
93  for (const auto nod: bn->nodes().asNodeSet()) {
94  auto p = bn->cpt(nod).isNonZeroMap().scale(epsilon) + bn->cpt(nod);
95  p.normalizeAsCPT();
96  bn->installCPT(nod, p);
97  }
98  }
99 
100  template < typename GUM_SCALAR >
102  for (const auto ev: this->hardEvidenceNodes()) {
103  bn->uninstallCPT(ev);
104  bn->installCPT(ev, *(this->evidence()[ev]));
105  // we keep the variables with hard evidence but alone
106  // bn->uninstallNode( sid[i] );
107  }
110  if (minParam < minAccepted) this->unsharpenBN_(bn, float(minAccepted));
111  }
112 } // namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643