aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
chi2.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
51
53
54#ifndef DOXYGEN_SHOULD_SKIP_THIS
55
56// constants used by Gary Perlman for his code for computing chi2 critical
57// values
58# define GUM_Z_MAX 6.0 // maximum meaningful z value
59# define GUM_CHI_EPSILON 0.000001 // accuracy of critchi approximation
60# define GUM_CHI_MAX 99999.0 // maximum chi square value
61# define GUM_LOG_SQRT_PI 0.5723649429247000870717135 // std::log (std::sqrt (pi))
62# define GUM_I_SQRT_PI 0.5641895835477562869480795 // 1 / std::sqrt (pi)
63# define GUM_BIGX 20.0 // max value to represent exp (x)
64# define _gum_ex(x) (((x) < -GUM_BIGX) ? 0.0 : std::exp(x))
65
66#endif /* DOXYGEN_SHOULD_SKIP_THIS */
67
68// include the inlined functions if necessary
69#ifdef GUM_NO_INLINE
71#endif /* GUM_NO_INLINE */
72
73namespace gum {
74
75 // default constructor
76 Chi2::Chi2(const std::vector< std::size_t >& var_modalities, double confidence_proba) :
77 _modalities_(var_modalities),
78 _confidence_proba_(confidence_proba) { // for debugging purposes
79 GUM_CONSTRUCTOR(Chi2);
80 }
81
82 // destructor
84 // for debugging purposes
85 GUM_DESTRUCTOR(Chi2);
86 }
87
88 // computes the probability of normal z value (used by the cache)
89 double Chi2::_probaZValue_(double z) {
90 double y, x, w;
91
92 if (z == 0.0) x = 0.0;
93 else {
94 y = 0.5 * std::fabs(z);
95
96 if (y >= (GUM_Z_MAX * 0.5)) x = 1.0;
97 else if (y < 1.0) {
98 w = y * y;
99 x = ((((((((0.000124818987 * w - 0.001075204047) * w + 0.005198775019) * w - 0.019198292004)
100 * w
101 + 0.059054035642)
102 * w
103 - 0.151968751364)
104 * w
105 + 0.319152932694)
106 * w
107 - 0.531923007300)
108 * w
109 + 0.797884560593)
110 * y * 2.0;
111 } else {
112 y -= 2.0;
113 x = (((((((((((((-0.000045255659 * y + 0.000152529290) * y - 0.000019538132) * y
114 - 0.000676904986)
115 * y
116 + 0.001390604284)
117 * y
118 - 0.000794620820)
119 * y
120 - 0.002034254874)
121 * y
122 + 0.006549791214)
123 * y
124 - 0.010557625006)
125 * y
126 + 0.011630447319)
127 * y
128 - 0.009279453341)
129 * y
130 + 0.005353579108)
131 * y
132 - 0.002141268741)
133 * y
134 + 0.000535310849)
135 * y
136 + 0.999936657524;
137 }
138 }
139
140 return (z > 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5));
141 }
142
143 // computes the probability of chi2 value (used by the cache)
144 double Chi2::probaChi2(double x, Size df) {
145 double a, y = 0, s;
146 double e, c, z;
147 int even; /* true if df is an even number */
148
149 if ((x <= 0.0) || (df < 1)) return (1.0);
150
151 a = 0.5 * x;
152
153 even = (2 * (df / 2)) == df;
154
155 if (df > 1) y = _gum_ex(-a);
156
157 s = (even ? y : (2.0 * _probaZValue_(-std::sqrt(x))));
158
159 if (df > 2) {
160 x = 0.5 * (df - 1.0);
161 z = (even ? 1.0 : 0.5);
162
163 if (a > GUM_BIGX) {
164 e = (even ? 0.0 : GUM_LOG_SQRT_PI);
165 c = std::log(a);
166
167 while (z <= x) {
168 e = std::log(z) + e;
169 s += _gum_ex(c * z - a - e);
170 z += 1.0;
171 }
172
173 return (s);
174 } else {
175 e = (even ? 1.0 : (GUM_I_SQRT_PI / std::sqrt(a)));
176 c = 0.0;
177
178 while (z <= x) {
179 e = e * (a / z);
180 c = c + e;
181 z += 1.0;
182 }
183
184 return (c * y + s);
185 }
186 } else return (s);
187 }
188
189 // computes the critical value of a given chi2 test (used by the cache)
190 double Chi2::_criticalValue_(double proba, Size df) {
191 double minchisq = 0.0;
192 double maxchisq = GUM_CHI_MAX;
193 double chisqval;
194
195 if (proba <= 0.0) return (maxchisq);
196 else if (proba >= 1.0) return (0.0);
197
198 chisqval = df / std::sqrt(proba); /* fair first value */
199
200 while (maxchisq - minchisq > GUM_CHI_EPSILON) {
201 if (probaChi2(chisqval, df) < proba) maxchisq = chisqval;
202 else minchisq = chisqval;
203
204 chisqval = (maxchisq + minchisq) * 0.5;
205 }
206
207 return (chisqval);
208 }
209
210} /* namespace gum */
The class that represents the chi2 distribution.
The class that represents the chi2 distribution.
double _confidence_proba_
The confidence probability used for critical values.
Definition chi2.h:176
const std::vector< std::size_t > & _modalities_
The modalities of the random variables.
Definition chi2.h:173
~Chi2()
Class destructor.
Definition chi2.cpp:83
static double _probaZValue_(double z)
Computes the probability of normal z value.
Definition chi2.cpp:89
static double _criticalValue_(double proba, Size df)
Computes the critical value of a given chi2 test (used by the cache).
Definition chi2.cpp:190
Chi2(const std::vector< std::size_t > &var_modalities, double confidence_proba=GUM_LEARNING_CONFIDENCE_PROBA)
Default constructor.
Definition chi2.cpp:76
static double probaChi2(double x, Size df)
Computes the probability of chi2 value.
Definition chi2.cpp:144
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
gum is the global namespace for all aGrUM entities
Definition agrum.h:46