aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
GTestPolicy_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 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
();
attrIter
!=
_conTab_
.
attrAEndSafe
();
65
++
attrIter
) {
66
// Mise en cache de valeur utile
67
double
semiExpected
= (
double
)(
attrIter
.
val
()) / (
double
)
this
->
nbObservation
();
68
69
// Itération sur l'axe 2 du tableau
70
for
(
auto
valIter
=
_conTab_
.
attrBBeginSafe
();
valIter
!=
_conTab_
.
attrBEndSafe
();
71
++
valIter
) {
72
// Récupération de la valeur en cellule
73
Idx
cell
=
_conTab_
.
joint
(
attrIter
.
key
(),
valIter
.
key
());
74
if
(
cell
< 5)
continue
;
75
76
// Récupération de la valeur en axe 2
77
double
expected
=
semiExpected
* (
double
)
valIter
.
val
();
78
79
_GStat_
+= 2 *
cell
*
log
(
cell
/
expected
);
80
}
81
}
82
}
83
84
// ============================================================================
85
// Returns the performance of current variable according to the test
86
// ============================================================================
87
template
<
typename
GUM_SCALAR
>
88
double
GTestPolicy
<
GUM_SCALAR
>::
score
()
const
{
89
if
(
this
->
isModified_
())
computeScore
();
90
// std::cout << this->toString() << std::endl;
91
double
score
92
= 1 -
ChiSquare
::
probaChi2
(
_GStat_
, (
_conTab_
.
attrASize
() - 1) * (
_conTab_
.
attrBSize
() - 1));
93
return
score
;
94
}
95
96
// ============================================================================
97
// Returns a second criterion to severe ties
98
// ============================================================================
99
template
<
typename
GUM_SCALAR
>
100
double
GTestPolicy
<
GUM_SCALAR
>::
secondaryscore
()
const
{
101
if
(
this
->
isModified_
())
computeScore
();
102
return
_GStat_
;
103
}
104
105
template
<
typename
GUM_SCALAR
>
106
void
GTestPolicy
<
GUM_SCALAR
>::
add
(
const
GTestPolicy
<
GUM_SCALAR
>&
src
) {
107
ITestPolicy
<
GUM_SCALAR
>::
add
(
src
);
108
_conTab_
+=
src
.
ct
();
109
}
110
111
}
// End of namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643