aGrUM
3.1.1
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-2026 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-2026 *
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
#ifdef GUM_NO_INLINE
56
# include <
agrum/FMDP/learning/datastructure/leaves/leafPair_inl.h
>
57
#endif
// GUM_NO_INLINE
58
59
// =======================================================
60
61
62
namespace
gum
{
63
64
// ############################################################################
65
// Miscelleanous Methods
66
// ############################################################################
67
68
void
LeafPair::updateLikelyhood
() {
69
_likelyhood1_
= 0.0;
70
_likelyhood2_
= 0.0;
71
72
if
(!
_l1_
->total() || !
_l2_
->total()) {
73
_likelyhood1_
= std::numeric_limits< double >::max();
74
_likelyhood2_
= std::numeric_limits< double >::max();
75
return
;
76
}
77
78
double
scaleFactor1 =
_l1_
->total() / (
_l1_
->total() +
_l2_
->total());
79
double
scaleFactor2 =
_l2_
->total() / (
_l1_
->total() +
_l2_
->total());
80
for
(
Idx
moda = 0; moda <
_l1_
->nbModa(); ++moda) {
81
if
(
_l1_
->effectif(moda)) {
82
double
add =
_l1_
->effectif(moda)
83
* std::log(
_l1_
->effectif(moda)
84
/ (scaleFactor1 * (
_l1_
->effectif(moda) +
_l2_
->effectif(moda))));
85
_likelyhood1_
+= add;
86
}
87
if
(
_l2_
->effectif(moda)) {
88
double
add =
_l2_
->effectif(moda)
89
* std::log(
_l2_
->effectif(moda)
90
/ (scaleFactor2 * (
_l1_
->effectif(moda) +
_l2_
->effectif(moda))));
91
_likelyhood2_
+= add;
92
}
93
}
94
95
_likelyhood1_
*= 2;
96
_likelyhood2_
*= 2;
97
}
98
99
double
LeafPair::likelyhood
() {
100
// updateLikelyhood();
101
return
1
102
-
ChiSquare::probaChi2
(
_likelyhood1_
>
_likelyhood2_
?
_likelyhood1_
:
_likelyhood2_
,
103
(
_l1_
->nbModa() - 1));
104
}
105
106
std::string
LeafPair::toString
() {
107
// std::format("~ M={}.L1={}.L2={} ~", moda, _l1_->effectif(moda), _l2_->effectif(moda))
108
return
std::format(
"\t[ Leaf1 : {} - Leaf2 : {} - GStat : {} ]"
,
109
_l1_
->toString(),
110
_l2_
->toString(),
111
this->likelyhood());
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:137
gum::LeafPair::_likelyhood1_
double _likelyhood1_
Definition
leafPair.h:137
gum::LeafPair::_likelyhood2_
double _likelyhood2_
Definition
leafPair.h:138
gum::LeafPair::updateLikelyhood
void updateLikelyhood()
Updates GStatistic.
Definition
leafPair.cpp:68
gum::LeafPair::_l1_
AbstractLeaf * _l1_
Definition
leafPair.h:134
gum::LeafPair::toString
std::string toString()
Definition
leafPair.cpp:106
gum::LeafPair::_l2_
AbstractLeaf * _l2_
Definition
leafPair.h:135
gum::LeafPair::likelyhood
double likelyhood()
Updates GStatistic.
Definition
leafPair.cpp:99
gum::Idx
Size Idx
Type for indexes.
Definition
types.h:79
leafPair.h
Headers of the leaf pair class.
leafPair_inl.h
math_utils.h
Useful macros for maths.
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46