aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
GTestPolicy_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 Template implementations for the GTestPolicy class.
25
*
26
* @author Jean-Christophe MAGNAN
27
*/
28
// =========================================================================
29
#
include
<
agrum
/
tools
/
core
/
math
/
math_utils
.
h
>
30
#
include
<
agrum
/
FMDP
/
learning
/
core
/
testPolicy
/
GTestPolicy
.
h
>
31
// =========================================================================
32
33
34
namespace
gum
{
35
36
37
// ##########################################################################
38
//
39
// ##########################################################################
40
41
// ==========================================================================
42
//
43
// ==========================================================================
44
template
<
typename
GUM_SCALAR >
45
void
GTestPolicy<
GUM_SCALAR
>::
addObservation
(
Idx
iattr
,
GUM_SCALAR
ivalue
) {
46
ITestPolicy
<
GUM_SCALAR
>::
addObservation
(
iattr
,
ivalue
);
47
conTab__
.
add
(
iattr
,
ivalue
);
48
}
49
50
51
// ############################################################################
52
// @name Test result
53
// ############################################################################
54
55
// ============================================================================
56
// Computes the GStat of current variable according to the test
57
// ============================================================================
58
template
<
typename
GUM_SCALAR
>
59
void
GTestPolicy
<
GUM_SCALAR
>::
computeScore
()
const
{
60
ITestPolicy
<
GUM_SCALAR
>::
computeScore
();
61
GStat__
= 0;
62
63
// Itération sur l'axe 1 du tableau
64
for
(
auto
attrIter
=
conTab__
.
attrABeginSafe
();
65
attrIter
!=
conTab__
.
attrAEndSafe
();
66
++
attrIter
) {
67
// Mise en cache de valeur utile
68
double
semiExpected
69
= (
double
)(
attrIter
.
val
()) / (
double
)
this
->
nbObservation
();
70
71
// Itération sur l'axe 2 du tableau
72
for
(
auto
valIter
=
conTab__
.
attrBBeginSafe
();
73
valIter
!=
conTab__
.
attrBEndSafe
();
74
++
valIter
) {
75
// Récupération de la valeur en cellule
76
Idx
cell
=
conTab__
.
joint
(
attrIter
.
key
(),
valIter
.
key
());
77
if
(
cell
< 5)
continue
;
78
79
// Récupération de la valeur en axe 2
80
double
expected
=
semiExpected
* (
double
)
valIter
.
val
();
81
82
GStat__
+= 2 *
cell
*
log
(
cell
/
expected
);
83
}
84
}
85
}
86
87
// ============================================================================
88
// Returns the performance of current variable according to the test
89
// ============================================================================
90
template
<
typename
GUM_SCALAR
>
91
double
GTestPolicy
<
GUM_SCALAR
>::
score
()
const
{
92
if
(
this
->
isModified_
())
computeScore
();
93
// std::cout << this->toString() << std::endl;
94
double
score
= 1
95
-
ChiSquare
::
probaChi2
(
GStat__
,
96
(
conTab__
.
attrASize
() - 1)
97
* (
conTab__
.
attrBSize
() - 1));
98
return
score
;
99
}
100
101
// ============================================================================
102
// Returns a second criterion to severe ties
103
// ============================================================================
104
template
<
typename
GUM_SCALAR
>
105
double
GTestPolicy
<
GUM_SCALAR
>::
secondaryscore
()
const
{
106
if
(
this
->
isModified_
())
computeScore
();
107
return
GStat__
;
108
}
109
110
template
<
typename
GUM_SCALAR
>
111
void
GTestPolicy
<
GUM_SCALAR
>::
add
(
const
GTestPolicy
<
GUM_SCALAR
>&
src
) {
112
ITestPolicy
<
GUM_SCALAR
>::
add
(
src
);
113
conTab__
+=
src
.
ct
();
114
}
115
116
}
// End of namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669