aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
leafPair.cpp
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 Sources for concrete leaf class
25
*
26
* @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27
* GONZALES(@AMU)
28
*/
29
// =======================================================
30
#
include
<
agrum
/
tools
/
core
/
math
/
math_utils
.
h
>
31
#
include
<
agrum
/
FMDP
/
learning
/
core
/
chiSquare
.
h
>
32
#
include
<
agrum
/
FMDP
/
learning
/
datastructure
/
leaves
/
leafPair
.
h
>
33
// =======================================================
34
35
36
namespace
gum
{
37
38
// ############################################################################
39
// Miscelleanous Methods
40
// ############################################################################
41
42
void
LeafPair
::
updateLikelyhood
() {
43
likelyhood1__
= 0.0;
44
likelyhood2__
= 0.0;
45
46
if
(!
l1__
->
total
() || !
l2__
->
total
()) {
47
likelyhood1__
=
std
::
numeric_limits
<
double
>::
max
();
48
likelyhood2__
=
std
::
numeric_limits
<
double
>::
max
();
49
return
;
50
}
51
52
double
scaleFactor1
=
l1__
->
total
() / (
l1__
->
total
() +
l2__
->
total
());
53
double
scaleFactor2
=
l2__
->
total
() / (
l1__
->
total
() +
l2__
->
total
());
54
for
(
Idx
moda
= 0;
moda
<
l1__
->
nbModa
(); ++
moda
) {
55
if
(
l1__
->
effectif
(
moda
)) {
56
double
add
=
l1__
->
effectif
(
moda
)
57
*
std
::
log
(
l1__
->
effectif
(
moda
)
58
/ (
scaleFactor1
59
* (
l1__
->
effectif
(
moda
) +
l2__
->
effectif
(
moda
))));
60
likelyhood1__
+=
add
;
61
}
62
if
(
l2__
->
effectif
(
moda
)) {
63
double
add
=
l2__
->
effectif
(
moda
)
64
*
std
::
log
(
l2__
->
effectif
(
moda
)
65
/ (
scaleFactor2
66
* (
l1__
->
effectif
(
moda
) +
l2__
->
effectif
(
moda
))));
67
likelyhood2__
+=
add
;
68
}
69
}
70
71
likelyhood1__
*= 2;
72
likelyhood2__
*= 2;
73
}
74
75
double
LeafPair
::
likelyhood
() {
76
// updateLikelyhood();
77
return
1
78
-
ChiSquare
::
probaChi2
(
likelyhood1__
>
likelyhood2__
?
likelyhood1__
79
:
likelyhood2__
,
80
(
l1__
->
nbModa
() - 1));
81
}
82
83
std
::
string
LeafPair
::
toString
() {
84
std
::
stringstream
ss
;
85
ss
<<
"\t[ Leaf1 : "
<<
l1__
->
toString
() <<
" - Leaf2 : "
<<
l2__
->
toString
();
86
// ss << " - L1 Total : " << l1__->total() << " - L2 Total : " <<
87
// l2__->total();
88
// for( Idx moda = 0; moda < l1__->nbModa(); ++moda )
89
// ss << "~ M=" << moda << ".L1=" << l1__->effectif(moda) << ".L2="
90
// << l2__->effectif(moda) << " ~";
91
ss
<<
" - GStat : "
<<
this
->
likelyhood
() <<
" ]"
;
92
return
ss
.
str
();
93
}
94
95
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669