aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
importanceSampling_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright 2005-2020 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(
38
const
IBayesNet< GUM_SCALAR >* bn) :
39
SamplingInference< GUM_SCALAR >(bn) {
40
GUM_CONSTRUCTOR(ImportanceSampling);
41
}
42
43
44
/// destructor
45
template
<
typename
GUM_SCALAR
>
46
ImportanceSampling
<
GUM_SCALAR
>::~
ImportanceSampling
() {
47
GUM_DESTRUCTOR
(
ImportanceSampling
);
48
}
49
50
/// no burn in needed for Importance sampling
51
template
<
typename
GUM_SCALAR
>
52
Instantiation
ImportanceSampling
<
GUM_SCALAR
>::
burnIn_
() {
53
Instantiation
I
;
54
return
I
;
55
}
56
57
template
<
typename
GUM_SCALAR
>
58
Instantiation
ImportanceSampling
<
GUM_SCALAR
>::
draw_
(
GUM_SCALAR
*
w
,
59
Instantiation
prev
) {
60
GUM_SCALAR
pSurQ
;
61
62
do
{
63
prev
.
clear
();
64
pSurQ
= 1.;
65
for
(
const
auto
ev
:
this
->
hardEvidenceNodes
()) {
66
prev
.
add
(
this
->
BN
().
variable
(
ev
));
67
prev
.
chgVal
(
this
->
BN
().
variable
(
ev
),
this
->
hardEvidence
()[
ev
]);
68
}
69
70
for
(
const
auto
nod
:
this
->
BN
().
topologicalOrder
()) {
71
if
(!
this
->
hasHardEvidence
(
nod
)) {
this
->
addVarSample_
(
nod
, &
prev
); }
72
auto
probaP
=
this
->
BN
().
cpt
(
nod
).
get
(
prev
);
73
auto
probaQ
=
this
->
samplingBN
().
cpt
(
nod
).
get
(
prev
);
74
if
((
probaP
== 0) || (
probaQ
== 0)) {
75
pSurQ
= 0;
76
}
else
{
77
pSurQ
=
probaP
/
probaQ
;
78
}
79
}
80
if
(
pSurQ
> 0.0) {
81
for
(
const
auto
ev
:
this
->
hardEvidenceNodes
()) {
82
pSurQ
*=
this
->
samplingBN
().
cpt
(
ev
).
get
(
prev
);
83
}
84
}
85
}
while
(
pSurQ
== 0);
86
87
*
w
=
pSurQ
;
88
return
prev
;
89
}
90
91
92
template
<
typename
GUM_SCALAR
>
93
void
ImportanceSampling
<
GUM_SCALAR
>::
unsharpenBN_
(
94
BayesNetFragment
<
GUM_SCALAR
>*
bn
,
95
float
epsilon
) {
96
for
(
const
auto
nod
:
bn
->
nodes
().
asNodeSet
()) {
97
auto
p
=
bn
->
cpt
(
nod
).
isNonZeroMap
().
scale
(
epsilon
) +
bn
->
cpt
(
nod
);
98
p
.
normalizeAsCPT
();
99
bn
->
installCPT
(
nod
,
p
);
100
}
101
}
102
103
template
<
typename
GUM_SCALAR
>
104
void
ImportanceSampling
<
GUM_SCALAR
>::
onContextualize_
(
105
BayesNetFragment
<
GUM_SCALAR
>*
bn
) {
106
for
(
const
auto
ev
:
this
->
hardEvidenceNodes
()) {
107
bn
->
uninstallCPT
(
ev
);
108
bn
->
installCPT
(
ev
, *(
this
->
evidence
()[
ev
]));
109
// we keep the variables with hard evidence but alone
110
// bn->uninstallNode( sid[i] );
111
}
112
GUM_SCALAR
minParam
=
bn
->
minNonZeroParam
();
113
GUM_SCALAR
minAccepted
=
GUM_SCALAR
(
this
->
epsilon
() /
bn
->
maxVarDomainSize
());
114
if
(
minParam
<
minAccepted
)
this
->
unsharpenBN_
(
bn
,
float
(
minAccepted
));
115
}
116
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669