aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
chiSquare.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
48
// =========================================================================
49
#include <
agrum/FMDP/learning/core/chiSquare.h
>
50
51
#ifdef GUM_NO_INLINE
52
# include <
agrum/FMDP/learning/core/chiSquare_inl.h
>
53
#endif
54
55
#include <
agrum/base/core/math/math_utils.h
>
56
57
// =========================================================================
58
59
60
// constants used by Gary Perlman for his code for computing chi2 critical
61
// values
62
63
namespace
gum
{
64
65
// ==========================================================================
67
// ==========================================================================
68
double
ChiSquare::_probaZValue_
(
double
z) {
69
// ++nbZt;
70
71
// z = std::round(z * std::pow(10, 3)) / std::pow(10, 3);
72
// if( ! _ZCache_.exists(z) ){
73
74
double
y, x, w;
75
76
if
(z == 0.0) x = 0.0;
77
else
{
78
y = 0.5 * fabs(z);
79
80
if
(y >= (
_Z_MAX_
* 0.5)) x = 1.0;
81
else
if
(y < 1.0) {
82
w = y * y;
83
x = ((((((((0.000124818987 * w - 0.001075204047) * w + 0.005198775019) * w - 0.019198292004)
84
* w
85
+ 0.059054035642)
86
* w
87
- 0.151968751364)
88
* w
89
+ 0.319152932694)
90
* w
91
- 0.531923007300)
92
* w
93
+ 0.797884560593)
94
* y * 2.0;
95
}
else
{
96
y -= 2.0;
97
x = (((((((((((((-0.000045255659 * y + 0.000152529290) * y - 0.000019538132) * y
98
- 0.000676904986)
99
* y
100
+ 0.001390604284)
101
* y
102
- 0.000794620820)
103
* y
104
- 0.002034254874)
105
* y
106
+ 0.006549791214)
107
* y
108
- 0.010557625006)
109
* y
110
+ 0.011630447319)
111
* y
112
- 0.009279453341)
113
* y
114
+ 0.005353579108)
115
* y
116
- 0.002141268741)
117
* y
118
+ 0.000535310849)
119
* y
120
+ 0.999936657524;
121
}
122
}
123
124
// _ZCache_.insert(z, ( z > 0.0 ? (( x + 1.0 ) * 0.5 ) : (( 1.0 - x )
125
// * 0.5 ) ) );
126
// } else {
127
// ++nbZ;
128
// }
129
130
// return _ZCache_[z];
131
return
(z > 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5));
132
}
133
134
// ==========================================================================
136
// ==========================================================================
137
double
ChiSquare::probaChi2
(
double
x,
Size
df) {
138
double
retVal = 0.0;
139
// ++nbChit;
140
141
// std::pair<double, unsigned long> conty(x, df);
142
// if( ! _chi2Cache_.exists(conty) ){
143
144
double
a, y = 0, s;
145
double
e, c, z;
146
int
even;
/* true if df is an even number */
147
148
if
((x <= 0.0) || (df < 1)) {
149
// _chi2Cache_.insert(conty,1.0);
150
retVal = 1.0;
151
}
else
{
152
a = 0.5 * x;
153
154
even = (2 * (df / 2)) == df;
155
156
if
(df > 1) y =
_exp_
(-a);
157
158
s = (even ? y : (2.0 *
_probaZValue_
(-sqrt(x))));
159
160
if
(df > 2) {
161
x = 0.5 * (df - 1.0);
162
z = (even ? 1.0 : 0.5);
163
164
if
(a >
_BIGX_
) {
165
e = (even ? 0.0 :
_LOG_SQRT_PI_
);
166
c = log(a);
167
168
while
(z <= x) {
169
e = log(z) + e;
170
s +=
_exp_
(c * z - a - e);
171
z += 1.0;
172
}
173
174
// _chi2Cache_.insert(conty,s);
175
retVal = s;
176
177
}
else
{
178
e = (even ? 1.0 : (
_I_SQRT_PI_
/ sqrt(a)));
179
c = 0.0;
180
181
while
(z <= x) {
182
e = e * (a / z);
183
c = c + e;
184
z += 1.0;
185
}
186
187
// _chi2Cache_.insert(conty,( c * y + s ));
188
retVal = (c * y + s);
189
}
190
}
else
{
191
// _chi2Cache_.insert(conty,s);
192
retVal = s;
193
}
194
}
195
// } else {
196
// ++nbChi;
197
// }
198
// std::cout << "Z avoid : " << nbZ << " / " << nbZt << ". Chi avoid :
199
// " << nbChi << " / " << nbChit << "." << std::endl;
200
// return _chi2Cache_[conty];
201
return
retVal;
202
}
203
204
}
// End of namespace gum
205
206
// HashTable<std::pair<double, unsigned long>, double> ChiSquare:: _chi2Cache_;
207
// HashTable<double, double> ChiSquare:: _ZCache_;
208
// Idx ChiSquare::nbZ = 0;
209
// Idx ChiSquare::nbChi = 0;
210
// Idx ChiSquare::nbZt = 0;
211
// Idx ChiSquare::nbChit = 0;
chiSquare.h
Headers of the ChiSquare class.
chiSquare_inl.h
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::ChiSquare::_probaZValue_
static double _probaZValue_(double z)
computes the probability of normal z value (used by the cache)
Definition
chiSquare.cpp:68
gum::ChiSquare::_exp_
static double _exp_(double x)
Required constant to compute the cdf.
Definition
chiSquare_inl.h:48
gum::ChiSquare::_I_SQRT_PI_
static constexpr double _I_SQRT_PI_
1 / sqrt (pi)
Definition
chiSquare.h:89
gum::ChiSquare::_Z_MAX_
static constexpr double _Z_MAX_
Required constant to compute the cdf.
Definition
chiSquare.h:77
gum::ChiSquare::_LOG_SQRT_PI_
static constexpr double _LOG_SQRT_PI_
log (sqrt (pi))
Definition
chiSquare.h:86
gum::ChiSquare::_BIGX_
static constexpr double _BIGX_
max value to represent exp (x)
Definition
chiSquare.h:92
gum::Size
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition
types.h:74
math_utils.h
Useful macros for maths.
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0