aGrUM  0.14.2
chiSquare.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 // =========================================================================
27 #include <agrum/core/math/math.h>
29 // =========================================================================
30 
31 
32 // constants used by Gary Perlman for his code for computing chi2 critical
33 // values
34 
35 namespace gum {
36 
37  // ==========================================================================
39  // ==========================================================================
40  double ChiSquare::__probaZValue(double z) {
41  // ++nbZt;
42 
43  // z = std::round(z * std::pow(10, 3)) / std::pow(10, 3);
44  // if( !__ZCache.exists(z) ){
45 
46  double y, x, w;
47 
48  if (z == 0.0)
49  x = 0.0;
50  else {
51  y = 0.5 * fabs(z);
52 
53  if (y >= (__Z_MAX * 0.5))
54  x = 1.0;
55  else if (y < 1.0) {
56  w = y * y;
57  x = ((((((((0.000124818987 * w - 0.001075204047) * w + 0.005198775019) * w
58  - 0.019198292004)
59  * w
60  + 0.059054035642)
61  * w
62  - 0.151968751364)
63  * w
64  + 0.319152932694)
65  * w
66  - 0.531923007300)
67  * w
68  + 0.797884560593)
69  * y * 2.0;
70  } else {
71  y -= 2.0;
72  x =
73  (((((((((((((-0.000045255659 * y + 0.000152529290) * y - 0.000019538132)
74  * y
75  - 0.000676904986)
76  * y
77  + 0.001390604284)
78  * y
79  - 0.000794620820)
80  * y
81  - 0.002034254874)
82  * y
83  + 0.006549791214)
84  * y
85  - 0.010557625006)
86  * y
87  + 0.011630447319)
88  * y
89  - 0.009279453341)
90  * y
91  + 0.005353579108)
92  * y
93  - 0.002141268741)
94  * y
95  + 0.000535310849)
96  * y
97  + 0.999936657524;
98  }
99  }
100 
101  // __ZCache.insert(z, ( z > 0.0 ? (( x + 1.0 ) * 0.5 ) : (( 1.0 - x )
102  // * 0.5 ) ) );
103  // } else {
104  // ++nbZ;
105  // }
106 
107  // return __ZCache[z];
108  return (z > 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5));
109  }
110 
111 
112  // ==========================================================================
114  // ==========================================================================
115  double ChiSquare::probaChi2(double x, Size df) {
116  double retVal = 0.0;
117  // ++nbChit;
118 
119  // std::pair<double, unsigned long> conty(x, df);
120  // if( !__chi2Cache.exists(conty) ){
121 
122  double a, y = 0, s;
123  double e, c, z;
124  int even; /* true if df is an even number */
125 
126  if ((x <= 0.0) || (df < 1)) {
127  // __chi2Cache.insert(conty,1.0);
128  retVal = 1.0;
129  } else {
130  a = 0.5 * x;
131 
132  even = (2 * (df / 2)) == df;
133 
134  if (df > 1) y = __exp(-a);
135 
136  s = (even ? y : (2.0 * __probaZValue(-sqrt(x))));
137 
138  if (df > 2) {
139  x = 0.5 * (df - 1.0);
140  z = (even ? 1.0 : 0.5);
141 
142  if (a > __BIGX) {
143  e = (even ? 0.0 : __LOG_SQRT_PI);
144  c = log(a);
145 
146  while (z <= x) {
147  e = log(z) + e;
148  s += __exp(c * z - a - e);
149  z += 1.0;
150  }
151 
152  // __chi2Cache.insert(conty,s);
153  retVal = s;
154 
155  } else {
156  e = (even ? 1.0 : (__I_SQRT_PI / sqrt(a)));
157  c = 0.0;
158 
159  while (z <= x) {
160  e = e * (a / z);
161  c = c + e;
162  z += 1.0;
163  }
164 
165  // __chi2Cache.insert(conty,( c * y + s ));
166  retVal = (c * y + s);
167  }
168  } else {
169  // __chi2Cache.insert(conty,s);
170  retVal = s;
171  }
172  }
173  // } else {
174  // ++nbChi;
175  // }
176  // std::cout << "Z avoid : " << nbZ << " / " << nbZt << ". Chi avoid :
177  // " << nbChi << " / " << nbChit << "." << std::endl;
178  // return __chi2Cache[conty];
179  return retVal;
180  }
181 
182 } // End of namespace gum
183 
184 // HashTable<std::pair<double, unsigned long>, double> ChiSquare::__chi2Cache;
185 // HashTable<double, double> ChiSquare::__ZCache;
186 // Idx ChiSquare::nbZ = 0;
187 // Idx ChiSquare::nbChi = 0;
188 // Idx ChiSquare::nbZt = 0;
189 // Idx ChiSquare::nbChit = 0;
Useful macros for maths.
static constexpr double __LOG_SQRT_PI
log (sqrt (pi))
Definition: chiSquare.h:63
static constexpr double __BIGX
max value to represent exp (x)
Definition: chiSquare.h:69
static double __probaZValue(double z)
computes the probability of normal z value (used by the cache)
Definition: chiSquare.cpp:40
static double __exp(double x)
Required constant to compute the cdf.
Definition: chiSquare.h:77
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
static constexpr double __I_SQRT_PI
1 / sqrt (pi)
Definition: chiSquare.h:66
static constexpr double __Z_MAX
Required constant to compute the cdf.
Definition: chiSquare.h:54
Headers of the ChiSquare class.
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
Definition: chiSquare.cpp:115
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45