aGrUM  0.16.0
chiSquare.cpp
Go to the documentation of this file.
1 
29 // =========================================================================
30 #include <agrum/core/math/math.h>
32 // =========================================================================
33 
34 
35 // constants used by Gary Perlman for his code for computing chi2 critical
36 // values
37 
38 namespace gum {
39 
40  // ==========================================================================
42  // ==========================================================================
43  double ChiSquare::__probaZValue(double z) {
44  // ++nbZt;
45 
46  // z = std::round(z * std::pow(10, 3)) / std::pow(10, 3);
47  // if( !__ZCache.exists(z) ){
48 
49  double y, x, w;
50 
51  if (z == 0.0)
52  x = 0.0;
53  else {
54  y = 0.5 * fabs(z);
55 
56  if (y >= (__Z_MAX * 0.5))
57  x = 1.0;
58  else if (y < 1.0) {
59  w = y * y;
60  x = ((((((((0.000124818987 * w - 0.001075204047) * w + 0.005198775019) * w
61  - 0.019198292004)
62  * w
63  + 0.059054035642)
64  * w
65  - 0.151968751364)
66  * w
67  + 0.319152932694)
68  * w
69  - 0.531923007300)
70  * w
71  + 0.797884560593)
72  * y * 2.0;
73  } else {
74  y -= 2.0;
75  x =
76  (((((((((((((-0.000045255659 * y + 0.000152529290) * y - 0.000019538132)
77  * y
78  - 0.000676904986)
79  * y
80  + 0.001390604284)
81  * y
82  - 0.000794620820)
83  * y
84  - 0.002034254874)
85  * y
86  + 0.006549791214)
87  * y
88  - 0.010557625006)
89  * y
90  + 0.011630447319)
91  * y
92  - 0.009279453341)
93  * y
94  + 0.005353579108)
95  * y
96  - 0.002141268741)
97  * y
98  + 0.000535310849)
99  * y
100  + 0.999936657524;
101  }
102  }
103 
104  // __ZCache.insert(z, ( z > 0.0 ? (( x + 1.0 ) * 0.5 ) : (( 1.0 - x )
105  // * 0.5 ) ) );
106  // } else {
107  // ++nbZ;
108  // }
109 
110  // return __ZCache[z];
111  return (z > 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5));
112  }
113 
114 
115  // ==========================================================================
117  // ==========================================================================
118  double ChiSquare::probaChi2(double x, Size df) {
119  double retVal = 0.0;
120  // ++nbChit;
121 
122  // std::pair<double, unsigned long> conty(x, df);
123  // if( !__chi2Cache.exists(conty) ){
124 
125  double a, y = 0, s;
126  double e, c, z;
127  int even; /* true if df is an even number */
128 
129  if ((x <= 0.0) || (df < 1)) {
130  // __chi2Cache.insert(conty,1.0);
131  retVal = 1.0;
132  } else {
133  a = 0.5 * x;
134 
135  even = (2 * (df / 2)) == df;
136 
137  if (df > 1) y = __exp(-a);
138 
139  s = (even ? y : (2.0 * __probaZValue(-sqrt(x))));
140 
141  if (df > 2) {
142  x = 0.5 * (df - 1.0);
143  z = (even ? 1.0 : 0.5);
144 
145  if (a > __BIGX) {
146  e = (even ? 0.0 : __LOG_SQRT_PI);
147  c = log(a);
148 
149  while (z <= x) {
150  e = log(z) + e;
151  s += __exp(c * z - a - e);
152  z += 1.0;
153  }
154 
155  // __chi2Cache.insert(conty,s);
156  retVal = s;
157 
158  } else {
159  e = (even ? 1.0 : (__I_SQRT_PI / sqrt(a)));
160  c = 0.0;
161 
162  while (z <= x) {
163  e = e * (a / z);
164  c = c + e;
165  z += 1.0;
166  }
167 
168  // __chi2Cache.insert(conty,( c * y + s ));
169  retVal = (c * y + s);
170  }
171  } else {
172  // __chi2Cache.insert(conty,s);
173  retVal = s;
174  }
175  }
176  // } else {
177  // ++nbChi;
178  // }
179  // std::cout << "Z avoid : " << nbZ << " / " << nbZt << ". Chi avoid :
180  // " << nbChi << " / " << nbChit << "." << std::endl;
181  // return __chi2Cache[conty];
182  return retVal;
183  }
184 
185 } // End of namespace gum
186 
187 // HashTable<std::pair<double, unsigned long>, double> ChiSquare::__chi2Cache;
188 // HashTable<double, double> ChiSquare::__ZCache;
189 // Idx ChiSquare::nbZ = 0;
190 // Idx ChiSquare::nbChi = 0;
191 // Idx ChiSquare::nbZt = 0;
192 // Idx ChiSquare::nbChit = 0;
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
static constexpr double __LOG_SQRT_PI
log (sqrt (pi))
Definition: chiSquare.h:66
static constexpr double __BIGX
max value to represent exp (x)
Definition: chiSquare.h:72
static double __probaZValue(double z)
computes the probability of normal z value (used by the cache)
Definition: chiSquare.cpp:43
static double __exp(double x)
Required constant to compute the cdf.
Definition: chiSquare.h:80
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
static constexpr double __I_SQRT_PI
1 / sqrt (pi)
Definition: chiSquare.h:69
static constexpr double __Z_MAX
Required constant to compute the cdf.
Definition: chiSquare.h:57
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
static double probaChi2(double x, Size df)
computes the probability of chi2 value (used by the cache)
Definition: chiSquare.cpp:118
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48