aGrUM  0.16.0
gum::ChiSquare Class Reference

<agrum/FMDP/learning/core/testPolicy/chiSquare.h> More...

#include <chiSquare.h>

+ Collaboration diagram for gum::ChiSquare:

Static Public Member Functions

static double probaChi2 (double x, Size df)
 computes the probability of chi2 value (used by the cache) More...
 

Detailed Description

<agrum/FMDP/learning/core/testPolicy/chiSquare.h>

Definition at line 48 of file chiSquare.h.

Member Function Documentation

◆ __exp()

static double gum::ChiSquare::__exp ( double  x)
inlinestaticprivate

Required constant to compute the cdf.

Definition at line 80 of file chiSquare.h.

References __probaZValue().

Referenced by probaChi2().

80 { return (((x) < -__BIGX) ? 0.0 : exp(x)); }
static constexpr double __BIGX
max value to represent exp (x)
Definition: chiSquare.h:72
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __probaZValue()

double gum::ChiSquare::__probaZValue ( double  z)
staticprivate

computes the probability of normal z value (used by the cache)

Definition at line 43 of file chiSquare.cpp.

References __Z_MAX.

Referenced by __exp(), and probaChi2().

43  {
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  }
static constexpr double __Z_MAX
Required constant to compute the cdf.
Definition: chiSquare.h:57
+ Here is the caller graph for this function:

◆ probaChi2()

double gum::ChiSquare::probaChi2 ( double  x,
Size  df 
)
static

computes the probability of chi2 value (used by the cache)

Definition at line 118 of file chiSquare.cpp.

References __BIGX, __exp(), __I_SQRT_PI, __LOG_SQRT_PI, and __probaZValue().

Referenced by gum::LeafPair::likelyhood(), gum::Chi2TestPolicy< GUM_SCALAR >::score(), and gum::GTestPolicy< GUM_SCALAR >::score().

118  {
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  }
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
static constexpr double __I_SQRT_PI
1 / sqrt (pi)
Definition: chiSquare.h:69
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ __BIGX

constexpr double gum::ChiSquare::__BIGX = 20.0
staticprivate

max value to represent exp (x)

Definition at line 72 of file chiSquare.h.

Referenced by probaChi2().

◆ __CHI_EPSILON

constexpr double gum::ChiSquare::__CHI_EPSILON = 0.000001
staticprivate

accuracy of critchi approximation

Definition at line 60 of file chiSquare.h.

◆ __CHI_MAX

constexpr double gum::ChiSquare::__CHI_MAX = 99999.0
staticprivate

maximum chi square value

Definition at line 63 of file chiSquare.h.

◆ __I_SQRT_PI

constexpr double gum::ChiSquare::__I_SQRT_PI = 0.5641895835477562869480795
staticprivate

1 / sqrt (pi)

Definition at line 69 of file chiSquare.h.

Referenced by probaChi2().

◆ __LOG_SQRT_PI

constexpr double gum::ChiSquare::__LOG_SQRT_PI = 0.5723649429247000870717135
staticprivate

log (sqrt (pi))

Definition at line 66 of file chiSquare.h.

Referenced by probaChi2().

◆ __Z_MAX

constexpr double gum::ChiSquare::__Z_MAX = 6.0
staticprivate

Required constant to compute the cdf.

maximum meaningful z value

Definition at line 57 of file chiSquare.h.

Referenced by __probaZValue().


The documentation for this class was generated from the following files: