aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
Chi2TestPolicy_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 Chi2TestPolicy class.
25
*
26
* @author Jean-Christophe MAGNAN
27
*/
28
// =========================================================================
29
#
include
<
agrum
/
FMDP
/
learning
/
core
/
testPolicy
/
Chi2TestPolicy
.
h
>
30
// =========================================================================
31
32
33
namespace
gum
{
34
35
36
// ##########################################################################
37
//
38
// ##########################################################################
39
40
// ==========================================================================
41
//
42
// ==========================================================================
43
template
<
typename
GUM_SCALAR >
44
void
Chi2TestPolicy<
GUM_SCALAR
>::
addObservation
(
Idx
iattr
,
GUM_SCALAR
ivalue
) {
45
ITestPolicy
<
GUM_SCALAR
>::
addObservation
(
iattr
,
ivalue
);
46
_conTab_
.
add
(
iattr
,
ivalue
);
47
}
48
49
50
// ############################################################################
51
// @name Test result
52
// ############################################################################
53
54
// ============================================================================
55
// Computes the GStat of current variable according to the test
56
// ============================================================================
57
template
<
typename
GUM_SCALAR
>
58
void
Chi2TestPolicy
<
GUM_SCALAR
>::
computeScore
()
const
{
59
ITestPolicy
<
GUM_SCALAR
>::
computeScore
();
60
_chi2Score_
= 0;
61
for
(
auto
attrIter
=
_conTab_
.
attrABeginSafe
();
attrIter
!=
_conTab_
.
attrAEndSafe
();
62
++
attrIter
) {
63
double
semiExpected
= (
double
)(
attrIter
.
val
()) / (
double
)
this
->
nbObservation
();
64
for
(
auto
valIter
=
_conTab_
.
attrBBeginSafe
();
valIter
!=
_conTab_
.
attrBEndSafe
();
65
++
valIter
) {
66
double
cell
= (
double
)
_conTab_
.
joint
(
attrIter
.
key
(),
valIter
.
key
());
67
if
(
cell
< 5)
continue
;
68
double
expected
=
semiExpected
* (
double
)(
valIter
.
val
());
69
70
_chi2Score_
+=
std
::
pow
(
cell
-
expected
, 2.0) /
expected
;
71
}
72
}
73
}
74
75
// ============================================================================
76
// Returns the performance of current variable according to the test
77
// ============================================================================
78
template
<
typename
GUM_SCALAR
>
79
double
Chi2TestPolicy
<
GUM_SCALAR
>::
score
()
const
{
80
if
(
this
->
isModified_
())
computeScore
();
81
double
score
82
= 1
83
-
ChiSquare
::
probaChi2
(
_chi2Score_
, (
_conTab_
.
attrASize
() - 1) * (
_conTab_
.
attrBSize
() - 1));
84
return
score
;
85
}
86
87
// ============================================================================
88
// Returns a second criterion to severe ties
89
// ============================================================================
90
template
<
typename
GUM_SCALAR
>
91
double
Chi2TestPolicy
<
GUM_SCALAR
>::
secondaryscore
()
const
{
92
if
(
this
->
isModified_
())
computeScore
();
93
return
_chi2Score_
;
94
}
95
96
template
<
typename
GUM_SCALAR
>
97
void
Chi2TestPolicy
<
GUM_SCALAR
>::
add
(
const
Chi2TestPolicy
<
GUM_SCALAR
>&
src
) {
98
ITestPolicy
<
GUM_SCALAR
>::
add
(
src
);
99
_conTab_
+=
src
.
ct
();
100
}
101
102
}
// End of namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643