aGrUM
2.3.2
a C++ library for (probabilistic) graphical models
leafPair.cpp
Go to the documentation of this file.
1
/****************************************************************************
2
* This file is part of the aGrUM/pyAgrum library. *
3
* *
4
* Copyright (c) 2005-2025 by *
5
* - Pierre-Henri WUILLEMIN(_at_LIP6) *
6
* - Christophe GONZALES(_at_AMU) *
7
* *
8
* The aGrUM/pyAgrum library is free software; you can redistribute it *
9
* and/or modify it under the terms of either : *
10
* *
11
* - the GNU Lesser General Public License as published by *
12
* the Free Software Foundation, either version 3 of the License, *
13
* or (at your option) any later version, *
14
* - the MIT license (MIT), *
15
* - or both in dual license, as here. *
16
* *
17
* (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18
* *
19
* This aGrUM/pyAgrum library is distributed in the hope that it will be *
20
* useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26
* OTHER DEALINGS IN THE SOFTWARE. *
27
* *
28
* See LICENCES for more details. *
29
* *
30
* SPDX-FileCopyrightText: Copyright 2005-2025 *
31
* - Pierre-Henri WUILLEMIN(_at_LIP6) *
32
* - Christophe GONZALES(_at_AMU) *
33
* SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34
* *
35
* Contact : info_at_agrum_dot_org *
36
* homepage : http://agrum.gitlab.io *
37
* gitlab : https://gitlab.com/agrumery/agrum *
38
* *
39
****************************************************************************/
40
41
49
// =======================================================
50
#include <
agrum/FMDP/learning/core/chiSquare.h
>
51
#include <
agrum/FMDP/learning/datastructure/leaves/leafPair.h
>
52
53
#include <
agrum/base/core/math/math_utils.h
>
54
55
// =======================================================
56
57
58
namespace
gum
{
59
60
// ############################################################################
61
// Miscelleanous Methods
62
// ############################################################################
63
64
void
LeafPair::updateLikelyhood
() {
65
_likelyhood1_
= 0.0;
66
_likelyhood2_
= 0.0;
67
68
if
(!
_l1_
->total() || !
_l2_
->total()) {
69
_likelyhood1_
= std::numeric_limits< double >::max();
70
_likelyhood2_
= std::numeric_limits< double >::max();
71
return
;
72
}
73
74
double
scaleFactor1 =
_l1_
->total() / (
_l1_
->total() +
_l2_
->total());
75
double
scaleFactor2 =
_l2_
->total() / (
_l1_
->total() +
_l2_
->total());
76
for
(
Idx
moda = 0; moda <
_l1_
->nbModa(); ++moda) {
77
if
(
_l1_
->effectif(moda)) {
78
double
add =
_l1_
->effectif(moda)
79
* std::log(
_l1_
->effectif(moda)
80
/ (scaleFactor1 * (
_l1_
->effectif(moda) +
_l2_
->effectif(moda))));
81
_likelyhood1_
+= add;
82
}
83
if
(
_l2_
->effectif(moda)) {
84
double
add =
_l2_
->effectif(moda)
85
* std::log(
_l2_
->effectif(moda)
86
/ (scaleFactor2 * (
_l1_
->effectif(moda) +
_l2_
->effectif(moda))));
87
_likelyhood2_
+= add;
88
}
89
}
90
91
_likelyhood1_
*= 2;
92
_likelyhood2_
*= 2;
93
}
94
95
double
LeafPair::likelyhood
() {
96
// updateLikelyhood();
97
return
1
98
-
ChiSquare::probaChi2
(
_likelyhood1_
>
_likelyhood2_
?
_likelyhood1_
:
_likelyhood2_
,
99
(
_l1_
->nbModa() - 1));
100
}
101
102
std::string
LeafPair::toString
() {
103
std::stringstream ss;
104
ss <<
"\t[ Leaf1 : "
<<
_l1_
->toString() <<
" - Leaf2 : "
<<
_l2_
->toString();
105
// ss << " - L1 Total : " << _l1_->total() << " - L2 Total : " <<
106
// _l2_->total();
107
// for( Idx moda = 0; moda < _l1_->nbModa(); ++moda )
108
// ss << "~ M=" << moda << ".L1=" << _l1_->effectif(moda) << ".L2="
109
// << _l2_->effectif(moda) << " ~";
110
ss <<
" - GStat : "
<< this->
likelyhood
() <<
" ]"
;
111
return
ss.str();
112
}
113
114
}
// namespace gum
chiSquare.h
Headers of the ChiSquare class.
gum::ChiSquare::probaChi2
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
Definition
chiSquare.cpp:133
gum::LeafPair::_likelyhood1_
double _likelyhood1_
Definition
leafPair.h:142
gum::LeafPair::_likelyhood2_
double _likelyhood2_
Definition
leafPair.h:143
gum::LeafPair::updateLikelyhood
void updateLikelyhood()
Updates GStatistic.
Definition
leafPair.cpp:64
gum::LeafPair::_l1_
AbstractLeaf * _l1_
Definition
leafPair.h:139
gum::LeafPair::toString
std::string toString()
Definition
leafPair.cpp:102
gum::LeafPair::_l2_
AbstractLeaf * _l2_
Definition
leafPair.h:140
gum::LeafPair::likelyhood
double likelyhood()
Updates GStatistic.
Definition
leafPair.cpp:95
gum::Idx
Size Idx
Type for indexes.
Definition
types.h:79
leafPair.h
Headers of the leaf pair class.
math_utils.h
Useful macros for maths.
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46