aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
chiSquare.cpp
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Sources for the ChiSquare class.
25  *
26  * @author Jean-Christophe MAGNAN
27  */
28 // =========================================================================
29 #include <agrum/tools/core/math/math_utils.h>
30 #include <agrum/FMDP/learning/core/chiSquare.h>
31 // =========================================================================
32 
33 
34 // constants used by Gary Perlman for his code for computing chi2 critical
35 // values
36 
37 namespace gum {
38 
39  // ==========================================================================
40  /// computes the probability of normal z value (used by the cache)
41  // ==========================================================================
42  double ChiSquare::probaZValue__(double z) {
43  // ++nbZt;
44 
45  // z = std::round(z * std::pow(10, 3)) / std::pow(10, 3);
46  // if( !ZCache__.exists(z) ){
47 
48  double y, x, w;
49 
50  if (z == 0.0)
51  x = 0.0;
52  else {
53  y = 0.5 * fabs(z);
54 
55  if (y >= (Z_MAX__ * 0.5))
56  x = 1.0;
57  else if (y < 1.0) {
58  w = y * y;
59  x = ((((((((0.000124818987 * w - 0.001075204047) * w + 0.005198775019) * w
60  - 0.019198292004)
61  * w
62  + 0.059054035642)
63  * w
64  - 0.151968751364)
65  * w
66  + 0.319152932694)
67  * w
68  - 0.531923007300)
69  * w
70  + 0.797884560593)
71  * y * 2.0;
72  } else {
73  y -= 2.0;
74  x = (((((((((((((-0.000045255659 * y + 0.000152529290) * y
75  - 0.000019538132)
76  * y
77  - 0.000676904986)
78  * y
79  + 0.001390604284)
80  * y
81  - 0.000794620820)
82  * y
83  - 0.002034254874)
84  * y
85  + 0.006549791214)
86  * y
87  - 0.010557625006)
88  * y
89  + 0.011630447319)
90  * y
91  - 0.009279453341)
92  * y
93  + 0.005353579108)
94  * y
95  - 0.002141268741)
96  * y
97  + 0.000535310849)
98  * y
99  + 0.999936657524;
100  }
101  }
102 
103  // ZCache__.insert(z, ( z > 0.0 ? (( x + 1.0 ) * 0.5 ) : (( 1.0 - x )
104  // * 0.5 ) ) );
105  // } else {
106  // ++nbZ;
107  // }
108 
109  // return ZCache__[z];
110  return (z > 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5));
111  }
112 
113 
114  // ==========================================================================
115  /// computes the probability of chi2 value (used by the cache)
116  // ==========================================================================
117  double ChiSquare::probaChi2(double x, Size df) {
118  double retVal = 0.0;
119  // ++nbChit;
120 
121  // std::pair<double, unsigned long> conty(x, df);
122  // if( !chi2Cache__.exists(conty) ){
123 
124  double a, y = 0, s;
125  double e, c, z;
126  int even; /* true if df is an even number */
127 
128  if ((x <= 0.0) || (df < 1)) {
129  // chi2Cache__.insert(conty,1.0);
130  retVal = 1.0;
131  } else {
132  a = 0.5 * x;
133 
134  even = (2 * (df / 2)) == df;
135 
136  if (df > 1) y = exp__(-a);
137 
138  s = (even ? y : (2.0 * probaZValue__(-sqrt(x))));
139 
140  if (df > 2) {
141  x = 0.5 * (df - 1.0);
142  z = (even ? 1.0 : 0.5);
143 
144  if (a > BIGX__) {
145  e = (even ? 0.0 : LOG_SQRT_PI__);
146  c = log(a);
147 
148  while (z <= x) {
149  e = log(z) + e;
150  s += exp__(c * z - a - e);
151  z += 1.0;
152  }
153 
154  // chi2Cache__.insert(conty,s);
155  retVal = s;
156 
157  } else {
158  e = (even ? 1.0 : (I_SQRT_PI__ / sqrt(a)));
159  c = 0.0;
160 
161  while (z <= x) {
162  e = e * (a / z);
163  c = c + e;
164  z += 1.0;
165  }
166 
167  // chi2Cache__.insert(conty,( c * y + s ));
168  retVal = (c * y + s);
169  }
170  } else {
171  // chi2Cache__.insert(conty,s);
172  retVal = s;
173  }
174  }
175  // } else {
176  // ++nbChi;
177  // }
178  // std::cout << "Z avoid : " << nbZ << " / " << nbZt << ". Chi avoid :
179  // " << nbChi << " / " << nbChit << "." << std::endl;
180  // return chi2Cache__[conty];
181  return retVal;
182  }
183 
184 } // End of namespace gum
185 
186 // HashTable<std::pair<double, unsigned long>, double> ChiSquare::chi2Cache__;
187 // HashTable<double, double> ChiSquare::ZCache__;
188 // Idx ChiSquare::nbZ = 0;
189 // Idx ChiSquare::nbChi = 0;
190 // Idx ChiSquare::nbZt = 0;
191 // Idx ChiSquare::nbChit = 0;
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669