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