aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
chiSquare.cpp
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 - 0.019198292004)
60  * w
61  + 0.059054035642)
62  * w
63  - 0.151968751364)
64  * w
65  + 0.319152932694)
66  * w
67  - 0.531923007300)
68  * w
69  + 0.797884560593)
70  * y * 2.0;
71  } else {
72  y -= 2.0;
73  x = (((((((((((((-0.000045255659 * y + 0.000152529290) * y - 0.000019538132) * y
74  - 0.000676904986)
75  * y
76  + 0.001390604284)
77  * y
78  - 0.000794620820)
79  * y
80  - 0.002034254874)
81  * y
82  + 0.006549791214)
83  * y
84  - 0.010557625006)
85  * y
86  + 0.011630447319)
87  * y
88  - 0.009279453341)
89  * y
90  + 0.005353579108)
91  * y
92  - 0.002141268741)
93  * y
94  + 0.000535310849)
95  * y
96  + 0.999936657524;
97  }
98  }
99 
100  // _ZCache_.insert(z, ( z > 0.0 ? (( x + 1.0 ) * 0.5 ) : (( 1.0 - x )
101  // * 0.5 ) ) );
102  // } else {
103  // ++nbZ;
104  // }
105 
106  // return _ZCache_[z];
107  return (z > 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5));
108  }
109 
110 
111  // ==========================================================================
112  /// computes the probability of chi2 value (used by the cache)
113  // ==========================================================================
114  double ChiSquare::probaChi2(double x, Size df) {
115  double retVal = 0.0;
116  // ++nbChit;
117 
118  // std::pair<double, unsigned long> conty(x, df);
119  // if( ! _chi2Cache_.exists(conty) ){
120 
121  double a, y = 0, s;
122  double e, c, z;
123  int even; /* true if df is an even number */
124 
125  if ((x <= 0.0) || (df < 1)) {
126  // _chi2Cache_.insert(conty,1.0);
127  retVal = 1.0;
128  } else {
129  a = 0.5 * x;
130 
131  even = (2 * (df / 2)) == df;
132 
133  if (df > 1) y = _exp_(-a);
134 
135  s = (even ? y : (2.0 * _probaZValue_(-sqrt(x))));
136 
137  if (df > 2) {
138  x = 0.5 * (df - 1.0);
139  z = (even ? 1.0 : 0.5);
140 
141  if (a > _BIGX_) {
142  e = (even ? 0.0 : _LOG_SQRT_PI_);
143  c = log(a);
144 
145  while (z <= x) {
146  e = log(z) + e;
147  s += _exp_(c * z - a - e);
148  z += 1.0;
149  }
150 
151  // _chi2Cache_.insert(conty,s);
152  retVal = s;
153 
154  } else {
155  e = (even ? 1.0 : (_I_SQRT_PI_ / sqrt(a)));
156  c = 0.0;
157 
158  while (z <= x) {
159  e = e * (a / z);
160  c = c + e;
161  z += 1.0;
162  }
163 
164  // _chi2Cache_.insert(conty,( c * y + s ));
165  retVal = (c * y + s);
166  }
167  } else {
168  // _chi2Cache_.insert(conty,s);
169  retVal = s;
170  }
171  }
172  // } else {
173  // ++nbChi;
174  // }
175  // std::cout << "Z avoid : " << nbZ << " / " << nbZt << ". Chi avoid :
176  // " << nbChi << " / " << nbChit << "." << std::endl;
177  // return _chi2Cache_[conty];
178  return retVal;
179  }
180 
181 } // End of namespace gum
182 
183 // HashTable<std::pair<double, unsigned long>, double> ChiSquare:: _chi2Cache_;
184 // HashTable<double, double> ChiSquare:: _ZCache_;
185 // Idx ChiSquare::nbZ = 0;
186 // Idx ChiSquare::nbChi = 0;
187 // Idx ChiSquare::nbZt = 0;
188 // Idx ChiSquare::nbChit = 0;
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643